Skip to content

Building AI agents, atomically

Deprecate `stream_response_async` in favor of `run_async` for improved clarity & Updated examples to reflect this change

KennyVaneetvelde7734ecc7b761
diff --git a/atomic-agents/atomic_agents/agents/base_agent.py b/atomic-agents/atomic_agents/agents/base_agent.py
index d8b7e2b..18769df 100644
--- a/atomic-agents/atomic_agents/agents/base_agent.py
+++ b/atomic-agents/atomic_agents/agents/base_agent.py
@@ -10,6 +10,7 @@ from atomic_agents.lib.base.base_io_schema import BaseIOSchema
from instructor.dsl.partial import PartialBase
from jiter import from_json
+import warnings
def model_from_chunks_patched(cls, json_chunks, **kwargs):
@@ -164,55 +165,7 @@ class BaseAgent:
return response
- async def get_response_async(self, response_model=None) -> Type[BaseModel]:
- """
- Obtains a response from the language model asynchronously.
-
- Args:
- response_model (Type[BaseModel], optional):
- The schema for the response data. If not set, self.output_schema is used.
-
- Returns:
- Type[BaseModel]: The response from the language model.
- """
- if response_model is None:
- response_model = self.output_schema
-
- messages = [
- {
- "role": "system",
- "content": self.system_prompt_generator.generate_prompt(),
- }
- ] + self.memory.get_history()
- response = await self.client.chat.completions.create(
- model=self.model,
- messages=messages,
- response_model=response_model,
- temperature=self.temperature,
- )
- return response
-
- async def run_async(self, user_input: Optional[Type[BaseIOSchema]] = None) -> Type[BaseIOSchema]:
- """
- Runs the chat agent with the given user input asynchronously.
-
- Args:
- user_input (Optional[Type[BaseIOSchema]]): The input from the user. If not provided, skips adding to memory.
-
- Returns:
- Type[BaseIOSchema]: The response from the chat agent.
- """
- if user_input:
- self.memory.initialize_turn()
- self.current_user_input = user_input
- self.memory.add_message("user", user_input)
-
- response = await self.get_response_async(response_model=self.output_schema)
- self.memory.add_message("assistant", response)
-
- return response
-
- async def stream_response_async(self, user_input: Optional[Type[BaseIOSchema]] = None):
+ async def run_async(self, user_input: Optional[Type[BaseIOSchema]] = None):
"""
Runs the chat agent with the given user input, supporting streaming output asynchronously.
@@ -248,6 +201,24 @@ class BaseAgent:
full_response_content = self.output_schema(**partial_response.model_dump())
self.memory.add_message("assistant", full_response_content)
+ async def stream_response_async(self, user_input: Optional[Type[BaseIOSchema]] = None):
+ """
+ Deprecated method for streaming responses asynchronously. Use run_async instead.
+
+ Args:
+ user_input (Optional[Type[BaseIOSchema]]): The input from the user. If not provided, skips adding to memory.
+
+ Yields:
+ BaseModel: Partial responses from the chat agent.
+ """
+ warnings.warn(
+ "stream_response_async is deprecated and will be removed in version 1.1. Use run_async instead which can be used in the exact same way.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ async for response in self.run_async(user_input):
+ yield response
+
def get_context_provider(self, provider_name: str) -> Type[SystemPromptContextProviderBase]:
"""
Retrieves a context provider by name.
@@ -338,7 +309,7 @@ if __name__ == "__main__":
console.print("[bold blue]Assistant:[/bold blue]")
if streaming:
with Live(console=console, refresh_per_second=4) as live:
- async for partial_response in agent.stream_response_async(user_input):
+ async for partial_response in agent.run_async(user_input):
response_json = partial_response.model_dump()
json_str = json.dumps(response_json, indent=2)
live.update(json_str)
diff --git a/atomic-examples/quickstart/quickstart/1_1_basic_chatbot_streaming.py b/atomic-examples/quickstart/quickstart/1_1_basic_chatbot_streaming.py
index 9b77be2..7c5aa46 100644
--- a/atomic-examples/quickstart/quickstart/1_1_basic_chatbot_streaming.py
+++ b/atomic-examples/quickstart/quickstart/1_1_basic_chatbot_streaming.py
@@ -65,9 +65,9 @@ async def main():
console.print() # Add newline before response
# Use Live display to show streaming response
- with Live("", refresh_per_second=4, auto_refresh=True) as live:
+ with Live("", refresh_per_second=10, auto_refresh=True) as live:
current_response = ""
- async for partial_response in agent.stream_response_async(input_schema):
+ async for partial_response in agent.run_async(input_schema):
if hasattr(partial_response, "chat_message") and partial_response.chat_message:
# Only update if we have new content
if partial_response.chat_message != current_response:
diff --git a/atomic-examples/quickstart/quickstart/3_1_basic_custom_chatbot_with_custom_schema_streaming.py b/atomic-examples/quickstart/quickstart/3_1_basic_custom_chatbot_with_custom_schema_streaming.py
index 2eeae2c..14b0aae 100644
--- a/atomic-examples/quickstart/quickstart/3_1_basic_custom_chatbot_with_custom_schema_streaming.py
+++ b/atomic-examples/quickstart/quickstart/3_1_basic_custom_chatbot_with_custom_schema_streaming.py
@@ -109,11 +109,11 @@ async def main():
console.print() # Add newline before response
# Use Live display to show streaming response
- with Live("", refresh_per_second=4, auto_refresh=True) as live:
+ with Live("", refresh_per_second=10, auto_refresh=True) as live:
current_response = ""
current_questions: List[str] = []
- async for partial_response in agent.stream_response_async(input_schema):
+ async for partial_response in agent.run_async(input_schema):
if hasattr(partial_response, "chat_message") and partial_response.chat_message:
# Update the message part
if partial_response.chat_message != current_response: