Skip to content

Building AI agents, atomically

Added BaseAgent.run_async_batch() and BaseAgent.prepare_messages()

Koichi KAWAMURAcb7ae1b162c7
diff --git a/atomic-agents/atomic_agents/agents/base_agent.py b/atomic-agents/atomic_agents/agents/base_agent.py
index ca16d54..a2a8360 100644
--- a/atomic-agents/atomic_agents/agents/base_agent.py
+++ b/atomic-agents/atomic_agents/agents/base_agent.py
@@ -145,6 +145,19 @@ class BaseAgent:
"""
self.memory = self.initial_memory.copy()
+ def prepare_messages(self):
+ if self.system_role is None:
+ self.messages = []
+ else:
+ self.messages = [
+ {
+ "role": self.system_role,
+ "content": self.system_prompt_generator.generate_prompt(),
+ }
+ ]
+
+ self.messages += self.memory.get_history()
+
def get_response(self, response_model=None) -> Type[BaseModel]:
"""
Obtains a response from the language model synchronously.
@@ -159,18 +172,7 @@ class BaseAgent:
if response_model is None:
response_model = self.output_schema
- if self.system_role is None:
- self.messages = []
- else:
- self.messages = [
- {
- "role": self.system_role,
- "content": self.system_prompt_generator.generate_prompt(),
- }
- ]
-
- self.messages += self.memory.get_history()
-
+ self.prepare_messages()
response = self.client.chat.completions.create(
messages=self.messages,
model=self.model,
@@ -215,17 +217,7 @@ class BaseAgent:
self.current_user_input = user_input
self.memory.add_message("user", user_input)
- if self.system_role is None:
- self.messages = []
- else:
- self.messages = [
- {
- "role": self.system_role,
- "content": self.system_prompt_generator.generate_prompt(),
- }
- ]
-
- self.messages += self.memory.get_history()
+ self.prepare_messages()
response_stream = self.client.chat.completions.create_partial(
model=self.model,
@@ -256,27 +248,15 @@ class BaseAgent:
self.current_user_input = user_input
self.memory.add_message("user", user_input)
- if self.system_role is None:
- self.messages = []
- else:
- self.messages = [
- {
- "role": self.system_role,
- "content": self.system_prompt_generator.generate_prompt(),
- }
- ]
-
- self.messages += self.memory.get_history()
+ self.prepare_messages()
response = await self.client.chat.completions.create(
- model=self.model,
- messages=self.messages,
- response_model=self.output_schema,
- **self.model_api_parameters
+ model=self.model, messages=self.messages, response_model=self.output_schema, **self.model_api_parameters
)
self.memory.add_message("assistant", response)
return response
+
async def stream_response_async(self, user_input: Optional[Type[BaseIOSchema]] = None):
"""
Deprecated method for streaming responses asynchronously. Use run_async instead.
diff --git a/atomic-agents/tests/agents/test_base_agent.py b/atomic-agents/tests/agents/test_base_agent.py
index 103bb71..95bb356 100644
--- a/atomic-agents/tests/agents/test_base_agent.py
+++ b/atomic-agents/tests/agents/test_base_agent.py
@@ -28,6 +28,12 @@ def mock_instructor_async():
mock = Mock(spec=instructor.Instructor)
mock.chat.completions.create = Mock()
+ # Make create method awaitable by using an async function
+ async def mock_create(*args, **kwargs):
+ return BaseAgentOutputSchema(chat_message="Test output")
+
+ mock.chat.completions.create = mock_create
+
# Mock the create_partial method to return an async generator
async def mock_create_partial(*args, **kwargs):
yield BaseAgentOutputSchema(chat_message="Mocked response")
@@ -297,27 +303,25 @@ async def test_run_async(agent, mock_memory):
@pytest.mark.asyncio
-async def test_run_async_with_no_system_role(mock_instructor_async, mock_memory):
+async def test_run_async_batch(mock_instructor_async, mock_memory):
# Create a BaseAgentConfig with system_role set to None
config = BaseAgentConfig(
client=mock_instructor_async,
model="gpt-4o-mini",
memory=mock_memory,
system_prompt_generator=None, # No system prompt generator
- system_role=None, # Ensure system_role is None
)
agent = BaseAgent(config)
# Create a mock input
mock_input = BaseAgentInputSchema(chat_message="Test input")
+ mock_output = BaseAgentOutputSchema(chat_message="Test output")
- # Collect all responses from the actual run_async method
- responses = []
- async for response in agent.run_async(mock_input):
- responses.append(response)
+ # Get response from run_async_batch method
+ response = await agent.run_async_batch(mock_input)
# Assertions
- assert agent.messages == [] # Ensure self.messages was set to an empty list
+ assert response == mock_output # Ensure self.messages was set to an empty list
@pytest.mark.asyncio
diff --git a/atomic-examples/quickstart/quickstart/5_custom_system_role_for_reasoning_models.py b/atomic-examples/quickstart/quickstart/5_custom_system_role_for_reasoning_models copy.py
similarity index 100%
rename from atomic-examples/quickstart/quickstart/5_custom_system_role_for_reasoning_models.py
rename to atomic-examples/quickstart/quickstart/5_custom_system_role_for_reasoning_models copy.py
diff --git a/atomic-examples/quickstart/quickstart/6_asynchronous_processing.py b/atomic-examples/quickstart/quickstart/6_asynchronous_processing.py
new file mode 100644
index 0000000..f169091
--- /dev/null
+++ b/atomic-examples/quickstart/quickstart/6_asynchronous_processing.py
@@ -0,0 +1,88 @@
+import os
+import asyncio
+import instructor
+import openai
+from rich.console import Console
+from atomic_agents.agents.base_agent import BaseIOSchema, BaseAgent, BaseAgentConfig, BaseAgentInputSchema
+from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator
+
+# API Key setup
+API_KEY = ""
+if not API_KEY:
+ API_KEY = os.getenv("OPENAI_API_KEY")
+
+if not API_KEY:
+ raise ValueError(
+ "API key is not set. Please set the API key as a static variable or in the environment variable OPENAI_API_KEY."
+ )
+
+# Initialize a Rich Console for pretty console outputs
+console = Console()
+
+# OpenAI client setup using the Instructor library
+client = instructor.from_openai(openai.AsyncOpenAI(api_key=API_KEY))
+
+
+# Define a schema for the output data
+class PersonSchema(BaseIOSchema):
+ """Schema for person information."""
+
+ name: str
+ age: int
+ pronouns: list[str]
+ profession: str
+
+
+# System prompt generator setup
+sysyem_prompt_generator = SystemPromptGenerator(
+ background=["You parse a sentence and extract elements."],
+ steps=[],
+ output_instructions=[],
+)
+
+dataset = [
+ "My name is Mike, I am 30 years old, my pronouns are he/him, and I am a software engineer.",
+ "My name is Sarah, I am 25 years old, my pronouns are she/her, and I am a data scientist.",
+ "My name is John, I am 40 years old, my pronouns are he/him, and I am a product manager.",
+ "My name is Emily, I am 35 years old, my pronouns are she/her, and I am a UX designer.",
+ "My name is David, I am 28 years old, my pronouns are he/him, and I am a web developer.",
+ "My name is Anna, I am 32 years old, my pronouns are she/her, and I am a graphic designer.",
+]
+
+sem = asyncio.Semaphore(2)
+
+# Agent setup with specified configuration
+agent = BaseAgent(
+ config=BaseAgentConfig(
+ client=client, model="gpt-4o-mini", sysyem_prompt_generator=sysyem_prompt_generator, output_schema=PersonSchema # type: ignore
+ )
+)
+
+
+async def exec_agent(message: str):
+ """Execute the agent with the provided message."""
+ user_input = BaseAgentInputSchema(chat_message=message)
+ agent.reset_memory()
+ partial_response = None
+ async for partial_response in agent.run_async(user_input):
+ pass
+
+ assert partial_response
+ response = PersonSchema(**partial_response.model_dump())
+
+ return response
+
+
+async def process(dataset: list[str]):
+ """Process the dataset asynchronously."""
+ async with sem:
+ # Run the agent asynchronously for each message in the dataset
+ # and collect the responses
+ responses = await asyncio.gather(*(exec_agent(message) for message in dataset))
+
+ # Prompt the user for input with a styled prompt
+ return responses
+
+
+responses = asyncio.run(process(dataset))
+console.print(responses)
diff --git a/atomic-examples/quickstart/quickstart/7_asynchronous_processing_batch.py b/atomic-examples/quickstart/quickstart/7_asynchronous_processing_batch.py
new file mode 100644
index 0000000..c374e8c
--- /dev/null
+++ b/atomic-examples/quickstart/quickstart/7_asynchronous_processing_batch.py
@@ -0,0 +1,81 @@
+import os
+import asyncio
+import instructor
+import openai
+from rich.console import Console
+from atomic_agents.agents.base_agent import BaseIOSchema, BaseAgent, BaseAgentConfig, BaseAgentInputSchema
+from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator
+
+# API Key setup
+API_KEY = ""
+if not API_KEY:
+ API_KEY = os.getenv("OPENAI_API_KEY")
+
+if not API_KEY:
+ raise ValueError(
+ "API key is not set. Please set the API key as a static variable or in the environment variable OPENAI_API_KEY."
+ )
+
+# Initialize a Rich Console for pretty console outputs
+console = Console()
+
+# OpenAI client setup using the Instructor library
+client = instructor.from_openai(openai.AsyncOpenAI(api_key=API_KEY))
+
+
+# Define a schema for the output data
+class PersonSchema(BaseIOSchema):
+ """Schema for person information."""
+
+ name: str
+ age: int
+ pronouns: list[str]
+ profession: str
+
+
+# System prompt generator setup
+sysyem_prompt_generator = SystemPromptGenerator(
+ background=["You parse a sentence and extract elements."],
+ steps=[],
+ output_instructions=[],
+)
+
+dataset = [
+ "My name is Mike, I am 30 years old, my pronouns are he/him, and I am a software engineer.",
+ "My name is Sarah, I am 25 years old, my pronouns are she/her, and I am a data scientist.",
+ "My name is John, I am 40 years old, my pronouns are he/him, and I am a product manager.",
+ "My name is Emily, I am 35 years old, my pronouns are she/her, and I am a UX designer.",
+ "My name is David, I am 28 years old, my pronouns are he/him, and I am a web developer.",
+ "My name is Anna, I am 32 years old, my pronouns are she/her, and I am a graphic designer.",
+]
+
+sem = asyncio.Semaphore(2)
+
+# Agent setup with specified configuration
+agent = BaseAgent(
+ config=BaseAgentConfig(
+ client=client, model="gpt-4o-mini", sysyem_prompt_generator=sysyem_prompt_generator, output_schema=PersonSchema # type: ignore
+ )
+)
+
+
+async def exec_agent(message: str):
+ """Execute the agent with the provided message."""
+ user_input = BaseAgentInputSchema(chat_message=message)
+ agent.reset_memory()
+ response = await agent.run_async_batch(user_input)
+ return response
+
+
+async def process(dataset: list[str]):
+ """Process the dataset asynchronously."""
+ async with sem:
+ # Run the agent asynchronously for each message in the dataset
+ # and collect the responses
+ responses = await asyncio.gather(*(exec_agent(message) for message in dataset))
+
+ return responses
+
+
+responses = asyncio.run(process(dataset))
+console.print(responses)