MCPcopy
hub / github.com/openai/swarm / Swarm

Class Swarm

swarm/core.py:26–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24
25
26class Swarm:
27 def __init__(self, client=None):
28 if not client:
29 client = OpenAI()
30 self.client = client
31
32 def get_chat_completion(
33 self,
34 agent: Agent,
35 history: List,
36 context_variables: dict,
37 model_override: str,
38 stream: bool,
39 debug: bool,
40 ) -> ChatCompletionMessage:
41 context_variables = defaultdict(str, context_variables)
42 instructions = (
43 agent.instructions(context_variables)
44 if callable(agent.instructions)
45 else agent.instructions
46 )
47 messages = [{"role": "system", "content": instructions}] + history
48 debug_print(debug, "Getting chat completion for...:", messages)
49
50 tools = [function_to_json(f) for f in agent.functions]
51 # hide context_variables from model
52 for tool in tools:
53 params = tool["function"]["parameters"]
54 params["properties"].pop(__CTX_VARS_NAME__, None)
55 if __CTX_VARS_NAME__ in params["required"]:
56 params["required"].remove(__CTX_VARS_NAME__)
57
58 create_params = {
59 "model": model_override or agent.model,
60 "messages": messages,
61 "tools": tools or None,
62 "tool_choice": agent.tool_choice,
63 "stream": stream,
64 }
65
66 if tools:
67 create_params["parallel_tool_calls"] = agent.parallel_tool_calls
68
69 return self.client.chat.completions.create(**create_params)
70
71 def handle_function_result(self, result, debug) -> Result:
72 match result:
73 case Result() as result:
74 return result
75
76 case Agent() as agent:
77 return Result(
78 value=json.dumps({"assistant": agent.name}),
79 agent=agent,
80 )
81 case _:
82 try:
83 return Result(value=str(result))

Callers 5

run_demo_loopFunction · 0.90
test_tool_callFunction · 0.90
test_execute_tools_falseFunction · 0.90
test_handoffFunction · 0.90

Calls

no outgoing calls

Tested by 4

test_tool_callFunction · 0.72
test_execute_tools_falseFunction · 0.72
test_handoffFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…