Building AI agents, atomically
Improve documentation
KennyVaneetvelde1c7649026fa1
diff --git a/README.md b/README.mdindex 695a740..6b28e92 100644--- a/README.md+++ b/README.md@@ -25,10 +25,10 @@ pip install -r requirements.txtWhile we do our best to provide excellent documentation, we are aware that it is not perfect. If you see anything missing or anything that could be improved, please don't hesitate to open an issue or a pull request.### Examples-Further examples can be found in the [examples](./examples/) directory.+All examples can be found in the [examples](./examples/) directory. We do our best to thoroughly document each example, but if something is unclear, please don't hesitate to open an issue or a pull request in order to improve the documentation.### Docs-The documentation can be found in the [docs](./docs/) directory. Here you will find both API documentation and guides.+The documentation can be found in the [docs](./docs/) directory. Here you will find both API documentation and some general guides such as [How to create a new tool](./docs/guides/creating_a_new_tool.md).## Instructor & Model CompatibilityAtomic Agents depends on the [Instructor](https://github.com/jxnl/instructor) package. This means that in all examples where OpenAI is used, any other API supported by Instructor can be used, such as Cohere, Anthropic, Gemini, and more. For a complete list please refer to the instructor documentation on its [GitHub page](https://github.com/jxnl/instructor).diff --git a/examples/article_idea_outline_gen_with_search.py b/examples/article_idea_outline_gen_with_search.pyindex 2bcffa3..26430c7 100644--- a/examples/article_idea_outline_gen_with_search.py+++ b/examples/article_idea_outline_gen_with_search.py@@ -1,3 +1,10 @@+"""+This example demonstrates how to create a chat agent that generates article ideas and outlines based on a given topic.+The agent uses a search tool to gather relevant information and provides a structured outline for an article based on the search results.+This script also demonstrates the usage of dynamic info providers to include additional dynamic information at runtime in the system prompt context.+This can be useful if you only want to include the latest search results to save tokens (and thus, money) and avoid unnecessary repetition.+"""+from datetime import datetimefrom rich.console import Consolefrom atomic_agents.lib.components.chat_memory import ChatMemory@@ -8,8 +15,8 @@ import openaifrom atomic_agents.lib.tools.searx import SearxNGSearchToolfrom atomic_agents.lib.utils.logger import logger-console = Console()+# For this example, we extend the BaseChatAgent to create a custom chat agent that interacts with a search tool.class MyChatAgent(BaseChatAgent):def __init__(self, **kwargs):super().__init__(**kwargs)@@ -22,18 +29,21 @@ class MyChatAgent(BaseChatAgent):search_results = self.search_tool.run(search_input)self.system_prompt_generator.dynamic_info_providers['search'].search_results = search_resultsself.memory.add_message('assistant', 'I have gathered the search results and they have been added to the context.')-++# We can define dynamic info providers to provide additional information in the system prompt context.+# Each provider must have a title and a `get_info` method that returns a string.+# Each run, get_info will be called in order to provide the information at runtime.class CurrentDateProvider(DynamicInfoProviderBase):- def __init__(self, title: str, format: str = '%Y-%m-%d %H:%M:%S'):- super().__init__(title)+ def __init__(self, format: str = '%Y-%m-%d %H:%M:%S', **kwargs):+ super().__init__(**kwargs)self.format = formatdef get_info(self) -> str:return f'The current date, in the format "{self.format}", is {datetime.now().strftime(self.format)}'class SearchResultsProvider(DynamicInfoProviderBase):- def __init__(self, *args, **kwargs):- super().__init__(*args, **kwargs)+ def __init__(self, **kwargs):+ super().__init__(**kwargs)self.search_results: SearxNGSearchTool.output_schema = Nonedef get_info(self) -> str:@@ -49,13 +59,13 @@ class SearchResultsProvider(DynamicInfoProviderBase):return response-# Define dynamic info providers+# Define dynamic info providers.dynamic_info_providers = {- 'date': CurrentDateProvider('Current date', format='%Y-%m-%d %H:%M:%S'),- 'search': SearchResultsProvider('Search results')+ 'date': CurrentDateProvider(title='Current date', format='%Y-%m-%d %H:%M:%S'),+ 'search': SearchResultsProvider(title='Search results')}-+# Define the system prompt information including background, steps, and output instructions.system_prompt = SystemPromptInfo(background=['This assistant is an expert in brainstorming ideas and creating article outlines.',@@ -75,18 +85,24 @@ system_prompt = SystemPromptInfo()system_prompt_generator = SystemPromptGenerator(system_prompt, dynamic_info_providers)+# Optionally define the memory with an initial message from the assistant.memory = ChatMemory()initial_memory = [{'role': 'assistant', 'content': 'Hello! I\'m an AI assistant that can help you brainstorm ideas and generate article outlines. Please provide me with a topic to get started.'}]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,model='gpt-3.5-turbo',memory=memory,)++console = Console()+console.print(f'Agent: {initial_memory[0]["content"]}')while True:diff --git a/examples/basic_custom_chatbot.py b/examples/basic_custom_chatbot.pyindex c70085c..ea75365 100644--- a/examples/basic_custom_chatbot.py+++ b/examples/basic_custom_chatbot.py@@ -1,25 +1,15 @@-from datetime import datetime+"""+This example demonstrates how to create a custom chatbot with custom personality using the Atomic Agents library.+"""+from rich.console import Consolefrom atomic_agents.lib.components.chat_memory import ChatMemoryfrom atomic_agents.agents.base_chat_agent import BaseChatAgent-from atomic_agents.lib.components.system_prompt_generator import DynamicInfoProviderBase, SystemPromptGenerator, SystemPromptInfo+from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfoimport instructorimport openai-console = Console()--class CurrentDateProvider(DynamicInfoProviderBase):- def __init__(self, title: str, format: str = '%Y-%m-%d %H:%M:%S'):- super().__init__(title)- self.format = format-- def get_info(self) -> str:- return f'The current date, in the format "{self.format}", is {datetime.now().strftime(self.format)}'--dynamic_info_providers = {- 'date': CurrentDateProvider('Current date', format='%Y-%m-%d'),-}-+# Define system prompt information including background, steps, and output instructionssystem_prompt = SystemPromptInfo(background=['This assistant is a general-purpose AI designed to be helpful and friendly.',@@ -34,26 +24,34 @@ system_prompt = SystemPromptInfo('Always answer in rhyming verse. Preferably in alexandrine verse.'])-system_prompt_generator = SystemPromptGenerator(system_prompt, dynamic_info_providers)+# Initialize the system prompt generator with the defined system prompt and dynamic info providers+system_prompt_generator = SystemPromptGenerator(system_prompt)+# Initialize chat memory to store conversation historymemory = ChatMemory()+# Define initial memory with a greeting message from the assistantinitial_memory = [{'role': 'assistant', 'content': 'How do you do? What can I do for you? Tell me, pray, what is your need today?'}]-+# 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,model='gpt-3.5-turbo',memory=memory,)++# Main chat loop for testing the chat agent+console = Console()console.print(f'Agent: {initial_memory[0]["content"]}')while True:user_input = input('You: ')- if user_input.lower() in ['exit', 'quit']:+ if user_input.lower() in ['/exit', '/quit']:print('Exiting chat...')breakdiff --git a/examples/minimal_chatbot.py b/examples/minimal_chatbot.pyindex aee17cb..844d02c 100644--- a/examples/minimal_chatbot.py+++ b/examples/minimal_chatbot.py@@ -6,9 +6,7 @@ import openaifrom atomic_agents.agents.base_chat_agent import BaseChatAgent# Create an instance of `BaseChatAgent` with default settings.-# The `from_openai` function wraps the OpenAI client to work seamlessly with the Atomic Agents framework.-# `openai.OpenAI()` creates a client instance for the OpenAI API.-# The `model` parameter specifies the language model to use, in this case, 'gpt-3.5-turbo'.+# 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',@@ -17,16 +15,12 @@ agent = BaseChatAgent(# Initialize a `Console` object for rich text output in the terminal.console = Console()-# Start an infinite loop to keep the chatbot running until the user decides to exit.+# Main chat loop for testing the chat agent.while True:- # Prompt the user for input.user_input = input('You: ')- # Check if the user wants to exit the chat.if user_input.lower() in ['/exit', '/quit']:print('Exiting chat...')break- # Process the user input through the chat agent to get a response.response = agent.run(user_input)- # Use the `console` object to print the agent's response in a rich format.console.print(f'Agent: {response.response}')\ No newline at end of filediff --git a/examples/simple_tool_interface_agent.py b/examples/simple_tool_interface_agent.pyindex 8fca4bf..4c964e9 100644--- a/examples/simple_tool_interface_agent.py+++ b/examples/simple_tool_interface_agent.py@@ -8,6 +8,7 @@ from atomic_agents.lib.tools.searx import SearxNGSearchToolconsole = 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