本文为Agentic AI所需的大 模型 API调用 的一些API示范注重于使用SCNet以及DeepSeek的基于OpenAI 以及 OpenAI SDK 的 API调用。本文为公益类代码由DeepSeek辅助生成经过实例测试。有关SCNet和DeepSeek API的调用见原文https://blog.csdn.net/YucongCai/article/details/159774133在OpenAI Agents SDK 和目前主流的Agentic AI系统在多Agents 的情况下常常会遇到任务交接或权限交接的需求这就是 handoff 的目的。在OpenAI SDK 中agent 在定义之初或之后可以指定一个或几个handoff agents以此让agent将任务在特定状态将任务和权限装交给被指定的agent。例如 agent a 的 handoff 如果被指定为 agent b和 agent c那么agent a就可以在满足条件时根据定义将任务转交给agent b 或agent c中的一个agent而收到任务的agent 就会接替agent a去尝试完成任务同时获取agent a的权限。要注意一个agent只能将其任务和权限handoff 给一个agent。而且权限在这里指的是agent a所控制的任务状态和workflowagents 可以使用的工具仍被各自定义不会被自动转接。可以理解成一个员工将任务交给了另一个员工但员工各自仍是独立的个体而guardrail等系统设定是独立定义的。而OpenAI SDK的 agent(s) astool是另一个概念。在OpenAI SDK中agent可以通过 as_tool function 被转化为工具以此被其他其它agents调用。需要注意的是一个agent仅可将任务handoff给一个agent然而一个agent可以同时使用多个agents as_tool 并行使用。接下来本文会展示两组源代码读者可以此测试相关性能。Agentic AI的SDK仍处于发展阶段相关概念比代码本身更为重要。其实有需求的读者可以根据需求自主编写代码处理history/sessionagents handoffas_tool 等只不过使用OpenAI SDK相对而言方便一些对于简单系统可以降低成本。handoffimport asyncio import os from dotenv import load_dotenv from openai import AsyncOpenAI from agents import ( Agent, Runner, OpenAIChatCompletionsModel, set_tracing_disabled ) # --- Setup --- load_dotenv(overrideTrue) set_tracing_disabled(True) # optional: disable OpenAI tracing client AsyncOpenAI( api_keyos.getenv(DEEPSEEK_API_KEY), base_urlhttps://api.deepseek.com/v1 ) model OpenAIChatCompletionsModel( modeldeepseek-chat, openai_clientclient ) # --- Specialist agents --- billing_agent Agent( namebilling_agent, instructionsYou are a billing expert. Answer questions about invoices, payments, and charges., modelmodel ) refund_agent Agent( namerefund_agent, instructionsYou are a refund expert. Handle all refund requests and policy questions., modelmodel ) # --- Triage agent (handoff, no session) --- triage_agent Agent( nametriage_agent, instructions( You are a customer service triage agent. If the user asks about a bill or payment, hand off to billing_agent. If they ask for a refund, hand off to refund_agent. ), handoffs[billing_agent, refund_agent], modelmodel ) # --- Run without any session --- async def main(): # This will automatically hand off to billing_agent result await Runner.run( triage_agent, I was charged twice for my last subscription. Can you help? # no session parameter → no history persistence ) print(result.final_output) if __name__ __main__: # asyncio.run(main()) await main()as_tool (并行的示例)import asyncio import os from dotenv import load_dotenv from openai import AsyncOpenAI from agents import ( Agent, Runner, SQLiteSession, OpenAIChatCompletionsModel, set_tracing_disabled, ModelSettings # needed for parallel tool calls ) # -------------------- Environment SDK Setup -------------------- load_dotenv(overrideTrue) set_tracing_disabled(True) # disable OpenAI tracing (optional) # 1. Configure DeepSeek client (OpenAI‑compatible) client AsyncOpenAI( api_keyos.getenv(DEEPSEEK_API_KEY), base_urlhttps://api.deepseek.com/v1 ) # 2. Model wrapper – DeepSeek supports tool calling model OpenAIChatCompletionsModel( modeldeepseek-chat, openai_clientclient ) # -------------------- Define Specialist Agents -------------------- billing_agent Agent( namebilling_agent, instructionsYou are a billing expert. Answer questions about invoices, payments, and charges., modelmodel ) refund_agent Agent( namerefund_agent, instructionsYou are a refund expert. Handle all refund requests and policy questions., modelmodel ) # -------------------- Triage Agent (Handoff) -------------------- triage_agent Agent( nametriage_agent, instructions( You are a customer service triage agent. If the user asks about a bill or payment, hand off to billing_agent. If they ask for a refund, hand off to refund_agent. ), handoffs[billing_agent, refund_agent], modelmodel ) # -------------------- Agents for Parallel Tool Calls -------------------- # These will be wrapped as tools and called in parallel by the manager finance_agent Agent( nameFinance_Analyst, instructionsAnalyze the financial health of a given company (revenue, profit, debt)., modelmodel ) market_agent Agent( nameMarket_Analyst, instructionsAnalyze the market position and key competitors of a given company., modelmodel ) # Wrap them as tools finance_tool finance_agent.as_tool( tool_nameget_financial_analysis, tool_descriptionGet a financial analysis of a company. ) market_tool market_agent.as_tool( tool_nameget_market_analysis, tool_descriptionGet a market analysis of a company. ) # Manager agent that calls both tools in parallel manager_agent Agent( nameResearch_Manager, instructions( You are a research manager. For any company, you MUST call BOTH get_financial_analysis and get_market_analysis in parallel to provide a complete report. ), tools[finance_tool, market_tool], modelmodel, model_settingsModelSettings(parallel_tool_callsTrue) # enables LLM‑driven parallelism ) # -------------------- Optional: Session for multi‑turn -------------------- session SQLiteSession(demo_user, conversations.db) # -------------------- Main Async Runner -------------------- async def main(): print( Demo 1: Handoff (triage → billing) \n) result_handoff await Runner.run( triage_agent, I was charged twice for my last subscription. Can you help?, sessionsession # optional, keeps conversation history ) print(fHandoff result:\n{result_handoff.final_output}\n) print( Demo 2: Agent‑as‑Tool (parallel calls) \n) result_parallel await Runner.run( manager_agent, Give me a full analysis of Tesla (TSLA)., sessionsession ) print(fParallel tools result:\n{result_parallel.final_output}\n) if __name__ __main__: await main()我在找工作HR或项目合作请联系yucongcai_businessoutlook.com与科研相关的请联系yucongcai_researchoutlook.com