Elffuss BraidHub

kikocis/Based-Agent public

Updates util.py
Changesetb47944ff9368038c29aa0880d0a0065c90bc252495960264de62b68708f4175d
AuthorShyamal H Anadkat <shyamal@openai.com> · 2024-10-11 17:27 -07:00
CommitterShyamal H Anadkat <shyamal@openai.com> · 2024-10-11 17:27 -07:00
Parents78d4e2805395

Browse the files at this changeset

4 files changed

modified examples/customer_service_streaming/src/swarm/swarm.py +12 −11
@@ -1,33 +1,35 @@
11 import json
22 from openai import OpenAI
3-from src.tasks.task import Task,EvaluationTask
3+from src.tasks.task import Task, EvaluationTask
44 from src.swarm.engines.assistants_engine import AssistantsEngine
55 from src.swarm.engines.local_engine import LocalEngine
66 from configs.general import Colors, tasks_path
77
8+# This class represents the main control unit for deploying and managing tasks within the swarm system.
9+
10+
811 class Swarm:
9- def __init__(self,engine_name,tasks=[], persist=False):
12+ def __init__(self, engine_name, tasks=[], persist=False):
1013 self.tasks = tasks
1114 self.engine_name = engine_name
1215 self.engine = None
1316 self.persist = persist
1417
15-
16- def deploy(self, test_mode=False,test_file_paths=None):
18+ def deploy(self, test_mode=False, test_file_paths=None):
1719 """
1820 Processes all tasks in the order they are listed in self.tasks.
1921 """
2022 client = OpenAI()
21- #Initialize swarm first
23+ # Initialize swarm first
2224 if self.engine_name == 'assistants':
2325 print(f"{Colors.GREY}Selected engine: Assistants{Colors.ENDC}")
24- self.engine = AssistantsEngine(client,self.tasks)
25- self.engine.deploy(client,test_mode,test_file_paths)
26+ self.engine = AssistantsEngine(client, self.tasks)
27+ self.engine.deploy(client, test_mode, test_file_paths)
2628
27- elif self.engine_name =='local':
29+ elif self.engine_name == 'local':
2830 print(f"{Colors.GREY}Selected engine: Local{Colors.ENDC}")
29- self.engine = LocalEngine(client,self.tasks, persist=self.persist)
30- self.engine.deploy(client,test_mode,test_file_paths)
31+ self.engine = LocalEngine(client, self.tasks, persist=self.persist)
32+ self.engine.deploy(client, test_mode, test_file_paths)
3133
3234 def load_tasks(self):
3335 self.tasks = []
@@ -42,4 +44,3 @@
4244
4345 def add_task(self, task):
4446 self.tasks.append(task)
45-
modified examples/customer_service_streaming/src/swarm/tool.py +4 −0
@@ -1,21 +1,25 @@
11 from pydantic import BaseModel, Field
22 from typing import Dict, List, Optional, Literal
33
4+
45 class Parameter(BaseModel):
56 type: str
67 description: Optional[str] = None
78 enum: Optional[List[str]] = Field(None, alias='choices')
89
10+
911 class FunctionParameters(BaseModel):
1012 type: Literal['object'] # Ensuring it's always 'object'
1113 properties: Dict[str, Parameter] = {}
1214 required: Optional[List[str]] = None
1315
16+
1417 class FunctionTool(BaseModel):
1518 name: str
1619 description: Optional[str]
1720 parameters: FunctionParameters
1821
22+
1923 class Tool(BaseModel):
2024 type: str
2125 function: Optional[FunctionTool]
modified swarm/core.py +6 −3
@@ -94,7 +94,8 @@
9494 debug: bool,
9595 ) -> Response:
9696 function_map = {f.__name__: f for f in functions}
97- partial_response = Response(messages=[], agent=None, context_variables={})
97+ partial_response = Response(
98+ messages=[], agent=None, context_variables={})
9899
99100 for tool_call in tool_calls:
100101 name = tool_call.function.name
@@ -111,7 +112,8 @@
111112 )
112113 continue
113114 args = json.loads(tool_call.function.arguments)
114- debug_print(debug, f"Processing tool call: {name} with arguments {args}")
115+ debug_print(
116+ debug, f"Processing tool call: {name} with arguments {args}")
115117
116118 func = function_map[name]
117119 # pass context_variables to agent functions
@@ -186,7 +188,8 @@
186188 merge_chunk(message, delta)
187189 yield {"delim": "end"}
188190
189- message["tool_calls"] = list(message.get("tool_calls", {}).values())
191+ message["tool_calls"] = list(
192+ message.get("tool_calls", {}).values())
190193 if not message["tool_calls"]:
191194 message["tool_calls"] = None
192195 debug_print(debug, "Received completion:", message)
modified swarm/util.py +11 −0
@@ -29,6 +29,17 @@
2929
3030
3131 def function_to_json(func) -> dict:
32+ """
33+ Converts a Python function into a JSON-serializable dictionary
34+ that describes the function's signature, including its name,
35+ description, and parameters.
36+
37+ Args:
38+ func: The function to be converted.
39+
40+ Returns:
41+ A dictionary representing the function's signature in JSON format.
42+ """
3243 type_map = {
3344 str: "string",
3445 int: "integer",