By Markus van Kempen |
Series: WXO Labs & Tutorial Guide (Part 4 of 15)
Interactive Lab Portal: https://markusvankempen.github.io/wxo-labs/
GitHub Repository: https://github.com/markusvankempen/wxo-labs
Enterprise chat bots should never ask a user 'What is your email?' when the user is already logged in to the corporate portal. watsonx Orchestrate provides ambient context injection (`agent_run.context`). In this lab, we build a context-aware HR assistant that automatically knows the user's corporate email, employee ID, and department.
Ensure your environment has the required dependencies:
pip install ibm-watsonx-orchestrate
Verify active environment:
orchestrate env list
context_aware_tool.py)# Critical Pattern: The AgentRun import fallback for Cloud Sandbox
try:
from ibm_watsonx_orchestrate.run.context import AgentRun
except ImportError:
AgentRun = object
from ibm_watsonx_orchestrate import tool
@tool(context_access_enabled=True)
def get_user_profile(agent_run: AgentRun) -> str:
"""
Retrieve ambient user profile and session identity from Orchestrate runtime.
"""
context = agent_run.context if hasattr(agent_run, 'context') else {}
user_email = context.get('wxo_email_id', 'user@example.com')
employee_id = context.get('employee_id', 'EMP-9821')
department = context.get('department', 'Cloud & Cognitive')
return f"""
👤 Ambient User Identity Detected:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Corporate Email: {user_email}
Employee ID: {employee_id}
Department: {department}
Session Mode: Authenticated SSO
"""
@tool(context_access_enabled=True)
def update_user_preferences(agent_run: AgentRun, preferred_language: str) -> str:
"""Update conversational preferences in the active context state."""
return f"✅ Context Updated: Preferred language set to '{preferred_language}' for this session."
requirements.txt)ibm-watsonx-orchestrate>=2.9.0
agent.yaml)spec_version: v1
kind: native
name: hr_context_assistant
title: "HR Context Assistant"
description: "Seamlessly identifies logged-in employees using ambient context injection."
model: ibm/granite-3-8b-instruct
instructions: |
You are an HR concierge. Greet the employee by extracting their identity from
the `get_user_profile` tool without prompting them for their personal info.
tools:
- get_user_profile
- update_user_preferences
Import tool and test conversationally:
orchestrate chat ask -n hr_context_assistant "What are my current employee benefits?"
Live Response:
╭─ 🤖 hr_context_assistant ──────────────────────────────────────────────╮
│ │
│ Hello Markus (EMP-9821)! I see you belong to the Cloud & Cognitive │
│ department. Based on your full-time status, here is your 2026 │
│ benefits summary... │
╰────────────────────────────────────────────────────────────────────────╯
When publishing this blog post, select or enter the following tags in the community editor:
watsonx Orchestrate, Context Injection, User Identity, AgentRun, State Management, AI Personalization
This tutorial is Part 4 of 15 in the complete enterprise hands-on series:
Explore the interactive curriculum and code repository: