|
1 | | -import dotenv |
2 | | -from agents import Agent, AgentOutputSchema, Runner, trace |
| 1 | +from agents import Agent, AgentOutputSchema |
| 2 | +from agents.models import _openai_shared |
3 | 3 | from pydantic import BaseModel, Field |
4 | | -from uipath.tracing import traced |
5 | 4 |
|
6 | | -dotenv.load_dotenv() |
| 5 | +from uipath_openai_agents.chat import UiPathChatOpenAI |
7 | 6 |
|
8 | 7 | """ |
9 | 8 | This example shows the agents-as-tools pattern adapted for UiPath coded agents. |
@@ -39,95 +38,58 @@ class TranslationOutput(BaseModel): |
39 | 38 | ) |
40 | 39 |
|
41 | 40 |
|
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." |
83 | 77 | ), |
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), |
133 | 93 | ) |
| 94 | + |
| 95 | + return orchestrator_agent |
0 commit comments