Skip to content

Commit 690fb8b

Browse files
committed
fix: use UiPathChatOpenAI
1 parent df5449a commit 690fb8b

5 files changed

Lines changed: 36 additions & 87 deletions

File tree

packages/uipath-openai-agents/samples/agent-as-tools/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import dotenv
21
from agents import Agent, AgentOutputSchema, Runner, trace
32
from pydantic import BaseModel, Field
43
from uipath.tracing import traced
54

6-
dotenv.load_dotenv()
7-
85
"""
96
This example shows the agents-as-tools pattern adapted for UiPath coded agents.
107
The frontline agent receives a user message and then picks which agents to call,
@@ -100,6 +97,15 @@ async def main(input_data: TranslationInput) -> TranslationOutput:
10097
Returns:
10198
TranslationOutput: Result containing translations for requested languages
10299
"""
100+
# Configure UiPath OpenAI client for agent execution
101+
# This routes all OpenAI API calls through UiPath's LLM Gateway
102+
from agents.models import _openai_shared
103+
104+
from uipath_openai_agents.chat import UiPathChatOpenAI
105+
106+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
107+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
108+
103109
print(f"\nTranslating: '{input_data.text}'")
104110
print(f"Target languages: {', '.join(input_data.target_languages)}\n")
105111

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
- Streaming responses support
1010
"""
1111

12-
import dotenv
1312
from agents import Agent, Runner
1413
from pydantic import BaseModel, Field
1514
from uipath.tracing import traced
1615

17-
dotenv.load_dotenv()
18-
1916

2017
# Required Input/Output models for UiPath coded agents
2118
class Input(BaseModel):
@@ -58,6 +55,15 @@ async def main(input_data: Input) -> Output:
5855
Returns:
5956
Output: Result containing the answer and agent used
6057
"""
58+
# Configure UiPath OpenAI client for agent execution
59+
# This routes all OpenAI API calls through UiPath's LLM Gateway
60+
from agents.models import _openai_shared
61+
62+
from uipath_openai_agents.chat import UiPathChatOpenAI
63+
64+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
65+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
66+
6167
print(f"\n🔍 Question: {input_data.question}\n")
6268

6369
# Run the assistant agent (non-streaming for simplicity)

packages/uipath-openai-agents/samples/triage-agent/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import asyncio
22

3-
import dotenv
43
from agents import Agent, RawResponsesStreamEvent, Runner, trace
54
from openai.types.responses import ResponseContentPartDoneEvent, ResponseTextDeltaEvent
65
from pydantic import BaseModel
76
from uipath.tracing import traced
87

9-
dotenv.load_dotenv()
10-
118
"""
129
This example shows the handoffs/routing pattern adapted for UiPath coded agents.
1310
The triage agent receives the first message, and then hands off to the appropriate
@@ -65,6 +62,15 @@ async def main(input_data: Input) -> Output:
6562
Returns:
6663
Output: Result containing the agent's response and which agent was used.
6764
"""
65+
# Configure UiPath OpenAI client for agent execution
66+
# This routes all OpenAI API calls through UiPath's LLM Gateway
67+
from agents.models import _openai_shared
68+
69+
from uipath_openai_agents.chat import UiPathChatOpenAI
70+
71+
uipath_openai_client = UiPathChatOpenAI(model_name="gpt-4o-2024-11-20")
72+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
73+
6874
print(f"\nProcessing message: {input_data.message}")
6975

7076
with trace("Language Routing Agent"):

packages/uipath-openai-agents/src/uipath_openai_agents/runtime/runtime.py

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
"""Runtime class for executing OpenAI Agents within the UiPath framework."""
22

33
import json
4-
import os
54
from typing import Any, AsyncGenerator
65
from uuid import uuid4
76

8-
from agents import (
9-
Agent,
10-
Runner,
11-
)
7+
from agents import Agent, Runner
128
from uipath.runtime import (
139
UiPathExecuteOptions,
1410
UiPathRuntimeResult,
@@ -51,76 +47,6 @@ def __init__(
5147
self.runtime_id: str = runtime_id or "default"
5248
self.entrypoint: str | None = entrypoint
5349

54-
# Configure OpenAI Agents SDK to use Responses API
55-
# UiPath supports both APIs via X-UiPath-LlmGateway-ApiFlavor header
56-
# Using responses API for enhanced agent capabilities (conversation state, reasoning)
57-
from agents import set_default_openai_api
58-
59-
set_default_openai_api("responses")
60-
61-
# Inject UiPath OpenAI client if UiPath credentials are available
62-
self._setup_uipath_client()
63-
64-
def _setup_uipath_client(self) -> None:
65-
"""Set up UiPath OpenAI client for agents to use UiPath gateway.
66-
67-
This injects the UiPath OpenAI client into the OpenAI Agents SDK
68-
so all agents use the UiPath LLM Gateway instead of direct OpenAI.
69-
70-
The model is automatically extracted from the agent's `model` parameter.
71-
If not specified in Agent(), the SDK uses agents.models.get_default_model().
72-
73-
If UiPath credentials are not available, falls back to default OpenAI client.
74-
"""
75-
try:
76-
# Import here to avoid circular dependency
77-
from uipath_openai_agents.chat import UiPathChatOpenAI
78-
79-
# Check if UiPath credentials are available
80-
org_id = os.getenv("UIPATH_ORGANIZATION_ID")
81-
tenant_id = os.getenv("UIPATH_TENANT_ID")
82-
token = os.getenv("UIPATH_ACCESS_TOKEN")
83-
uipath_url = os.getenv("UIPATH_URL")
84-
85-
if org_id and tenant_id and token and uipath_url:
86-
# Extract model from agent definition
87-
from agents.models import get_default_model
88-
89-
from uipath_openai_agents.chat.supported_models import OpenAIModels
90-
91-
if hasattr(self.agent, "model") and self.agent.model:
92-
model_name = str(self.agent.model)
93-
else:
94-
model_name = get_default_model()
95-
96-
# Normalize generic model names to UiPath-specific versions
97-
model_name = OpenAIModels.normalize_model_name(model_name)
98-
99-
# Update agent's model to normalized version so SDK sends correct model in body
100-
self.agent.model = model_name
101-
102-
# Create UiPath OpenAI client
103-
uipath_client = UiPathChatOpenAI(
104-
token=token,
105-
org_id=org_id,
106-
tenant_id=tenant_id,
107-
model_name=model_name,
108-
)
109-
110-
# Inject into OpenAI Agents SDK
111-
# This makes all agents use UiPath gateway
112-
from agents.models import _openai_shared
113-
114-
_openai_shared.set_default_openai_client(uipath_client.async_client)
115-
116-
except ImportError:
117-
# UiPath chat module not available, skip injection
118-
pass
119-
except Exception:
120-
# If injection fails, fall back to default OpenAI client
121-
# Agents will use OPENAI_API_KEY if set
122-
pass
123-
12450
async def execute(
12551
self,
12652
input: dict[str, Any] | None = None,

packages/uipath-openai-agents/testcases/triage-agent/src/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
66
"""
77

8-
import dotenv
98
from agents import Agent
9+
from agents.models import _openai_shared
1010

11-
dotenv.load_dotenv()
11+
from uipath_openai_agents.chat import UiPathChatOpenAI
12+
13+
# Configure UiPath OpenAI client for agent execution
14+
# This routes all OpenAI API calls through UiPath's LLM Gateway
15+
MODEL = "gpt-4o-2024-11-20"
16+
uipath_openai_client = UiPathChatOpenAI(model_name=MODEL)
17+
_openai_shared.set_default_openai_client(uipath_openai_client.async_client)
1218

1319
# Define specialized agents for different languages
1420
# Explicitly set model to gpt-4o-2024-11-20 (OpenAI Agents SDK normalizes gpt-4.1 automatically)
15-
MODEL = "gpt-4o-2024-11-20"
1621

1722
french_agent = Agent(
1823
name="french_agent",

0 commit comments

Comments
 (0)