ToCode
Kanalga Telegramโda oโtish
ืืืคืื ืงืฆืจืื ืืืชืื ืชืื ืืืช ืื ืื ืคืจืง
Ko'proq ko'rsatish1 419
Obunachilar
Ma'lumot yo'q24 soatlar
Ma'lumot yo'q7 kunlar
-430 kunlar
Postlar arxiv
1 419
ืืื 15 - ืฉืืืืฉ ืืืืื ืืฉืจืชื MCP
ื ืกืืื ืืช ืืืืง ืขื ืฉืืืืฉ ืืืืื ืืกืงืืจื ืงืฆืจื ืฉื ืฉืจืชื MCP ืืืืืืจ ืืกืืื ืื ืฉืื ื ืืืืื.
ืืื ืฉืจืชื MCP
ืจืืื ื ืฉืื ืื ื ืืืืืจืื ืืืื ืืกืืื ืืืืฆืขืืช ืืขืืจืช ืืขืจื ืืืื, ืืืื ืืื ืืขืฆื ืคืื ืงืฆืื:
agent = Agent(
name="Assistant",
tools=[read_shells_file],
instructions="Answer questions about the file /etc/shells",
)
ืื ืงืืจื ืื ืื ืื ื ืจืืฆืื ืืฉืชืฃ ืืืื ืขื ืืืจืื ืื ืืงืืช ืืืื ืฉืืืจืื ืืฉืชืคืื? ืืชืืื ืืช ืคืืืชืื ืจืืืื ืืืืชื ืืืืจ ืฉืืฉืืื ืืฉืชืฃ "ืืื", ืืืืืจ ืคืื ืงืฆืื, ื ืฆืืจื ืืืืืืจ ืืืืืช ืคืืืชืื, ืืืขืืืช ืืจืฉืช ืืืืฉืื ืืืจ ืืชืงืื ืืช ืืืืืื ืืืฉืชืืฉ ื import ืืื ืืืืืข ืืคืื ืงืฆืื.
ืืืขืื ืขื ืกืืื ืื ืืื ืฉืื ืืืื ืืืชืืื ืืช ืืกืืื ืื ืฉืืื ืืคืืืชืื - ืืฉ ืกืคืจืืืช ืกืืื ืื ืืืืืคืกืงืจืืคื, ืืจืืื, ื Java, ืืืืืงืกืืจ ืืืขืฆื ืืื ืฉืคื ืฉืืชื ืืืืืื ืืืฉืื ืขืืื ืืืฉืื ืืืจ ืืชื ืกืคืจืืืช ืกืืื ืื. ืืื ืื ื"ืืื" ืฉืื ื ืจืืฆื ืืฉืชืฃ ืืืื ืคืื ืงืฆืืืช ืคืืืชืื ืืจืื ืื ืฉืื ืื ืืืืื ืืืฉืชืืฉ ืื.
ืคืจืืืืงืื MCP ืืื ืคืจืืืืงืื ืืฉืืชืืฃ "ืืืื" ืืื ืกืคืจืืืช ืกืืื ืื ืฉืืืคืฉืจ ืืชืืื ืฉื ืืืืื ืืื ืืฉืคืืช. ืืคืจืืืืงืื ืืืืืจ:
1. ืืื ื ืจืื "ืฉืจืช", ืฉืื ืืขืฆื ืืืกืฃ ืฉื ืืืื.
2. ืืื ื ืืืฉืื ืืืืชื ืฉืจืช - ืืจื ืืคืขืื ืืฉืืจืช ืืคืงืืื ืื ืืจื ืืจืฉืช.
3. ืืื ืืืจืจืื ืืืฉืจืช ืืืื ืืืื ืืฉ ืื.
4. ืืื ืืืจืจืื ืืืื ืคืจืืืจ ืื ืืื ืฆืจืื.
ืกืคืจืืืช OpenAI Agents SDK ืืืืขืช ืืืชืืืจ ืืฉืจืชื MCP ืืืชืช ืืกืืื ืืืฉื ืืืืื ืฉืืืืืกื ืื ืขื ืฉืจืชืื ืืื.
ืืื ืืืืจ ืกืืื ืืฉืจืช MCP
ื ืืงื ืืืืืื ืฉืจืช MCP ืืฉื fetch ืฉืืฉืชืฃ ืืื ืืื - ืืืื ื ืงืจื fetch ืืืื ืืงืื URL ืืืืฉื ืืช ืชืืื ื ืืืืื ืืจื ื ืืื ืฉืืกืืื ืืืื ืืืฉืชืืฉ ืืืืืข. ืืฉืจืช ื ืืฆื ืืืชืืืช:
https://remote.mcpservers.org/fetch/mcp
ืืื ืืื ืืืื ืืืชืืืจ ืืืื. ื ื ืื ืฉืืฉ ืื ืกืืื ืฉืฆืจืื ืืืืื ืืืืข ืืืืื ืืจื ื ืืืฉื ื ynet, ืืืืืจ ืืืื ืื ื ืจืืฆื ืืืจืืฅ ืืช ืืคืจืืืคื ืืื:
result = await Runner.run(agent, "Summarize the top stories from ynet today. URL is: https://www.ynet.co.il/news/category/184")
ืืกืืื ืืื ืื ื ืืืื ืืืขืืืจ Tool ืฉืืืื ืืฃ ืืืืื ืืจื ื ืฉืื ื ืืืชื, ืืืฉื ืืืืฆืขืืช requests, ืื ืืืฉืชืืฉ ืืืื fetch ืืชืื ืฉืจืช ื MCP ืืฆืืืืจื ืฉืจืืื ื ืงืืื. ืืืืจ ืืื ืื ื ืืืจ ืืืืขืื ืืืชืื Tool ืืขืฆืื ื ืืืื ื ืจืื ืืช ืืงืื ืฉืืชืืืจ ืืฉืจืช ื MCP ืืฆืืืืจื - ืืืกืชืืจ ืฉืืื ืื ืืกืืื:
import asyncio
from agents import Agent, Runner
from agents.mcp import MCPServer, MCPServerStreamableHttp
from agents.model_settings import ModelSettings
async def run(mcp_server: MCPServer):
agent = Agent(
name="Assistant",
instructions="Use the tools to answer the questions.",
mcp_servers=[mcp_server],
model_settings=ModelSettings(tool_choice="required"),
)
result = await Runner.run(agent, "Summarize the top stories from ynet today. URL is: https://www.ynet.co.il/news/category/184")
print(result.to_input_list())
print(result.final_output)
async def main():
async with MCPServerStreamableHttp(
name="Streamable HTTP Python Server",
params={
"url": "https://remote.mcpservers.org/fetch/mcp",
},
) as server:
await run(server)
if __name__ == "__main__":
asyncio.run(main())
ืคืื ืงืฆืืืช main ืืืฆืจืช ืืช ืืืืืืจ ืืฉืจืช ื mcp ืืืขืืืจื ืืืชื ืืคืื ืงืฆืื run. ืืคืื ืงืฆืื run ืืขืืืจื ืืช ืืฉืจืช ืืกืืื ืืฉืืืืช ืืช ืืฉืืืืชื. ืืชืืฆืื ืืื ืจืฉืืืช ืืืืชืจืืช ื ynet ืฉืืกืืื ืืืจืื ืืืืื ืืจื ื.
ืขืืฉืื ืืชื
ืืขืืื ืืื:
https://github.com/modelcontextprotocol/servers
ืชืืืื ืืืฆืื ืืืื ืฉืจืชื MCP ืืขื ืืื ืื ืขืืืจ ืืกืืื ืื ืฉืืื. ืืฆืื ืฉืจืช ืฉืืขื ืืื ืืชืื ืืืชืื ืชืืื ืืช ืคืืืชืื ืฉืืืืืฉื ืืื ืืกืืื ืืชืืืจ ืืืืกืฃ ืืืืื ืืฉืจืช ื MCP ืฉืืืจืชื.1 419
ืกื ืืื Handoff ืืื ืงืื ืกืคื ื ืืื ืืื ืืจืื ืคืขืืื ืืืื ืื ื ืืืชืจ ืงื ืืืืฉ ืืืชื ืืขืฆืื ื ืขื ืืืืจืช ืืืื ืืืืืื ืืืืจืื ืื ืืจืืืืืืืืื. ืืชืื ืืืืืช ื Runner ืฉื Agents SDK ืืชืื ืืช ืืืจืืืืช ืื ื ืจืฆื ืืืฉืชืืฉ ื Handoff ืืื ืกืืื ืจืืฉื ืฉืืืืื ืคื ืืืช ืืกืืื ืื ืกืคืฆืืคืืื ืฉืืืคืืื ืืกืืื ืืคื ืืืช ืืืฉืจ ืืืืคืื ืื ืืืจืฉ ืืืฉื ืืื ืืจืงืฆืื ืขื ืืืฉืชืืฉ.
1 419
ืืื 14 - ืืขืืจืช ืฉืืืื ืืกืืื ืืืจ handoff
ืืขืืจืช ืฉืืืื ืืื ืกืื ืืืืื ืฉื ืืคืขืืช ืืื ืื ืกืืื ืืื ืืืื ืฉืืฉ ืกืืื ืืืจ ืฉืืืื ืืืชืจ ืืืืื ืืื ื ืืคืชืืจ ืืขืื ืกืคืฆืืคืืช ืืืื ืืขืืืจ ืืช ืืฉืืืื ืืกืืื ืืฉื ื. ืืืื ื ืจืื ืืื ืื ืืืืืจ ื Agents SDK ืืืื ืืฉืื ืืฉืื ืื.
ืืื ืฆืจืื ืืกืคืจ ืกืืื ืื
ืืคื ื ืฉื ืืืข ืืงืื ืฉื handoff ืืืื ืืขืฆืืจ ืืืฉืืื - ืืื ืฆืจืื ืืกืคืจ ืกืืื ืื? ืืื ืืกืืื ืืจืืฉื ืื ืืืื ืืืฆืข ืืช ืื ืืืฉืืืืช? ืื ื ืืื ืจืขืืื ืืช ืืชืจืืืฉืื ืืื ืืกืคืจ ืกืืื ืื ืืืืืื ืืขืืืจ:
1. ืื ืื ืื ื ืืืืืื ืืืืืช ืกืื ืืกืืื ืฉื ืืงืฉืืช ืฉืืงืืื ืืืคืื ืืื ืืืชืจ ืืืืื ืืืจ - ืืืืืื ืจืื ืืืงืฉืืช ืฉืื ืืืืคืืืช ืืฆืืจื ืืืื ืขื ืืื gpt-4.1-mini ืืื ืืฉ ืืงืฉืืช ืืกืืืืืช ืืืืืื ืฉืืืื ืื ืืืขืืืจ ืืืืื ืืืชืจ ืืืื. ืื ืืืื ืืฉ ืื ืกื ืฉื ืืงืฉืืช ืฉืื ื ืืืื ืืคืชืืจ ืืขืืจืช ืืืื ืืื ืืืชืจ ืืื ืืืืจืื ืขืืืืืช.
2. ืื ืื ืื ื ืืืืื ืกืื ืืกืืื ืฉื ืืงืฉืืช ืขืืืจื ืกื ืืืจืืืช (instructions) ืฉืื ื ืืืชื ืชืืฆืืืช ืืืืืช ืืืชืจ.
3. ืื ืื ืื ื ืจืืฆืื ืืฉืืื ืืื ืืืชืจ ืืงืื ืืงืกื, ืืืืืจ ืืืขืืืจ ืืงืฉื ืืกืืืืช ืืื ืืืืกืืืจืื ืฉืื ืื ืขื ืืืกืืืจืื ืืืงืืช ืืืืื ืืืจ ืืืืคืื.
ืืืงืจืื ืืืื ื ืืื ืืืืืืจ handoff, ืืืืืจ ืืคืขืืช ืฉื ืืื ืืืืื ืฉืคืื ื ืืกืืื ืืืจ ืืืืงืฉ ืืื ื ืืฉืืื. ืื ืืขืื ืืฉืืื ืืชืื ืืฉืืื ืืื ืืืืืข ืืชืฉืืื ืกืืคืืช.
ืืืืื - ืกืืื ื ืชืจืืื
ืืืืืื ืืจืืฉืื ื ืื ื ืืืืืจ ืฉืืืฉื ืกืืื ืื: ืกืืื ืืื ืืืืข ืืชืจืื ืืืจืื ืืฆืจืคืชืืช, ืกืืื ืฉื ื ืืชืจืื ืืกืคืจืืืช ืืกืืื ืฉืืืฉื ืืืจืื ืขื ืจืื ืืฉืืื. ืื ืืกืืื ืืจืืฉื ืืืื ืืงืฉื ืืชืจืืื ืืื ืืขืืืจ ืืช ืืืงืฉื ืืกืืื ืืืชืืื:
french_agent = Agent(
name="French Translator",
instructions="Translate everything to french"
)
spanish_agent = Agent(
name="Spanish Translator",
instructions="Translate everything to Spanish")
triage_agent = Agent(name="Triage agent", handoffs=[french_agent, spanish_agent])
async def main():
session = SQLiteSession("handoffs", "handoffs.db")
result = await Runner.run(triage_agent, "translate to French: 'hello world'", session=session)
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืคืขืืช ืืงืื ืืืคืืกื bonjour le monde ืฉืื ืืชืจืืื ืืฆืจืคืชืืช ืฉื hello world.
ืืืื ืช ืจืฉืืืช ืืืืืขืืช ืืืจื ืืคืขืืช ืืชืืื ืืช ืืฆืืื ืืช ืืจืฉืืื ืืืื:
1|handoffs|{"content": "translate to French: 'hello world'", "role": "user"}|2025-08-17 11:49:06
2|handoffs|{"arguments": "{}", "call_id": "call_Abq5WDOv6C1RZVKNsCg4WO0F", "name": "transfer_to_french_translator", "type": "function_call", "id": "fc_68a1c1b13b8081988a6174005b308dd20e79e771496ac495", "status": "completed"}|2025-08-17 11:49:06
3|handoffs|{"call_id": "call_Abq5WDOv6C1RZVKNsCg4WO0F", "output": "{\"assistant\": \"French Translator\"}", "type": "function_call_output"}|2025-08-17 11:49:06
4|handoffs|{"id": "msg_68a1c1b27324819892a9f00a9d0b95f50e79e771496ac495", "content": [{"annotations": [], "text": "'hello world' se traduit par 'bonjour le monde'.", "type": "output_text", "logprobs": []}], "role": "assistant", "status": "completed", "type": "message"}|2025-08-17 11:49:06
ืื ืื ื ืจืืืื ืืคืื:
1. ืืืืขื ืจืืฉืื ื ืืืืฉืชืืฉ ืขื ืืงืฉื ืืชืจืื ืืฆืจืคืชืืช.
2. ืืืืขื ืขื Tool Call ืฉื ืืขืืจืช ืฉืืืื ืืกืืื ืฉื ื.
3. ืกืืื Tool Call ืขื ืืคืื "{"assistant": "French Translator"}".
4. ืคื ืื ืฉื ื Runner ืืกืืื ืืชืจืืื ืืฆืจืคืชืืช ืืืืืขื ืืื ื ืขื ืืืงืกื ืืืชืืจืื.
ืืื ืืฉืื ืื ืืฉืืืฆืขืื Handoff
ืืื ื ืงืืืืช ืฉืืืื ืืืืืจ ืืืื Handoff ืืืืคืื ืืฉืืืืฉ ืืขืื ืืคืืฆ'ืจ:
1. ืื ืืชื ืืขืืืจืื ืฉืืืื ืืกืืื ืฉืฆืจืื ืืืืฉืื ืืชืงืฉืจ ืขื ืืืฉืชืืฉ ืฉืืื ืื ืฉืชืฆืืจืื ืืขืฆืืื ืืืขืืืจ ืืช ืืกืืื ืืืืฉ ื Runner.run ืืืืืขื ืืืื (ืืืจืช ืืื ืืจืืฅ ืขื ืืกืืื ืฉืขืืืจ ืืชืืจ ืคืจืืืจ ืจืืฉืื).
2. ืืืืื ืช ืืืืื Handoff ืืื ืืกื ืืื Tool Calling. ืฉื ืืืื ืืืจืืจืช ืืืื ืืื transfer_to_<agent_name>. ืฉืืื ืื ืื ืืืชื ืืฉ ืขื ืืืื ืงืืืืื ืฉืืชื ืืืืืจืื ืืกืืื.
3. ืื ืืฉ ืืื ืงืื ืืืฉืง ืืฉืชืืฉ ืฉืืฆืื ืืืกืืืจืืืช ืฉืืื ืืืื ืืืืืช Handoff ืืืชืช ืื ืืืฆืื ืืืืืืื ืฉืื ื ื Tool Calling ืจืืื ืืื ืืืชืืื ืืฆืืคืืืช ืืืฉืชืืฉืื.1 419
if result.tool.name == "open_box":
return ToolsToFinalOutputResult(is_final_output=True, final_output=result.output)
# Otherwise, continue
return ToolsToFinalOutputResult(is_final_output=False)
ืืชื ืืฉืชืืฉืื ืืื ืืจื
ืืคืืกื ืจืืื ื ืฉืชื ืืจืืื ืืฉืืื ืืืขืฆืืจ ืืช ืืืืืช ืืคืขืืช ืืืืื. ืืืขืจืืืช ืืืืชืืืช ื ืจืฆื ืืฉืื ืืช ืฉืชื ืืืจืืื:
1. ื ืจืฆื ืืืฉืชืืฉ ื max_turns ืืื ืืืืื ืฉืืืืื ืื ืืืฆื ืืฉืืืื ืืืคืขืืช ืืืืื. ืืคืขืื ืฉื ืืืชืจ ืืื ืืืื ืขืืืื ืืืืืช ืกืืื ืฉืืืืื ื ืื ืก ืืืืืื ืืื ืืฆืืื ืืืฉื ืืช ืืชืฉืืื ืืืื.
2. ื ืจืฆื ืืืฉืชืืฉ ืืคืื ืงืฆืืืช ืืืงืจื ืืื ืืืืฉ ืกืืื ืื ืฉืืืืขืื "ืืืืชืจ ืขื ืืฉืืืื", ืืืืืื ืกืืื ืฉืืืจืื ืขื ืืืฆืืข ืฉืื ืืกืืื ืืชืืืื ืืืฉืืื ืืกืืื ืืื ืืืื ืืืคืขืื ืืื "ืกืืื" ืฉืืขืืืจ ืืช ืืชืืฆืื ืืกืืื ืืืจ ืืืืฉื ืขืืืื. ื ืืื ืืืฉืชืืฉ ืืคืื ืงืฆืื ืื ืืืงืจื ืฉืืกืืื ืื ืื ืกื ืืืคืขืื ืืืื ืืืชืืกืก ืขื ืืืืข ืฉืืื ืืืืืฆืื ืืืชื ืืืืืืืช ืฉื ืืืฉืืืื ืฉืืืืื.
ืืืืฉื ืืกืืืจื ื ืืืจ ืขื ืขืื ืฉื ื ืื ืื ืื ืื ืืฉืืืื ืขื ืกืืื ืื - ืื ืื ืื Handoff ืืื ืื ืื Guardrails, ืื ืืื ืืื ืืืจืื ืืขืืืจ ืื ื ืืขืฆืืจ ืคืขืืื ืฉื ืกืืื ืืคื ื ืฉืืกืชืืืื.
ืขืืฉืื ืืชื
ืืชืื ืกืืื ืฉืืงืื ืฉืืืฉื ืืืื - ืืฆืืช ืจืฉืืืช ืืงืืฆืื ืืชืืงืืื, ืงืจืืืช ืืงืกื ืืงืืืฅ ืืื ืืกื ืืชืืงืื ืืืจืช. ืืฉืชืืฉื ืืกืืื ืืื ืืืคืฉ ืงืืืฅ ืฉืืืื ืืงืกื ืืกืืื. ืชืขืื ืืช ืืคืขืืช ืืืืื. ืืืืจ ืืื ืืืกืืคื "ืืืงืฉืื", ืงืืฆืื ืฉืืฉืคืืชืืื ืืืชื ืืืืืช ืืคืขืืช ืืืืื ืฉื ืืกืืื ืชืืขืฆืจ.1 419
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x104dd1bc0>,
strict_json_schema=True,
is_enabled=True
),
output='An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
run_item=ToolCallOutputItem(
agent=Agent(
name='Assistant',
handoff_description=None,
tools=[
FunctionTool(
name='open_box',
description='Open a box and search for the treasure inside. Returns True if found',
params_json_schema={
'properties': {
'id': {
'title': 'Id',
'type': 'string'
}
},
'required': ['id'],
'title': 'open_box_args',
'type': 'object',
'additionalProperties': False
},
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x104dd1bc0>,
strict_json_schema=True,
is_enabled=True
),
FunctionTool(
name='get_box_ids',
description='',
params_json_schema={
'properties': {},
'title': 'get_box_ids_args',
'type': 'object',
'additionalProperties': False,
'required': []
},
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x110d2a200>,
strict_json_schema=True,
is_enabled=True
)
],
mcp_servers=[],
mcp_config={},
instructions=None,
prompt=None,
handoffs=[],
model=<agents.extensions.models.litellm_model.LitellmModel object at 0x110aa7620>,
model_settings=ModelSettings(
temperature=None,
top_p=None,
frequency_penalty=None,
presence_penalty=None,
tool_choice=None,
parallel_tool_calls=None,
truncation=None,
max_tokens=None,
reasoning=None,
metadata=None,
store=None,
include_usage=None,
response_include=None,
extra_query=None,
extra_body=None,
extra_headers=None,
extra_args=None
),
input_guardrails=[],
output_guardrails=[],
output_type=None,
hooks=None,
tool_use_behavior=<function custom_tool_use_behavior at 0x110d2a0c0>,
reset_tool_choice=True
),
raw_item={
'call_id': 'call_4cPEq87qI3ccfPvCEQKZrelY',
'output': 'An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
'type': 'function_call_output'
},
output='An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
type='tool_call_output_item'
)
)
---
ืื ืื ื ืจืืืื ืฉืืคืื ืงืฆืื ื ืงืจืื ืื ืคืขื ืฉืืืืื ืืคืขืื ืืื ืขื ืืชืืฆืื ืฉื ืืืื ืฉืืืคืขื ืืคืจืื ืืืื. ืืื ืื ื ืืช ืืคืจืืืจืื ืฉืืืืื ืืขืืืจ ืืืื.
ืืฉืืื ืืขืฆืืจ ืืช ืืืืืื ืื ื ืืืื ืืืืืืจ ืืืืืงื ToolsToFinalOutputResult ืขื is_final_output ืืืืืจ ื True, ืืืืืื ืืงืื ืืื ืขืืฆืจ ืืช ืืืืืื ืืืจื ืคืชืืืช ืืงืืคืกื ืืจืืฉืื ื:
def custom_tool_use_behavior(context, tool_results: list[FunctionToolResult]) -> ToolsToFinalOutputResult:
print(context)
for result in tool_results:1 419
handoff_description=None,
tools=[
FunctionTool(
name='open_box',
description='Open a box and search for the treasure inside. Returns True if found',
params_json_schema={
'properties': {
'id': {
'title': 'Id',
'type': 'string'
}
},
'required': ['id'],
'title': 'open_box_args',
'type': 'object',
'additionalProperties': False
},
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x104dd1bc0>,
strict_json_schema=True,
is_enabled=True
),
FunctionTool(
name='get_box_ids',
description='',
params_json_schema={
'properties': {},
'title': 'get_box_ids_args',
'type': 'object',
'additionalProperties': False,
'required': []
},
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x110d2a200>,
strict_json_schema=True,
is_enabled=True
)
],
mcp_servers=[],
mcp_config={},
instructions=None,
prompt=None,
handoffs=[],
model=<agents.extensions.models.litellm_model.LitellmModel object at 0x110aa7620>,
model_settings=ModelSettings(
temperature=None,
top_p=None,
frequency_penalty=None,
presence_penalty=None,
tool_choice=None,
parallel_tool_calls=None,
truncation=None,
max_tokens=None,
reasoning=None,
metadata=None,
store=None,
include_usage=None,
response_include=None,
extra_query=None,
extra_body=None,
extra_headers=None,
extra_args=None
),
input_guardrails=[],
output_guardrails=[],
output_type=None,
hooks=None,
tool_use_behavior=<function custom_tool_use_behavior at 0x110d2a0c0>,
reset_tool_choice=True
),
raw_item={
'call_id': 'call_AakkVgr7fikDFcam0VADoa9z',
'output': 'An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
'type': 'function_call_output'
},
output='An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
type='tool_call_output_item'
)
)
---
RunContextWrapper(
context=None,
usage=Usage(
requests=2,
input_tokens=939,
input_tokens_details=InputTokensDetails(
cached_tokens=0
),
output_tokens=78,
output_tokens_details=OutputTokensDetails(
reasoning_tokens=0
),
total_tokens=1017
)
)
FunctionToolResult(
tool=FunctionTool(
name='open_box',
description='Open a box and search for the treasure inside. Returns True if found',
params_json_schema={
'properties': {
'id': {
'title': 'Id',
'type': 'string'
}
},
'required': ['id'],
'title': 'open_box_args',
'type': 'object',
'additionalProperties': False
},1 419
7|boxes|{"call_id": "call_0ukIo397uSCrgRHDNwJugFuC", "output": "True", "type": "function_call_output"}|2025-08-14 10:13:28
8|boxes|{"id": "__fake_id__", "content": [{"annotations": [], "text": "I opened the first box, but didn't find the treasure. Then I opened the second box and found the treasure inside! \n\nI found the treasure by opening only 2 boxes.", "type": "output_text"}], "role": "assistant", "status": "completed", "type": "message"}|2025-08-14 10:13:28
ืืคืขื ืื ืื ื ืจืืืื ืืื ื ืฉืื ื - ืืืืื ืื ืืื ืืืขืืืจ ืืช ืื ืืงืจืืืืช ืืืื ืืืช ืืชืฉืืื ืืืืืขื ืืจืืฉืื ื ืื ืืื ืื ืืืข ืื ืืืืืื ืฉื ืืงืืคืกืืืช. ืืคืืื ืืืจื ืฉืืื ืืฉืชืืฉ ืืืื ืืื ืืงืื ืจืฉืืืช ืืืืื ืืื ืขืืืื ืื ืจืฆื ืืคืชืื ืืช ืื ืฉืืืฉืช ืืงืืคืกืืืช ืืืื ืืืช ืื ืืืงืฉืชื ืืฆืืฆื ืืช ืืกืคืจ ืืคืชืืืืช, ืืื ืื ืื ื ืจืืืื ืืช ืืขืืืื ืคืขืืื ืืืจื ืคืขืืื.
ืื ืื ืืื ืืืืช ืื ืืืคื ืงืืคืกืืืช? ืื ืื ืืื ืื ืืื ืืืฆืจ?
ืฉืืืื ืขื ืืืืืช ืืคืขืืช ืืืืื
ื ืฉืื ืื ืฉืกืคืจืืืช OpenAI Agents SDK ืขืืืืช ืืืืืื - ืื ืคืขื ืฉืืืืื ืืืงืฉ ืืืคืขืื ืืื ืืกืคืจืื ืืคืขืืื ืืช ืืคืื ืงืฆืื, ืืืกืืคื ืืช ืืืืืขื ืืืชืืืื ืืจืฉืืืช ืืืืืขืืช ืืคืื ื ืฉืื ืืืืื. ืืืืื ืืืืช ืืืืื ืืงืืช ืืจืื ืืื ืืืืจ ืืื ืคื ืื ืืืืื ืืืืื ืืืืืช ืืืืืช ืืื ืืขืืืช ืืืจืื ืืืงื ืื ืืืื ืืจืื ืืกืฃ, ืืืืืื ืื ืืืืื ืืชืืืื ืืืคืขืื ืืืื ืืื ืืืืืข ืืชืืฆืื.
ืืจื ืืืช ืืฉืืื ืืืืืื ืืื ืืคืจืืืจ max_turns ืฉื ืืชื ืืืขืืืจ ื Runner.run. ืคืจืืืจ ืื ืืืืื ืืช ืืกืคืจ ืืืืืจืฆืืืช - ืื ืืืืื ืื ืกื ืืืคืขืื ืืืื ืืืชืจ ืคืขืืื ืืื ืฉืืืืืจ ืืืฉืชื ื ืืกืคืจืื ืชืคืกืืง ืืช ืืืืืื ืืชืกืืื ืขื ืฉืืืื. ืื ื ืจืื ืื:
async def main():
session = SQLiteSession("boxes", "boxes.db")
result = await Runner.run(agent, """
Let's play a game. You are given 3 boxes and you need to find the treasure hidden in one of them. You can open any box you want
to look inside.
Use open_box tool to open boxes and find the treasure.
Try to find it by opening the miniumum number of boxes.
""", session=session, max_turns=3)
print(result.final_output)
ืืืจืฆื ืขื ืฉืืืื ืืกืชืืืืช ืื:
agents.exceptions.MaxTurnsExceeded: Max turns (3) exceeded
ืืจื ื ืืกืคืช ืืฉืืื ืืืืืื ืืื ืืืืคืืื tool_use_behavior ืืืืืจืช ืกืืื:
agent = Agent(
name="Assistant",
model=LitellmModel(model="github/gpt-4.1", api_key=os.environ["GITHUB_TOKEN"]),
tools=[open_box, get_box_ids],
tool_use_behavior=custom_tool_use_behavior
)
ืืืคืืื ืื ืืืื ืืงืื ืคืื ืงืฆืื ืฉืชืืื ืืืืืื ืื ืืืืฉืื ืืช ืืืืืื ืื ืืืคืกืืง. ืื ื ืืจืืฅ ืืช ืืคืื ืงืฆืื ืืืื ืงืืื ืื ืืฉืืื ืืืงืืจ ืืช ืืคืจืืืจืื ืฉืื:
def custom_tool_use_behavior(context, tool_results: list[FunctionToolResult]) -> ToolsToFinalOutputResult:
print(context)
for result in tool_results:
print(result)
print("---")
# Otherwise, continue
return ToolsToFinalOutputResult(is_final_output=False)
ืืืคืื ืืืืจ ืกืืืืจ ืืื:
RunContextWrapper(
context=None,
usage=Usage(
requests=1,
input_tokens=437,
input_tokens_details=InputTokensDetails(
cached_tokens=0
),
output_tokens=40,
output_tokens_details=OutputTokensDetails(
reasoning_tokens=0
),
total_tokens=477
)
)
FunctionToolResult(
tool=FunctionTool(
name='open_box',
description='Open a box and search for the treasure inside. Returns True if found',
params_json_schema={
'properties': {
'id': {
'title': 'Id',
'type': 'string'
}
},
'required': ['id'],
'title': 'open_box_args',
'type': 'object',
'additionalProperties': False
},
on_invoke_tool=<function function_tool.<locals>._create_function_tool.<locals>._on_invoke_tool at 0x104dd1bc0>,
strict_json_schema=True,
is_enabled=True
),
output='An error occurred while running the tool. Please try again. Error: coroutine raised StopIteration',
run_item=ToolCallOutputItem(
agent=Agent(
name='Assistant',1 419
14|weather|{"id": "__fake_id__", "content": [{"annotations": [], "text": "Here is today's weather in major Israeli cities:\n\n- Tel Aviv: Clear sky, 36.9\u00b0C (feels like 40.23\u00b0C), 38% humidity\n- Jerusalem: Clear sky, 38.34\u00b0C (feels like 42.39\u00b0C), 36% humidity\n- Haifa: Clear sky, 33.39\u00b0C (feels like 34.41\u00b0C), 40% humidity\n- Eilat: Clear sky, 42.91\u00b0C (feels like 45.23\u00b0C), 23% humidity\n\nIt\u2019s a very hot and sunny day across the country\u2014plan accordingly!", "type": "output_text"}], "role": "assistant", "status": "completed", "type": "message"}|2025-08-14 09:59:42
ืืคืขื ืืืืขืช ืืืืื ืืืคืขืืช ืืื ืืืื ืืงืฉื ืืืคืขืืช ืืจืืขื ืืืื. ืกืคืจืืืช ืืกืืื ืื ืจืฉืื ืืช ืื ืืชืืจ ืืจืืข ืืืืขืืช ืืืืื, ืืืกืืคื 4 ืชืฉืืืืช ืืื ืคื ืชื ืฉืื ืืืืื ืืื ืืืืฆืจ ืืช ืืชืฉืืื ืืกืืคืืช.
ืืืืืช ืืคืขืืช ืืืื
ืื ืื ืื ืืคืขืืช ืืืืื ืฉืจืืื ื ื ืงืจื React ืฉืื ืงืืฆืืจ ืฉื Reason + Act. ืกืืื ื ืจืืืงื ืขืืืืื ืืืืืื ืืืืืืื ืืืคืขืื ืืืื ืฉืื ืืฉืื ืขื ืฉืืืืขืื ืืชืฉืืื ืกืืคืืช. ื ืจืื ืืืืื ืฉืืืืืช ืืคืขืืช ืืกืคืจ ืืืื ืืื ืืืจื ืืฉื ื - ืื ื ืืืืฆืจ ืฉืืืฉ ืงืืคืกืืืช ืขื ืืืืื ืืงืจืืืื ืืืืงืฉ ืืืืืื ืืคืชืื ืงืืคืกืืืช ืขื ืฉืืื ืืืืข ืืืืฆืจ:
from typing_extensions import TypedDict
from agents import Agent, function_tool, Runner, SQLiteSession
from agents.extensions.models.litellm_model import LitellmModel
from uuid import uuid4
import requests
import os
import asyncio
boxes = [
{
"id": str(uuid4()),
"has_treasure": False
},
{
"id": str(uuid4()),
"has_treasure": True
},
{
"id": str(uuid4()),
"has_treasure": False
}
]
@function_tool
async def open_box(id: str) -> bool:
"""Open a box and search for the treasure inside. Returns True if found"""
return next(b['has_treasure'] for b in boxes if b['id'] == id)
@function_tool
async def get_box_ids() -> list[str]:
return [b['id'] for b in boxes]
agent = Agent(
name="Assistant",
model=LitellmModel(model="github/gpt-4.1", api_key=os.environ["GITHUB_TOKEN"]),
tools=[open_box, get_box_ids],
)
async def main():
session = SQLiteSession("boxes", "boxes.db")
result = await Runner.run(agent, """
Let's play a game. You are given 3 boxes and you need to find the treasure hidden in one of them. You can open any box you want
to look inside.
Use open_box tool to open boxes and find the treasure.
Try to find it by opening the miniumum number of boxes.
""", session=session)
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืื ืืืืืขืืช ืฉื ืืฆืจื ืืคืขื:
sqlite> select * from agent_messages ;
1|boxes|{"content": "\n Let's play a game. You are given 3 boxes and you need to find the treasure hidden in one of them. You can open any box you want\n to look inside.\n Use open_box tool to open boxes and find the treasure. \n Try to find it by opening the miniumum number of boxes.\n ", "role": "user"}|2025-08-14 10:13:28
2|boxes|{"arguments": "{}", "call_id": "call_Rjqjj5dQyxCISwpPMR3hrrai", "name": "get_box_ids", "type": "function_call", "id": "__fake_id__"}|2025-08-14 10:13:28
3|boxes|{"call_id": "call_Rjqjj5dQyxCISwpPMR3hrrai", "output": "['c77fa56d-0375-4e8d-9470-3f494b16706c', '1e499625-705d-40f7-bae4-df71bf97c523', 'd3f99c22-33fd-4cce-b7c2-9f7de13c122e']", "type": "function_call_output"}|2025-08-14 10:13:28
4|boxes|{"arguments": "{\"id\":\"c77fa56d-0375-4e8d-9470-3f494b16706c\"}", "call_id": "call_MU2FdBwcGn9ayc0f04UyS1ea", "name": "open_box", "type": "function_call", "id": "__fake_id__"}|2025-08-14 10:13:28
5|boxes|{"call_id": "call_MU2FdBwcGn9ayc0f04UyS1ea", "output": "False", "type": "function_call_output"}|2025-08-14 10:13:28
6|boxes|{"arguments": "{\"id\":\"1e499625-705d-40f7-bae4-df71bf97c523\"}", "call_id": "call_0ukIo397uSCrgRHDNwJugFuC", "name": "open_box", "type": "function_call", "id": "__fake_id__"}|2025-08-14 10:13:281 419
2|weather|{"arguments": "{\"location\":{\"lat\":32.0853,\"long\":34.7818}}", "call_id": "call_obLi5Lbz93fzOIDRvC195udg", "name": "fetch_weather", "type": "function_call", "id": "__fake_id__"}|2025-08-14 09:54:56
3|weather|{"call_id": "call_obLi5Lbz93fzOIDRvC195udg", "output": "Weather: Clear Sky, Temperature: 36.9\u00b0C (feels like 40.23\u00b0C), Humidity: 38%", "type": "function_call_output"}|2025-08-14 09:54:56
4|weather|{"id": "__fake_id__", "content": [{"annotations": [], "text": "The weather in Tel Aviv is currently clear sky with a temperature of 36.9\u00b0C (feels like 40.23\u00b0C) and humidity at 38%. It's quite hot today!", "type": "output_text"}], "role": "assistant", "status": "completed", "type": "message"}|2025-08-14 09:54:56
ื ืฉืื ืื ืฉืฉืืื ื ืืืืขื ืืืืฉืชืืฉ, ืืชืืืื ืืืืื ืขื ื ืืืืืขื ืขื ืืงืฉื ืืืคืขืืช ืืื ืื ืชื ืื ืืืื call_obLi5Lbz93fzOIDRvC195udg, ืืืืจ ืืื ืกืคืจืืืช ืืกืืื ืื ืืคืขืืื ืืช ืืคืื ืงืฆืื ืืืืกืืคื ืชืืืื ืืืฉื ืขื JSON ืฉืืืื ืืช ืืืชื ืืืื ืืืกืืฃ ืืืืื ืืืกืืฃ ืืืืขื ืฉืขืื ื ืืืฉืชืืฉ ืืขืืจืช ืืชืฉืืื ืฉืืืืขื ืืืืื. ืกื ืืื ืืคืงืืื:
result = await Runner.run(agent, "What is the weather in Tel Aviv?", session=session)
ืคื ืชื ืืืืื ืคืขืืืื - ืืคืขื ืืจืืฉืื ื ืงืืืื ืืงืฉื ืืืคืขืืช ืืื, ืืืคืขื ืืฉื ืื ืงืืืื ืืช ืืืงืกื ืฉืืืืืจ ื result.final_output.
ืืืื ื ืืขื ืื ืขืื ืงืฆืช ืื ืืงืฉ ืืช ืืื ืืืืืืจ ืืืกืคืจ ืืงืืืืช:
async def main():
session = SQLiteSession("weather", "weather.db")
result = await Runner.run(agent, "I'm planning a trip to Israel, what is the weather in Tel Aviv, Jerusalem, Haifa and Eilat today?", session=session)
print(result.final_output)
ืืื ืืืืืขืืช ืืืกืืก ืื ืชืื ืื:
5|weather|{"content": "I'm planning a trip to Israel, what is the weather in Tel Aviv, Jerusalem, Haifa and Eilat today?", "role": "user"}|2025-08-14 09:59:42
6|weather|{"arguments": "{\"location\": {\"lat\": 32.0853, \"long\": 34.7818}}", "call_id": "call_la2QHswRUH0Xa80wZNLQ6dOv", "name": "fetch_weather", "type": "function_call", "id": "__fake_id__"}|2025-08-14 09:59:42
7|weather|{"arguments": "{\"location\": {\"lat\": 31.7683, \"long\": 35.2137}}", "call_id": "call_AEGdktcxYfR2MzGS7BzhA9AY", "name": "fetch_weather", "type": "function_call", "id": "__fake_id__"}|2025-08-14 09:59:42
8|weather|{"arguments": "{\"location\": {\"lat\": 32.794, \"long\": 34.9896}}", "call_id": "call_CFb2TkCz6M6eL19aT7c69Zae", "name": "fetch_weather", "type": "function_call", "id": "__fake_id__"}|2025-08-14 09:59:42
9|weather|{"arguments": "{\"location\": {\"lat\": 29.5577, \"long\": 34.9519}}", "call_id": "call_DWgN1qFAru8ScClVHj1jgqIz", "name": "fetch_weather", "type": "function_call", "id": "__fake_id__"}|2025-08-14 09:59:42
10|weather|{"call_id": "call_la2QHswRUH0Xa80wZNLQ6dOv", "output": "Weather: Clear Sky, Temperature: 36.9\u00b0C (feels like 40.23\u00b0C), Humidity: 38%", "type": "function_call_output"}|2025-08-14 09:59:42
11|weather|{"call_id": "call_AEGdktcxYfR2MzGS7BzhA9AY", "output": "Weather: Clear Sky, Temperature: 38.34\u00b0C (feels like 42.39\u00b0C), Humidity: 36%", "type": "function_call_output"}|2025-08-14 09:59:42
12|weather|{"call_id": "call_CFb2TkCz6M6eL19aT7c69Zae", "output": "Weather: Clear Sky, Temperature: 33.39\u00b0C (feels like 34.41\u00b0C), Humidity: 40%", "type": "function_call_output"}|2025-08-14 09:59:42
13|weather|{"call_id": "call_DWgN1qFAru8ScClVHj1jgqIz", "output": "Weather: Clear Sky, Temperature: 42.91\u00b0C (feels like 45.23\u00b0C), Humidity: 23%", "type": "function_call_output"}|2025-08-14 09:59:421 419
ืืื 12 - ืืืืืช ืืคืขืืช ืืืืื
ืืคืขืืช ืืืื ืืื ืืฆื ืืื ืืืืืืช ืืื ืืฉืืขืืชืืช ืฉื ืืืืืื, ืืื ืืืืื ืืกืืก ืืืจืื ืชืื ืืืช ืขืืืื ืขื ืืืืืื ืืื ืืฆื ืฉื ื ืืืื ืืืืคื ืืื ืืืื ืฉื ืืืืืื ืืื ืื ืืืืืืช ืืืงืืจ ืืืชื ื ืืื ืืืืืื. ืืืื ื ืืื ืืืชืจ ืืขืืืง ืืื ืื ืื ืื ืืคืขืืช ืืืืื ืขืืื, ืื ื ืงืืืืช ืืืงืจื ืฉืื ื ืืื ืื ืื ืืืืืื ืืืจืืช ื ืจืฆื ืืืฉืชืืฉ ืื.
ืืื ืื ืขืืื - ืืืืืช ืืคืขืืช ืืืื
ืืืื ื ืชืืื ืขื ืชืืื ืืช ืจืืฉืื ื ืืืขื ืืขืชืงื ืืืืืืื ืืชืืขืื ืฉืฉืืืืช ืืืื ืื ืืื ืืืืืืจ ืื ืืชื ืช ืื ืืื ืืืืฆืขืืชื ืืืื ืืขื ืืช:
from typing_extensions import TypedDict
from agents import Agent, function_tool, Runner
from agents.extensions.models.litellm_model import LitellmModel
import requests
import os
import asyncio
weather_api_key = os.getenv("OPENWEATHER_API_KEY")
if not weather_api_key:
raise "Error: OpenWeather API key not found in environment variables"
class Location(TypedDict):
lat: float
long: float
@function_tool
async def fetch_weather(location: Location) -> str:
base_url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"lat": location["lat"],
"lon": location["long"],
"appid": weather_api_key,
"units": "metric"
}
# Run the synchronous requests call in a thread pool
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(None, requests.get, base_url, params)
response.raise_for_status()
data = response.json()
weather_desc = data["weather"][0]["description"]
temp = data["main"]["temp"]
feels_like = data["main"]["feels_like"]
humidity = data["main"]["humidity"]
return f"Weather: {weather_desc.title()}, Temperature: {temp}ยฐC (feels like {feels_like}ยฐC), Humidity: {humidity}%"
agent = Agent(
name="Assistant",
model=LitellmModel(model="github/gpt-4.1", api_key=os.environ["GITHUB_TOKEN"]),
tools=[fetch_weather],
)
async def main():
result = await Runner.run(agent, "What is the weather in Tel Aviv?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืคืขืื ืฉื ืืชืืื ืืช ืืฆืื ืืืคืืกื ืืช ืืคืื:
The weather in Tel Aviv is currently clear with a temperature of 37.05ยฐC (feels like 40.16ยฐC) and humidity at 37%. It's quite hot outside!
ืืื ืืืื ื ืชืืงื ืืืื ืืืืคืขืืช ืืื - ืื ืื ืืืืจ ืฉืืืืื ืดืืคืขืื ืืืืด? ืืื ืืืื ืืืื ืืืคืขืื ืืื? ืืืื ืืืืื ืืืืข ืืืื ืืืื ืฆืจืื ืืืคืขืื?
ืืชืฉืืื ืืื ืฉืืืืื ืืชืงืฉืจ ืขื ืืงืื ืฉืื ื ืืืืฉืง ืืงืกืืืืื ืืืื - ืื ืื ื ืฉืืืืื ืืืืขืช ืืงืกื ืืืงืืืื ืืืืจื ืืงืกื. ืืืงืกื ืฉืืืืื ืืืืืจ ืืื ืืืืฉื ืืกืืืจ ืืืืชืจ ื"ืฉืืื" ืฉืืช ืชืืืืชื ืื ืื ื ืฉืืืืื. ืื ืขืืืืื ืืืืื ืฉืคื ืืืืืื.
ืืฉืืืืืจ ืืืืื ืืฉ ืคื ืฉืืชืืฃ ืคืขืืื ืืื ืืื ืจืืืืื ืืืขืจืืช:
1. ืืืืื ืขืฆืื ืืืืื ืืฆืืจื ืฉืืืืขืืช ืืกืื ืืกืืื ืืืืืื ืืชืฉืืืืช ืืกืื ืืกืืื. ืืคืจื ืื ืืฉื ืฉื ืืืื ืื ืืืืืขื ืืืืขื ืืืื ื ืืกืืื ืฉืืืื ืฉื ืฉื ืืื ืืจืฉืืืช ืคืจืืืจืื ืืืืื ืืืืื ืืขื ืืช ืืคืืจืื ืืชืืื ืืกืืื - ืฉื ืจืื ืืื JSON ืขื ืคืจืืื ืขื ืืืื ืืืชื ืจืืฆืื ืืืคืขืื ืืจืฉืืืช ืืคืจืืืจืื ืฉืื.
2. ืกืคืจืืืช ืืกืืื ืื OpenAI Agents ืืืื ืืช ืคืืจืื ืืชืฉืืื ืฉื ืืืื ืฉืืขืื ืืื ืืืคืขืื ืืื ืืืคืขืืื ืืช ืืคืื ืงืฆืื ืฉืืขืืจืชื, ืืืืืื ืฉืจืืื ื ืื ืคืื ืงืฆืืืช ืืืฉืื ืืื ืืืืืืจ. ืืช ืืชืืฆืื ืืื ืืขืืืจื ืืืจื ืืืืื. ืืืืฉืง ืืขืืจืช ืืืื ืืืืื ืืืงืฉ ืืคืขืืช ืืื ืืืฆืจืฃ ืืืงืฉื ืืืื ืืงืจืื, ืืกืคืจืืืช ืืกืืื ืื ืืฆืจืคืช ืืช ืืืชื ืืืื ืืืืืง ืืชืฉืืื.
3. ืืืืื ืืืื ืืืืืืฃ ืืืคืขืื ืืช ืืืื ืฉืื, ืืืืืจ ืืืืืืจ ืฉืื ืคืขื ืืงืกื ืฉืืืืฆื ืืคืขืืช ืืื, ืื ืืขืฆืืจ ืืืืืืืจ ืืืืขืช ืกืืื ืฉืืกืืืช ืืช ืืชืืฆืื ืฉื ืืืื.
ื ืืฉืื ืืืงืืจ - ืื ื ืืืกืืฃ SQLite Session ืืชืืื ืืช ืืื ืฉืืืื ืชืืขืื ืืื ืืืืืขืืช ืืืคืขืื ืืช ืืงืื ืืืขืืืื:
async def main():
session = SQLiteSession("weather", "weather.db")
result = await Runner.run(agent, "What is the weather in Tel Aviv?", session=session)
print(result.final_output)
ืขืืฉืื ื ืืฆืจ ืื ืงืืืฅ ืืืฉ ืืชืืงืืื ืืฉื weather.db ืขื ืื ืืืืืขืืช. ืื ืืชืืื:
sqlite> select * from agent_messages ;
1|weather|{"content": "What is the weather in Tel Aviv?", "role": "user"}|2025-08-14 09:54:561 419
name="Assistant",
tools=[
WebSearchTool(),
],
)
async def main():
result = await Runner.run(agent, "Which coffee shop should I go to, taking into account the weather today in SF?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืืืื ืืคืขืื ืืช ืืื ืืืืคืืฉ ืืืืื ื, ืืืคืฉ ืืชื ืงืคื ืืกืื ืคืจื ืกืืกืงื ืืืืืืจ ืจืฉืืื ืฉื ืืืืฆืืช. ืื ืื ืงืืจื ืืืืคื ืืืืืืื ืืืืื ื ืื. ืืืขืื ืืืืืื ืืื ืืืืืจ ืื ืืืคืืฉ ืืจืฉืช ืืืงื ืืจืื ืืื ืืืจืื ืืืงื ืื. ืืจืื ืคืขืืื ืืคืชืืื ืฉืฆืจืืืื ืืฉืื ืืืคืืฉ ืืจืฉืช ืขื ืืขืจืืช ืืฉืืืืช ืกืืื ืืฉืชืืฉืื ืืืื ืืืคืืฉ ืืชืืจืื ืืืืขืืืืื ืืกืืื ืื ืืืืืื ืื ืืข ืืืืคืืฉ ืชืืื-ืื ืฉืืืคืฉืจ ืืืคืฉ ืืจืฉืช ืืืืฆืขืืช ืงืื ืืื:
from tavily import TavilyClient
tavily_client = TavilyClient(api_key="tvly-YOUR_API_KEY")
response = tavily_client.search("Who is Leo Messi?")
print(response)
ืืขืจืืช ืืืืช ืชืืืืจ ืืื ืืืคืืฉื ืืงืืื ืืฉื "ืชืืื-ืื" ืฉืืฉืชืืฉ ื API ืฉื ืชืืื-ืื ืืื ืืืฆืข ืืช ืืืืคืืฉ.
ืขืืฉืื ืืชื
ืืืฉืื - ืืืื ืขืื ืืืื ืืืขืชืื ืื ืฉืื ืฆืจืืืื ืืงืฉื ืืืชืื ืื ืืืจืืฅ ืืฆืืจื ืืงืืืืช?1 419
ืืื 11 - ืืืื ืืืื ืื ื API
ืกืคืงื ืืืืฉื ืืืืืืื ืืื OpenAI ื Anthropic ืฉืื ืื ืืืกืคืจ ืืชืืจืื ืฉื ืืคืชืืื ืฉืืื ืื ืืขืจืืืช ืืืืกืกืืช ืกืืื ืื ืืงืฉืืจืื ืืืคืขืืช ืืืื ืืืืืืื ืืืืื ืืช ืขืกืงืืช ืืืืชื ืกืคืงื ืืืืืื:
1. ืื ืืืืื ืืืชื ืงืื ืืจืืฆื ืืืจืืฅ ืืืชื, ืืืฉืื ืฆืจืื ืืืจืื ืกืืืื ืืืจืืืืืืช ืื ืืืืื ืืืื ืืืจืืฅ ืืช ืืงืื ืืื ืืขืฉืืช ื ืืง.
2. ืื ืืืืื ืจืืฆื ืืืคืฉ ืืจืฉืช, ืืืฉืื ืฆืจืื ืืชืช ืื ืืืฉื ืืื ืืข ืืืคืืฉ, ืืื ืื ืืขื ืืืคืืฉ ืืื ืืืื ืื ืืกืคืงืื API ืืื ืืืืืื ืฉืืืืื ืืจืืฆืื ืืจืื ืืืคืืฉืื (ืื ืืืืื ืื ืจืืืื ืคืจืกืืืืช).
3. ืื ืืืืื ืจืืฆื ืืืฆืืจ ืชืืื ื ืืฉ ืืืฉืชืืฉ ืืื ืื ืื AI ืืืจ ืฉืืืฆืจ ืชืืื ืืช.
ืจืืื ื ืฉืื ืื ื ืืืืืื ืืืขืืืจ ืื ืคืื ืงืฆืืืช ืคืืืชืื ืืืืื ืืชืืจ Tool ืืืืืื ืืจืืฅ ืืืชื ืื ืืื ืชืขืืืจ ืืขื ืืช ืขื ืืคืจืืืคื, ืืื ืขืืืจ ืฉืืืฉืช ืืืืื ืืืชืืืจืื ืืืขืื ืืชืืืช ื"ืืื", ืืืืืจ ืืชืืื ืืืจืฆื ืฉื ืืคืื ืงืฆืื ืขืฆืื ืฉืืจืืฆื ืงืื, ืืืคืฉืช ืืจืฉืช ืื ืืืฆืจืช ืชืืื ื ืื ืืชืืจ.
ืืื ืฆื ืฉืจืช ืื ืืืื ืืืื ืื ืื ืืืจื ืฉื ืืืชื ืกืคืงืืืช ืืืืืื ืืขืืืจ ืืฉืืืฉืช ืืืชืืจืื ืืืื ืืืืชืืจืื ื ืืกืคืื ืืืืื.
ืืืืื 1 - ืืื ืืจืฆืช ืงืื
ืืืื ื ืืชืื ืืืืื ืจืืฉืื ื ืขื ืืื ืืจืฆืช ืงืื, ืืื ืืคื ื ืฉืืฉืชืืฉ ืืืื ืืืืื ื ืฉื OpenAI ืื ื ืจืืฆื ืืืชืื ืืช ืืืืืื ืขื ืืื ืืจืฆืช ืงืื ืขืฆืืื ืฉืื ื ืืืชื ืืืืืจ ืขื ืืืขืืืช ืฉืื. ืืืช ืืชืืื ืืช:
import asyncio
from agents import Agent, Runner
import json
from typing_extensions import TypedDict, Any
import os
from agents import Agent, FunctionTool, RunContextWrapper, function_tool
from agents.extensions.models.litellm_model import LitellmModel
@function_tool
async def run_python(code: str) -> str:
"""Run provided python code and return the value of "result" global variable as string.
Args:
code: python code to execute
Example input to return the value of 5 + 7:
x = 5
y = 7
result = x + y
"""
print(f"Running Code: \n\n{code}")
ns = {}
exec(code, ns)
print(f"Result = {ns['result']}")
return str(ns['result'])
agent = Agent(
name="Assistant",
model=LitellmModel(model="github/gpt-4.1", api_key=os.environ["GITHUB_TOKEN"]),
tools=[run_python],
)
async def main():
result = await Runner.run(agent, "What is the current os.name ? use Python to find out")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืงืื ืขืืื - ืืืืื ืืชื ืืช ืืชืืื ืืช:
import os
result = os.name
ืืงืื ืฉืื ืืจืืฅ ืืืชื ืขื exec ืืืกืืฃ ืืืืืจ ืืช ืขืจื ืืืฉืชื ื result. ืืื ืื ืงื ืืจืืืช ืืช ืืืขืืืช:
1. ืื ื ืื ืืืคื ืืฉืืืืืช ืชืืืืจ ืื ืืขืืืช ืกืืืืช ืจืืฆื. ืืื ื ืืื ืืืชืจ ืืื ืืืื ืื ืืฉ ืฉืืืื ืืืขืืืจ ืืืชื ืืืืื ืืื ืฉืืืืื ืืืชืื ืชืืื ืืช ืืืจืช.
2. ืืืชืจ ืืจืืข - ืืืื ืจืฅ ืืฆืื ืขื ืืืืื ื ืื ืืืขืจืืช ืืืืชืืช ืขื ืฉืจืช ืืคืจืืืงืฉื. ืืืืืื ืื ืื ืืฆืืจืื ืืืื ืื ืืชืงืื ืื ืคืจืืืคื ืืืื ื ืืืืืื ืืขืฉืืช ืืจืื ื ืืง.
ืขืืฉืื ืืืื ื ืจืื ืืื ืื ืขืืื ืขื ืืืื ืืืืื ื ืฉื OpenAI. ืื ืืงืื ืืืขืืืื:
import asyncio
from agents import Agent, Runner, CodeInterpreterTool
agent = Agent(
name="Assistant",
tools=[CodeInterpreterTool(
tool_config={
"type": "code_interpreter",
"container": { "type": "auto" }
}
)],
)
async def main():
result = await Runner.run(agent, "What is the current os.name ? use Python to find out")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
ืืงืื ื ืจืื ืืจืื ืืืชืจ ืืื! ืื ืื ืฉืงืืจื ืืคืขื:
1. ืืื Code Interpreter ืจืฅ ืืขื ื. ืื ื ืืขืืืจ ืงืื ืคืืืืจืฆืื ืฉืืืืจืช ืฉืื ืคืขื ืฉืืื ืฆืจืื ืืืจืืฅ ืงืื ืืื ืืืื ืืืฆืืจ ืืขืฆืื ืงืื ืืืื ืจ.
2. ืืฉืืื ืืืฉืชืืฉ ืืืื ืืจืฆืช ืืงืื ืื ื ืืืจ ืื ืืืื ืืขืืื ืืจื LiteLLM ืืืืืื ืฉื ืืืืืื ืืืืื ืืืืื ืก ืืืืื ืืชืฉืืื ืฉื OpenAI. ืื ืืืงืฉื ืขืฆืื ืืืงืืช ืืจืื ืืืชืจ ืืื ืืืืชืจ ืืืงื ืื.
3. ืืงืื ืจืฅ ืืขื ื ืฉื OpenAI ืขื ืงืื ืืืื ืจ ืื ืืชืง ืืืฉืจืช ืฉืื ืืื ื ืืงืื ืจืง ืืช ืชืฉืืืช ืืืืื.
4. ื OpenAI ืืฉ API ืฉืื ืืขืืืื ืขื ืงืืฆืื ืืงืื ืืืื ืจืื ืื ืฉืืฉืชืืฉืื ืืชืงืืืื ืืืืืื ืืืฆืืจ ืืขืฆืื ืืช ืืงืื ืืืื ืจ, ืืืขืืืช ืืืื ืงืืฆืื, ืืชืช ืืืืื ืืืชืื ืงืืฆืื ืืืฉืื ืืืืจืืฅ ืงืื ืืืกืืฃ ืืืืจืื ืงืืฆื ืชืืฆืื.
ืืืืื 2 - ืืืคืืฉ ืืจืฉืช
ืืื ืืืื ื ืฉื ื ืืื ืืื ืืืืคืืฉ ืืจืฉืช. ืืืื ื ืจืื ืืืชื ืืืืฆืขืืช ืืืืืื ืืืื:
import asyncio
from agents import Agent, Runner, WebSearchTool
agent = Agent(1 419
ืืื 10 - ืคืืชืื ืกืืื AI ืืชืืจ ืืื ืืืืืจื
ืขืื ืืืฉืง ืืฉืชืืฉ ืืขื ืืื ืืืืืื ืืื ืืคืืงืืฆืืืช ืืืกืจืื ืืืืคืื. ืืืจืืช ืฉืืืื ืืืืืื ืืืืจ ืืืืืกืืค ืืชืืืช ืืืืื ืืืืืจื ืื ืืจืื ืืืชืจ ืงื, ื ืืืฉ ืืืื ืื ืืืื ืืืืืื ืืืื ืืื ืขื ืืืืจื. ืืืื ื ืจืื ืืื ืืืชืื ืืื AI ืฉืื ืืืฉืื ืฉืืืืช ืืชืืื ืืืื ืขืืืจืื ืืืืืจื.
ืืื ืืืชืื ืืื ืืืืืจื
ืื ื ืืืืจ ืืคืืืชืื ืืืฉืืื ืืืชืื ืืื ืืืืืจื ื ืืื ืืืฉืชืืฉ ืืกืคืจืื python-telegram-bot. ืื ืืืืื ืืืื ืืืืจื ืจืืฉืื ืืชืื ืืชืืขืื ืฉืืื:
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
await update.message.reply_html(
rf"Hi {user.mention_html()}!",
reply_markup=ForceReply(selective=True),
)
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
await update.message.reply_text(update.message.text)
def main() -> None:
token = os.environ["TELEGRAM_BOT_TOKEN"]
application = Application.builder().token(token).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()
ืืฉืืื ืฉืืืื ืืขืืื ืขืืื ืืืืืืจ ืืฉืชื ื ืกืืืื ืืฉื TELEGRAM_BOT_TOKEN ืืื ืืืชืื ืืช ืืืืงื ืฉื ืืืื ืฉืื. ืืื ืืื ืืืืืจื ืืฉ ืืืงื ืฉืืืื ืืืชื ืืืชื ืืงืืืื ืืช ืืืืงื ืืืฆืืจืช ืืื ืืืฉ.
ืืื ืืืฆืจืื ืืื ืืืฉ ืืชื ืฉืืืืื? ืืื ืงื ืืขืืื, ืฉืืืืื ืืืืขื ื @BotFather ืขื ืืคืงืืื /newbot ืขืื ืื ืขื ืืื ืฉืืืืช ืืืงืืืื ืืืงื. ืืช ืืืืงื ืฉืืื ืืืฉืชื ื ืกืืืื ืืื ืชืืืื ืืืจืืฅ ืืช ืืืื ืืงืื ืฉืืืขืื. ืชืจืื ืฉืื ืขืืื ืืืชื ืืืืืื ืืืชืื ืืืื ืืฉืื ืืืงืื ืืืืจื ืืช ืืืชื ืืงืกื ืืื ื ืืฉืื ืืืืจ ืืช ืืกืืื.
ืฉืืืื AI
ืืฉืืื ืืืคืื ืืื ืืืืจื ืืกืืื AI ืืฉ ืื ื ืฉื ื ืืชืืจืื:
1. ืกืืื AI ืฆืจืื ืืืืืจ ืืช ืืืืืขืืช ืืืฉื ืืช. ืืืื ื ืืื ืืฉืืืจ ืืืืขืืช ืืฉื ืืช ืขื ืืืืืงื Session ืืืคืืื ืืื ืืฉืืืจ ืืช ื sessions ืืืื ืืืกืืก ื ืชืื ืื. ื ืจืฆื ืืื ืืกืืก ื ืชืื ืื ืืืืืขืืช ืืืืื ืฉืื ื.
2. ืืกืืื AI ืืืื ืืงืืช ืืื ืืขื ืืช. ืืขืืืื ื web ืจืืื ื ืืื ืืืฉืชืืฉ ืืืืฉืง ืืืจืืช ืืงืกื ืืื ืืืจืืืช ืืช ืืืงืกื ื ืืชื ืืืื ืฉืืื ื ืืฆืจ. ืืืืืจื ืืื ืื ืื ืื ืืืจืื ืืื ืื ืืคืฉืจ ืืืฆืื ืืช ืืืืืงืื ืฉืืืืจ ืฉืืืื ืืืชื.
ื ืฉืื ืืช ืฉื ื ืืืืจืื ืืคืื ืงืฆืื echo:
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
chat_id = update.effective_chat.id
session_id = f"chat_{chat_id}"
session = SQLiteSession(session_id, "bot.sql")
await update.message.reply_chat_action(ChatAction.TYPING)
result = await Runner.run(
agent,
update.message.text,
session=session
)
await update.message.reply_text(result.final_output)
ืื ืฉืืื ืืืืืจื ืืืืขื ืขื chat_id ืฉืืืื ืืช ืืฉืืื. ืื ืืืืืง ืืืื ืืื ืืฉืืื ืฉืื ืืืกืืก ืื ืชืื ืื. ืื ื ืืืืืจ session ืืชืื ืืืชื chat_id ืืฉืืืจ ืืืชื ืืืกืืก ื ืชืื ืื ืืงืืืฅ ืืฉื bot.sql. ืืคืขืืื:
await update.message.reply_chat_action(ChatAction.TYPING)
ืืืืืจื ืืืืืจื ืฉืฆืจืื ืืืฆืื ืฉืืืื ืืงืืื ืชืฉืืื ืืืืืคื ืืืืืืื ืคืขืืื ืื ืชืกืชืืื ืืฉื ืฉืื ืืช ืืชืืืื ืืืกืืืจืช ืืืจื ืฉืืกืืื ืืกืืื ืืืฉื ืืืชื.
ืขืืฉืื ืืชื
ืืขืื ืืืช ืืงืื ืืื ืืื ืฉื Sessions ืืืื ืืืื ืืจืืืื ืืืืจื ืืื. ืืื ืฉื ืืืจ ืืืชืจ ืขื ื AI ืื ืืืื ืืืชืจ ืืืืขืืช ื Session ืืื ืื ืืืืขื ืืกืืื ืชืืื ืืืชืจ ืืืืข (ืืืชืจ ืืืืขืืช ืืฉื ืืช), ืื ืฉืืืจืื ืืืืืจ ืฉื ืื ืืืืขื ืืขืืืช ืืื ืฉืืฉืืื ืืชืืจืืช.
ืืงืจื ืืช ืืกืืก ืื ืชืื ืื ืืงืืืฅ bot.sql ืฉื ืืฆืจ. ืืฉืื - ืืื ืืืืชื ืืืืืืื ืืช ืืกืคืจ ืืืืืขืืช ื session? ืืื ืืืืขืืช ืืืจืื ืืช ืืืขืชืื ืืืื ืืฉืืืจ ืื ืืฉืืื?1 419
content: prev.at(-1)!.content + decoder.decode(value),
role: prev.at(-1)!.role,
}));
}
} catch (error) {
console.error('Streaming error:', error);
}
}
return (
<div >
<main >
<ul>
{messages.map(msg => (
<li>{msg.role}: {msg.content}</li>
))}
</ul>
<form onSubmit={sendMessage}>
<input type="text" name="message" />
<input type="submit" value="Send" />
</form>
</main>
</div>
);
}
ืืฉ ืคื ืืจืื ืืืจืื ืืืฉืื ืื ื ืขืืืจ ืขืืืื ืืื ืืื:
1. ืื ื ืฉืืืจ ืืกืืืื ืืขืจื ืฉื ืืืืขืืช. ืื ืืืืขื ืืืืื ืืช ืืชืืื (content) ืืื ืืชื ืืืชื (role).
2. ืืฉืืฉืชืืฉ ืืืืฉ ืืช ืืืืคืก ืื ื ืืืกืืฃ ืืืขืจื ืืืืืขืืช ืฉืชื ืืืืขืืช, ืืืืขื ืืืืฉืชืืฉ ืขื ืืชืืื ืฉืืงืืชื ืืืืืคืก ืืืืืขื ืจืืงื ืฉื ื AI. ืขืื ืืขื ืืฉืจืช ืืชืืื ืืืืจืื ืื ืืช ืชืืื ืืืืืขื ืืื ื ืืฉืชืืฉ ืื ืืื ืืืื ืืช ืืฉืชื ื ืืกืืืื.
3. ืืฉืื ื ืืงืื ืืืจืื ืืืฉืจืช ืื ื ืืขืืื ืืช ืืืืืขื ืืืืจืื ื (ืืจืืงื ืฉืื ื ืืฆืจืชื) ืขื ืืชืืื:
const { done, value } = await reader.read();
if (done) break;
setMessages(prev => prev.with(-1, {
content: prev.at(-1)!.content + decoder.decode(value),
role: prev.at(-1)!.role,
}));
ืืฆืืจืช ืืืืขืช ืืกืืื ืจืืงื ืืจืืฉ ืืืกืืช ืื ืืืคืื ืืืืื ืืืืืขื ืืจืืฉืื ื.
ืขืืฉืื ื ืขืืืจ ืืฆื ืืฉืจืช - ืืฉืจืช ืืงืื ืืคืขื ืืขืจื ืฉื ืืืืขืืช ืืขืืื ืืืคืขืื ืืช ืืกืืื ืขื ืื ืืืืกืืืจืื. ื SDK ืฉื ืืืืคืกืงืจืืคื ืืฉ ืฉืชื ืคืื ืงืฆืืืช ืฉื ืืกืคืจืื ืืื ืขืืื ื ืืืฉืชืืฉ ืืื "ืืืคืื" ืืช ืืืืืืงืื ืืืืืขืืช ืฉืื ื ืืืืืืงืื ืืืืขืืช ืฉืืกืืื ืฆืจืื.
ืื ืื ื ืืงืืืื ืืืืงืื ืืืืืงื ืืืืขื ืฉื ืจืื ืื:
{ role: 'user', content: 'hello' }
ืืื ืืฉืืื ืืขืจื ืืืืืขืืช ืฉืืืฉืื ืืกืืื ืื ืื ื ืฆืจืืืื ืืืขืืืจ ืืืืขื ืฉื ืจืืืช ืื:
{ role: 'user', content: [
{ type: 'input_text', text: 'hello' }
] }
ืืชืืื ืฉื ืืืืขื ืืืจืื ืืืขืจื ืฉื "ืคืจืืืื" ืฉืืืืืื ืืืืืข ืืกืืืื ืฉืื ืื - ืื ืืฉ ืืงืกื ืืื ืืืืืืช ืืืืืช ืื ืชืืื ืืช ืืงืืฆืื. ืฉืชื ืคืื ืงืฆืืืช ืืืืจื ื ืงืจืืืช user ื assistant ืืืื ืืงืื ืื ืชืื ืืฆื ืืฉืจืช ืขืืื ืืืชืื:
const messages = await request.json();
const messagesForAgent = messages.map((m: any) =>
(m.role === 'user' ? user(m.content) : assistant(m.content)))
ืื ืืฉืืจ ื ืฉืืจ ืืืื ืืืืืื ืืงืืืืช ืืงืื ืฆื ืืฉืจืช ืืืื ืืงืืืฅ route.ts ืืื:
import { Agent, run, user, assistant } from '@openai/agents';
const agent = new Agent({
name: 'Assistant',
instructions: 'You are a helpful assistant',
});
export async function POST(request: Request) {
const messages = await request.json();
const messagesForAgent = messages.map((m: any) =>
(m.role === 'user' ? user(m.content) : assistant(m.content)))
console.log(messagesForAgent);
const result = await run(agent, messagesForAgent, {
stream: true,
});
// Convert the result to a standard ReadableStream
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
try {
for await (const event of result) {
if (event.type === 'raw_model_stream_event') {
if (event.data.type === "output_text_delta") {
controller.enqueue(encoder.encode(event.data.delta));
}
}
}
} catch (error) {
controller.error(error);
} finally {
controller.close();
}
},
});
return new Response(stream, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Transfer-Encoding': 'chunked',
},
});
}
ืืชื ืืืืืื ืืืฆืื ืืช ืืงืื ืืืื ืฉื ืืืืืื ืืืืืจ ืืืื:
https://github.com/ynonp/next-openai-agents-demo
ืขืืฉืื ืืชื
ืื ืื ืื ื Tools ืงืืื ืื ืืืืจืกืช ืืืืืคืกืงืจืืคื ืืืืื ืืืืจืกืช ืืคืืืชืื. ืงืจืื ืืช ืืืจืื ื Tools ืืื:
https://openai.github.io/openai-agents-js/guides/tools/
ืืืืกืืคื ืืกืืื ืืืืืช ืืืคืฉ ืืจืฉืช. ืฉืืื ืื ืืื ื ืจืืืช ืืืืขื ืฉื ืฉืืืืฉ ืืืื ืื ืกื ืืืฉืื ืขื ืืคืงื ืืืฉืง ืืฉืชืืฉ ืืคื ืฉืชืืืื ืืืืฉื ืืืืืขืืช ืืื.
Endi mavjud! Telegram Tadqiqoti 2025 โ yilning asosiy insaytlari 
