Building AI agents, atomically
Refactor everything to use a config class
KennyVaneetvelde30dfa5d7960a
diff --git a/atomic_agents/agents/base_chat_agent.py b/atomic_agents/agents/base_chat_agent.pyindex 9c5a1cd..9b699da 100644--- a/atomic_agents/agents/base_chat_agent.py+++ b/atomic_agents/agents/base_chat_agent.py@@ -1,4 +1,6 @@from typing import List, Optional, Type+import instructor+import openaifrom pydantic import BaseModel, Fieldfrom atomic_agents.lib.components.chat_memory import ChatMemoryfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator@@ -18,6 +20,17 @@ class BaseChatAgentResponse(BaseModel):'description': description}+class BaseChatAgentConfig(BaseModel):+ client: instructor.client.Instructor = Field(..., description='Client for interacting with the language model.')+ model: str = Field("gpt-3.5-turbo", description='The model to use for generating responses.')+ memory: Optional[ChatMemory] = Field(None, description='Memory component for storing chat history.')+ system_prompt_generator: Optional[SystemPromptGenerator] = Field(None, description='Component for generating system prompts.')+ input_schema: Type[BaseModel] = Field(BaseChatAgentInputSchema, description='Schema for the input data.')+ output_schema: Type[BaseModel] = Field(BaseChatAgentResponse, description='Schema for the output data.')++ class Config:+ arbitrary_types_allowed = True+class BaseChatAgent:"""Base class for chat agents.@@ -35,24 +48,19 @@ class BaseChatAgent:initial_memory (ChatMemory): Initial state of the memory."""- def __init__(self, client, system_prompt_generator: SystemPromptGenerator = None, model: str = 'gpt-3.5-turbo', memory: ChatMemory = None, include_planning_step=False, input_schema=BaseChatAgentInputSchema, output_schema=BaseChatAgentResponse):+ def __init__(self, config: BaseChatAgentConfig = BaseChatAgentConfig(client=instructor.from_openai(openai.OpenAI()))):"""Initializes the BaseChatAgent.Args:- client: Client for interacting with the language model.- system_prompt_generator (SystemPromptGenerator, optional): Component for generating system prompts. Defaults to None.- model (str, optional): The model to use for generating responses. Defaults to 'gpt-3.5-turbo'.- memory (ChatMemory, optional): Memory component for storing chat history. Defaults to None.- input_schema (Type[BaseModel], optional): Schema for the input data. Defaults to BaseChatAgentInputSchema.- output_schema (Type[BaseModel], optional): Schema for the output data. Defaults to BaseChatAgentResponse.- """- self.input_schema = input_schema- self.output_schema = output_schema- self.client = client- self.model = model- self.memory = memory or ChatMemory()- self.system_prompt_generator = system_prompt_generator or SystemPromptGenerator()+ config (BaseChatAgentConfig): Configuration for the chat agent.+ """+ self.input_schema = config.input_schema+ self.output_schema = config.output_schema+ self.client = config.client+ self.model = config.model+ self.memory = config.memory or ChatMemory()+ self.system_prompt_generator = config.system_prompt_generator or SystemPromptGenerator()self.initial_memory = self.memory.copy()def reset_memory(self):diff --git a/atomic_agents/agents/tool_interface_agent.py b/atomic_agents/agents/tool_interface_agent.pyindex ed21c53..fba6729 100644--- a/atomic_agents/agents/tool_interface_agent.py+++ b/atomic_agents/agents/tool_interface_agent.py@@ -1,9 +1,16 @@import jsonfrom pydantic import Field, create_model-from atomic_agents.agents.base_chat_agent import BaseChatAgent+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfigfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo+from atomic_agents.lib.tools.base import BaseToolfrom atomic_agents.lib.utils.format_tool_message import format_tool_message++class ToolInterfaceAgentConfig(BaseChatAgentConfig):+ tool_instance: BaseTool+ return_raw_output: bool = False++class ToolInterfaceAgent(BaseChatAgent):"""A specialized chat agent designed to interact with a specific tool.@@ -16,19 +23,17 @@ class ToolInterfaceAgent(BaseChatAgent):return_raw_output (bool): Whether to return the raw output from the tool."""- def __init__(self, tool_instance, return_raw_output=False, **kwargs):+ def __init__(self, config: ToolInterfaceAgentConfig):"""Initializes the ToolInterfaceAgent.Args:- tool_instance: The instance of the tool this agent will interact with.- return_raw_output (bool, optional): Whether to return the raw output from the tool. Defaults to False.- *args: Variable length argument list.- **kwargs: Arbitrary keyword arguments.+ config (ToolInterfaceAgentConfig): Configuration for the tool interface agent."""- super().__init__(**kwargs)+ super().__init__(config=config)- self.tool_instance = tool_instance+ self.tool_instance = config.tool_instance+ self.return_raw_output = config.return_raw_outputself.input_schema = create_model(self.tool_instance.tool_name,@@ -43,7 +48,6 @@ class ToolInterfaceAgent(BaseChatAgent):}))- self.return_raw_output = return_raw_outputif self.return_raw_output:self.output_schema = self.tool_instance.output_schema@@ -61,7 +65,7 @@ class ToolInterfaceAgent(BaseChatAgent):],output_instructions=["Make sure the tool call will maximize the utility of the tool in the context of the user input.",- "Process the output of the tool into a human readable format and/or use it to respond to the user input." if return_raw_output else "Return the raw output of the tool."+ "Process the output of the tool into a human readable format and/or use it to respond to the user input." if self.return_raw_output else "Return the raw output of the tool."]))diff --git a/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py b/atomic_agents/lib/tools/yelp_restaurant_finder_tool.pyindex 1bd10fe..c0b376c 100644--- a/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py+++ b/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py@@ -42,7 +42,7 @@ class YelpSearchToolSchema(BaseModel):location: str = Field(..., description="Location to search for food.")term: Optional[str] = Field(None, description="Search term (e.g., 'pizza', 'sushi').")categories: Optional[List[YelpCategory]] = Field(None, description="Categories to filter by (e.g., 'italian, mexican').")- price: Optional[List[PriceRange]] = Field(None, description="Price range to filter by (e.g., '1', '2', '3', '4').")+ price: Optional[List[PriceRange]] = Field(None, description="Price range to filter by (e.g., '1', '2', '3', '4'). Can be multiple. 1 is cheap. 2 and 3 are mid-range. 4 is the most high-end.")open_now: Optional[bool] = Field(False, description="Filter for businesses that are open now.")sort_by: Optional[str] = Field("best_match", description="Sort by criteria (e.g., 'best_match', 'rating', 'review_count', 'distance').")limit: Optional[int] = Field(10, description="Number of results to return.")@@ -126,7 +126,8 @@ class YelpSearchTool(BaseTool):"categories": ",".join([category.value for category in params.categories]) if params.categories else None,"price": ",".join([price.value for price in params.price]) if params.price else None,"sort_by": params.sort_by,- "limit": self.max_results+ "limit": self.max_results,+ "open_now": params.open_now}# Make the GET requestdiff --git a/examples/article_idea_outline_gen_with_search.py b/examples/article_idea_outline_gen_with_search.pyindex cc1c1b1..4ce481e 100644--- a/examples/article_idea_outline_gen_with_search.py+++ b/examples/article_idea_outline_gen_with_search.py@@ -9,7 +9,7 @@ from datetime import datetimeimport osfrom rich.console import Consolefrom atomic_agents.lib.components.chat_memory import ChatMemory-from atomic_agents.agents.base_chat_agent import BaseChatAgent+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfigfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfoimport instructorimport openai@@ -92,15 +92,22 @@ initial_memory = []memory.load(initial_memory)-# Create a chat agent with the specified model, system prompt generator, and memory.-# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.-agent = MyChatAgent(- client=instructor.from_openai(openai.OpenAI()),- system_prompt_generator=system_prompt_generator,+# Define ArticleIdeaOutlineGenConfig class - this is optional if you don't have any customization,+# but I like to do this for consistnecy and in order to not have to do it later if I need to add something.+class ArticleIdeaOutlineGenConfig(BaseChatAgentConfig):+ pass++# Update the configuration to use ArticleIdeaOutlineGenConfig+config = ArticleIdeaOutlineGenConfig(+ client=instructor.from_openai(openai.OpenAI()),model='gpt-3.5-turbo',+ system_prompt_generator=system_prompt_generator,memory=memory,)+# Create an instance of MyChatAgent with the specified configuration+agent = MyChatAgent(config=config)+console = Console()console.print(f'Agent: {initial_memory[0]["content"]}')diff --git a/examples/basic_custom_chatbot.py b/examples/basic_custom_chatbot.pyindex 80d5936..73e295f 100644--- a/examples/basic_custom_chatbot.py+++ b/examples/basic_custom_chatbot.py@@ -4,7 +4,7 @@ This example demonstrates how to create a custom chatbot with custom personalityfrom rich.console import Consolefrom atomic_agents.lib.components.chat_memory import ChatMemory-from atomic_agents.agents.base_chat_agent import BaseChatAgent+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfigfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfoimport instructorimport openai@@ -39,10 +39,12 @@ memory.load(initial_memory)# Create a chat agent with the specified model, system prompt generator, and memory# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.agent = BaseChatAgent(- client=instructor.from_openai(openai.OpenAI()),- system_prompt_generator=system_prompt_generator,- model='gpt-3.5-turbo',- memory=memory,+ config=BaseChatAgentConfig(+ client=instructor.from_openai(openai.OpenAI()),+ system_prompt_generator=system_prompt_generator,+ model='gpt-3.5-turbo',+ memory=memory,+ ))# Main chat loop for testing the chat agentdiff --git a/examples/minimal_chatbot.py b/examples/minimal_chatbot.pyindex 844d02c..874ebff 100644--- a/examples/minimal_chatbot.py+++ b/examples/minimal_chatbot.py@@ -3,13 +3,15 @@ import instructorimport openai# `BaseChatAgent` is the core class for creating chat agents in the Atomic Agents framework.-from atomic_agents.agents.base_chat_agent import BaseChatAgent+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfig# Create an instance of `BaseChatAgent` with default settings.# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.agent = BaseChatAgent(- client=instructor.from_openai(openai.OpenAI()),- model='gpt-3.5-turbo',+ config=BaseChatAgentConfig(+ client=instructor.from_openai(openai.OpenAI()),+ model='gpt-3.5-turbo'+ ))# Initialize a `Console` object for rich text output in the terminal.diff --git a/examples/notebooks/orchestrator.ipynb b/examples/notebooks/orchestrator.ipynbindex 1ad4c5a..6a9ed60 100644--- a/examples/notebooks/orchestrator.ipynb+++ b/examples/notebooks/orchestrator.ipynb@@ -29,7 +29,7 @@"cell_type": "markdown","metadata": {},"source": [- "## Step 1: Install Necessary Packages\n",+ "# Install Necessary Packages\n","\n","First, we need to install the required packages. Run the following command to install `atomic-agents`, `openai`, and `instructor` libraries."]@@ -48,13 +48,13 @@"cell_type": "markdown","metadata": {},"source": [- "## Step 2: Import Libraries\n",+ "# Import Libraries\n","\n","We will import the necessary libraries for creating the orchestrator agent. Each library serves a specific purpose:\n","- `ChatMemory`: Manages the chat history.\n","- `BaseChatAgent`: The base class to create a custom chatbot. Can be extended for additional functionality if needed.\n","- `SystemPromptGenerator` and `SystemPromptInfo`: To define and generate system prompts.\n",- "- `BaseTool`, `CalculatorTool`, `SerperSearchTool`: Tools that the orchestrator agent can use to perform tasks."+ "- `BaseTool`, `CalculatorTool`, `SearxNGSearchTool`: Tools that the orchestrator agent can use to perform tasks."]},{@@ -67,61 +67,64 @@"from typing import List, Type, Union\n","from pydantic import BaseModel, Field\n","from rich.console import Console\n",- "from atomic_agents.lib.components.chat_memory import ChatMemory\n",- "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse\n",- "from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo\n",+ "from rich.markdown import Markdown\n","import instructor\n","import openai\n",+ "from atomic_agents.lib.components.chat_memory import ChatMemory\n",+ "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse, BaseChatAgentConfig\n",+ "from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo\n","from atomic_agents.lib.tools.base import BaseTool\n","from atomic_agents.lib.tools.calculator_tool import CalculatorTool, CalculatorToolSchema\n",- "from atomic_agents.lib.tools.search.serper_tool import SerperSearchTool, SerperSearchToolConfig, SerperSearchToolSchema"+ "from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig, SearxNGSearchToolSchema\n",+ "\n",+ "console = Console()\n"]},{"cell_type": "markdown","metadata": {},"source": [- "## Step 3: Define System Prompt Information\n",+ "# Set Up API Keys\n","\n",- "In this step, we will define the system prompt information including background, steps, and output instructions. This helps the orchestrator agent understand how to respond to user inputs and manage tasks."+ "To use the **OpenAI** and **SearxNG** APIs, you need to set up your API keys. You can either enter them directly in the code or set them as environment variables."]},{- "cell_type": "markdown",+ "cell_type": "code",+ "execution_count": null,"metadata": {},+ "outputs": [],"source": [- "### Tool Definition\n",+ "##################################################################\n",+ "# ENTER YOUR OPENAI API KEY BELOW, OR SET IT AS AN ENVIRONMENT VARIABLE #\n",+ "##################################################################\n",+ "API_KEY = ''\n",+ "if not API_KEY:\n",+ " # Get the environment variable\n",+ " API_KEY = os.getenv('OPENAI_API_KEY')\n","\n",- "First, we define the tools that the orchestrator agent can use. These tools will help the agent perform various tasks such as searching the web or performing calculations."+ "if not API_KEY:\n",+ " raise ValueError('API key is not set. Please set the API key as a static variable or in the environment variable OPENAI_API_KEY.')\n",+ "\n",+ "client = instructor.from_openai(openai.OpenAI(api_key=API_KEY))\n"]},{"cell_type": "markdown","metadata": {},"source": [- "#### Serper Setup\n",+ "# Define System Prompt Information\n","\n",- "Set up your Serper API key. This key is required to access Serper's search capabilities."+ "In this step, we will define the system prompt information including background, steps, and output instructions. This helps the orchestrator agent understand how to respond to user inputs and manage tasks."]},{- "cell_type": "code",- "execution_count": null,+ "cell_type": "markdown","metadata": {},- "outputs": [],"source": [- "##################################################################\n",- "# ENTER YOUR SERPER API KEY BELOW, OR SET IT AS AN ENVIRONMENT VARIABLE #\n",- "##################################################################\n",- "SERPER_API_KEY = ''\n",- "if not SERPER_API_KEY:\n",- " # Get the environment variable\n",- " SERPER_API_KEY = os.getenv('SERPER_API_KEY')\n",- "\n",- "if not SERPER_API_KEY:\n",- " raise ValueError('API key is not set. Please set the API key as a static variable or in the environment variable SERPER_API_KEY.')\n",+ "### Tool Definition\n","\n",- "search_tool = SerperSearchTool(SerperSearchToolConfig(api_key=SERPER_API_KEY, max_results=10))\n"+ "First, we define the tools that the orchestrator agent can use. These tools will help the agent perform various tasks such as searching the web or performing calculations."]},{@@ -130,7 +133,8 @@"metadata": {},"outputs": [],"source": [- "# Create the calculator tool. This does not require any special configuration.\n",+ "# Initialize tools\n",+ "search_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10))\n","calculator_tool = CalculatorTool()"]},@@ -138,7 +142,7 @@"cell_type": "markdown","metadata": {},"source": [- "#### Create the tool info provider"+ "Now that we have defined the tools, we can define the ToolInfoProvider. This will format the tool information in a way that is nice to put in the system prompt."]},{@@ -159,7 +163,7 @@"cell_type": "markdown","metadata": {},"source": [- "### System Prompt Generator\n",+ "## System Prompt Generator\n","\n","Next, we define the system prompt information including background, steps, output instructions and the ToolInfoProvider we defined above. This helps the orchestrator agent understand how to respond to user inputs and manage tasks."]@@ -177,7 +181,11 @@" ],\n"," steps=[\n"," 'Understand the user\\'s input and the current context.',\n",- " 'Look at the tools available and determine which to use next based on the current context, user input, and history.',\n",+ " 'Take a step back and think methodically and step-by-step about how to proceed by using internal reasoning.',\n",+ " 'Evaluate the available tools and decide whether to use a tool based on the current context, user input, and history.',\n",+ " 'If a tool can be used, decide which tool to use and how to use it.',\n",+ " 'If a tool can be used it must always be explicitly mentioned in the internal reasoning response.',\n",+ " 'If a tool can be used, return nothing but the tool response to the user. If no tool is needed or if the tool has finished, return a chat response.',\n"," 'Execute the chosen tool and provide a relevant response to the user.'\n"," ],\n"," output_instructions=[\n",@@ -191,28 +199,19 @@")\n","\n","# Initialize the system prompt generator with the defined system prompt and dynamic info providers\n",- "system_prompt_generator = SystemPromptGenerator(system_prompt)"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [+ "system_prompt_generator = SystemPromptGenerator(system_prompt)\n",+ "\n","# Test out the system prompt generator\n","from rich.markdown import Markdown\n","from rich.console import Console\n",- "Console().print('='*50)\n",- "Console().print(Markdown(system_prompt_generator.generate_prompt()))\n",- "Console().print('='*50)"+ "console.print(Markdown(system_prompt_generator.generate_prompt()))"]},{"cell_type": "markdown","metadata": {},"source": [- "## Step 4: Initialize Chat Memory\n",+ "## Initialize Chat Memory\n","\n","We will initialize the chat memory to store conversation history and load an initial greeting message."]@@ -234,18 +233,8 @@"cell_type": "markdown","metadata": {},"source": [- "## Step 5: Set Up API Keys\n",- "\n",- "To use the **OpenAI** and **Serper** APIs, you need to set up your API keys. You can either enter them directly in the code or set them as environment variables."- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "### OpenAI Setup\n",- "\n",- "Set up your OpenAI API key. This key is required to access OpenAI's language models."+ "## Define InternalReasoningResponse\n",+ "To improve the agent's responses, we define an InternalReasoningResponse class that will be interspersed with the agent's responses. This class will help the agent reason about the next steps in the conversation."]},{@@ -254,27 +243,37 @@"metadata": {},"outputs": [],"source": [- "##################################################################\n",- "# ENTER YOUR OPENAI API KEY BELOW, OR SET IT AS AN ENVIRONMENT VARIABLE #\n",- "##################################################################\n",- "API_KEY = ''\n",- "if not API_KEY:\n",- " # Get the environment variable\n",- " API_KEY = os.getenv('OPENAI_API_KEY')\n",+ "# Define InternalReasoningResponse class\n",+ "class InternalReasoningResponse(BaseModel):\n",+ " observation: str = Field(..., description='What is the current state of the conversation and context? What is the user saying or asking?')\n",+ " action_plan: List[str] = Field(..., min_length=1, description='What steps could be taken to address the current observation? Is there a tool that could be used?')\n","\n",- "if not API_KEY:\n",- " raise ValueError('API key is not set. Please set the API key as a static variable or in the environment variable OPENAI_API_KEY.')\n",+ " class Config:\n",+ " title = 'InternalReasoningResponse'\n",+ " description = 'The internal reasoning response schema for the chat agent, following the ReACT pattern.'\n",+ " json_schema_extra = {\n",+ " 'title': title,\n",+ " 'description': description,\n",+ " }\n","\n",- "client = instructor.from_openai(openai.OpenAI(api_key=API_KEY))\n"+ "# Define ResponseSchema class\n",+ "class ResponseSchema(BaseModel):\n",+ " chosen_schema: Union[BaseChatAgentResponse, SearxNGSearchToolSchema, CalculatorToolSchema] = Field(..., description='The response from the chat agent, which may include the result of using a tool if one was deemed necessary.')\n",+ " class Config:\n",+ " title = 'ResponseSchema'\n",+ " description = 'The response schema for the chat agent, including potential tool usage results.'\n",+ " json_schema_extra = {\n",+ " 'title': title,\n",+ " 'description': description,\n",+ " }\n"]},{"cell_type": "markdown","metadata": {},"source": [- "## Step 6: Create Orchestrator Agent\n",- "\n",- "Now that we have set up the API keys, we can create the orchestrator agent. This agent will use the OpenAI client, system prompt generator, and chat memory to manage tasks and respond to user inputs."+ "Now that we have defined the necessary components, we can create the orchestrator agent.\n",+ "Personally, I like to extend the OrchestratorAgentConfig even if I don't add any additional fields. This way, I can easily add additional fields in the future if needed with minimal impact on the existing code."]},{@@ -283,40 +282,27 @@"metadata": {},"outputs": [],"source": [- "class ResponseSchema(BaseModel):\n",- " chosen_schema: Union[BaseChatAgentResponse, SerperSearchToolSchema, CalculatorToolSchema] = Field(..., description='The response from the chat agent.')\n",- " \n",- " class Config:\n",- " title = 'ResponseSchema'\n",- " description = 'The response schema for the chat agent.'\n",- " json_schema_extra = {\n",- " 'title': title,\n",- " 'description': description,\n",- " }\n","\n",- "# Create a chat agent with the specified model, system prompt generator, and memory\n",- "# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.\n",- "agent = BaseChatAgent(\n",- " client=client, \n",- " system_prompt_generator=system_prompt_generator,\n",- " model='gpt-3.5-turbo',\n",- " memory=memory,\n",- " output_schema=ResponseSchema\n",- ")\n",+ "# Define OrchestratorAgentConfig class\n",+ "class OrchestratorAgentConfig(BaseChatAgentConfig):\n",+ " pass\n","\n",- "# Print the response schema\n",- "Console().print(ResponseSchema.model_json_schema())\n",- "Console().print('='*50)"+ "# Define OrchestratorAgent class\n",+ "class OrchestratorAgent(BaseChatAgent):\n",+ " def _pre_run(self):\n",+ " self.memory.add_message('assistant', 'First, I will plan my steps and think about the tools at my disposal.')\n",+ " response = self.get_response(response_model=InternalReasoningResponse)\n",+ " self.memory.add_message('assistant', f'INTERNAL THOUGHT: I have observed \"{response.observation}\" and will take the following steps: {\", \".join(response.action_plan)}')\n",+ " return\n"]},{"cell_type": "markdown","metadata": {},"source": [- "## Step 7: Main Chat Loop\n",+ "Now that everything is set up, we can create an instance of the orchestrator agent and start interacting with it. Try asking it questions such as _\"What is 10 + log(5)?\"_ or _\"Can you find information about the history of the internet?\"_ and see how it responds.\n","\n",- "We will create a main chat loop for testing the orchestrator agent. This loop will allow you to interact with the chatbot in real-time.\n",- "To exit the loop, type `/exit` or `/quit`."+ "To keep this guide simple and to the point, we don't actually call the tools and do further processing, however since everything is typed you can check the type of the response and call the appropriate tool."]},{@@ -325,7 +311,19 @@"metadata": {},"outputs": [],"source": [- "console = Console()\n",+ "\n",+ "# Create an instance of OrchestratorAgent with the specified configuration\n",+ "agent = OrchestratorAgent(\n",+ " config=OrchestratorAgentConfig(\n",+ " client=instructor.from_openai(openai.OpenAI()),\n",+ " model='gpt-3.5-turbo',\n",+ " system_prompt_generator=system_prompt_generator,\n",+ " memory=memory,\n",+ " output_schema=ResponseSchema\n",+ " )\n",+ ")\n",+ "\n",+ "# Print the initial message from the assistant\n","console.print(f'Agent: {initial_memory[0][\"content\"]}')\n","\n","while True:\n",@@ -335,16 +333,7 @@" break\n","\n"," response = agent.run(user_input)\n",- " \n",- " # Check the type of the response schema\n",- " if isinstance(response.chosen_schema, SerperSearchToolSchema):\n",- " output = search_tool.run(response.chosen_schema)\n",- " elif isinstance(response.chosen_schema, CalculatorToolSchema):\n",- " output = calculator_tool.run(response.chosen_schema)\n",- " else:\n",- " output = response.chosen_schema\n",- " \n",- " console.print(f'Agent: {output}')"+ " console.print(f'Agent: {response}')\n"]}],diff --git a/examples/notebooks/quickstart.ipynb b/examples/notebooks/quickstart.ipynbindex 94c6c70..45d00fc 100644--- a/examples/notebooks/quickstart.ipynb+++ b/examples/notebooks/quickstart.ipynb@@ -66,7 +66,7 @@"import instructor\n","import openai\n","from atomic_agents.lib.components.chat_memory import ChatMemory\n",- "from atomic_agents.agents.base_chat_agent import BaseChatAgent\n",+ "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfig\n","from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo"]},@@ -159,10 +159,12 @@"client = instructor.from_openai(openai.OpenAI(api_key=API_KEY))\n","\n","agent = BaseChatAgent(\n",- " client=client, \n",- " system_prompt_generator=system_prompt_generator, # Set now\n",- " model='gpt-3.5-turbo',\n",- " memory=memory, # Set now\n",+ " config=BaseChatAgentConfig(\n",+ " client=client,\n",+ " system_prompt_generator=system_prompt_generator,\n",+ " model='gpt-3.5-turbo',\n",+ " memory=memory,\n",+ " )\n",")"]},diff --git a/examples/notebooks/yelp_agent.ipynb b/examples/notebooks/yelp_agent.ipynbindex 938ae0b..ff28e5b 100644--- a/examples/notebooks/yelp_agent.ipynb+++ b/examples/notebooks/yelp_agent.ipynb@@ -69,7 +69,7 @@"\n","from atomic_agents.lib.components.chat_memory import ChatMemory\n","from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo\n",- "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse\n",+ "from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse, BaseChatAgentConfig\n","from atomic_agents.lib.tools.yelp_restaurant_finder_tool import YelpSearchTool, YelpSearchToolConfig, YelpSearchToolSchema"]},@@ -221,11 +221,13 @@"source": ["# Create a chat agent with the specified model, system prompt generator, and memory\n","agent = BaseChatAgent(\n",- " client=client, \n",- " system_prompt_generator=system_prompt_generator,\n",- " model='gpt-3.5-turbo',\n",- " memory=memory,\n",- " output_schema=ResponseSchema\n",+ " config=BaseChatAgentConfig(\n",+ " client=client, \n",+ " system_prompt_generator=system_prompt_generator,\n",+ " model='gpt-3.5-turbo',\n",+ " memory=memory,\n",+ " output_schema=ResponseSchema\n",+ " )\n",")\n","\n","console.print(\"BaseChatAgent with YelpSearchTool is ready.\")\n",@@ -238,7 +240,8 @@"source": ["## Step 9: Run the Agent\n","\n",- "Start the agent to interact with the user. The agent will ask for user input, process it, and provide responses based on the gathered information."+ "Start the agent to interact with the user. The agent will ask for user input, process it, and provide responses based on the gathered information.\n",+ "Try telling it you would like some pizza, it should ask for your location and other information until it feels it can properly call the Yelp API with the information it has gathered."]},{@@ -277,8 +280,22 @@}],"metadata": {+ "kernelspec": {+ "display_name": "venv",+ "language": "python",+ "name": "python3"+ },"language_info": {- "name": "python"+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3",+ "version": "3.12.3"}},"nbformat": 4,diff --git a/examples/orchestrator_agent.py b/examples/orchestrator_agent.pyindex 227434a..3a6c53b 100644--- a/examples/orchestrator_agent.py+++ b/examples/orchestrator_agent.py@@ -1,20 +1,25 @@import os-from typing import List, Type, Union+from typing import List, Union+from pydantic import BaseModel, Fieldfrom rich.console import Console-from atomic_agents.lib.components.chat_memory import ChatMemory-from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse-from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfo+from rich.markdown import Markdown+import instructorimport openai+from atomic_agents.lib.components.chat_memory import ChatMemory+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse, BaseChatAgentConfig+from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfofrom atomic_agents.lib.tools.base import BaseToolfrom atomic_agents.lib.tools.calculator_tool import CalculatorTool, CalculatorToolSchemafrom atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig, SearxNGSearchToolSchema+# Initialize toolssearch_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10))calculator_tool = CalculatorTool()+# Define ToolInfoProvider classclass ToolInfoProvider(SystemPromptContextProviderBase):def get_info(self) -> str:response = 'The available tools are:\n'@@ -29,7 +34,11 @@ system_prompt = SystemPromptInfo(],steps=['Understand the user\'s input and the current context.',- 'Look at the tools available and determine which to use next based on the current context, user input, and history.',+ 'Take a step back and think methodically and step-by-step about how to proceed by using internal reasoning.',+ 'Evaluate the available tools and decide whether to use a tool based on the current context, user input, and history.',+ 'If a tool can be used, decide which tool to use and how to use it.',+ 'If a tool can be used it must always be explicitly mentioned in the internal reasoning response.',+ 'If a tool can be used, return nothing but the tool response to the user. If no tool is needed or if the tool has finished, return a chat response.','Execute the chosen tool and provide a relevant response to the user.'],output_instructions=[@@ -42,12 +51,26 @@ system_prompt = SystemPromptInfo(})+# Define InternalReasoningResponse class+class InternalReasoningResponse(BaseModel):+ observation: str = Field(..., description='What is the current state of the conversation and context? What is the user saying or asking?')+ action_plan: List[str] = Field(..., min_length=1, description='What steps could be taken to address the current observation? Is there a tool that could be used?')++ class Config:+ title = 'InternalReasoningResponse'+ description = 'The internal reasoning response schema for the chat agent, following the ReACT pattern.'+ json_schema_extra = {+ 'title': title,+ 'description': description,+ }++# Define ResponseSchema classclass ResponseSchema(BaseModel):- chosen_schema: Union[BaseChatAgentResponse, SearxNGSearchToolSchema, CalculatorToolSchema] = Field(..., description='The response from the chat agent.')+ chosen_schema: Union[BaseChatAgentResponse, SearxNGSearchToolSchema, CalculatorToolSchema] = Field(..., description='The response from the chat agent, which may include the result of using a tool if one was deemed necessary.')class Config:title = 'ResponseSchema'- description = 'The response schema for the chat agent.'+ description = 'The response schema for the chat agent, including potential tool usage results.'json_schema_extra = {'title': title,'description': description,@@ -57,15 +80,14 @@ class ResponseSchema(BaseModel):system_prompt_generator = SystemPromptGenerator(system_prompt)# Test out the system prompt generator-from rich.markdown import Markdown-from rich.console import Console-Console().print('='*50)-Console().print(Markdown(system_prompt_generator.generate_prompt()))-Console().print('='*50)+console = Console()+console.print('='*50)+console.print(Markdown(system_prompt_generator.generate_prompt()))+console.print('='*50)# Print the response schema-Console().print(ResponseSchema.model_json_schema())-Console().print('='*50)+console.print(ResponseSchema.model_json_schema())+console.print('='*50)# Initialize chat memory to store conversation historymemory = ChatMemory()@@ -76,18 +98,31 @@ initial_memory = [# Load the initial memory into the chat memorymemory.load(initial_memory)-# Create a chat agent with the specified model, system prompt generator, and memory-# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.-agent = BaseChatAgent(- client=instructor.from_openai(openai.OpenAI()),- system_prompt_generator=system_prompt_generator,+# Define OrchestratorAgentConfig class+class OrchestratorAgentConfig(BaseChatAgentConfig):+ pass++# Define OrchestratorAgent class+class OrchestratorAgent(BaseChatAgent):+ def _pre_run(self):+ self.memory.add_message('assistant', 'First, I will plan my steps and think about the tools at my disposal.')+ response = self.get_response(response_model=InternalReasoningResponse)+ self.memory.add_message('assistant', f'INTERNAL THOUGHT: I have observed "{response.observation}" and will take the following steps: {", ".join(response.action_plan)}')+ return++# Update the configuration to use OrchestratorAgentConfig+config = OrchestratorAgentConfig(+ client=instructor.from_openai(openai.OpenAI()),model='gpt-3.5-turbo',+ system_prompt_generator=system_prompt_generator,memory=memory,output_schema=ResponseSchema)+# Create an instance of OrchestratorAgent with the specified configuration+agent = OrchestratorAgent(config=config)+# Main chat loop for testing the chat agent-console = Console()console.print(f'Agent: {initial_memory[0]["content"]}')while True:diff --git a/examples/simple_web_search_agent.py b/examples/simple_web_search_agent.pyindex 3bd9a5a..40de828 100644--- a/examples/simple_web_search_agent.py+++ b/examples/simple_web_search_agent.py@@ -1,34 +1,48 @@+# simple_web_search_agent.pyimport osimport instructorimport openai-from rich.console import Console+from rich.console import Console-from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent+from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent, ToolInterfaceAgentConfigfrom atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig-console = Console()--# Initialize the client-# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.-client = instructor.from_openai(openai.OpenAI())--# Initialize the SearxNGSearchTool-searx_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10))--# Initialize the ToolInterfaceAgent with the SearxNGSearchTool-agent = ToolInterfaceAgent(- tool_instance=searx_tool,- client=client,- model='gpt-3.5-turbo'-)--console.print("ToolInterfaceAgent with SearxNGSearchTool is ready.")--while True:- user_input = input('You: ')- if user_input.lower() in ['exit', 'quit']:- print('Exiting chat...')- break-- response = agent.run(user_input)- console.print(f'Agent: {response.response}')\ No newline at end of file+def initialize_searx_tool():+ """+ Initialize the SearxNGSearchTool with configuration.+ """+ base_url = os.getenv('SEARXNG_BASE_URL')+ config = SearxNGSearchToolConfig(base_url=base_url, max_results=10)+ return SearxNGSearchTool(config)++def initialize_agent(client, searx_tool):+ """+ Initialize the ToolInterfaceAgent with the given client and SearxNGSearchTool.+ """+ agent_config = ToolInterfaceAgentConfig(+ client=client,+ model='gpt-3.5-turbo',+ tool_instance=searx_tool,+ return_raw_output=False+ )+ return ToolInterfaceAgent(config=agent_config)++def main():+ console = Console()+ client = instructor.from_openai(openai.OpenAI())+ searx_tool = initialize_searx_tool()+ agent = initialize_agent(client, searx_tool)++ console.print("ToolInterfaceAgent with SearxNGSearchTool is ready.")++ while True:+ user_input = input('You: ')+ if user_input.lower() in ['exit', 'quit']:+ print('Exiting chat...')+ break++ response = agent.run(user_input)+ console.print(f'Agent: {response.response}')++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/examples/simple_yt_agent.py b/examples/simple_yt_agent.pyindex 70fcf30..aaca846 100644--- a/examples/simple_yt_agent.py+++ b/examples/simple_yt_agent.py@@ -6,7 +6,7 @@ from rich.console import Consolefrom rich.markdown import Markdownfrom typing import List, Optional-from atomic_agents.agents.base_chat_agent import BaseChatAgent+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentConfigfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator, SystemPromptInfofrom atomic_agents.lib.tools.yt_transcript_scraper import YouTubeTranscriptTool, YouTubeTranscriptToolConfig, YouTubeTranscriptToolSchema@@ -58,16 +58,15 @@ class ResponseModel(BaseModel):references: List[str] = Field(..., description="All mentions of writing, art, tools, projects, and other sources of inspiration mentioned by the speakers.")one_sentence_takeaway: str = Field(..., description="The most potent takeaways and recommendations condensed into a single 20-word sentence.")-# Initialize the ToolInterfaceAgent with the SearxNGSearchToolagent = BaseChatAgent(- client=client,- model='gpt-3.5-turbo',- system_prompt_generator=SystemPromptGenerator(system_prompt_info),- output_schema=ResponseModel+ config = BaseChatAgentConfig(+ client=client,+ model='gpt-3.5-turbo',+ system_prompt_generator=SystemPromptGenerator(system_prompt_info),+ output_schema=ResponseModel+ ))-console.print("ToolInterfaceAgent with SearxNGSearchTool is ready.")-video_url = input('Enter the YouTube video URL: ')scraped_transcript = yt_scraper_tool.run(YouTubeTranscriptToolSchema(video_url=video_url))transcript_provider.transcript = scraped_transcript.transcriptdiff --git a/examples/yelp_restaurant_finder.py b/examples/yelp_restaurant_finder.pyindex 5239865..12d2c49 100644--- a/examples/yelp_restaurant_finder.py+++ b/examples/yelp_restaurant_finder.py@@ -8,11 +8,11 @@ from rich.console import Consolefrom atomic_agents.lib.components.chat_memory import ChatMemoryfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo-from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse+from atomic_agents.agents.base_chat_agent import BaseChatAgent, BaseChatAgentResponse, BaseChatAgentConfigfrom atomic_agents.lib.tools.yelp_restaurant_finder_tool import YelpSearchTool, YelpSearchToolConfig, YelpSearchToolSchema# Configure logging-logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')+logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)# Define system prompt information including background, steps, and output instructions@@ -34,7 +34,6 @@ system_prompt = SystemPromptInfo(])-# Initialize the system prompt generator with the defined system prompt and dynamic info providerssystem_prompt_generator = SystemPromptGenerator(system_prompt)@@ -68,15 +67,18 @@ class ResponseSchema(BaseModel):'description': description,}-# Create a chat agent with the specified model, system prompt generator, and memory-agent = BaseChatAgent(- client=client,+# Create a config for the chat agent+agent_config = BaseChatAgentConfig(+ client=client,system_prompt_generator=system_prompt_generator,model='gpt-3.5-turbo',memory=memory,output_schema=ResponseSchema)+# Create a chat agent with the specified config+agent = BaseChatAgent(config=agent_config)+console.print("BaseChatAgent with YelpSearchTool is ready.")console.print(f'Agent: {initial_memory[0]["content"]}')@@ -94,6 +96,10 @@ while True:# Check the type of the response schemaif isinstance(response.chosen_schema, YelpSearchToolSchema):output = yelp_tool.run(response.chosen_schema)++ # In this example, we will add an "internal thought" to the chat memory followed by an empty agent.run() call.+ # This will make the agent continue the conversation without user input.+ # In a more complex example, it might be preferable to extend the BaseChatAgent class and override the _get_and_handle_response method.agent.memory.add_message('assistant', f'INTERNAL THOUGHT: I have found the following information: {output.results}\n\n I will now summarize the results for the user.')output = agent.run().chosen_schema.responseelse: