Skip to content

Commit 9def028

Browse files
modify examples
1 parent a647e8d commit 9def028

File tree

8 files changed

+262
-189
lines changed

8 files changed

+262
-189
lines changed

atomic_agents/lib/tools/yelp_restaurant_finder_tool.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
from atomic_agents.agents.base_agent import BaseIOSchema
1010
from atomic_agents.lib.tools.base_tool import BaseTool, BaseToolConfig
1111

12+
1213
################
1314
# INPUT SCHEMA #
1415
################
15-
16-
1716
class YelpCategory(Enum):
1817
ITALIAN = "italian"
1918
MEXICAN = "mexican"

examples/basic_custom_chatbot.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
This example demonstrates how to create a custom chatbot with custom personality using the Atomic Agents library.
3-
"""
4-
51
import os
62
from rich.console import Console
73
from atomic_agents.lib.components.agent_memory import AgentMemory

examples/minimal_multi_agent_tool_use.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
orchestration_agent = BaseAgent(config=BaseAgentConfig(client=client, model="gpt-4o-mini", output_schema=UnionResponse))
4040

4141
while True:
42-
# user_input = input("You: ")
43-
user_input = "5log(10)-69"
42+
user_input = input("You: ")
4443
if user_input.lower() in ["exit", "quit"]:
4544
print("Exiting chat...")
4645
break

examples/product_finder.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
###########
2+
# IMPORTS #
3+
###########
14
import os
25
import logging
36
from typing import Union, List
@@ -11,10 +14,9 @@
1114
from atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig, BaseIOSchema
1215
from atomic_agents.lib.tools.search.searxng_tool import SearxNGTool, SearxNGToolConfig, SearxNGToolInputSchema
1316

14-
# Configure logging
15-
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
16-
logger = logging.getLogger(__name__)
17-
17+
#######################
18+
# AGENT CONFIGURATION #
19+
#######################
1820
# Define system prompt information
1921
system_prompt_generator = SystemPromptGenerator(
2022
background=[
@@ -45,23 +47,39 @@
4547
]
4648
memory.load(initial_memory)
4749

50+
# Initialize the console
4851
console = Console()
4952

50-
# Initialize the client
53+
# Initialize the OpenAI client
5154
client = instructor.from_openai(openai.OpenAI())
5255

5356
# Initialize the SearxNGTool
5457
searxng_tool = SearxNGTool(SearxNGToolConfig(base_url=os.getenv("SEARXNG_BASE_URL"), max_results=5))
5558

5659

57-
# Define a custom response schema
60+
class ChatOutputSchema(BaseIOSchema):
61+
"""This schema defines a markdown-enabled chat output."""
62+
63+
markdown_output: str = Field(..., description="The answer to the question in markdown format.")
64+
65+
66+
######################
67+
# SCHEMA DEFINITIONS #
68+
######################
5869
class OutputSchema(BaseIOSchema):
59-
"""Output schema for the agent."""
70+
"""
71+
Output schema for the agent.
6072
61-
internal_reasoning: List[str] = Field(..., description="The internal reasoning behind the response.")
62-
chosen_schema: Union[BaseAgentOutputSchema, SearxNGToolInputSchema] = Field(
73+
This schema defines the structure of the agent's output, including its internal reasoning
74+
and the next action it plans to take.
75+
"""
76+
77+
internal_reasoning: List[str] = Field(
78+
..., description="A list of strings representing the agent's step-by-step thought process leading to its decision."
79+
)
80+
action: Union[ChatOutputSchema, SearxNGToolInputSchema] = Field(
6381
...,
64-
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)",
82+
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.",
6583
)
6684

6785

@@ -77,6 +95,9 @@ class OutputSchema(BaseIOSchema):
7795
# Create a chat agent
7896
agent = BaseAgent(config=agent_config)
7997

98+
#############
99+
# MAIN LOOP #
100+
#############
80101
console.print("Product Finder Agent is ready.")
81102
console.print(f'Agent: {initial_memory[0]["content"]}')
82103

@@ -88,17 +109,15 @@ class OutputSchema(BaseIOSchema):
88109

89110
response = agent.run(agent.input_schema(chat_message=user_input))
90111

91-
logger.info(f"Chosen schema: {response.chosen_schema}")
92-
93-
if isinstance(response.chosen_schema, SearxNGToolInputSchema):
94-
search_results = searxng_tool.run(response.chosen_schema)
112+
if isinstance(response.action, SearxNGToolInputSchema):
113+
search_results = searxng_tool.run(response.action)
95114

96115
agent.memory.add_message(
97116
"assistant",
98117
f"INTERNAL THOUGHT: I have found the following products: {search_results.results}\n\n I will now summarize the results for the user.",
99118
)
100-
output = agent.run().chosen_schema.chat_message
119+
output = agent.run().action.markdown_output
101120
else:
102-
output = response.chosen_schema.chat_message
121+
output = response.action.markdown_output
103122

104123
console.print(f"Agent: {output}")

examples/yelp_restaurant_finder.py

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
###########
2+
# IMPORTS #
3+
###########
14
import os
25
import logging
3-
from typing import Union
6+
from typing import Union, List
47
from pydantic import BaseModel, Field
58
import instructor
69
import openai
710
from rich.console import Console
811

912
from atomic_agents.lib.components.agent_memory import AgentMemory
1013
from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator
11-
from atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig
14+
from atomic_agents.agents.base_agent import BaseAgent, BaseAgentOutputSchema, BaseAgentConfig, BaseIOSchema
1215
from atomic_agents.lib.tools.yelp_restaurant_finder_tool import YelpSearchTool, YelpSearchToolConfig, YelpSearchToolInputSchema
1316

14-
# Configure logging
15-
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
16-
logger = logging.getLogger(__name__)
17-
18-
# Define system prompt information including background, steps, and output instructions
17+
#######################
18+
# AGENT CONFIGURATION #
19+
#######################
20+
# Define system prompt information
1921
system_prompt_generator = SystemPromptGenerator(
2022
background=[
2123
"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 @@
2628
"Ask the user questions to gather information for each filter until all required information is clear.",
2729
"Use the chat responses to gather all necessary information from the user.",
2830
"Once all required information is gathered, use the YelpSearchTool schema to search Yelp for restaurants.",
31+
"Summarize the search results and provide recommendations to the user.",
2932
],
3033
output_instructions=[
34+
"Always think in steps before answering using internal reasoning.",
3135
"Provide helpful and relevant information to assist the user.",
3236
"Be friendly and respectful in all interactions.",
3337
"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.",
3438
],
3539
)
3640

37-
# Initialize chat memory to store conversation history
41+
# Initialize chat memory
3842
memory = AgentMemory()
39-
# Define initial memory with a greeting message from the assistant
40-
initial_memory = [{"role": "assistant", "content": "Hello, can I help you find a restaurant?"}]
41-
# Load the initial memory into the chat memory
43+
initial_memory = [
44+
{
45+
"role": "assistant",
46+
"content": "Hello! I'm your restaurant finder assistant. How can I help you find a great place to eat today?",
47+
}
48+
]
4249
memory.load(initial_memory)
4350

51+
# Initialize the console
4452
console = Console()
4553

46-
# Initialize the client
47-
# For all supported clients such as Anthropic & Gemini, have a look at the `instructor` library documentation.
54+
# Initialize the OpenAI client
4855
client = instructor.from_openai(openai.OpenAI())
4956

5057
# Initialize the YelpSearchTool
5158
yelp_tool = YelpSearchTool(YelpSearchToolConfig(api_key=os.getenv("YELP_API_KEY"), max_results=10))
5259

5360

54-
# Define a custom response schema that can handle both chat responses and Yelp search tool responses
55-
class OutputSchema(BaseModel):
56-
"""The output schema for the agent."""
61+
######################
62+
# SCHEMA DEFINITIONS #
63+
######################
64+
class ChatOutputSchema(BaseIOSchema):
65+
"""This schema defines a markdown-enabled chat output."""
66+
67+
markdown_output: str = Field(..., description="The answer to the question in markdown format.")
68+
69+
70+
class OutputSchema(BaseIOSchema):
71+
"""
72+
Output schema for the agent.
5773
58-
chosen_schema: Union[BaseAgentOutputSchema, YelpSearchToolInputSchema] = Field(
59-
..., description="The response from the chat agent."
74+
This schema defines the structure of the agent's output, including its internal reasoning
75+
and the next action it plans to take.
76+
"""
77+
78+
internal_reasoning: List[str] = Field(
79+
..., description="A list of strings representing the agent's step-by-step thought process leading to its decision."
80+
)
81+
action: Union[ChatOutputSchema, YelpSearchToolInputSchema] = Field(
82+
...,
83+
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.",
6084
)
6185

6286

@@ -69,10 +93,13 @@ class OutputSchema(BaseModel):
6993
output_schema=OutputSchema,
7094
)
7195

72-
# Create a chat agent with the specified config
96+
# Create a chat agent
7397
agent = BaseAgent(config=agent_config)
7498

75-
console.print("BaseAgent with YelpSearchTool is ready.")
99+
#############
100+
# MAIN LOOP #
101+
#############
102+
console.print("Restaurant Finder Agent is ready.")
76103
console.print(f'Agent: {initial_memory[0]["content"]}')
77104

78105
while True:
@@ -83,22 +110,15 @@ class OutputSchema(BaseModel):
83110

84111
response = agent.run(agent.input_schema(chat_message=user_input))
85112

86-
# Log the chosen schema
87-
logger.info(f"Chosen schema: {response.chosen_schema}")
88-
89-
# Check the type of the response schema
90-
if isinstance(response.chosen_schema, YelpSearchToolInputSchema):
91-
output = yelp_tool.run(response.chosen_schema)
113+
if isinstance(response.action, YelpSearchToolInputSchema):
114+
search_results = yelp_tool.run(response.action)
92115

93-
# In this example, we will add a simple "internal thought" to the chat memory followed by an empty agent.run() call.
94-
# This will make the agent continue the conversation without user input.
95-
# In a more complex example, it might be preferable to extend the BaseAgent class and override the _get_and_handle_response method.
96116
agent.memory.add_message(
97117
"assistant",
98-
f"INTERNAL THOUGHT: I have found the following information: {output.results}\n\n I will now summarize the results for the user.",
118+
f"INTERNAL THOUGHT: I have found the following restaurants: {search_results.results}\n\n I will now summarize the results for the user.",
99119
)
100-
output = agent.run().chosen_schema.chat_message
120+
output = agent.run().action.markdown_output
101121
else:
102-
output = response.chosen_schema.chat_message
122+
output = response.action.markdown_output
103123

104124
console.print(f"Agent: {output}")

0 commit comments

Comments
 (0)