Building AI agents, atomically
modify examples
KennyVaneetvelde9def0286dabb
diff --git a/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py b/atomic_agents/lib/tools/yelp_restaurant_finder_tool.pyindex 507a3de..8eff828 100644--- a/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py+++ b/atomic_agents/lib/tools/yelp_restaurant_finder_tool.py@@ -9,11 +9,10 @@ from rich.console import Consolefrom atomic_agents.agents.base_agent import BaseIOSchemafrom atomic_agents.lib.tools.base_tool import BaseTool, BaseToolConfig+################# INPUT SCHEMA #################--class YelpCategory(Enum):ITALIAN = "italian"MEXICAN = "mexican"diff --git a/examples/basic_custom_chatbot.py b/examples/basic_custom_chatbot.pyindex 45999ee..fc50896 100644--- a/examples/basic_custom_chatbot.py+++ b/examples/basic_custom_chatbot.py@@ -1,7 +1,3 @@-"""-This example demonstrates how to create a custom chatbot with custom personality using the Atomic Agents library.-"""-import osfrom rich.console import Consolefrom atomic_agents.lib.components.agent_memory import AgentMemorydiff --git a/examples/minimal_multi_agent_tool_use.py b/examples/minimal_multi_agent_tool_use.pyindex 623077b..87f2ec6 100644--- a/examples/minimal_multi_agent_tool_use.py+++ b/examples/minimal_multi_agent_tool_use.py@@ -39,8 +39,7 @@ UnionResponse = create_model(orchestration_agent = BaseAgent(config=BaseAgentConfig(client=client, model="gpt-4o-mini", output_schema=UnionResponse))while True:- # user_input = input("You: ")- user_input = "5log(10)-69"+ user_input = input("You: ")if user_input.lower() in ["exit", "quit"]:print("Exiting chat...")breakdiff --git a/examples/product_finder.py b/examples/product_finder.pyindex 05bfacb..afe2c2f 100644--- a/examples/product_finder.py+++ b/examples/product_finder.py@@ -1,3 +1,6 @@+###########+# IMPORTS #+###########import osimport loggingfrom typing import Union, List@@ -11,10 +14,9 @@ from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenfrom atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig, BaseIOSchemafrom atomic_agents.lib.tools.search.searxng_tool import SearxNGTool, SearxNGToolConfig, SearxNGToolInputSchema-# Configure logging-logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")-logger = logging.getLogger(__name__)-+#######################+# AGENT CONFIGURATION #+######################## Define system prompt informationsystem_prompt_generator = SystemPromptGenerator(background=[@@ -45,23 +47,39 @@ initial_memory = []memory.load(initial_memory)+# Initialize the consoleconsole = Console()-# Initialize the client+# Initialize the OpenAI clientclient = instructor.from_openai(openai.OpenAI())# Initialize the SearxNGToolsearxng_tool = SearxNGTool(SearxNGToolConfig(base_url=os.getenv("SEARXNG_BASE_URL"), max_results=5))-# Define a custom response schema+class ChatOutputSchema(BaseIOSchema):+ """This schema defines a markdown-enabled chat output."""++ markdown_output: str = Field(..., description="The answer to the question in markdown format.")+++######################+# SCHEMA DEFINITIONS #+######################class OutputSchema(BaseIOSchema):- """Output schema for the agent."""+ """+ Output schema for the agent.- internal_reasoning: List[str] = Field(..., description="The internal reasoning behind the response.")- chosen_schema: Union[BaseAgentOutputSchema, SearxNGToolInputSchema] = Field(+ This schema defines the structure of the agent's output, including its internal reasoning+ and the next action it plans to take.+ """++ internal_reasoning: List[str] = Field(+ ..., description="A list of strings representing the agent's step-by-step thought process leading to its decision."+ )+ action: Union[ChatOutputSchema, SearxNGToolInputSchema] = Field(...,- description="The response from the chat agent. Every response must use chosen_schema to indicate the type of response (A chat message, or a search request)",+ description="The next action to be taken by the agent. This can be either a chat response (ChatOutputSchema) or a search request (SearxNGToolInputSchema), depending on whether the agent needs to communicate with the user or perform a product search.",)@@ -77,6 +95,9 @@ agent_config = BaseAgentConfig(# Create a chat agentagent = BaseAgent(config=agent_config)+#############+# MAIN LOOP #+#############console.print("Product Finder Agent is ready.")console.print(f'Agent: {initial_memory[0]["content"]}')@@ -88,17 +109,15 @@ while True:response = agent.run(agent.input_schema(chat_message=user_input))- logger.info(f"Chosen schema: {response.chosen_schema}")-- if isinstance(response.chosen_schema, SearxNGToolInputSchema):- search_results = searxng_tool.run(response.chosen_schema)+ if isinstance(response.action, SearxNGToolInputSchema):+ search_results = searxng_tool.run(response.action)agent.memory.add_message("assistant",f"INTERNAL THOUGHT: I have found the following products: {search_results.results}\n\n I will now summarize the results for the user.",)- output = agent.run().chosen_schema.chat_message+ output = agent.run().action.markdown_outputelse:- output = response.chosen_schema.chat_message+ output = response.action.markdown_outputconsole.print(f"Agent: {output}")diff --git a/examples/yelp_restaurant_finder.py b/examples/yelp_restaurant_finder.pyindex acf5790..4321990 100644--- a/examples/yelp_restaurant_finder.py+++ b/examples/yelp_restaurant_finder.py@@ -1,6 +1,9 @@+###########+# IMPORTS #+###########import osimport logging-from typing import Union+from typing import Union, Listfrom pydantic import BaseModel, Fieldimport instructorimport openai@@ -8,14 +11,13 @@ from rich.console import Consolefrom atomic_agents.lib.components.agent_memory import AgentMemoryfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator-from atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig+from atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig, BaseIOSchemafrom atomic_agents.lib.tools.yelp_restaurant_finder_tool import YelpSearchTool, YelpSearchToolConfig, YelpSearchToolInputSchema-# Configure logging-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+#######################+# AGENT CONFIGURATION #+#######################+# Define system prompt informationsystem_prompt_generator = SystemPromptGenerator(background=["This assistant is a restaurant finder AI designed to help users find the best restaurants based on their preferences by asking clarifying questions.",@@ -26,37 +28,59 @@ system_prompt_generator = SystemPromptGenerator("Ask the user questions to gather information for each filter until all required information is clear.","Use the chat responses to gather all necessary information from the user.","Once all required information is gathered, use the YelpSearchTool schema to search Yelp for restaurants.",+ "Summarize the search results and provide recommendations to the user.",],output_instructions=[+ "Always think in steps before answering using internal reasoning.","Provide helpful and relevant information to assist the user.","Be friendly and respectful in all interactions.","Ensure that the chat responses are used to ask clarifying questions and gather information, and the Yelp schema is used to perform the actual search.",],)-# Initialize chat memory to store conversation history+# Initialize chat memorymemory = AgentMemory()-# Define initial memory with a greeting message from the assistant-initial_memory = [{"role": "assistant", "content": "Hello, can I help you find a restaurant?"}]-# Load the initial memory into the chat memory+initial_memory = [+ {+ "role": "assistant",+ "content": "Hello! I'm your restaurant finder assistant. How can I help you find a great place to eat today?",+ }+]memory.load(initial_memory)+# Initialize the consoleconsole = Console()-# Initialize the client-# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.+# Initialize the OpenAI clientclient = instructor.from_openai(openai.OpenAI())# Initialize the YelpSearchToolyelp_tool = YelpSearchTool(YelpSearchToolConfig(api_key=os.getenv("YELP_API_KEY"), max_results=10))-# Define a custom response schema that can handle both chat responses and Yelp search tool responses-class OutputSchema(BaseModel):- """The output schema for the agent."""+######################+# SCHEMA DEFINITIONS #+######################+class ChatOutputSchema(BaseIOSchema):+ """This schema defines a markdown-enabled chat output."""++ markdown_output: str = Field(..., description="The answer to the question in markdown format.")+++class OutputSchema(BaseIOSchema):+ """+ Output schema for the agent.- chosen_schema: Union[BaseAgentOutputSchema, YelpSearchToolInputSchema] = Field(- ..., description="The response from the chat agent."+ This schema defines the structure of the agent's output, including its internal reasoning+ and the next action it plans to take.+ """++ internal_reasoning: List[str] = Field(+ ..., description="A list of strings representing the agent's step-by-step thought process leading to its decision."+ )+ action: Union[ChatOutputSchema, YelpSearchToolInputSchema] = Field(+ ...,+ description="The next action to be taken by the agent. This can be either a chat response (ChatOutputSchema) or a Yelp search request (YelpSearchToolInputSchema), depending on whether the agent needs to communicate with the user or perform a restaurant search.",)@@ -69,10 +93,13 @@ agent_config = BaseAgentConfig(output_schema=OutputSchema,)-# Create a chat agent with the specified config+# Create a chat agentagent = BaseAgent(config=agent_config)-console.print("BaseAgent with YelpSearchTool is ready.")+#############+# MAIN LOOP #+#############+console.print("Restaurant Finder Agent is ready.")console.print(f'Agent: {initial_memory[0]["content"]}')while True:@@ -83,22 +110,15 @@ while True:response = agent.run(agent.input_schema(chat_message=user_input))- # Log the chosen schema- logger.info(f"Chosen schema: {response.chosen_schema}")-- # Check the type of the response schema- if isinstance(response.chosen_schema, YelpSearchToolInputSchema):- output = yelp_tool.run(response.chosen_schema)+ if isinstance(response.action, YelpSearchToolInputSchema):+ search_results = yelp_tool.run(response.action)- # In this example, we will add a simple "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 BaseAgent 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.",+ f"INTERNAL THOUGHT: I have found the following restaurants: {search_results.results}\n\n I will now summarize the results for the user.",)- output = agent.run().chosen_schema.chat_message+ output = agent.run().action.markdown_outputelse:- output = response.chosen_schema.chat_message+ output = response.action.markdown_outputconsole.print(f"Agent: {output}")diff --git a/examples/youtube_knowledge_extraction.py b/examples/youtube_knowledge_extraction.pydeleted file mode 100644index e008601..0000000--- a/examples/youtube_knowledge_extraction.py+++ /dev/null@@ -1,133 +0,0 @@-import os-import instructor-import openai-from pydantic import BaseModel, Field-from rich.console import Console-from rich.markdown import Markdown-from typing import List, Optional--from atomic_agents.agents.base_agent import BaseAgent, BaseAgentConfig-from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator-from atomic_agents.lib.tools.yt_transcript_scraper_tool import (- YouTubeTranscriptTool,- YouTubeTranscriptToolConfig,- YouTubeTranscriptToolInputSchema,-)--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())--yt_scraper_tool = YouTubeTranscriptTool(config=YouTubeTranscriptToolConfig())---class YtTranscriptProvider(SystemPromptContextProviderBase):- def __init__(self, title):- super().__init__(title)- self.transcript = None- self.duration = None- self.metadata = None-- def get_info(self) -> str:- return f'VIDEO TRANSCRIPT: "{self.transcript}"\n\nDURATION: {self.duration}\n\nMETADATA: {self.metadata}'---transcript_provider = YtTranscriptProvider(title="YouTube Transcript")--# Define the system prompt information-system_prompt_generator = SystemPromptGenerator(- background=[- "This Assistant is an expert at extracting knowledge and other insightful and interesting information from YouTube transcripts."- ],- steps=[- "Analyse the YouTube transcript thoroughly to extract the most valuable insights, facts, and recommendations.",- "Adhere strictly to the provided schema when extracting information from the input content.",- "Ensure that the output matches the field descriptions, types and constraints exactly.",- ],- output_instructions=[- "Only output Markdown-compatible strings.",- "Ensure you follow ALL these instructions when creating your output.",- ],- context_providers={"yt_transcript": transcript_provider},-)---# Define the response model with detailed descriptions, constraints, and field types-class ResponseModel(BaseModel):- summary: str = Field(- ..., description="A short summary of the content, including who is presenting and the content being discussed."- )- insights: List[str] = Field(- ..., min_items=5, max_items=5, description="exactly 5 of the best insights and ideas from the input."- )- quotes: List[str] = Field(- None,- min_items=5,- max_items=5,- description="exactly 5 of the most surprising, insightful, and/or interesting quotes from the input.",- )- habits: Optional[List[str]] = Field(- None,- min_items=5,- max_items=5,- description="exactly 5 of the most practical and useful personal habits mentioned by the speakers.",- )- facts: List[str] = Field(- ...,- min_items=5,- max_items=5,- description="exactly 5 of the most surprising, insightful, and/or interesting valid facts about the greater world mentioned in the content.",- )- recommendations: List[str] = Field(- ...,- min_items=5,- max_items=5,- description="exactly 5 of the most surprising, insightful, and/or interesting recommendations from the content.",- )- 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."- )---agent = BaseAgent(- config=BaseAgentConfig(- client=client, model="gpt-4o-mini", system_prompt_generator=system_prompt_generator, output_schema=ResponseModel- )-)--video_url = input("Enter the YouTube video URL: ")-scraped_transcript = yt_scraper_tool.run(YouTubeTranscriptToolInputSchema(video_url=video_url))-transcript_provider.transcript = scraped_transcript.transcript-transcript_provider.duration = scraped_transcript.duration-transcript_provider.metadata = scraped_transcript.metadata--response = agent.run(- agent.input_schema(- chat_message="Perform your assignment on the YouTube video transcript present in your context. Do not reply with anything other than the output of the assignment."- )-)--# Convert the ResponseModel to a dictionary and then to a Markdown string-response_dict = response.model_dump()---def format_markdown_section(title, items):- if isinstance(items, list):- return f"## {title}\n" + "\n".join([f"- {item}" for item in items]) + "\n"- return f"## {title}\n{items}\n"---markdown_string = ""-for key, value in response_dict.items():- title = key.replace("_", " ").title()- markdown_string += format_markdown_section(title, value)--# Print the response in a pretty Markdown formatted way-markdown_response = Markdown(markdown_string)-console.print(markdown_response)diff --git a/examples/youtube_knowledge_extraction/agents/youtube_knowledge_extraction_agent.py b/examples/youtube_knowledge_extraction/agents/youtube_knowledge_extraction_agent.pynew file mode 100644index 0000000..1570734--- /dev/null+++ b/examples/youtube_knowledge_extraction/agents/youtube_knowledge_extraction_agent.py@@ -0,0 +1,93 @@+import instructor+import openai+from pydantic import BaseModel, Field+from typing import List, Optional++from atomic_agents.agents.base_agent import BaseAgent, BaseAgentConfig, BaseIOSchema+from atomic_agents.lib.components.system_prompt_generator import SystemPromptContextProviderBase, SystemPromptGenerator+++class YtTranscriptProvider(SystemPromptContextProviderBase):+ def __init__(self, title):+ super().__init__(title)+ self.transcript = None+ self.duration = None+ self.metadata = None++ def get_info(self) -> str:+ return f'VIDEO TRANSCRIPT: "{self.transcript}"\n\nDURATION: {self.duration}\n\nMETADATA: {self.metadata}'+++class YouTubeKnowledgeExtractionInputSchema(BaseIOSchema):+ """This schema defines the input schema for the YouTubeKnowledgeExtractionAgent."""++ video_url: str = Field(..., description="The URL of the YouTube video to analyze")+++class YouTubeKnowledgeExtractionOutputSchema(BaseIOSchema):+ """This schema defines an elaborate set of insights about the contentof the video."""++ summary: str = Field(+ ..., description="A short summary of the content, including who is presenting and the content being discussed."+ )+ insights: List[str] = Field(+ ..., min_items=5, max_items=5, description="exactly 5 of the best insights and ideas from the input."+ )+ quotes: List[str] = Field(+ ...,+ min_items=5,+ max_items=5,+ description="exactly 5 of the most surprising, insightful, and/or interesting quotes from the input.",+ )+ habits: Optional[List[str]] = Field(+ None,+ min_items=5,+ max_items=5,+ description="exactly 5 of the most practical and useful personal habits mentioned by the speakers.",+ )+ facts: List[str] = Field(+ ...,+ min_items=5,+ max_items=5,+ description="exactly 5 of the most surprising, insightful, and/or interesting valid facts about the greater world mentioned in the content.",+ )+ recommendations: List[str] = Field(+ ...,+ min_items=5,+ max_items=5,+ description="exactly 5 of the most surprising, insightful, and/or interesting recommendations from the content.",+ )+ 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."+ )+++transcript_provider = YtTranscriptProvider(title="YouTube Transcript")++youtube_knowledge_extraction_agent = BaseAgent(+ config=BaseAgentConfig(+ client=instructor.from_openai(openai.OpenAI()),+ model="gpt-4o-mini",+ system_prompt_generator=SystemPromptGenerator(+ background=[+ "This Assistant is an expert at extracting knowledge and other insightful and interesting information from YouTube transcripts."+ ],+ steps=[+ "Analyse the YouTube transcript thoroughly to extract the most valuable insights, facts, and recommendations.",+ "Adhere strictly to the provided schema when extracting information from the input content.",+ "Ensure that the output matches the field descriptions, types and constraints exactly.",+ ],+ output_instructions=[+ "Only output Markdown-compatible strings.",+ "Ensure you follow ALL these instructions when creating your output.",+ ],+ context_providers={"yt_transcript": transcript_provider},+ ),+ input_schema=YouTubeKnowledgeExtractionInputSchema,+ output_schema=YouTubeKnowledgeExtractionOutputSchema,+ )+)diff --git a/examples/youtube_knowledge_extraction/pipeline.py b/examples/youtube_knowledge_extraction/pipeline.pynew file mode 100644index 0000000..a49e0b5--- /dev/null+++ b/examples/youtube_knowledge_extraction/pipeline.py@@ -0,0 +1,80 @@+from typing import Dict, List, Type+from pydantic import Field+from rich.console import Console+from rich.markdown import Markdown++from atomic_agents.agents.base_agent import BaseIOSchema+from atomic_agents.lib.tools.yt_transcript_scraper_tool import (+ YouTubeTranscriptTool,+ YouTubeTranscriptToolConfig,+ YouTubeTranscriptToolInputSchema,+)++from agents.youtube_knowledge_extraction_agent import (+ youtube_knowledge_extraction_agent,+ YouTubeKnowledgeExtractionInputSchema,+ YouTubeKnowledgeExtractionOutputSchema,+ transcript_provider,+)++console = Console()++# Type aliases+SchemaType = Type[BaseIOSchema]+OrchestrationGraphType = Dict[BaseIOSchema, List[BaseIOSchema]]++# Tool initialization+yt_scraper_tool = YouTubeTranscriptTool(config=YouTubeTranscriptToolConfig())+++# Schema models+class InputSchema(BaseIOSchema):+ """This schema defines the input for the pipeline."""++ user_input: str = Field(..., description="The YouTube video URL provided by the user.")+++class ChatReplySchema(BaseIOSchema):+ """This schema defines a standard chat reply."""++ chat_message: str = Field(..., description="The chat message to be sent back to the user.")+++# Orchestration flow config+OrchestrationGraph: OrchestrationGraphType = {+ InputSchema: [YouTubeKnowledgeExtractionInputSchema],+ YouTubeKnowledgeExtractionInputSchema: [YouTubeKnowledgeExtractionOutputSchema],+ YouTubeKnowledgeExtractionOutputSchema: [ChatReplySchema],+}+++def format_markdown_section(title, items):+ if isinstance(items, list):+ return f"## {title}\n" + "\n".join([f"- {item}" for item in items]) + "\n"+ return f"## {title}\n{items}\n"+++def run_pipeline(video_url: str):+ # Step 1: Scrape YouTube transcript+ scraped_transcript = yt_scraper_tool.run(YouTubeTranscriptToolInputSchema(video_url=video_url))+ transcript_provider.transcript = scraped_transcript.transcript+ transcript_provider.duration = scraped_transcript.duration+ transcript_provider.metadata = scraped_transcript.metadata++ # Step 2: Run YouTube knowledge extraction agent+ response = youtube_knowledge_extraction_agent.run(YouTubeKnowledgeExtractionInputSchema(video_url=video_url))++ # Step 3: Format and display the response+ response_dict = response.model_dump()+ markdown_string = ""+ for key, value in response_dict.items():+ title = key.replace("_", " ").title()+ markdown_string += format_markdown_section(title, value)++ markdown_response = Markdown(markdown_string)+ console.print(markdown_response)+++if __name__ == "__main__":+ video_url = input("Enter the YouTube video URL: ")+ run_pipeline(video_url)