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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328 | 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", "litellm"] = None,
model: str = None,
base_url: str = None,
api_key: str = None,
) -> Model:
type = type or os.getenv("UTU_LLM_TYPE", "chat.completions")
model = model or os.getenv("UTU_LLM_MODEL")
if type == "litellm":
# Ref: https://docs.litellm.ai/docs/providers
# NOTE: should set .evn properly! e.g. AZURE_API_KEY, AZURE_API_BASE, AZURE_API_VERSION for Azure
# https://docs.litellm.ai/docs/providers/azure/
from agents.extensions.models.litellm_model import LitellmModel
return LitellmModel(model=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, agent_name: str = None) -> dict:
if agent_name is None:
agent_name = agent_result.last_agent.name
return {
"agent": 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_item.added":
match event.data.item.type:
# computer_call, code_interpreter_call, custom_tool_call, file_search_call, function_call,
# we_search_call, image_generation_call, local_shell_call,
# mcp_call, mcp_list_tools, mcp_approval_request, message, reasoning
case "message":
pass
case "function_call":
PrintUtils.print_bot(
f"<toolcall name={event.data.item.name}>{event.data.item.arguments}", end=""
)
case _:
PrintUtils.print_bot(f"<{event.data.item.type}>", end="")
elif event.data.type == "response.output_item.done":
match event.data.item.type:
case "message":
pass
# PrintUtils.print_bot("") # add a new line?
case "function_call":
PrintUtils.print_bot("</toolcall>")
case _:
# PrintUtils.print_bot(f"</{event.data.item.type}>")
logger.info(f"</{event.data.item.type}>") # It seems that vllm's output order is wrong
elif event.data.type == "response.content_part.added":
match event.data.part.type:
# output_text, refusal
case "output_text":
pass
case "refusal":
PrintUtils.print_bot(f"<refusal>{event.data.part.refusal}", end="")
case _:
logger.warning(f"Unknown part type: {event.data.part.type}! {event}")
elif event.data.type == "response.content_part.done":
match event.data.part.type:
case "output_text":
pass
case "refusal":
PrintUtils.print_bot("</refusal>")
case _:
logger.warning(f"Unknown part type: {event.data.part.type}! {event}")
elif event.data.type == "response.reasoning_summary_part.added":
PrintUtils.print_info("<reasoning_summary>", end="")
elif event.data.type == "response.reasoning_summary_part.done":
# PrintUtils.print_info("</reasoning_summary>", end="")
logger.info("</reasoning_summary>") # It seems that vllm's output order is wrong
elif event.data.type == "response.reasoning_summary_text.delta":
PrintUtils.print_info(f"{event.data.delta}", end="")
elif event.data.type == "response.function_call_arguments.delta":
PrintUtils.print_bot(f"{event.data.delta}", end="")
elif event.data.type == "response.function_call_arguments.done":
pass
elif event.data.type == "response.output_text.delta":
PrintUtils.print_bot(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",
):
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":
pass # do not print twice to avoid duplicate! (already handled `response.output_text.delta`)
# PrintUtils.print_bot(f"{ItemHelpers.text_message_output(item).strip()}")
elif item.type == "reasoning_item":
pass
elif item.type == "tool_call_item":
pass
# PrintUtils.print_bot([tool_call] {item.raw_item.name}({item.raw_item.arguments})")
elif item.type == "tool_call_output_item":
PrintUtils.print_tool(f"[tool_output] {item.output}") # item.raw_item
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 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}")
# skip events from youtu-agent
elif event.type in ("orchestrator_stream_event", "orchestra_stream_event"):
pass
else:
logger.warning(f"Unknown event type: {event.type}! {event}")
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,
)
@staticmethod
def get_message_from_image(image_url: str) -> dict:
"""Get a message dict for image input."""
# from openai.types.responses.response_input_item_param import Message
# from openai.types.responses.response_input_image_param import ResponseInputImageParam
return {"role": "user", "content": [{"type": "input_image", "image_url": encode_image(image_url)}]}
|