From 547065d64122cf56ea786ee5a11f8a47aea70494 Mon Sep 17 00:00:00 2001 From: Erik Peterson Date: Tue, 20 Jun 2023 16:49:05 -0700 Subject: [PATCH 01/14] Implement openai functions feature flag Signed-off-by: Merwane Hamadi --- .env.template | 6 +- autogpt/agent/agent.py | 44 +++- autogpt/agent/agent_manager.py | 8 +- autogpt/app.py | 24 +- autogpt/command_decorator.py | 12 +- autogpt/config/ai_config.py | 2 +- autogpt/config/config.py | 2 + autogpt/json_utils/utilities.py | 10 +- autogpt/llm/chat.py | 10 +- autogpt/llm/utils/__init__.py | 24 +- autogpt/memory/message_history.py | 2 +- autogpt/models/chat_completion_response.py | 4 + autogpt/models/command.py | 8 +- autogpt/models/command_argument.py | 9 + autogpt/models/command_function.py | 18 ++ autogpt/processing/text.py | 7 +- autogpt/prompts/generator.py | 26 ++- autogpt/setup.py | 2 +- tests/unit/test_agent_manager.py | 5 +- tests/unit/test_commands.py | 28 +-- tests/unit/test_message_history.py | 10 +- tests/unit/test_prompt_generator.py | 255 ++++++++++++--------- 22 files changed, 350 insertions(+), 166 deletions(-) create mode 100644 autogpt/models/chat_completion_response.py create mode 100644 autogpt/models/command_argument.py create mode 100644 autogpt/models/command_function.py diff --git a/.env.template b/.env.template index 06745245793..4eb344a7b8b 100644 --- a/.env.template +++ b/.env.template @@ -25,10 +25,14 @@ OPENAI_API_KEY=your-openai-api-key ## PROMPT_SETTINGS_FILE - Specifies which Prompt Settings file to use (defaults to prompt_settings.yaml) # PROMPT_SETTINGS_FILE=prompt_settings.yaml -## OPENAI_API_BASE_URL - Custom url for the OpenAI API, useful for connecting to custom backends. No effect if USE_AZURE is true, leave blank to keep the default url +## OPENAI_API_BASE_URL - Custom url for the OpenAI API, useful for connecting to custom backends. No effect if USE_AZURE is true, leave blank to keep the default url # the following is an example: # OPENAI_API_BASE_URL=http://localhost:443/v1 +## OPENAI_FUNCTIONS - Enables OpenAI functions: https://platform.openai.com/docs/guides/gpt/function-calling +# the following is an example: +# OPENAI_FUNCTIONS=False + ## AUTHORISE COMMAND KEY - Key to authorise commands # AUTHORISE_COMMAND_KEY=y diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index 7537233efc0..1a8248b8a11 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -20,6 +20,7 @@ from autogpt.logs import logger, print_assistant_thoughts from autogpt.memory.message_history import MessageHistory from autogpt.memory.vector import VectorMemory +from autogpt.models.command_function import CommandFunction from autogpt.models.command_registry import CommandRegistry from autogpt.speech import say_text from autogpt.spinner import Spinner @@ -138,11 +139,14 @@ def signal_handler(signum, frame): self.system_prompt, self.triggering_prompt, self.fast_token_limit, + self.get_functions_from_commands(), self.config.fast_llm_model, ) try: - assistant_reply_json = extract_json_from_response(assistant_reply) + assistant_reply_json = extract_json_from_response( + assistant_reply.content + ) validate_json(assistant_reply_json, self.config) except json.JSONDecodeError as e: logger.error(f"Exception while validating assistant reply JSON: {e}") @@ -160,7 +164,9 @@ def signal_handler(signum, frame): print_assistant_thoughts( self.ai_name, assistant_reply_json, self.config ) - command_name, arguments = get_command(assistant_reply_json) + command_name, arguments = get_command( + assistant_reply_json, assistant_reply, self.config + ) if self.config.speak_mode: say_text(f"I want to execute {command_name}") @@ -314,3 +320,37 @@ def _resolve_pathlike_command_args(self, command_args): self.workspace.get_path(command_args[pathlike]) ) return command_args + + def get_functions_from_commands(self) -> list[CommandFunction]: + """Get functions from the commands. "functions" in this context refers to OpenAI functions + see https://platform.openai.com/docs/guides/gpt/function-calling + """ + functions = [] + if not self.config.openai_functions: + return functions + for command in self.command_registry.commands.values(): + properties = {} + required = [] + + for argument in command.arguments: + properties[argument.name] = { + "type": argument.type, + "description": argument.description, + } + if argument.required: + required.append(argument.name) + + parameters = { + "type": "object", + "properties": properties, + "required": required, + } + functions.append( + CommandFunction( + name=command.name, + description=command.description, + parameters=parameters, + ) + ) + + return functions diff --git a/autogpt/agent/agent_manager.py b/autogpt/agent/agent_manager.py index 1f1c8a1de05..eaecbf3b41a 100644 --- a/autogpt/agent/agent_manager.py +++ b/autogpt/agent/agent_manager.py @@ -41,7 +41,9 @@ def create_agent( if plugin_messages := plugin.pre_instruction(messages.raw()): messages.extend([Message(**raw_msg) for raw_msg in plugin_messages]) # Start GPT instance - agent_reply = create_chat_completion(prompt=messages, config=self.config) + agent_reply = create_chat_completion( + prompt=messages, config=self.config + ).content messages.add("assistant", agent_reply) @@ -92,7 +94,9 @@ def message_agent(self, key: str | int, message: str) -> str: messages.extend([Message(**raw_msg) for raw_msg in plugin_messages]) # Start GPT instance - agent_reply = create_chat_completion(prompt=messages, config=self.config) + agent_reply = create_chat_completion( + prompt=messages, config=self.config + ).content messages.add("assistant", agent_reply) diff --git a/autogpt/app.py b/autogpt/app.py index 78e3a4dd206..4f1c3a9a436 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -3,6 +3,8 @@ from typing import Dict from autogpt.agent.agent import Agent +from autogpt.config import Config +from autogpt.models.chat_completion_response import ChatCompletionResponse def is_valid_int(value: str) -> bool: @@ -21,11 +23,13 @@ def is_valid_int(value: str) -> bool: return False -def get_command(response_json: Dict): +def get_command( + assistant_reply_json: Dict, assistant_reply: ChatCompletionResponse, config: Config +): """Parse the response and return the command name and arguments Args: - response_json (json): The response from the AI + assistant_reply_json (json): The response from the AI Returns: tuple: The command name and arguments @@ -35,14 +39,22 @@ def get_command(response_json: Dict): Exception: If any other error occurs """ + if config.openai_functions: + assistant_reply_json["command"] = { + "name": assistant_reply.function_call.name, + "args": json.loads(assistant_reply.function_call.arguments), + } try: - if "command" not in response_json: + if "command" not in assistant_reply_json: return "Error:", "Missing 'command' object in JSON" - if not isinstance(response_json, dict): - return "Error:", f"'response_json' object is not dictionary {response_json}" + if not isinstance(assistant_reply_json, dict): + return ( + "Error:", + f"'assistant_reply_json' object is not dictionary {assistant_reply_json}", + ) - command = response_json["command"] + command = assistant_reply_json["command"] if not isinstance(command, dict): return "Error:", "'command' object is not a dictionary" diff --git a/autogpt/command_decorator.py b/autogpt/command_decorator.py index 1edd766ec4d..d86e41096b2 100644 --- a/autogpt/command_decorator.py +++ b/autogpt/command_decorator.py @@ -3,6 +3,7 @@ from autogpt.config import Config from autogpt.models.command import Command +from autogpt.models.command_argument import CommandArgument # Unique identifier for auto-gpt commands AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command" @@ -18,11 +19,20 @@ def command( """The command decorator is used to create Command objects from ordinary functions.""" def decorator(func: Callable[..., Any]) -> Command: + typed_arguments = [ + CommandArgument( + name=arg_name, + description=argument.get("description"), + type=argument.get("type", "string"), + required=argument.get("required", False), + ) + for arg_name, argument in arguments.items() + ] cmd = Command( name=name, description=description, method=func, - signature=arguments, + arguments=typed_arguments, enabled=enabled, disabled_reason=disabled_reason, ) diff --git a/autogpt/config/ai_config.py b/autogpt/config/ai_config.py index 6b9e15f181b..3c645abe36f 100644 --- a/autogpt/config/ai_config.py +++ b/autogpt/config/ai_config.py @@ -164,5 +164,5 @@ def construct_full_prompt( if self.api_budget > 0.0: full_prompt += f"\nIt takes money to let you run. Your API budget is ${self.api_budget:.3f}" self.prompt_generator = prompt_generator - full_prompt += f"\n\n{prompt_generator.generate_prompt_string()}" + full_prompt += f"\n\n{prompt_generator.generate_prompt_string(config)}" return full_prompt diff --git a/autogpt/config/config.py b/autogpt/config/config.py index 5e0999b1566..d032f8224ea 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -88,6 +88,8 @@ def __init__(self) -> None: if self.openai_organization is not None: openai.organization = self.openai_organization + self.openai_functions = os.getenv("OPENAI_FUNCTIONS", "False") == "True" + self.elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY") # ELEVENLABS_VOICE_1_ID is deprecated and included for backwards-compatibility self.elevenlabs_voice_id = os.getenv( diff --git a/autogpt/json_utils/utilities.py b/autogpt/json_utils/utilities.py index 4fbf0c0578b..b78d6322ed7 100644 --- a/autogpt/json_utils/utilities.py +++ b/autogpt/json_utils/utilities.py @@ -29,11 +29,15 @@ def extract_json_from_response(response_content: str) -> dict: def llm_response_schema( - schema_name: str = LLM_DEFAULT_RESPONSE_FORMAT, + config: Config, schema_name: str = LLM_DEFAULT_RESPONSE_FORMAT ) -> dict[str, Any]: filename = os.path.join(os.path.dirname(__file__), f"{schema_name}.json") with open(filename, "r") as f: - return json.load(f) + json_schema = json.load(f) + if config.openai_functions: + del json_schema["properties"]["command"] + json_schema["required"] = ["thoughts"] + return json_schema def validate_json( @@ -47,7 +51,7 @@ def validate_json( Returns: bool: Whether the json_object is valid or not """ - schema = llm_response_schema(schema_name) + schema = llm_response_schema(config, schema_name) validator = Draft7Validator(schema) if errors := sorted(validator.iter_errors(json_object), key=lambda e: e.path): diff --git a/autogpt/llm/chat.py b/autogpt/llm/chat.py index 0a088d061be..5952da58b84 100644 --- a/autogpt/llm/chat.py +++ b/autogpt/llm/chat.py @@ -1,7 +1,9 @@ from __future__ import annotations import time -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, List + +from autogpt.models.command_function import CommandFunction if TYPE_CHECKING: from autogpt.agent.agent import Agent @@ -21,6 +23,7 @@ def chat_with_ai( system_prompt: str, triggering_prompt: str, token_limit: int, + functions: List[CommandFunction], model: str | None = None, ): """ @@ -94,6 +97,7 @@ def chat_with_ai( current_tokens_used += count_message_tokens([user_input_msg], model) current_tokens_used += 500 # Reserve space for new_summary_message + current_tokens_used += 500 # Reserve space for the openai functions TODO improve # Add Messages until the token limit is reached or there are no more messages to add. for cycle in reversed(list(agent.history.per_cycle(agent.config))): @@ -193,11 +197,13 @@ def chat_with_ai( assistant_reply = create_chat_completion( prompt=message_sequence, config=agent.config, + functions=functions, max_tokens=tokens_remaining, ) # Update full message history agent.history.append(user_input_msg) - agent.history.add("assistant", assistant_reply, "ai_response") + + agent.history.add("assistant", assistant_reply.content, "ai_response") return assistant_reply diff --git a/autogpt/llm/utils/__init__.py b/autogpt/llm/utils/__init__.py index 3b0d3e17608..d083bb80a7a 100644 --- a/autogpt/llm/utils/__init__.py +++ b/autogpt/llm/utils/__init__.py @@ -7,6 +7,8 @@ from autogpt.config import Config from autogpt.logs import logger +from ...models.chat_completion_response import ChatCompletionResponse +from ...models.command_function import CommandFunction from ..api_manager import ApiManager from ..base import ChatSequence, Message from ..providers import openai as iopenai @@ -52,7 +54,7 @@ def call_ai_function( Message("user", arg_str), ], ) - return create_chat_completion(prompt=prompt, temperature=0) + return create_chat_completion(prompt=prompt, temperature=0).content def create_text_completion( @@ -88,10 +90,11 @@ def create_text_completion( def create_chat_completion( prompt: ChatSequence, config: Config, + functions: Optional[List[CommandFunction]] = [], model: Optional[str] = None, temperature: Optional[float] = None, max_tokens: Optional[int] = None, -) -> str: +) -> ChatCompletionResponse: """Create a chat completion using the OpenAI API Args: @@ -134,6 +137,10 @@ def create_chat_completion( chat_completion_kwargs[ "deployment_id" ] = config.get_azure_deployment_id_for_model(model) + if functions: + chat_completion_kwargs["functions"] = [ + function.__dict__ for function in functions + ] response = iopenai.create_chat_completion( messages=prompt.raw(), @@ -141,19 +148,20 @@ def create_chat_completion( ) logger.debug(f"Response: {response}") - resp = "" - if not hasattr(response, "error"): - resp = response.choices[0].message["content"] - else: + if hasattr(response, "error"): logger.error(response.error) raise RuntimeError(response.error) + first_message = response.choices[0].message + content = first_message["content"] + function_call = first_message.get("function_call", {}) + for plugin in config.plugins: if not plugin.can_handle_on_response(): continue - resp = plugin.on_response(resp) + content = plugin.on_response(content) - return resp + return ChatCompletionResponse(content=content, function_call=function_call) def check_model( diff --git a/autogpt/memory/message_history.py b/autogpt/memory/message_history.py index 4dba13dd892..f3e1dc30c94 100644 --- a/autogpt/memory/message_history.py +++ b/autogpt/memory/message_history.py @@ -228,7 +228,7 @@ def summarize_batch(self, new_events_batch, config): PROMPT_SUMMARY_FILE_NAME, ) - self.summary = create_chat_completion(prompt, config) + self.summary = create_chat_completion(prompt, config).content self.agent.log_cycle_handler.log_cycle( self.agent.ai_name, diff --git a/autogpt/models/chat_completion_response.py b/autogpt/models/chat_completion_response.py new file mode 100644 index 00000000000..c2b241533a5 --- /dev/null +++ b/autogpt/models/chat_completion_response.py @@ -0,0 +1,4 @@ +class ChatCompletionResponse: + def __init__(self, content: str, function_call: dict[str, str]): + self.content = content + self.function_call = function_call diff --git a/autogpt/models/command.py b/autogpt/models/command.py index f88bbcae608..aa0883a34f8 100644 --- a/autogpt/models/command.py +++ b/autogpt/models/command.py @@ -9,7 +9,7 @@ class Command: Attributes: name (str): The name of the command. description (str): A brief description of what the command does. - signature (str): The signature of the function that the command executes. Defaults to None. + arguments (str): The arguments of the function that the command executes. Defaults to None. """ def __init__( @@ -17,14 +17,14 @@ def __init__( name: str, description: str, method: Callable[..., Any], - signature: Dict[str, Dict[str, Any]], + arguments: Dict[str, Dict[str, Any]], enabled: bool | Callable[[Config], bool] = True, disabled_reason: Optional[str] = None, ): self.name = name self.description = description self.method = method - self.signature = signature + self.arguments = arguments self.enabled = enabled self.disabled_reason = disabled_reason @@ -38,4 +38,4 @@ def __call__(self, *args, **kwargs) -> Any: return self.method(*args, **kwargs) def __str__(self) -> str: - return f"{self.name}: {self.description}, args: {self.signature}" + return f"{self.name}: {self.description}, args: {self.arguments}" diff --git a/autogpt/models/command_argument.py b/autogpt/models/command_argument.py new file mode 100644 index 00000000000..c70c0880d6e --- /dev/null +++ b/autogpt/models/command_argument.py @@ -0,0 +1,9 @@ +class CommandArgument: + def __init__(self, name: str, type: str, description: str, required: bool): + self.name = name + self.type = type + self.description = description + self.required = required + + def __repr__(self): + return f"CommandArgument('{self.name}', '{self.type}', '{self.description}', {self.required})" diff --git a/autogpt/models/command_function.py b/autogpt/models/command_function.py new file mode 100644 index 00000000000..7cd85e453ab --- /dev/null +++ b/autogpt/models/command_function.py @@ -0,0 +1,18 @@ +from typing import Any + + +class CommandFunction: + """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" + + def __init__(self, name: str, description: str, parameters: dict[str, Any]): + self.name = name + self.description = description + self.parameters = parameters + + @property + def __dict__(self) -> dict: + return { + "name": self.name, + "description": self.description, + "parameters": self.parameters, + } diff --git a/autogpt/processing/text.py b/autogpt/processing/text.py index 78eabf45bbb..30caa5970cb 100644 --- a/autogpt/processing/text.py +++ b/autogpt/processing/text.py @@ -67,7 +67,8 @@ def summarize_text( Args: text (str): The text to summarize - config (Config): The config object + config (Config): Thtext( + "\n\n".joine config object instruction (str): Additional instruction for summarization, e.g. "focus on information related to polar bears", "omit personal information contained in the text" question (str): Question to answer in the summary @@ -114,8 +115,8 @@ def summarize_text( logger.debug(f"Summarizing with {model}:\n{summarization_prompt.dump()}\n") summary = create_chat_completion( - summarization_prompt, config, temperature=0, max_tokens=500 - ) + prompt=summarization_prompt, config=config, temperature=0, max_tokens=500 + ).content logger.debug(f"\n{'-'*16} SUMMARY {'-'*17}\n{summary}\n{'-'*42}\n") return summary.strip(), None diff --git a/autogpt/prompts/generator.py b/autogpt/prompts/generator.py index 2a0334bf45a..f48816247a1 100644 --- a/autogpt/prompts/generator.py +++ b/autogpt/prompts/generator.py @@ -1,7 +1,10 @@ """ A module for generating custom prompt strings.""" +import json from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional +from autogpt.config import Config from autogpt.json_utils.utilities import llm_response_schema +from autogpt.models.command import Command if TYPE_CHECKING: from autogpt.models.command_registry import CommandRegistry @@ -127,7 +130,7 @@ def _generate_numbered_list(self, items: List[Any], item_type="list") -> str: else: return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) - def generate_prompt_string(self) -> str: + def generate_prompt_string(self, config: Config) -> str: """ Generate a prompt string based on the constraints, commands, resources, and performance evaluations. @@ -137,11 +140,26 @@ def generate_prompt_string(self) -> str: """ return ( f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" - "Commands:\n" - f"{self._generate_numbered_list(self.commands, item_type='command')}\n\n" f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" + f"{generate_commands(self, self.commands, config)}" "Performance Evaluation:\n" f"{self._generate_numbered_list(self.performance_evaluation)}\n\n" "Respond with only valid JSON conforming to the following schema: \n" - f"{llm_response_schema()}\n" + f"{json.dumps(llm_response_schema(config))}\n" ) + + +def generate_commands(self, commands: List[Command], config: Config) -> str: + """ + Generate a prompt string based on the constraints, commands, resources, + and performance evaluations. + + Returns: + str: The generated prompt string. + """ + if config.openai_functions: + return "" + return ( + "Commands:\n" + f"{self._generate_numbered_list(self.commands, item_type='command')}\n\n" + ) diff --git a/autogpt/setup.py b/autogpt/setup.py index 2fe8b3a9f1e..f17a91e05ec 100644 --- a/autogpt/setup.py +++ b/autogpt/setup.py @@ -185,7 +185,7 @@ def generate_aiconfig_automatic(user_prompt: str, config: Config) -> AIConfig: ], ), config, - ) + ).content # Debug LLM Output logger.debug(f"AI Config Generator Raw Output: {output}") diff --git a/tests/unit/test_agent_manager.py b/tests/unit/test_agent_manager.py index a372b7260da..d9b99efea47 100644 --- a/tests/unit/test_agent_manager.py +++ b/tests/unit/test_agent_manager.py @@ -2,6 +2,7 @@ from autogpt.agent.agent_manager import AgentManager from autogpt.llm.chat import create_chat_completion +from autogpt.models.chat_completion_response import ChatCompletionResponse @pytest.fixture @@ -32,7 +33,9 @@ def mock_create_chat_completion(mocker): "autogpt.agent.agent_manager.create_chat_completion", wraps=create_chat_completion, ) - mock_create_chat_completion.return_value = "irrelevant" + mock_create_chat_completion.return_value = ChatCompletionResponse( + content="irrelevant", function_call={} + ) return mock_create_chat_completion diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index cb3f539acec..2730a3bd7fc 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -8,7 +8,7 @@ from autogpt.models.command import Command from autogpt.models.command_registry import CommandRegistry -SIGNATURE = "(arg1: int, arg2: str) -> str" +ARGUMENTS = "(arg1: int, arg2: str) -> str" class TestCommand: @@ -26,13 +26,13 @@ def test_command_creation(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) assert cmd.name == "example" assert cmd.description == "Example command" assert cmd.method == self.example_command_method - assert cmd.signature == "(arg1: int, arg2: str) -> str" + assert cmd.arguments == "(arg1: int, arg2: str) -> str" def test_command_call(self): """Test that Command(*args) calls and returns the result of method(*args).""" @@ -41,7 +41,7 @@ def test_command_call(self): name="example", description="Example command", method=self.example_command_method, - signature={ + arguments={ "prompt": { "type": "string", "description": "The prompt used to generate the image", @@ -58,21 +58,21 @@ def test_command_call_with_invalid_arguments(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) with pytest.raises(TypeError): cmd(arg1="invalid", does_not_exist="test") - def test_command_custom_signature(self): - custom_signature = "custom_arg1: int, custom_arg2: str" + def test_command_custom_arguments(self): + custom_arguments = "custom_arg1: int, custom_arg2: str" cmd = Command( name="example", description="Example command", method=self.example_command_method, - signature=custom_signature, + arguments=custom_arguments, ) - assert cmd.signature == custom_signature + assert cmd.arguments == custom_arguments class TestCommandRegistry: @@ -87,7 +87,7 @@ def test_register_command(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) registry.register(cmd) @@ -102,7 +102,7 @@ def test_unregister_command(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) registry.register(cmd) @@ -117,7 +117,7 @@ def test_get_command(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) registry.register(cmd) @@ -139,7 +139,7 @@ def test_call_command(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) registry.register(cmd) @@ -161,7 +161,7 @@ def test_get_command_prompt(self): name="example", description="Example command", method=self.example_command_method, - signature=SIGNATURE, + arguments=ARGUMENTS, ) registry.register(cmd) diff --git a/tests/unit/test_message_history.py b/tests/unit/test_message_history.py index 14b60895ecf..6e6c2d2a4e5 100644 --- a/tests/unit/test_message_history.py +++ b/tests/unit/test_message_history.py @@ -11,6 +11,7 @@ from autogpt.llm.providers.openai import OPEN_AI_CHAT_MODELS from autogpt.llm.utils import count_string_tokens from autogpt.memory.message_history import MessageHistory +from autogpt.models.chat_completion_response import ChatCompletionResponse @pytest.fixture @@ -45,10 +46,13 @@ def test_message_history_batch_summary(mocker, agent, config): message_count = 0 # Setting the mock output and inputs - mock_summary_text = "I executed browse_website command for each of the websites returned from Google search, but none of them have any job openings." + mock_summary_response = ChatCompletionResponse( + content="I executed browse_website command for each of the websites returned from Google search, but none of them have any job openings.", + function_call={}, + ) mock_summary = mocker.patch( "autogpt.memory.message_history.create_chat_completion", - return_value=mock_summary_text, + return_value=mock_summary_response, ) system_prompt = 'You are AIJobSearcher, an AI designed to search for job openings for software engineer role\nYour decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n\nGOALS:\n\n1. Find any job openings for software engineers online\n2. Go through each of the websites and job openings to summarize their requirements and URL, and skip that if you already visit the website\n\nIt takes money to let you run. Your API budget is $5.000\n\nConstraints:\n1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n3. No user assistance\n4. Exclusively use the commands listed in double quotes e.g. "command name"\n\nCommands:\n1. google_search: Google Search, args: "query": ""\n2. browse_website: Browse Website, args: "url": "", "question": ""\n3. task_complete: Task Complete (Shutdown), args: "reason": ""\n\nResources:\n1. Internet access for searches and information gathering.\n2. Long Term memory management.\n3. GPT-3.5 powered Agents for delegation of simple tasks.\n4. File output.\n\nPerformance Evaluation:\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n2. Constructively self-criticize your big-picture behavior constantly.\n3. Reflect on past decisions and strategies to refine your approach.\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n5. Write all code to a file.\n\nYou should only respond in JSON format as described below \nResponse Format: \n{\n "thoughts": {\n "text": "thought",\n "reasoning": "reasoning",\n "plan": "- short bulleted\\n- list that conveys\\n- long-term plan",\n "criticism": "constructive self-criticism",\n "speak": "thoughts summary to say to user"\n },\n "command": {\n "name": "command name",\n "args": {\n "arg name": "value"\n }\n }\n} \nEnsure the response can be parsed by Python json.loads' @@ -139,6 +143,6 @@ def test_message_history_batch_summary(mocker, agent, config): assert new_summary_message == Message( role="system", content="This reminds you of these events from your past: \n" - + mock_summary_text, + + mock_summary_response.content, type=None, ) diff --git a/tests/unit/test_prompt_generator.py b/tests/unit/test_prompt_generator.py index 1fa1754d744..c5ffaf78cd1 100644 --- a/tests/unit/test_prompt_generator.py +++ b/tests/unit/test_prompt_generator.py @@ -1,115 +1,152 @@ -from unittest import TestCase - from autogpt.prompts.generator import PromptGenerator -class TestPromptGenerator(TestCase): +def test_add_constraint(): + """ + Test if the add_constraint() method adds a constraint to the generator's constraints list. + """ + constraint = "Constraint1" + generator = PromptGenerator() + generator.add_constraint(constraint) + assert constraint in generator.constraints + + +def test_add_command(): + """ + Test if the add_command() method adds a command to the generator's commands list. + """ + command_label = "Command Label" + command_name = "command_name" + args = {"arg1": "value1", "arg2": "value2"} + generator = PromptGenerator() + generator.add_command(command_label, command_name, args) + command = { + "label": command_label, + "name": command_name, + "args": args, + "function": None, + } + assert command in generator.commands + + +def test_add_resource(): + """ + Test if the add_resource() method adds a resource to the generator's resources list. + """ + resource = "Resource1" + generator = PromptGenerator() + generator.add_resource(resource) + assert resource in generator.resources + + +def test_add_performance_evaluation(): + """ + Test if the add_performance_evaluation() method adds an evaluation to the generator's + performance_evaluation list. + """ + evaluation = "Evaluation1" + generator = PromptGenerator() + generator.add_performance_evaluation(evaluation) + assert evaluation in generator.performance_evaluation + + +def test_generate_prompt_string(config): + """ + Test if the generate_prompt_string() method generates a prompt string with all the added + constraints, commands, resources, and evaluations. + """ + + # Define the test data + constraints = ["Constraint1", "Constraint2"] + commands = [ + { + "label": "Command1", + "name": "command_name1", + "args": {"arg1": "value1"}, + }, + { + "label": "Command2", + "name": "command_name2", + "args": {}, + }, + ] + resources = ["Resource1", "Resource2"] + evaluations = ["Evaluation1", "Evaluation2"] + + # Add test data to the generator + generator = PromptGenerator() + for constraint in constraints: + generator.add_constraint(constraint) + for command in commands: + generator.add_command(command["label"], command["name"], command["args"]) + for resource in resources: + generator.add_resource(resource) + for evaluation in evaluations: + generator.add_performance_evaluation(evaluation) + + # Generate the prompt string and verify its correctness + prompt_string = generator.generate_prompt_string(config) + assert prompt_string is not None + + # Check if all constraints, commands, resources, and evaluations are present in the prompt string + for constraint in constraints: + assert constraint in prompt_string + for command in commands: + assert command["name"] in prompt_string + for key, value in command["args"].items(): + assert f'"{key}": "{value}"' in prompt_string + for resource in resources: + assert resource in prompt_string + for evaluation in evaluations: + assert evaluation in prompt_string + + +def test_generate_prompt_string(config): """ - Test cases for the PromptGenerator class, which is responsible for generating - prompts for the AI with constraints, commands, resources, and performance evaluations. + Test if the generate_prompt_string() method generates a prompt string with all the added + constraints, commands, resources, and evaluations. """ - @classmethod - def setUpClass(cls): - """ - Set up the initial state for each test method by creating an instance of PromptGenerator. - """ - cls.generator = PromptGenerator() - - # Test whether the add_constraint() method adds a constraint to the generator's constraints list - def test_add_constraint(self): - """ - Test if the add_constraint() method adds a constraint to the generator's constraints list. - """ - constraint = "Constraint1" - self.generator.add_constraint(constraint) - self.assertIn(constraint, self.generator.constraints) - - # Test whether the add_command() method adds a command to the generator's commands list - def test_add_command(self): - """ - Test if the add_command() method adds a command to the generator's commands list. - """ - command_label = "Command Label" - command_name = "command_name" - args = {"arg1": "value1", "arg2": "value2"} - self.generator.add_command(command_label, command_name, args) - command = { - "label": command_label, - "name": command_name, - "args": args, - "function": None, - } - self.assertIn(command, self.generator.commands) - - def test_add_resource(self): - """ - Test if the add_resource() method adds a resource to the generator's resources list. - """ - resource = "Resource1" - self.generator.add_resource(resource) - self.assertIn(resource, self.generator.resources) - - def test_add_performance_evaluation(self): - """ - Test if the add_performance_evaluation() method adds an evaluation to the generator's - performance_evaluation list. - """ - evaluation = "Evaluation1" - self.generator.add_performance_evaluation(evaluation) - self.assertIn(evaluation, self.generator.performance_evaluation) - - def test_generate_prompt_string(self): - """ - Test if the generate_prompt_string() method generates a prompt string with all the added - constraints, commands, resources, and evaluations. - """ - # Define the test data - constraints = ["Constraint1", "Constraint2"] - commands = [ - { - "label": "Command1", - "name": "command_name1", - "args": {"arg1": "value1"}, - }, - { - "label": "Command2", - "name": "command_name2", - "args": {}, - }, - ] - resources = ["Resource1", "Resource2"] - evaluations = ["Evaluation1", "Evaluation2"] - - # Add test data to the generator - for constraint in constraints: - self.generator.add_constraint(constraint) - for command in commands: - self.generator.add_command( - command["label"], command["name"], command["args"] - ) - for resource in resources: - self.generator.add_resource(resource) - for evaluation in evaluations: - self.generator.add_performance_evaluation(evaluation) - - # Generate the prompt string and verify its correctness - prompt_string = self.generator.generate_prompt_string() - self.assertIsNotNone(prompt_string) - - # Check if all constraints, commands, resources, and evaluations are present in the prompt string - for constraint in constraints: - self.assertIn(constraint, prompt_string) - for command in commands: - self.assertIn(command["name"], prompt_string) - for key, value in command["args"].items(): - self.assertIn(f'"{key}": "{value}"', prompt_string) - for resource in resources: - self.assertIn(resource, prompt_string) - for evaluation in evaluations: - self.assertIn(evaluation, prompt_string) - - self.assertIn("constraints", prompt_string.lower()) - self.assertIn("commands", prompt_string.lower()) - self.assertIn("resources", prompt_string.lower()) - self.assertIn("performance evaluation", prompt_string.lower()) + # Define the test data + constraints = ["Constraint1", "Constraint2"] + commands = [ + { + "label": "Command1", + "name": "command_name1", + "args": {"arg1": "value1"}, + }, + { + "label": "Command2", + "name": "command_name2", + "args": {}, + }, + ] + resources = ["Resource1", "Resource2"] + evaluations = ["Evaluation1", "Evaluation2"] + + # Add test data to the generator + generator = PromptGenerator() + for constraint in constraints: + generator.add_constraint(constraint) + for command in commands: + generator.add_command(command["label"], command["name"], command["args"]) + for resource in resources: + generator.add_resource(resource) + for evaluation in evaluations: + generator.add_performance_evaluation(evaluation) + + # Generate the prompt string and verify its correctness + prompt_string = generator.generate_prompt_string(config) + assert prompt_string is not None + + # Check if all constraints, commands, resources, and evaluations are present in the prompt string + for constraint in constraints: + assert constraint in prompt_string + for command in commands: + assert command["name"] in prompt_string + for key, value in command["args"].items(): + assert f'"{key}": "{value}"' in prompt_string + for resource in resources: + assert resource in prompt_string + for evaluation in evaluations: + assert evaluation in prompt_string From fb6c9671e1a5aeada12086d08c4bc09afff415e8 Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Wed, 21 Jun 2023 09:19:35 -0700 Subject: [PATCH 02/14] Update autogpt/models/command_function.py Co-authored-by: Reinier van der Leer --- autogpt/models/command_function.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/autogpt/models/command_function.py b/autogpt/models/command_function.py index 7cd85e453ab..e59967b078f 100644 --- a/autogpt/models/command_function.py +++ b/autogpt/models/command_function.py @@ -1,18 +1,10 @@ from typing import Any +@dataclass class CommandFunction: """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" - def __init__(self, name: str, description: str, parameters: dict[str, Any]): - self.name = name - self.description = description - self.parameters = parameters - - @property - def __dict__(self) -> dict: - return { - "name": self.name, - "description": self.description, - "parameters": self.parameters, - } + name: str + description: str + parameters: dict[str, Any] From 3bbdd3faa6db7c17f43189d9841b7eb2819fdb35 Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Wed, 21 Jun 2023 09:21:00 -0700 Subject: [PATCH 03/14] Update autogpt/json_utils/utilities.py Co-authored-by: Reinier van der Leer --- autogpt/json_utils/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/json_utils/utilities.py b/autogpt/json_utils/utilities.py index b78d6322ed7..7162abc58c4 100644 --- a/autogpt/json_utils/utilities.py +++ b/autogpt/json_utils/utilities.py @@ -36,7 +36,7 @@ def llm_response_schema( json_schema = json.load(f) if config.openai_functions: del json_schema["properties"]["command"] - json_schema["required"] = ["thoughts"] + json_schema["required"].remove("command") return json_schema From d763723ce6a98614d11a424031b7b80665f02af8 Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Wed, 21 Jun 2023 10:00:32 -0700 Subject: [PATCH 04/14] Cleanup OpenAI functions implementation Signed-off-by: Merwane Hamadi --- autogpt/agent/agent.py | 36 ---------------------- autogpt/app.py | 8 +++-- autogpt/llm/base.py | 12 +++++++- autogpt/llm/chat.py | 8 ++--- autogpt/llm/providers/openai.py | 36 ++++++++++++++++++++++ autogpt/llm/utils/__init__.py | 16 ++++++---- autogpt/models/chat_completion_response.py | 4 --- autogpt/models/command_argument.py | 13 +++++--- autogpt/models/command_function.py | 10 ------ autogpt/processing/text.py | 3 +- autogpt/prompts/generator.py | 5 ++- tests/unit/test_agent_manager.py | 11 ++++--- tests/unit/test_message_history.py | 6 ++-- 13 files changed, 86 insertions(+), 82 deletions(-) delete mode 100644 autogpt/models/chat_completion_response.py delete mode 100644 autogpt/models/command_function.py diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index 1a8248b8a11..fca03a5fb3b 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -20,7 +20,6 @@ from autogpt.logs import logger, print_assistant_thoughts from autogpt.memory.message_history import MessageHistory from autogpt.memory.vector import VectorMemory -from autogpt.models.command_function import CommandFunction from autogpt.models.command_registry import CommandRegistry from autogpt.speech import say_text from autogpt.spinner import Spinner @@ -139,7 +138,6 @@ def signal_handler(signum, frame): self.system_prompt, self.triggering_prompt, self.fast_token_limit, - self.get_functions_from_commands(), self.config.fast_llm_model, ) @@ -320,37 +318,3 @@ def _resolve_pathlike_command_args(self, command_args): self.workspace.get_path(command_args[pathlike]) ) return command_args - - def get_functions_from_commands(self) -> list[CommandFunction]: - """Get functions from the commands. "functions" in this context refers to OpenAI functions - see https://platform.openai.com/docs/guides/gpt/function-calling - """ - functions = [] - if not self.config.openai_functions: - return functions - for command in self.command_registry.commands.values(): - properties = {} - required = [] - - for argument in command.arguments: - properties[argument.name] = { - "type": argument.type, - "description": argument.description, - } - if argument.required: - required.append(argument.name) - - parameters = { - "type": "object", - "properties": properties, - "required": required, - } - functions.append( - CommandFunction( - name=command.name, - description=command.description, - parameters=parameters, - ) - ) - - return functions diff --git a/autogpt/app.py b/autogpt/app.py index 4f1c3a9a436..a73bb82024b 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -4,7 +4,7 @@ from autogpt.agent.agent import Agent from autogpt.config import Config -from autogpt.models.chat_completion_response import ChatCompletionResponse +from autogpt.llm import ChatModelResponse def is_valid_int(value: str) -> bool: @@ -24,12 +24,14 @@ def is_valid_int(value: str) -> bool: def get_command( - assistant_reply_json: Dict, assistant_reply: ChatCompletionResponse, config: Config + assistant_reply_json: Dict, assistant_reply: ChatModelResponse, config: Config ): """Parse the response and return the command name and arguments Args: - assistant_reply_json (json): The response from the AI + assistant_reply_json (json): The json response from the AI + assistant_reply (ChatModelResponse): The model response from the AI + config (Config): The config object Returns: tuple: The command name and arguments diff --git a/autogpt/llm/base.py b/autogpt/llm/base.py index d372ad252ea..48f5b43bb8c 100644 --- a/autogpt/llm/base.py +++ b/autogpt/llm/base.py @@ -2,7 +2,7 @@ from dataclasses import dataclass, field from math import ceil, floor -from typing import List, Literal, TypedDict +from typing import Any, Dict, List, Literal, TypedDict MessageRole = Literal["system", "user", "assistant"] MessageType = Literal["ai_response", "action_result"] @@ -157,3 +157,13 @@ class ChatModelResponse(LLMResponse): """Standard response struct for a response from an LLM model.""" content: str = None + function_call: Dict[str, Any] = None + + +@dataclass +class OpenAIFunctionSpec: + """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" + + name: str + description: str + parameters: dict[str, Any] diff --git a/autogpt/llm/chat.py b/autogpt/llm/chat.py index 5952da58b84..c5d5a945a7e 100644 --- a/autogpt/llm/chat.py +++ b/autogpt/llm/chat.py @@ -1,9 +1,9 @@ from __future__ import annotations import time -from typing import TYPE_CHECKING, List +from typing import TYPE_CHECKING -from autogpt.models.command_function import CommandFunction +from autogpt.llm.providers.openai import get_openai_command_specs if TYPE_CHECKING: from autogpt.agent.agent import Agent @@ -23,7 +23,6 @@ def chat_with_ai( system_prompt: str, triggering_prompt: str, token_limit: int, - functions: List[CommandFunction], model: str | None = None, ): """ @@ -197,13 +196,12 @@ def chat_with_ai( assistant_reply = create_chat_completion( prompt=message_sequence, config=agent.config, - functions=functions, + functions=get_openai_command_specs(agent), max_tokens=tokens_remaining, ) # Update full message history agent.history.append(user_input_msg) - agent.history.add("assistant", assistant_reply.content, "ai_response") return assistant_reply diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index ef384667109..c92feb1dfd0 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -13,6 +13,7 @@ ChatModelInfo, EmbeddingModelInfo, MessageDict, + OpenAIFunctionSpec, TextModelInfo, TText, ) @@ -267,3 +268,38 @@ def create_embedding( input=input, **kwargs, ) + + +def get_openai_command_specs(agent) -> list[OpenAIFunctionSpec]: + """Get functions from the commands. "functions" in this context refers to OpenAI functions + see https://platform.openai.com/docs/guides/gpt/function-calling + """ + functions = [] + if not agent.config.openai_functions: + return functions + for command in agent.command_registry.commands.values(): + properties = {} + required = [] + + for argument in command.arguments: + properties[argument.name] = { + "type": argument.type, + "description": argument.description, + } + if argument.required: + required.append(argument.name) + + parameters = { + "type": "object", + "properties": properties, + "required": required, + } + functions.append( + OpenAIFunctionSpec( + name=command.name, + description=command.description, + parameters=parameters, + ) + ) + + return functions diff --git a/autogpt/llm/utils/__init__.py b/autogpt/llm/utils/__init__.py index d083bb80a7a..a86727866e0 100644 --- a/autogpt/llm/utils/__init__.py +++ b/autogpt/llm/utils/__init__.py @@ -7,11 +7,10 @@ from autogpt.config import Config from autogpt.logs import logger -from ...models.chat_completion_response import ChatCompletionResponse -from ...models.command_function import CommandFunction from ..api_manager import ApiManager -from ..base import ChatSequence, Message +from ..base import ChatModelResponse, ChatSequence, Message, OpenAIFunctionSpec from ..providers import openai as iopenai +from ..providers.openai import OPEN_AI_CHAT_MODELS from .token_counter import * @@ -90,11 +89,11 @@ def create_text_completion( def create_chat_completion( prompt: ChatSequence, config: Config, - functions: Optional[List[CommandFunction]] = [], + functions: Optional[List[OpenAIFunctionSpec]] = [], model: Optional[str] = None, temperature: Optional[float] = None, max_tokens: Optional[int] = None, -) -> ChatCompletionResponse: +) -> ChatModelResponse: """Create a chat completion using the OpenAI API Args: @@ -106,6 +105,7 @@ def create_chat_completion( Returns: str: The response from the chat completion """ + if model is None: model = prompt.model.name if temperature is None: @@ -161,7 +161,11 @@ def create_chat_completion( continue content = plugin.on_response(content) - return ChatCompletionResponse(content=content, function_call=function_call) + return ChatModelResponse( + model_info=OPEN_AI_CHAT_MODELS[model], + content=content, + function_call=function_call, + ) def check_model( diff --git a/autogpt/models/chat_completion_response.py b/autogpt/models/chat_completion_response.py deleted file mode 100644 index c2b241533a5..00000000000 --- a/autogpt/models/chat_completion_response.py +++ /dev/null @@ -1,4 +0,0 @@ -class ChatCompletionResponse: - def __init__(self, content: str, function_call: dict[str, str]): - self.content = content - self.function_call = function_call diff --git a/autogpt/models/command_argument.py b/autogpt/models/command_argument.py index c70c0880d6e..67d4b3004b5 100644 --- a/autogpt/models/command_argument.py +++ b/autogpt/models/command_argument.py @@ -1,9 +1,12 @@ +import dataclasses + + +@dataclasses.dataclass class CommandArgument: - def __init__(self, name: str, type: str, description: str, required: bool): - self.name = name - self.type = type - self.description = description - self.required = required + name: str + type: str + description: str + required: bool def __repr__(self): return f"CommandArgument('{self.name}', '{self.type}', '{self.description}', {self.required})" diff --git a/autogpt/models/command_function.py b/autogpt/models/command_function.py deleted file mode 100644 index e59967b078f..00000000000 --- a/autogpt/models/command_function.py +++ /dev/null @@ -1,10 +0,0 @@ -from typing import Any - - -@dataclass -class CommandFunction: - """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" - - name: str - description: str - parameters: dict[str, Any] diff --git a/autogpt/processing/text.py b/autogpt/processing/text.py index 30caa5970cb..24851b1c4dd 100644 --- a/autogpt/processing/text.py +++ b/autogpt/processing/text.py @@ -67,8 +67,7 @@ def summarize_text( Args: text (str): The text to summarize - config (Config): Thtext( - "\n\n".joine config object + config (Config): The config object instruction (str): Additional instruction for summarization, e.g. "focus on information related to polar bears", "omit personal information contained in the text" question (str): Question to answer in the summary diff --git a/autogpt/prompts/generator.py b/autogpt/prompts/generator.py index f48816247a1..8296d3dcf75 100644 --- a/autogpt/prompts/generator.py +++ b/autogpt/prompts/generator.py @@ -4,7 +4,6 @@ from autogpt.config import Config from autogpt.json_utils.utilities import llm_response_schema -from autogpt.models.command import Command if TYPE_CHECKING: from autogpt.models.command_registry import CommandRegistry @@ -141,7 +140,7 @@ def generate_prompt_string(self, config: Config) -> str: return ( f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" - f"{generate_commands(self, self.commands, config)}" + f"{generate_commands(self, config)}" "Performance Evaluation:\n" f"{self._generate_numbered_list(self.performance_evaluation)}\n\n" "Respond with only valid JSON conforming to the following schema: \n" @@ -149,7 +148,7 @@ def generate_prompt_string(self, config: Config) -> str: ) -def generate_commands(self, commands: List[Command], config: Config) -> str: +def generate_commands(self, config: Config) -> str: """ Generate a prompt string based on the constraints, commands, resources, and performance evaluations. diff --git a/tests/unit/test_agent_manager.py b/tests/unit/test_agent_manager.py index d9b99efea47..7140db059d0 100644 --- a/tests/unit/test_agent_manager.py +++ b/tests/unit/test_agent_manager.py @@ -1,8 +1,9 @@ import pytest from autogpt.agent.agent_manager import AgentManager +from autogpt.llm import ChatModelResponse from autogpt.llm.chat import create_chat_completion -from autogpt.models.chat_completion_response import ChatCompletionResponse +from autogpt.llm.providers.openai import OPEN_AI_CHAT_MODELS @pytest.fixture @@ -28,13 +29,15 @@ def model(): @pytest.fixture(autouse=True) -def mock_create_chat_completion(mocker): +def mock_create_chat_completion(mocker, config): mock_create_chat_completion = mocker.patch( "autogpt.agent.agent_manager.create_chat_completion", wraps=create_chat_completion, ) - mock_create_chat_completion.return_value = ChatCompletionResponse( - content="irrelevant", function_call={} + mock_create_chat_completion.return_value = ChatModelResponse( + model_info=OPEN_AI_CHAT_MODELS[config.fast_llm_model], + content="irrelevant", + function_call={}, ) return mock_create_chat_completion diff --git a/tests/unit/test_message_history.py b/tests/unit/test_message_history.py index 6e6c2d2a4e5..a3650005ec2 100644 --- a/tests/unit/test_message_history.py +++ b/tests/unit/test_message_history.py @@ -7,11 +7,10 @@ from autogpt.agent import Agent from autogpt.config import AIConfig from autogpt.config.config import Config -from autogpt.llm.base import ChatSequence, Message +from autogpt.llm.base import ChatModelResponse, ChatSequence, Message from autogpt.llm.providers.openai import OPEN_AI_CHAT_MODELS from autogpt.llm.utils import count_string_tokens from autogpt.memory.message_history import MessageHistory -from autogpt.models.chat_completion_response import ChatCompletionResponse @pytest.fixture @@ -46,7 +45,8 @@ def test_message_history_batch_summary(mocker, agent, config): message_count = 0 # Setting the mock output and inputs - mock_summary_response = ChatCompletionResponse( + mock_summary_response = ChatModelResponse( + model_info=OPEN_AI_CHAT_MODELS[model], content="I executed browse_website command for each of the websites returned from Google search, but none of them have any job openings.", function_call={}, ) From 7eb696dee21864ad78e6136dcba06a2258c48e72 Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Wed, 21 Jun 2023 11:27:35 -0700 Subject: [PATCH 05/14] Update json to dict in docstrings Co-authored-by: Reinier van der Leer --- autogpt/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/app.py b/autogpt/app.py index a73bb82024b..5ff56839f50 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -29,7 +29,7 @@ def get_command( """Parse the response and return the command name and arguments Args: - assistant_reply_json (json): The json response from the AI + assistant_reply_json (dict): The response object from the AI assistant_reply (ChatModelResponse): The model response from the AI config (Config): The config object From b4f2077d3c073e134b052f6606c9d73d5fcc89c3 Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Wed, 21 Jun 2023 11:28:36 -0700 Subject: [PATCH 06/14] update docstrings of get_openai_command_specs Co-authored-by: Reinier van der Leer --- autogpt/llm/providers/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index c92feb1dfd0..697365a1cea 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -271,7 +271,7 @@ def create_embedding( def get_openai_command_specs(agent) -> list[OpenAIFunctionSpec]: - """Get functions from the commands. "functions" in this context refers to OpenAI functions + """Get OpenAI-consumable function specs for the agent's available commands. see https://platform.openai.com/docs/guides/gpt/function-calling """ functions = [] From 0c39937f9854c182b6799f88de74a0b026a93910 Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Wed, 21 Jun 2023 14:14:22 -0700 Subject: [PATCH 07/14] Implement complete data types Signed-off-by: Merwane Hamadi --- autogpt/app.py | 2 +- autogpt/llm/base.py | 27 ++++++++++++++++++++++++--- autogpt/llm/providers/openai.py | 20 +++++++++++--------- autogpt/llm/utils/__init__.py | 3 ++- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/autogpt/app.py b/autogpt/app.py index 5ff56839f50..2a630ec3eb7 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -53,7 +53,7 @@ def get_command( if not isinstance(assistant_reply_json, dict): return ( "Error:", - f"'assistant_reply_json' object is not dictionary {assistant_reply_json}", + f"The previous message sent was not a dictionary {assistant_reply_json}", ) command = assistant_reply_json["command"] diff --git a/autogpt/llm/base.py b/autogpt/llm/base.py index 48f5b43bb8c..6e8a8c590ac 100644 --- a/autogpt/llm/base.py +++ b/autogpt/llm/base.py @@ -2,7 +2,9 @@ from dataclasses import dataclass, field from math import ceil, floor -from typing import Any, Dict, List, Literal, TypedDict +from typing import List, Literal, TypedDict + +from autogpt.models.command_argument import CommandArgument MessageRole = Literal["system", "user", "assistant"] MessageType = Literal["ai_response", "action_result"] @@ -157,7 +159,7 @@ class ChatModelResponse(LLMResponse): """Standard response struct for a response from an LLM model.""" content: str = None - function_call: Dict[str, Any] = None + function_call: OpenAIFunctionCall = None @dataclass @@ -166,4 +168,23 @@ class OpenAIFunctionSpec: name: str description: str - parameters: dict[str, Any] + parameters: OpenAIFunctionParameter + + +@dataclass +class OpenAIFunctionCall: + name: str + arguments: List[CommandArgument] + + +@dataclass +class OpenAIFunctionParameter: + type: str + properties: OpenAIFunctionProperties + required: bool + + +@dataclass +class OpenAIFunctionProperties: + type: str + description: str diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index 697365a1cea..1423e7ffb98 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -13,6 +13,8 @@ ChatModelInfo, EmbeddingModelInfo, MessageDict, + OpenAIFunctionParameter, + OpenAIFunctionProperties, OpenAIFunctionSpec, TextModelInfo, TText, @@ -282,18 +284,18 @@ def get_openai_command_specs(agent) -> list[OpenAIFunctionSpec]: required = [] for argument in command.arguments: - properties[argument.name] = { - "type": argument.type, - "description": argument.description, - } + properties[argument.name] = OpenAIFunctionProperties( + type=argument.type, + description=argument.description, + ) if argument.required: required.append(argument.name) + parameters = OpenAIFunctionParameter( + type="object", + properties=properties, + required=required, + ) - parameters = { - "type": "object", - "properties": properties, - "required": required, - } functions.append( OpenAIFunctionSpec( name=command.name, diff --git a/autogpt/llm/utils/__init__.py b/autogpt/llm/utils/__init__.py index a86727866e0..dc34704acae 100644 --- a/autogpt/llm/utils/__init__.py +++ b/autogpt/llm/utils/__init__.py @@ -1,5 +1,6 @@ from __future__ import annotations +from dataclasses import asdict from typing import List, Literal, Optional from colorama import Fore @@ -139,7 +140,7 @@ def create_chat_completion( ] = config.get_azure_deployment_id_for_model(model) if functions: chat_completion_kwargs["functions"] = [ - function.__dict__ for function in functions + asdict(function) for function in functions ] response = iopenai.create_chat_completion( From f24108ee38e1f00830477dcd901f96568106e49b Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 03:34:28 +0200 Subject: [PATCH 08/14] Improve types in command system --- autogpt/app.py | 2 + autogpt/command_decorator.py | 29 +++-- autogpt/llm/base.py | 37 +----- autogpt/llm/providers/openai.py | 107 ++++++++++++------ autogpt/llm/utils/__init__.py | 14 ++- autogpt/models/command.py | 12 +- ...mmand_argument.py => command_parameter.py} | 4 +- autogpt/models/command_registry.py | 2 + tests/unit/test_commands.py | 49 ++++---- 9 files changed, 140 insertions(+), 116 deletions(-) rename autogpt/models/{command_argument.py => command_parameter.py} (51%) diff --git a/autogpt/app.py b/autogpt/app.py index 2a630ec3eb7..06db7938db9 100644 --- a/autogpt/app.py +++ b/autogpt/app.py @@ -42,6 +42,8 @@ def get_command( Exception: If any other error occurs """ if config.openai_functions: + if assistant_reply.function_call is None: + return "Error:", "No 'function_call' in assistant reply" assistant_reply_json["command"] = { "name": assistant_reply.function_call.name, "args": json.loads(assistant_reply.function_call.arguments), diff --git a/autogpt/command_decorator.py b/autogpt/command_decorator.py index d86e41096b2..f179f978d0d 100644 --- a/autogpt/command_decorator.py +++ b/autogpt/command_decorator.py @@ -1,38 +1,43 @@ import functools -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Optional, TypedDict from autogpt.config import Config -from autogpt.models.command import Command -from autogpt.models.command_argument import CommandArgument +from autogpt.models.command import Command, CommandParameter # Unique identifier for auto-gpt commands AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command" +class CommandParameterSpec(TypedDict): + type: str + description: str + required: bool + + def command( name: str, description: str, - arguments: Dict[str, Dict[str, Any]], + parameters: dict[str, CommandParameterSpec], enabled: bool | Callable[[Config], bool] = True, disabled_reason: Optional[str] = None, ) -> Callable[..., Any]: """The command decorator is used to create Command objects from ordinary functions.""" def decorator(func: Callable[..., Any]) -> Command: - typed_arguments = [ - CommandArgument( - name=arg_name, - description=argument.get("description"), - type=argument.get("type", "string"), - required=argument.get("required", False), + typed_parameters = [ + CommandParameter( + name=param_name, + description=parameter.get("description"), + type=parameter.get("type", "string"), + required=parameter.get("required", False), ) - for arg_name, argument in arguments.items() + for param_name, parameter in parameters.items() ] cmd = Command( name=name, description=description, method=func, - arguments=typed_arguments, + parameters=typed_parameters, enabled=enabled, disabled_reason=disabled_reason, ) diff --git a/autogpt/llm/base.py b/autogpt/llm/base.py index 6e8a8c590ac..4ff80dc73bd 100644 --- a/autogpt/llm/base.py +++ b/autogpt/llm/base.py @@ -2,9 +2,10 @@ from dataclasses import dataclass, field from math import ceil, floor -from typing import List, Literal, TypedDict +from typing import TYPE_CHECKING, List, Literal, Optional, TypedDict -from autogpt.models.command_argument import CommandArgument +if TYPE_CHECKING: + from autogpt.llm.providers.openai import OpenAIFunctionCall MessageRole = Literal["system", "user", "assistant"] MessageType = Literal["ai_response", "action_result"] @@ -158,33 +159,5 @@ def __post_init__(self): class ChatModelResponse(LLMResponse): """Standard response struct for a response from an LLM model.""" - content: str = None - function_call: OpenAIFunctionCall = None - - -@dataclass -class OpenAIFunctionSpec: - """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" - - name: str - description: str - parameters: OpenAIFunctionParameter - - -@dataclass -class OpenAIFunctionCall: - name: str - arguments: List[CommandArgument] - - -@dataclass -class OpenAIFunctionParameter: - type: str - properties: OpenAIFunctionProperties - required: bool - - -@dataclass -class OpenAIFunctionProperties: - type: str - description: str + content: Optional[str] = None + function_call: Optional[OpenAIFunctionCall] = None diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index 1423e7ffb98..1d093be79a3 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -1,6 +1,9 @@ +from __future__ import annotations + import functools import time -from typing import List +from dataclasses import dataclass +from typing import TYPE_CHECKING, List, Optional from unittest.mock import patch import openai @@ -9,13 +12,13 @@ from openai.error import APIError, RateLimitError, Timeout from openai.openai_object import OpenAIObject +if TYPE_CHECKING: + from autogpt.agent.agent import Agent + from autogpt.llm.base import ( ChatModelInfo, EmbeddingModelInfo, MessageDict, - OpenAIFunctionParameter, - OpenAIFunctionProperties, - OpenAIFunctionSpec, TextModelInfo, TText, ) @@ -272,36 +275,76 @@ def create_embedding( ) -def get_openai_command_specs(agent) -> list[OpenAIFunctionSpec]: +@dataclass +class OpenAIFunctionCall: + """Represents a function call as generated by an OpenAI model + + Attributes: + name: the name of the function that the LLM wants to call + arguments: a stringified JSON object (unverified) containing `arg: value` pairs + """ + + name: str + arguments: str + + +@dataclass +class OpenAIFunctionSpec: + """Represents a "function" in OpenAI, which is mapped to a Command in Auto-GPT""" + + name: str + description: str + parameters: dict[str, ParameterSpec] + + @dataclass + class ParameterSpec: + name: str + type: str + description: Optional[str] + required: bool = False + + @property + def __dict__(self): + """Output""" + return { + "name": self.name, + "description": self.description, + "parameters": { + "type": "object", + "properties": { + param.name: { + "type": param.type, + "description": param.description, + } + for param in self.parameters.values() + }, + "required": [ + param.name for param in self.parameters.values() if param.required + ], + }, + } + + +def get_openai_command_specs(agent: Agent) -> list[OpenAIFunctionSpec]: """Get OpenAI-consumable function specs for the agent's available commands. see https://platform.openai.com/docs/guides/gpt/function-calling """ - functions = [] if not agent.config.openai_functions: - return functions - for command in agent.command_registry.commands.values(): - properties = {} - required = [] - - for argument in command.arguments: - properties[argument.name] = OpenAIFunctionProperties( - type=argument.type, - description=argument.description, - ) - if argument.required: - required.append(argument.name) - parameters = OpenAIFunctionParameter( - type="object", - properties=properties, - required=required, + return [] + + return [ + OpenAIFunctionSpec( + name=command.name, + description=command.description, + parameters={ + param.name: OpenAIFunctionSpec.ParameterSpec( + name=param.name, + type=param.type, + required=param.required, + description=param.description, + ) + for param in command.parameters + }, ) - - functions.append( - OpenAIFunctionSpec( - name=command.name, - description=command.description, - parameters=parameters, - ) - ) - - return functions + for command in agent.command_registry.commands.values() + ] diff --git a/autogpt/llm/utils/__init__.py b/autogpt/llm/utils/__init__.py index dc34704acae..2328d000727 100644 --- a/autogpt/llm/utils/__init__.py +++ b/autogpt/llm/utils/__init__.py @@ -9,9 +9,13 @@ from autogpt.logs import logger from ..api_manager import ApiManager -from ..base import ChatModelResponse, ChatSequence, Message, OpenAIFunctionSpec +from ..base import ChatModelResponse, ChatSequence, Message from ..providers import openai as iopenai -from ..providers.openai import OPEN_AI_CHAT_MODELS +from ..providers.openai import ( + OPEN_AI_CHAT_MODELS, + OpenAIFunctionCall, + OpenAIFunctionSpec, +) from .token_counter import * @@ -54,7 +58,7 @@ def call_ai_function( Message("user", arg_str), ], ) - return create_chat_completion(prompt=prompt, temperature=0).content + return create_chat_completion(prompt=prompt, temperature=0, config=config).content def create_text_completion( @@ -154,8 +158,8 @@ def create_chat_completion( raise RuntimeError(response.error) first_message = response.choices[0].message - content = first_message["content"] - function_call = first_message.get("function_call", {}) + content: str | None = first_message.get("content") + function_call: OpenAIFunctionCall | None = first_message.get("function_call") for plugin in config.plugins: if not plugin.can_handle_on_response(): diff --git a/autogpt/models/command.py b/autogpt/models/command.py index aa0883a34f8..f7e25999dd8 100644 --- a/autogpt/models/command.py +++ b/autogpt/models/command.py @@ -1,7 +1,9 @@ -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Optional from autogpt.config import Config +from .command_parameter import CommandParameter + class Command: """A class representing a command. @@ -9,7 +11,7 @@ class Command: Attributes: name (str): The name of the command. description (str): A brief description of what the command does. - arguments (str): The arguments of the function that the command executes. Defaults to None. + parameters (list): The parameters of the function that the command executes. """ def __init__( @@ -17,14 +19,14 @@ def __init__( name: str, description: str, method: Callable[..., Any], - arguments: Dict[str, Dict[str, Any]], + parameters: list[CommandParameter], enabled: bool | Callable[[Config], bool] = True, disabled_reason: Optional[str] = None, ): self.name = name self.description = description self.method = method - self.arguments = arguments + self.parameters = parameters self.enabled = enabled self.disabled_reason = disabled_reason @@ -38,4 +40,4 @@ def __call__(self, *args, **kwargs) -> Any: return self.method(*args, **kwargs) def __str__(self) -> str: - return f"{self.name}: {self.description}, args: {self.arguments}" + return f"{self.name}: {self.description}, params: {self.parameters}" diff --git a/autogpt/models/command_argument.py b/autogpt/models/command_parameter.py similarity index 51% rename from autogpt/models/command_argument.py rename to autogpt/models/command_parameter.py index 67d4b3004b5..ec130c8751f 100644 --- a/autogpt/models/command_argument.py +++ b/autogpt/models/command_parameter.py @@ -2,11 +2,11 @@ @dataclasses.dataclass -class CommandArgument: +class CommandParameter: name: str type: str description: str required: bool def __repr__(self): - return f"CommandArgument('{self.name}', '{self.type}', '{self.description}', {self.required})" + return f"CommandParameter('{self.name}', '{self.type}', '{self.description}', {self.required})" diff --git a/autogpt/models/command_registry.py b/autogpt/models/command_registry.py index 29d0143d91a..96418d26b98 100644 --- a/autogpt/models/command_registry.py +++ b/autogpt/models/command_registry.py @@ -15,6 +15,8 @@ class CommandRegistry: directory. """ + commands: dict[str, Command] + def __init__(self): self.commands = {} diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index 2730a3bd7fc..fa8ca599022 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -5,10 +5,13 @@ import pytest -from autogpt.models.command import Command +from autogpt.models.command import Command, CommandParameter from autogpt.models.command_registry import CommandRegistry -ARGUMENTS = "(arg1: int, arg2: str) -> str" +PARAMETERS = [ + CommandParameter("arg1", "int", description="Argument 1", required=False), + CommandParameter("arg2", "str", description="Argument 2", required=False), +] class TestCommand: @@ -26,13 +29,13 @@ def test_command_creation(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) assert cmd.name == "example" assert cmd.description == "Example command" assert cmd.method == self.example_command_method - assert cmd.arguments == "(arg1: int, arg2: str) -> str" + assert cmd.parameters == "(arg1: int, arg2: str) -> str" def test_command_call(self): """Test that Command(*args) calls and returns the result of method(*args).""" @@ -41,13 +44,14 @@ def test_command_call(self): name="example", description="Example command", method=self.example_command_method, - arguments={ - "prompt": { - "type": "string", - "description": "The prompt used to generate the image", - "required": True, - }, - }, + parameters=[ + CommandParameter( + name="prompt", + type="string", + description="The prompt used to generate the image", + required=True, + ), + ], ) result = cmd(arg1=1, arg2="test") assert result == "1 - test" @@ -58,22 +62,11 @@ def test_command_call_with_invalid_arguments(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) with pytest.raises(TypeError): cmd(arg1="invalid", does_not_exist="test") - def test_command_custom_arguments(self): - custom_arguments = "custom_arg1: int, custom_arg2: str" - cmd = Command( - name="example", - description="Example command", - method=self.example_command_method, - arguments=custom_arguments, - ) - - assert cmd.arguments == custom_arguments - class TestCommandRegistry: @staticmethod @@ -87,7 +80,7 @@ def test_register_command(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) registry.register(cmd) @@ -102,7 +95,7 @@ def test_unregister_command(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) registry.register(cmd) @@ -117,7 +110,7 @@ def test_get_command(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) registry.register(cmd) @@ -139,7 +132,7 @@ def test_call_command(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) registry.register(cmd) @@ -161,7 +154,7 @@ def test_get_command_prompt(self): name="example", description="Example command", method=self.example_command_method, - arguments=ARGUMENTS, + parameters=PARAMETERS, ) registry.register(cmd) From f2cd88e48b946c1cfd80ae48fee55de66403d272 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:01:57 +0200 Subject: [PATCH 09/14] Fix command signature generation in text mode --- autogpt/models/command.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/autogpt/models/command.py b/autogpt/models/command.py index f7e25999dd8..92cf414a9ec 100644 --- a/autogpt/models/command.py +++ b/autogpt/models/command.py @@ -40,4 +40,8 @@ def __call__(self, *args, **kwargs) -> Any: return self.method(*args, **kwargs) def __str__(self) -> str: - return f"{self.name}: {self.description}, params: {self.parameters}" + params = [ + f"{param.name}: {param.type if param.required else f'Optional[{param.type}]'}" + for param in self.parameters + ] + return f"{self.name}: {self.description}, params: ({', '.join(params)})" From 6b10a74928f142831bc7f77a971bffd6ada361f0 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:03:04 +0200 Subject: [PATCH 10/14] Move back commands to their old position in system prompt --- autogpt/prompts/generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/prompts/generator.py b/autogpt/prompts/generator.py index 8296d3dcf75..3fff9536ab6 100644 --- a/autogpt/prompts/generator.py +++ b/autogpt/prompts/generator.py @@ -139,8 +139,8 @@ def generate_prompt_string(self, config: Config) -> str: """ return ( f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" - f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" f"{generate_commands(self, config)}" + f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" "Performance Evaluation:\n" f"{self._generate_numbered_list(self.performance_evaluation)}\n\n" "Respond with only valid JSON conforming to the following schema: \n" From c76ace7c2c3964181196db9f4cd68a9a4a389926 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:20:36 +0200 Subject: [PATCH 11/14] Fix function spec generation in OpenAI Function Call mode --- autogpt/llm/providers/openai.py | 2 +- autogpt/llm/utils/__init__.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index 1d093be79a3..3c16f5cfce4 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -305,7 +305,7 @@ class ParameterSpec: @property def __dict__(self): - """Output""" + """Output an OpenAI-consumable function specification""" return { "name": self.name, "description": self.description, diff --git a/autogpt/llm/utils/__init__.py b/autogpt/llm/utils/__init__.py index 2328d000727..41765314429 100644 --- a/autogpt/llm/utils/__init__.py +++ b/autogpt/llm/utils/__init__.py @@ -94,7 +94,7 @@ def create_text_completion( def create_chat_completion( prompt: ChatSequence, config: Config, - functions: Optional[List[OpenAIFunctionSpec]] = [], + functions: Optional[List[OpenAIFunctionSpec]] = None, model: Optional[str] = None, temperature: Optional[float] = None, max_tokens: Optional[int] = None, @@ -144,8 +144,9 @@ def create_chat_completion( ] = config.get_azure_deployment_id_for_model(model) if functions: chat_completion_kwargs["functions"] = [ - asdict(function) for function in functions + function.__dict__ for function in functions ] + logger.debug(f"Function dicts: {chat_completion_kwargs['functions']}") response = iopenai.create_chat_completion( messages=prompt.raw(), From 6f8a0d338b8117c45dad8e28ad7bd9261b132e13 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:26:02 +0200 Subject: [PATCH 12/14] Fix tests --- tests/unit/test_commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index fa8ca599022..663ec031616 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -9,7 +9,7 @@ from autogpt.models.command_registry import CommandRegistry PARAMETERS = [ - CommandParameter("arg1", "int", description="Argument 1", required=False), + CommandParameter("arg1", "int", description="Argument 1", required=True), CommandParameter("arg2", "str", description="Argument 2", required=False), ] @@ -35,7 +35,7 @@ def test_command_creation(self): assert cmd.name == "example" assert cmd.description == "Example command" assert cmd.method == self.example_command_method - assert cmd.parameters == "(arg1: int, arg2: str) -> str" + assert str(cmd) == "example: Example command, params: (arg1: int, arg2: Optional[str])" def test_command_call(self): """Test that Command(*args) calls and returns the result of method(*args).""" @@ -160,7 +160,7 @@ def test_get_command_prompt(self): registry.register(cmd) command_prompt = registry.command_prompt() - assert f"(arg1: int, arg2: str)" in command_prompt + assert f"(arg1: int, arg2: Optional[str])" in command_prompt def test_import_mock_commands_module(self): """Test that the registry can import a module with mock command plugins.""" From ce101696e1cb259d3a74c0d351dc309823f038c8 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:28:45 +0200 Subject: [PATCH 13/14] Fix linting --- tests/unit/test_commands.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index 663ec031616..9b52ceadc55 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -35,7 +35,10 @@ def test_command_creation(self): assert cmd.name == "example" assert cmd.description == "Example command" assert cmd.method == self.example_command_method - assert str(cmd) == "example: Example command, params: (arg1: int, arg2: Optional[str])" + assert ( + str(cmd) + == "example: Example command, params: (arg1: int, arg2: Optional[str])" + ) def test_command_call(self): """Test that Command(*args) calls and returns the result of method(*args).""" From 755c5b989617b5f9120407c4d30f76d729244a92 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Thu, 22 Jun 2023 04:34:45 +0200 Subject: [PATCH 14/14] Add notice to OPENAI_FUNCTIONS config entry --- .env.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.template b/.env.template index 4eb344a7b8b..c3fcb761da7 100644 --- a/.env.template +++ b/.env.template @@ -30,7 +30,7 @@ OPENAI_API_KEY=your-openai-api-key # OPENAI_API_BASE_URL=http://localhost:443/v1 ## OPENAI_FUNCTIONS - Enables OpenAI functions: https://platform.openai.com/docs/guides/gpt/function-calling -# the following is an example: +## WARNING: this feature is only supported by OpenAI's newest models. Until these models become the default on 27 June, add a '-0613' suffix to the model of your choosing. # OPENAI_FUNCTIONS=False ## AUTHORISE COMMAND KEY - Key to authorise commands