Skip to content

Building AI agents, atomically

refactor: Update chatbot to use 'chat_message' instead of 'chat_input'

KennyVaneetveldecad7cb577ef1
diff --git a/atomic_agents/agents/base_chat_agent.py b/atomic_agents/agents/base_chat_agent.py
index b7a1f03..a3ad659 100644
--- a/atomic_agents/agents/base_chat_agent.py
+++ b/atomic_agents/agents/base_chat_agent.py
@@ -18,19 +18,34 @@ class BaseAgentIO(BaseModel):
json_str = self.model_dump_json()
return JSON(json_str)
-# Input and Response Schemas
class BaseChatAgentInputSchema(BaseAgentIO):
- chat_input: str = Field(..., description='The input text for the chat agent.')
+ chat_message: str = Field(
+ ...,
+ description='The chat message exchanged between the user and the chat agent. '
+ 'This represents the message sent by the user.'
+ )
+
+ class Config:
+ title = 'BaseChatAgentInputSchema'
+ description = 'This schema represents the user input message exchanged between the user and the chat agent.'
+ json_schema_extra = {
+ "title": title,
+ "description": description,
+ }
+
+class BaseChatAgentResponseSchema(BaseAgentIO):
+ chat_message: str = Field(
+ ...,
+ description='The chat message exchanged between the user and the chat agent. '
+ 'This contains the markdown-enabled response generated by the chat agent.'
+ )
-class BaseChatAgentResponseSchema(BaseAgentIO):
- response: str = Field(..., description='The markdown-enabled response from the chat agent.')
-
class Config:
title = 'BaseChatAgentResponseSchema'
- description = 'Response from the base chat agent. The response can be in markdown format. This response is the only thing the user will see in the chat interface.'
+ description = 'This schema represents the response message exchanged between the user and the chat agent.'
json_schema_extra = {
- 'title': title,
- 'description': description
+ "title": title,
+ "description": description,
}
class BaseChatAgentConfig(BaseModel):
diff --git a/examples/basic_custom_chatbot.py b/examples/basic_custom_chatbot.py
index 55515b8..43c5658 100644
--- a/examples/basic_custom_chatbot.py
+++ b/examples/basic_custom_chatbot.py
@@ -57,5 +57,5 @@ while True:
print('Exiting chat...')
break
- response = agent.run(agent.input_schema(chat_input=user_input))
- console.print(f'Agent: {response.response}')
\ No newline at end of file
+ response = agent.run(agent.input_schema(chat_message=user_input))
+ console.print(f'Agent: {response.chat_message}')
\ No newline at end of file
diff --git a/examples/conversing_agents.py b/examples/conversing_agents.py
new file mode 100644
index 0000000..2d5feca
--- /dev/null
+++ b/examples/conversing_agents.py
@@ -0,0 +1,54 @@
+import json
+import random
+import instructor
+import openai
+from rich.console import Console
+from atomic_agents.agents.base_chat_agent import BaseChatAgentConfig, BaseChatAgent, BaseChatAgentInputSchema, BaseChatAgentResponseSchema
+from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo
+
+# Initialize the system prompts
+agent1_system_prompt_info = SystemPromptInfo(
+ background=['You are an agent that adds 1 to the number said by the previous person in the conversation, or says a random number if no number was said yet.'],
+ steps=['If the previous message contains a number, add 1 to it.', 'If no number was said, generate a random number between 1 and 100.'],
+ output_instructions=['Respond with only the resulting added number and never anything else. Do not include any additional text.']
+)
+
+agent2_system_prompt_info = SystemPromptInfo(
+ background=['You are an agent that adds 1 to the number said by the previous person in the conversation, or says a random number if no number was said yet.'],
+ steps=['If the previous message contains a number, add 1 to it.', 'If no number was said, generate a random number between 1 and 100.'],
+ output_instructions=['Respond with only the resulting added number and never anything else. Do not include any additional text.']
+)
+
+# Initialize the agents
+agent1 = BaseChatAgent(
+ config=BaseChatAgentConfig(
+ client=instructor.from_openai(openai.OpenAI()),
+ model='gpt-3.5-turbo',
+ system_prompt_generator=SystemPromptGenerator(system_prompt_info=agent1_system_prompt_info)
+ )
+)
+
+agent2 = BaseChatAgent(
+ config=BaseChatAgentConfig(
+ client=instructor.from_openai(openai.OpenAI()),
+ model='gpt-3.5-turbo',
+ system_prompt_generator=SystemPromptGenerator(system_prompt_info=agent2_system_prompt_info)
+ )
+)
+
+# Function to simulate a conversation between the two agents
+def simulate_conversation(num_turns=5):
+ console = Console()
+ next_input = "1861"
+
+ for _ in range(num_turns):
+ agent1_response = agent1.run(agent1.input_schema(chat_message=next_input))
+ console.print(f'[bold blue]Agent 1:[/bold blue] {agent1_response.chat_message}')
+ next_input = agent1_response.chat_message
+
+ agent2_response = agent2.run(agent2.input_schema(chat_message=next_input))
+ console.print(f'[bold green]Agent 2:[/bold green] {agent2_response.chat_message}')
+ next_input = agent2_response.chat_message
+
+if __name__ == "__main__":
+ simulate_conversation(num_turns=5)
diff --git a/examples/minimal_chatbot.py b/examples/minimal_chatbot.py
index d89f6d2..0344db6 100644
--- a/examples/minimal_chatbot.py
+++ b/examples/minimal_chatbot.py
@@ -24,5 +24,5 @@ while True:
print('Exiting chat...')
break
- response = agent.run(agent.input_schema(chat_input=user_input))
- console.print(f'Agent: {response.response}')
\ No newline at end of file
+ response = agent.run(agent.input_schema(chat_message=user_input))
+ console.print(f'Agent: {response.chat_message}')
\ No newline at end of file
diff --git a/examples/minimal_multi_agent_tool_use.py b/examples/minimal_multi_agent_tool_use.py
index bf0eebf..4803ba6 100644
--- a/examples/minimal_multi_agent_tool_use.py
+++ b/examples/minimal_multi_agent_tool_use.py
@@ -28,14 +28,14 @@ while True:
print('Exiting chat...')
break
- response = orchestration_agent.run(orchestration_agent.input_schema(chat_input=user_input))
- console.print(f'Agent: {response.response}')
+ response = orchestration_agent.run(orchestration_agent.input_schema(chat_message=user_input))
+ console.print(f'Agent: {response.chat_message}')
- if isinstance(response.response, searx_agent.input_schema):
+ if isinstance(response.chat_message, searx_agent.input_schema):
console.print(f'Using searx agent')
- response = searx_agent.run(response.response)
- elif isinstance(response.response, calc_agent.input_schema):
+ response = searx_agent.run(response.chat_message)
+ elif isinstance(response.chat_message, calc_agent.input_schema):
console.print(f'Using calc agent')
- response = calc_agent.run(response.response)
+ response = calc_agent.run(response.chat_message)
- console.print(f'Agent: {response.response}')
\ No newline at end of file
+ console.print(f'Agent: {response.chat_message}')
\ No newline at end of file
diff --git a/examples/notebooks/multi_agent_quickstart.ipynb b/examples/notebooks/multi_agent_quickstart.ipynb
index d09ebc3..ee97cac 100644
--- a/examples/notebooks/multi_agent_quickstart.ipynb
+++ b/examples/notebooks/multi_agent_quickstart.ipynb
@@ -191,17 +191,17 @@
" print('Exiting chat...')\n",
" break\n",
"\n",
- " response = orchestration_agent.run(orchestration_agent.input_schema(chat_input=user_input))\n",
- " console.print(f'Agent: {response.response}')\n",
+ " response = orchestration_agent.run(orchestration_agent.input_schema(chat_message=user_input))\n",
+ " console.print(f'Agent: {response.chat_message}')\n",
"\n",
- " if isinstance(response.response, searx_agent.input_schema):\n",
+ " if isinstance(response.chat_message, searx_agent.input_schema):\n",
" console.print(f'Using searx agent')\n",
- " response = searx_agent.run(response.response)\n",
- " elif isinstance(response.response, calc_agent.input_schema):\n",
+ " response = searx_agent.run(response.chat_message)\n",
+ " elif isinstance(response.chat_message, calc_agent.input_schema):\n",
" console.print(f'Using calc agent')\n",
- " response = calc_agent.run(response.response)\n",
+ " response = calc_agent.run(response.chat_message)\n",
"\n",
- " console.print(f'Agent: {response.response}')"
+ " console.print(f'Agent: {response.chat_message}')"
]
},
{
diff --git a/examples/notebooks/quickstart.ipynb b/examples/notebooks/quickstart.ipynb
index 57568cf..03aa399 100644
--- a/examples/notebooks/quickstart.ipynb
+++ b/examples/notebooks/quickstart.ipynb
@@ -194,10 +194,10 @@
"\n",
" # Note: The .model_dump() method is ONLY necessary in an ipython notebook.\n",
" # In a normal Python script, you can simply pass the input_schema() run() method like so:\n",
- " # response = agent.run(agent.input_schema(chat_input=user_input))\n",
+ " # response = agent.run(agent.input_schema(chat_message=user_input))\n",
" # The reason for this is unknown to me and I have not been able to find any documentation on it.\n",
- " response = agent.run(agent.input_schema(chat_input=user_input).model_dump())\n",
- " print(f'Agent: {response.response}')"
+ " response = agent.run(agent.input_schema(chat_message=user_input).model_dump())\n",
+ " print(f'Agent: {response.chat_message}')"
]
}
],
diff --git a/examples/notebooks/yelp_agent.ipynb b/examples/notebooks/yelp_agent.ipynb
index fbf13d5..186f90a 100644
--- a/examples/notebooks/yelp_agent.ipynb
+++ b/examples/notebooks/yelp_agent.ipynb
@@ -257,15 +257,15 @@
" print('Exiting chat...')\n",
" break\n",
"\n",
- " response = agent.run(agent.input_schema(chat_input=user_input).model_dump())\n",
+ " response = agent.run(agent.input_schema(chat_message=user_input).model_dump())\n",
" \n",
" # Check the type of the response schema\n",
" if isinstance(response.chosen_schema, YelpSearchToolSchema):\n",
" output = yelp_tool.run(response.chosen_schema)\n",
" agent.memory.add_message('system', f'The following information was found: {output.results}\\n\\n Please summarize the results for the user.')\n",
- " output = agent.run().chosen_schema.response\n",
+ " output = agent.run().chosen_schema.chat_message\n",
" else:\n",
- " output = response.chosen_schema.response\n",
+ " output = response.chosen_schema.chat_message\n",
" \n",
" console.print(f'Agent: {output}')"
]
diff --git a/examples/shared_context.py b/examples/shared_context.py
index 1c643f2..81c103e 100644
--- a/examples/shared_context.py
+++ b/examples/shared_context.py
@@ -46,11 +46,11 @@ def query_agents_with_random_number():
print(f'Updated shared context with random number: {random_number}')
# Get responses from both agents
- response_one = agent_one.run(agent_one.input_schema(chat_input="What is the random number?"))
- response_two = agent_two.run(agent_two.input_schema(chat_input="What is the random number?"))
+ response_one = agent_one.run(agent_one.input_schema(chat_message="What is the random number?"))
+ response_two = agent_two.run(agent_two.input_schema(chat_message="What is the random number?"))
- print(f'Agent One: {response_one.response}')
- print(f'Agent Two: {response_two.response}')
+ print(f'Agent One: {response_one.chat_message}')
+ print(f'Agent Two: {response_two.chat_message}')
if __name__ == "__main__":
query_agents_with_random_number()
\ No newline at end of file
diff --git a/examples/simple_web_search_agent.py b/examples/simple_web_search_agent.py
index ddfda71..c8fa0a6 100644
--- a/examples/simple_web_search_agent.py
+++ b/examples/simple_web_search_agent.py
@@ -47,7 +47,7 @@ def main():
# Fix this
response = agent.run(agent.input_schema(tool_input=user_input))
- console.print(f'Agent: {response.response}')
+ console.print(f'Agent: {response.chat_message}')
if __name__ == "__main__":
main()
\ No newline at end of file
diff --git a/examples/yelp_restaurant_finder.py b/examples/yelp_restaurant_finder.py
index d42beae..e273a55 100644
--- a/examples/yelp_restaurant_finder.py
+++ b/examples/yelp_restaurant_finder.py
@@ -88,7 +88,7 @@ while True:
print('Exiting chat...')
break
- response = agent.run(agent.input_schema(chat_input=user_input))
+ response = agent.run(agent.input_schema(chat_message=user_input))
# Log the chosen schema
logger.info(f'Chosen schema: {response.chosen_schema}')
@@ -101,8 +101,8 @@ while True:
# 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.response
+ output = agent.run().chosen_schema.chat_message
else:
- output = response.chosen_schema.response
+ output = response.chosen_schema.chat_message
console.print(f'Agent: {output}')
\ No newline at end of file
diff --git a/examples/youtube_knowledge_extraction.py b/examples/youtube_knowledge_extraction.py
index 25a8988..087515e 100644
--- a/examples/youtube_knowledge_extraction.py
+++ b/examples/youtube_knowledge_extraction.py
@@ -73,7 +73,7 @@ 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_input='Perform your assignment on the YouTube video transcript present in your context. Do not reply with anything other than the output of the assignment.'))
+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.dict()