kikocis/Based-Agent public
Changesets · Branches · Tags · Compare
| Changeset | b47944ff9368038c29aa0880d0a0065c90bc252495960264de62b68708f4175d |
|---|---|
| Author | Shyamal H Anadkat <shyamal@openai.com> · 2024-10-11 17:27 -07:00 |
| Committer | Shyamal H Anadkat <shyamal@openai.com> · 2024-10-11 17:27 -07:00 |
| Parents | 78d4e2805395 |
4 files changed
modified examples/customer_service_streaming/src/swarm/swarm.py +12 −11
| @@ -1,33 +1,35 @@ | ||
| 1 | 1 | import json |
| 2 | 2 | from openai import OpenAI |
| 3 | -from src.tasks.task import Task,EvaluationTask | |
| 3 | +from src.tasks.task import Task, EvaluationTask | |
| 4 | 4 | from src.swarm.engines.assistants_engine import AssistantsEngine |
| 5 | 5 | from src.swarm.engines.local_engine import LocalEngine |
| 6 | 6 | from configs.general import Colors, tasks_path |
| 7 | 7 | |
| 8 | +# This class represents the main control unit for deploying and managing tasks within the swarm system. | |
| 9 | + | |
| 10 | + | |
| 8 | 11 | class Swarm: |
| 9 | - def __init__(self,engine_name,tasks=[], persist=False): | |
| 12 | + def __init__(self, engine_name, tasks=[], persist=False): | |
| 10 | 13 | self.tasks = tasks |
| 11 | 14 | self.engine_name = engine_name |
| 12 | 15 | self.engine = None |
| 13 | 16 | self.persist = persist |
| 14 | 17 | |
| 15 | - | |
| 16 | - def deploy(self, test_mode=False,test_file_paths=None): | |
| 18 | + def deploy(self, test_mode=False, test_file_paths=None): | |
| 17 | 19 | """ |
| 18 | 20 | Processes all tasks in the order they are listed in self.tasks. |
| 19 | 21 | """ |
| 20 | 22 | client = OpenAI() |
| 21 | - #Initialize swarm first | |
| 23 | + # Initialize swarm first | |
| 22 | 24 | if self.engine_name == 'assistants': |
| 23 | 25 | 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) | |
| 26 | 28 | |
| 27 | - elif self.engine_name =='local': | |
| 29 | + elif self.engine_name == 'local': | |
| 28 | 30 | 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) | |
| 31 | 33 | |
| 32 | 34 | def load_tasks(self): |
| 33 | 35 | self.tasks = [] |
| @@ -42,4 +44,3 @@ | ||
| 42 | 44 | |
| 43 | 45 | def add_task(self, task): |
| 44 | 46 | self.tasks.append(task) |
| 45 | - |
modified examples/customer_service_streaming/src/swarm/tool.py +4 −0
| @@ -1,21 +1,25 @@ | ||
| 1 | 1 | from pydantic import BaseModel, Field |
| 2 | 2 | from typing import Dict, List, Optional, Literal |
| 3 | 3 | |
| 4 | + | |
| 4 | 5 | class Parameter(BaseModel): |
| 5 | 6 | type: str |
| 6 | 7 | description: Optional[str] = None |
| 7 | 8 | enum: Optional[List[str]] = Field(None, alias='choices') |
| 8 | 9 | |
| 10 | + | |
| 9 | 11 | class FunctionParameters(BaseModel): |
| 10 | 12 | type: Literal['object'] # Ensuring it's always 'object' |
| 11 | 13 | properties: Dict[str, Parameter] = {} |
| 12 | 14 | required: Optional[List[str]] = None |
| 13 | 15 | |
| 16 | + | |
| 14 | 17 | class FunctionTool(BaseModel): |
| 15 | 18 | name: str |
| 16 | 19 | description: Optional[str] |
| 17 | 20 | parameters: FunctionParameters |
| 18 | 21 | |
| 22 | + | |
| 19 | 23 | class Tool(BaseModel): |
| 20 | 24 | type: str |
| 21 | 25 | function: Optional[FunctionTool] |
modified swarm/core.py +6 −3
| @@ -94,7 +94,8 @@ | ||
| 94 | 94 | debug: bool, |
| 95 | 95 | ) -> Response: |
| 96 | 96 | 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={}) | |
| 98 | 99 | |
| 99 | 100 | for tool_call in tool_calls: |
| 100 | 101 | name = tool_call.function.name |
| @@ -111,7 +112,8 @@ | ||
| 111 | 112 | ) |
| 112 | 113 | continue |
| 113 | 114 | 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}") | |
| 115 | 117 | |
| 116 | 118 | func = function_map[name] |
| 117 | 119 | # pass context_variables to agent functions |
| @@ -186,7 +188,8 @@ | ||
| 186 | 188 | merge_chunk(message, delta) |
| 187 | 189 | yield {"delim": "end"} |
| 188 | 190 | |
| 189 | - message["tool_calls"] = list(message.get("tool_calls", {}).values()) | |
| 191 | + message["tool_calls"] = list( | |
| 192 | + message.get("tool_calls", {}).values()) | |
| 190 | 193 | if not message["tool_calls"]: |
| 191 | 194 | message["tool_calls"] = None |
| 192 | 195 | debug_print(debug, "Received completion:", message) |
modified swarm/util.py +11 −0
| @@ -29,6 +29,17 @@ | ||
| 29 | 29 | |
| 30 | 30 | |
| 31 | 31 | 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 | + """ | |
| 32 | 43 | type_map = { |
| 33 | 44 | str: "string", |
| 34 | 45 | int: "integer", |