uz
Feedback
ToCode

ToCode

Kanalga Telegramโ€™da oโ€˜tish

ื˜ื™ืคื™ื ืงืฆืจื™ื ืœืžืชื›ื ืชื™ื ืžืืช ื™ื ื•ืŸ ืคืจืง

Ko'proq ko'rsatish
1 419
Obunachilar
Ma'lumot yo'q24 soatlar
Ma'lumot yo'q7 kunlar
-430 kunlar
Postlar arxiv
ToCode
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 ืฉื‘ื—ืจืชื.

ToCode
1 419
ืกืš ื”ื›ืœ Handoff ื”ื•ื ืงื•ื ืกืคื˜ ื ื—ืžื“ ืื‘ืœ ื”ืจื‘ื” ืคืขืžื™ื ื™ื”ื™ื” ืœื ื• ื™ื•ืชืจ ืงืœ ืœืžืžืฉ ืื•ืชื• ื‘ืขืฆืžื ื• ืขื ื”ื’ื“ืจืช ื›ืœื™ื ื‘ืžื™ื•ื—ื“ ืœื“ื‘ืจื™ื ืœื ื˜ืจื™ื•ื•ื™ืืœื™ื™ื. ื‘ืชื•ืš ืœื•ืœืืช ื” Runner ืฉืœ Agents SDK ื”ืชื‘ื ื™ืช ื”ืžืจื›ื–ื™ืช ื‘ื” ื ืจืฆื” ืœื”ืฉืชืžืฉ ื‘ Handoff ื”ื™ื ืกื•ื›ืŸ ืจืืฉื™ ืฉืžืžื™ื™ืŸ ืคื ื™ื•ืช ื•ืกื•ื›ื ื™ื ืกืคืฆื™ืคื™ื™ื ืฉืžื˜ืคืœื™ื ื‘ืกื•ื’ื™ ื”ืคื ื™ื•ืช ื›ืืฉืจ ื”ื˜ื™ืคื•ืœ ืœื ื“ื•ืจืฉ ื”ืžืฉืš ืื™ื ื˜ืจืงืฆื™ื” ืขื ื”ืžืฉืชืžืฉ.

ToCode
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 ืจื’ื™ืœ ื›ื“ื™ ืœื”ืชืื™ื ืœืฆื™ืคื™ื•ืช ื”ืžืฉืชืžืฉื™ื.

ToCode
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, ื›ืœ ืื—ื“ ืžื”ื ื‘ื“ืจื›ื• ื™ืขื–ื•ืจ ืœื ื• ืœืขืฆื•ืจ ืคืขื•ืœื” ืฉืœ ืกื•ื›ืŸ ืœืคื ื™ ืฉื”ืกืชื™ื™ืžื”. ืขื›ืฉื™ื• ืืชื ื›ืชื‘ื• ืกื•ื›ืŸ ืฉืžืงื‘ืœ ืฉืœื•ืฉื” ื›ืœื™ื - ื”ืฆื’ืช ืจืฉื™ืžืช ื”ืงื‘ืฆื™ื ื‘ืชื™ืงื™ื™ื”, ืงืจื™ืืช ื˜ืงืกื˜ ื‘ืงื•ื‘ืฅ ื•ื›ื ื™ืกื” ืœืชื™ืงื™ื” ืื—ืจืช. ื”ืฉืชืžืฉื• ื‘ืกื•ื›ืŸ ื›ื“ื™ ืœื—ืคืฉ ืงื•ื‘ืฅ ืฉืžื›ื™ืœ ื˜ืงืกื˜ ืžืกื•ื™ื. ืชืขื“ื• ืืช ื”ืคืขืœืช ื”ื›ืœื™ื. ืœืื—ืจ ืžื›ืŸ ื”ื•ืกื™ืคื• "ืžื•ืงืฉื™ื", ืงื‘ืฆื™ื ืฉื›ืฉืคื•ืชื—ื™ื ืื•ืชื ืœื•ืœืืช ื”ืคืขืœืช ื”ื›ืœื™ื ืฉืœ ื”ืกื•ื›ืŸ ืชื™ืขืฆืจ.

ToCode
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:

ToCode
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
        },

ToCode
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',

ToCode
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:28

ToCode
1 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:42

ToCode
1 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:56

ToCode
1 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 ืฉืœ ืชื‘ื™ื-ืœื™ ื›ื“ื™ ืœื‘ืฆืข ืืช ื”ื—ื™ืคื•ืฉ. ืขื›ืฉื™ื• ืืชื ื—ื™ืฉื‘ื• - ืื™ื–ื” ืขื•ื“ ื›ืœื™ื ืœื“ืขืชื›ื ืื ืฉื™ื ืฆืจื™ื›ื™ื ื•ืงืฉื” ืœื›ืชื•ื‘ ืื• ืœื”ืจื™ืฅ ื‘ืฆื•ืจื” ืžืงื•ืžื™ืช?

ToCode
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(

ToCode
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? ื›ืžื” ื”ื•ื“ืขื•ืช ืื—ืจื•ื ื•ืช ืœื“ืขืชื›ื ื›ื“ืื™ ืœืฉืžื•ืจ ืื• ืœืฉืœื•ื—?

ToCode
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/ ื•ื”ื•ืกื™ืคื• ืœืกื•ื›ืŸ ื™ื›ื•ืœืช ืœื—ืคืฉ ื‘ืจืฉืช. ืฉื™ืžื• ืœื‘ ืื™ืš ื ืจืื™ืช ื”ื•ื“ืขื” ืฉืœ ืฉื™ืžื•ืฉ ื‘ื›ืœื™ ื•ื ืกื• ืœื—ืฉื•ื‘ ืขืœ ืืคืงื˜ ืžืžืฉืง ืžืฉืชืžืฉ ื™ืคื” ืฉืชื•ื›ืœื• ืœื™ื™ืฉื ืœื”ื•ื“ืขื•ืช ืืœื”.