Skip to content

Commit 632a563

Browse files
committed
fix: use UiPathChatOpenAI
1 parent df5449a commit 632a563

11 files changed

Lines changed: 173 additions & 327 deletions

File tree

Lines changed: 56 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import dotenv
2-
from agents import Agent, AgentOutputSchema, Runner, trace
1+
from agents import Agent, AgentOutputSchema
2+
from agents.models import _openai_shared
33
from pydantic import BaseModel, Field
4-
from uipath.tracing import traced
54

6-
dotenv.load_dotenv()
5+
from uipath_openai_agents.chat import UiPathChatOpenAI
76

87
"""
98
This example shows the agents-as-tools pattern adapted for UiPath coded agents.
@@ -39,95 +38,58 @@ class TranslationOutput(BaseModel):
3938
)
4039

4140

42-
spanish_agent = Agent(
43-
name="spanish_agent",
44-
instructions="You translate the user's message to Spanish",
45-
handoff_description="An english to spanish translator",
46-
)
47-
48-
french_agent = Agent(
49-
name="french_agent",
50-
instructions="You translate the user's message to French",
51-
handoff_description="An english to french translator",
52-
)
53-
54-
italian_agent = Agent(
55-
name="italian_agent",
56-
instructions="You translate the user's message to Italian",
57-
handoff_description="An english to italian translator",
58-
)
59-
60-
# Orchestrator agent that uses other agents as tools
61-
# Uses output_type for structured outputs (native OpenAI Agents pattern)
62-
# Note: Using AgentOutputSchema with strict_json_schema=False because
63-
# dict[str, str] is not compatible with OpenAI's strict JSON schema mode
64-
orchestrator_agent = Agent(
65-
name="orchestrator_agent",
66-
instructions=(
67-
"You are a translation agent. You use the tools given to you to translate. "
68-
"If asked for multiple translations, you call the relevant tools in order. "
69-
"You never translate on your own, you always use the provided tools."
70-
),
71-
tools=[
72-
spanish_agent.as_tool(
73-
tool_name="translate_to_spanish",
74-
tool_description="Translate the user's message to Spanish",
75-
),
76-
french_agent.as_tool(
77-
tool_name="translate_to_french",
78-
tool_description="Translate the user's message to French",
79-
),
80-
italian_agent.as_tool(
81-
tool_name="translate_to_italian",
82-
tool_description="Translate the user's message to Italian",
41+
def main() -> Agent:
42+
"""Configure UiPath OpenAI client and return the orchestrator agent."""
43+
# Configure UiPath OpenAI client for agent execution
44+
# This routes all OpenAI API calls through UiPath's LLM Gateway
45+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
46+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
47+
48+
# Define specialized translation agents
49+
spanish_agent = Agent(
50+
name="spanish_agent",
51+
instructions="You translate the user's message to Spanish",
52+
handoff_description="An english to spanish translator",
53+
)
54+
55+
french_agent = Agent(
56+
name="french_agent",
57+
instructions="You translate the user's message to French",
58+
handoff_description="An english to french translator",
59+
)
60+
61+
italian_agent = Agent(
62+
name="italian_agent",
63+
instructions="You translate the user's message to Italian",
64+
handoff_description="An english to italian translator",
65+
)
66+
67+
# Orchestrator agent that uses other agents as tools
68+
# Uses output_type for structured outputs (native OpenAI Agents pattern)
69+
# Note: Using AgentOutputSchema with strict_json_schema=False because
70+
# dict[str, str] is not compatible with OpenAI's strict JSON schema mode
71+
orchestrator_agent = Agent(
72+
name="orchestrator_agent",
73+
instructions=(
74+
"You are a translation agent. You use the tools given to you to translate. "
75+
"If asked for multiple translations, you call the relevant tools in order. "
76+
"You never translate on your own, you always use the provided tools."
8377
),
84-
],
85-
output_type=AgentOutputSchema(TranslationOutput, strict_json_schema=False),
86-
)
87-
88-
89-
@traced(name="Translation Orchestrator Main")
90-
async def main(input_data: TranslationInput) -> TranslationOutput:
91-
"""
92-
Main function to orchestrate translations using agent-as-tools pattern.
93-
94-
This function demonstrates parameter inference - the Input/Output models
95-
are automatically extracted to generate schemas for UiPath workflows.
96-
97-
Args:
98-
input_data: Input containing text and target languages
99-
100-
Returns:
101-
TranslationOutput: Result containing translations for requested languages
102-
"""
103-
print(f"\nTranslating: '{input_data.text}'")
104-
print(f"Target languages: {', '.join(input_data.target_languages)}\n")
105-
106-
# Build the prompt based on requested languages
107-
language_list = ", ".join(input_data.target_languages)
108-
prompt = f"Translate this text to {language_list}: {input_data.text}"
109-
110-
with trace("Translation Orchestrator"):
111-
# Run the orchestrator agent
112-
result = await Runner.run(
113-
starting_agent=orchestrator_agent,
114-
input=[{"content": prompt, "role": "user"}],
115-
)
116-
117-
# Extract translations from the response
118-
# In a real implementation, you'd parse the structured response
119-
final_response = result.final_output
120-
print(f"\nAgent response: {final_response}\n")
121-
122-
# For demonstration, create structured output
123-
# In production, you'd parse the agent's structured response
124-
translations = {}
125-
for lang in input_data.target_languages:
126-
# Placeholder - in real usage, extract from agent response
127-
translations[lang] = f"[Translation to {lang}]"
128-
129-
return TranslationOutput(
130-
original_text=input_data.text,
131-
translations=translations,
132-
languages_used=input_data.target_languages,
78+
tools=[
79+
spanish_agent.as_tool(
80+
tool_name="translate_to_spanish",
81+
tool_description="Translate the user's message to Spanish",
82+
),
83+
french_agent.as_tool(
84+
tool_name="translate_to_french",
85+
tool_description="Translate the user's message to French",
86+
),
87+
italian_agent.as_tool(
88+
tool_name="translate_to_italian",
89+
tool_description="Translate the user's message to Italian",
90+
),
91+
],
92+
output_type=AgentOutputSchema(TranslationOutput, strict_json_schema=False),
13393
)
94+
95+
return orchestrator_agent
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"agents": {
3-
"agent": "main.py:orchestrator_agent"
3+
"agent": "main.py:main"
44
}
55
}

packages/uipath-openai-agents/samples/rag-assistant/main.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
- Streaming responses support
1010
"""
1111

12-
import dotenv
13-
from agents import Agent, Runner
12+
from agents import Agent
13+
from agents.models import _openai_shared
1414
from pydantic import BaseModel, Field
15-
from uipath.tracing import traced
1615

17-
dotenv.load_dotenv()
16+
from uipath_openai_agents.chat import UiPathChatOpenAI
1817

1918

2019
# Required Input/Output models for UiPath coded agents
@@ -31,46 +30,25 @@ class Output(BaseModel):
3130
agent_used: str = Field(description="The name of the agent that answered")
3231

3332

34-
# Define the assistant agent
35-
# Model defaults to gpt-4.1 which automatically maps to gpt-4o-2024-11-20
36-
assistant_agent = Agent(
37-
name="assistant_agent",
38-
instructions="""You are a helpful AI assistant that provides clear, concise answers.
33+
def main() -> Agent:
34+
"""Configure UiPath OpenAI client and return the assistant agent."""
35+
# Configure UiPath OpenAI client for agent execution
36+
# This routes all OpenAI API calls through UiPath's LLM Gateway
37+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
38+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
39+
40+
# Define the assistant agent
41+
# Model defaults to gpt-4.1 which automatically maps to gpt-4o-2024-11-20
42+
assistant_agent = Agent(
43+
name="assistant_agent",
44+
instructions="""You are a helpful AI assistant that provides clear, concise answers.
3945
4046
Your capabilities:
4147
- Answer questions accurately
4248
- Provide well-structured responses
4349
- Be helpful and informative
4450
4551
Always aim for clarity and accuracy in your responses.""",
46-
)
47-
48-
49-
@traced(name="RAG Assistant Main")
50-
async def main(input_data: Input) -> Output:
51-
"""Main function for RAG assistant using OpenAI Agents SDK.
52-
53-
This function demonstrates the basic OpenAI Agents pattern with UiPath integration.
54-
55-
Args:
56-
input_data: Input containing the question to ask
57-
58-
Returns:
59-
Output: Result containing the answer and agent used
60-
"""
61-
print(f"\n🔍 Question: {input_data.question}\n")
62-
63-
# Run the assistant agent (non-streaming for simplicity)
64-
result = await Runner.run(
65-
starting_agent=assistant_agent,
66-
input=[{"content": input_data.question, "role": "user"}],
6752
)
6853

69-
# Extract the final response
70-
final_response = result.final_output
71-
agent_used = result.current_agent.name
72-
73-
print(f"\n💬 Answer: {final_response}")
74-
print(f"✅ Agent used: {agent_used}\n")
75-
76-
return Output(answer=final_response, agent_used=agent_used)
54+
return assistant_agent
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"agents": {
3-
"agent": "main.py:assistant_agent"
3+
"agent": "main.py:main"
44
}
55
}
Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import asyncio
2-
3-
import dotenv
4-
from agents import Agent, RawResponsesStreamEvent, Runner, trace
5-
from openai.types.responses import ResponseContentPartDoneEvent, ResponseTextDeltaEvent
1+
from agents import Agent
2+
from agents.models import _openai_shared
63
from pydantic import BaseModel
7-
from uipath.tracing import traced
84

9-
dotenv.load_dotenv()
5+
from uipath_openai_agents.chat import UiPathChatOpenAI
106

117
"""
128
This example shows the handoffs/routing pattern adapted for UiPath coded agents.
@@ -31,76 +27,34 @@ class Output(BaseModel):
3127
agent_used: str
3228

3329

34-
# Define specialized agents for different languages
35-
french_agent = Agent(
36-
name="french_agent",
37-
instructions="You only speak French",
38-
)
39-
40-
spanish_agent = Agent(
41-
name="spanish_agent",
42-
instructions="You only speak Spanish",
43-
)
44-
45-
english_agent = Agent(
46-
name="english_agent",
47-
instructions="You only speak English",
48-
)
49-
50-
# Triage agent routes to appropriate language agent
51-
triage_agent = Agent(
52-
name="triage_agent",
53-
instructions="Handoff to the appropriate agent based on the language of the request.",
54-
handoffs=[french_agent, spanish_agent, english_agent],
55-
)
56-
57-
58-
@traced(name="Language Routing Agent Main")
59-
async def main(input_data: Input) -> Output:
60-
"""Main function to run the language routing agent.
61-
62-
Args:
63-
input_data: Input model with a message for the agent.
64-
65-
Returns:
66-
Output: Result containing the agent's response and which agent was used.
67-
"""
68-
print(f"\nProcessing message: {input_data.message}")
69-
70-
with trace("Language Routing Agent"):
71-
# Run the agent with streaming
72-
result = Runner.run_streamed(
73-
triage_agent,
74-
input=[{"content": input_data.message, "role": "user"}],
75-
)
76-
77-
# Collect the response
78-
response_parts = []
79-
async for event in result.stream_events():
80-
if not isinstance(event, RawResponsesStreamEvent):
81-
continue
82-
data = event.data
83-
if isinstance(data, ResponseTextDeltaEvent):
84-
print(data.delta, end="", flush=True)
85-
response_parts.append(data.delta)
86-
elif isinstance(data, ResponseContentPartDoneEvent):
87-
print()
88-
89-
# Get the final response and agent used
90-
final_response = "".join(response_parts)
91-
agent_used = result.current_agent.name
92-
93-
print(f"\n\nAgent used: {agent_used}")
94-
return Output(response=final_response, agent_used=agent_used)
95-
96-
97-
if __name__ == "__main__":
98-
# Example usage with different languages:
99-
# 1. English message
100-
# asyncio.run(main(Input(message="Hello, how are you?")))
101-
102-
# 2. French message
103-
# asyncio.run(main(Input(message="Bonjour, comment allez-vous?")))
104-
105-
# 3. Spanish message
106-
asyncio.run(main(Input(message="Hola, ¿cómo estás?")))
30+
def main() -> Agent:
31+
"""Configure UiPath OpenAI client and return the triage agent."""
32+
# Configure UiPath OpenAI client for agent execution
33+
# This routes all OpenAI API calls through UiPath's LLM Gateway
34+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
35+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
36+
37+
# Define specialized agents for different languages
38+
french_agent = Agent(
39+
name="french_agent",
40+
instructions="You only speak French",
41+
)
42+
43+
spanish_agent = Agent(
44+
name="spanish_agent",
45+
instructions="You only speak Spanish",
46+
)
47+
48+
english_agent = Agent(
49+
name="english_agent",
50+
instructions="You only speak English",
51+
)
52+
53+
# Triage agent routes to appropriate language agent
54+
triage_agent = Agent(
55+
name="triage_agent",
56+
instructions="Handoff to the appropriate agent based on the language of the request.",
57+
handoffs=[french_agent, spanish_agent, english_agent],
58+
)
59+
60+
return triage_agent
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"agents": {
3-
"agent": "main.py:triage_agent"
3+
"agent": "main.py:main"
44
}
55
}

0 commit comments

Comments
 (0)