92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 | class AgentsUtils:
"""Utils for openai-agents SDK"""
@staticmethod
def generate_group_id() -> str:
"""Generate a unique group ID. (Used in OpenAI tracing)
Ref: https://openai.github.io/openai-agents-python/tracing/
"""
return uuid.uuid4().hex[:16]
@staticmethod
def gen_trace_id() -> str:
return gen_trace_id()
@staticmethod
def get_current_trace() -> Trace:
return get_current_trace()
@staticmethod
def get_agents_model(
type: Literal["responses", "chat.completions"] = None,
model: str = None,
base_url: str = None,
api_key: str = None,
) -> OpenAIChatCompletionsModel | OpenAIResponsesModel:
type = type or os.getenv("UTU_LLM_TYPE", "chat.completions")
model = model or os.getenv("UTU_LLM_MODEL")
base_url = base_url or os.getenv("UTU_LLM_BASE_URL")
api_key = api_key or os.getenv("UTU_LLM_API_KEY")
if not api_key or not base_url:
raise ValueError("UTU_LLM_API_KEY and UTU_LLM_BASE_URL must be set")
openai_client = AsyncOpenAI(
api_key=api_key,
base_url=base_url,
timeout=100,
)
if type == "chat.completions":
return OpenAIChatCompletionsModel(model=model, openai_client=openai_client)
elif type == "responses":
return OpenAIResponsesModel(model=model, openai_client=openai_client)
else:
raise ValueError("Invalid type: " + type)
@staticmethod
def get_trajectory_from_agent_result(agent_result: RunResult) -> dict:
return {
"agent": agent_result.last_agent.name,
"trajectory": ChatCompletionConverter.items_to_messages(agent_result.to_input_list()),
}
@staticmethod
def print_new_items(new_items: list[RunItem]) -> None:
"""Print new items generated by Runner.run()"""
for new_item in new_items:
agent_name = new_item.agent.name
if isinstance(new_item, MessageOutputItem):
PrintUtils.print_bot(f"{agent_name}: {ItemHelpers.text_message_output(new_item)}")
elif isinstance(new_item, HandoffOutputItem):
PrintUtils.print_info(f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}")
elif isinstance(new_item, ToolCallItem):
assert isinstance(new_item.raw_item, ResponseFunctionToolCall) # DONOT use openai's built-in tools
PrintUtils.print_info(
f"{agent_name}: Calling a tool: {new_item.raw_item.name}({json.loads(new_item.raw_item.arguments)})"
)
elif isinstance(new_item, ToolCallOutputItem):
PrintUtils.print_tool(f"Tool call output: {new_item.output}")
elif isinstance(new_item, ReasoningItem):
PrintUtils.print_info(f"{agent_name}: Reasoning: {new_item.raw_item}")
else:
PrintUtils.print_info(f"{agent_name}: Skipping item: {new_item.__class__.__name__}")
@staticmethod
async def print_stream_events(result: AsyncIterator[StreamEvent]) -> None:
"""Print stream events generated by Runner.run_streamed()"""
async for event in result:
# print(f"> [DEBUG] event: {event}")
if isinstance(event, RawResponsesStreamEvent):
# event.data -- ResponseStreamEvent
if event.data.type == "response.output_text.delta":
PrintUtils.print_info(f"{event.data.delta}", end="")
elif event.data.type == "response.reasoning_text.delta":
PrintUtils.print_info(f"{event.data.delta}", end="")
elif event.data.type == "response.reasoning_text.done":
PrintUtils.print_info("</reasoning_text>", end="")
elif event.data.type in ("response.output_text.done",):
PrintUtils.print_info("")
elif event.data.type in (
"response.created",
"response.completed",
"response.in_progress",
"response.content_part.added",
"response.content_part.done",
"response.output_item.added",
"response.output_item.done",
"response.function_call_arguments.delta",
"response.function_call_arguments.done",
):
pass
else:
PrintUtils.print_info(f"Unknown event type: {event.data.type}! {event}")
# raise ValueError(f"Unknown event type: {event.data.type}")
elif isinstance(event, RunItemStreamEvent):
item: RunItem = event.item
if item.type == "message_output_item":
PrintUtils.print_bot(f"<{item.agent.name}> {ItemHelpers.text_message_output(item).strip()}")
elif item.type == "handoff_call_item": # same as `ToolCallItem`
PrintUtils.print_bot(f"[handoff_call] {item.raw_item.name}({item.raw_item.arguments})")
elif item.type == "handoff_output_item":
PrintUtils.print_info(f">> Handoff from {item.source_agent.name} to {item.target_agent.name}")
elif item.type == "tool_call_item":
PrintUtils.print_bot(
f"<{item.agent.name}> [tool_call] {item.raw_item.name}({item.raw_item.arguments})"
)
elif item.type == "tool_call_output_item":
PrintUtils.print_tool(f"<{item.agent.name}> [tool_output] {item.output}") # item.raw_item
elif item.type == "reasoning_item":
pass
elif event.type in (
"mcp_list_tools_item",
"mcp_approval_request_item",
"mcp_approval_response_item",
):
PrintUtils.print_info(f" >>> Skipping item: {event}")
else:
PrintUtils.print_info(f" >>> Skipping item: {item.__class__.__name__}")
elif isinstance(event, AgentUpdatedStreamEvent):
PrintUtils.print_info(f">> new agent: {event.new_agent.name}")
else:
raise ValueError(f"Unknown event type: {event.type}")
print() # Newline after stream?
@staticmethod
def convert_model_settings(params: OpenAIChatCompletionParams) -> ModelSettings:
# "tools", "messages", "model"
# FIXME: move to extra_args
for p in ("max_completion_tokens", "top_logprobs", "logprobs", "seed", "stop"):
if p in params:
logger.warning(f"Parameter `{p}` is not supported in ModelSettings")
return ModelSettings(
max_tokens=params.get("max_tokens", None),
temperature=params.get("temperature", None),
top_p=params.get("top_p", None),
frequency_penalty=params.get("frequency_penalty", None),
presence_penalty=params.get("presence_penalty", None),
tool_choice=params.get("tool_choice", None),
parallel_tool_calls=params.get("parallel_tool_calls", None),
extra_query=params.get("extra_query", None),
extra_body=params.get("extra_body", None),
extra_headers=params.get("extra_headers", None),
)
@staticmethod
def convert_sp_input(
messages: list[ChatCompletionMessageParam],
) -> tuple[str | None, str | list[TResponseInputItem]]:
if isinstance(messages, str):
return None, messages
if messages[0].get("role", None) == "system":
return messages[0]["content"], messages[1:]
return None, messages
@staticmethod
def convert_tool(tool: ChatCompletionToolParam) -> FunctionTool:
assert tool["type"] == "function"
return FunctionTool(
name=tool["function"]["name"],
description=tool["function"].get("description", ""),
params_json_schema=tool["function"].get("parameters", None),
on_invoke_tool=None,
)
|