Building AI agents, atomically
implement info providers as generators
Kenny Vaneetvelde964b02ef2b90
diff --git a/atomic_agents/agents/basic_chat_agent_new.py b/atomic_agents/agents/basic_chat_agent_new.pyindex 1bcc8d6..2940b57 100644--- a/atomic_agents/agents/basic_chat_agent_new.py+++ b/atomic_agents/agents/basic_chat_agent_new.py@@ -2,6 +2,7 @@ import osfrom typing import Callable, Listfrom pydantic import BaseModel, Fieldfrom datetime import datetime+from abc import ABC, abstractmethodfrom groq import Groqimport instructorimport openai@@ -27,23 +28,38 @@ class SystemPrompt(BaseModel):steps: List[str]output_instructions: List[str]-class DynamicInfoProvider:- def __init__(self, provider_func: Callable[..., str], title: str, *args, **kwargs):- self.provider_func = provider_func+class DynamicInfoProviderBase(ABC):+ def __init__(self, title: str):self.title = title- self.args = args- self.kwargs = kwargs+ @abstractmethoddef get_info(self) -> str:- return self.provider_func(*self.args, **self.kwargs)+ pass++class CurrentDateProvider(DynamicInfoProviderBase):+ def __init__(self, title: str, format: str = '%Y-%m-%d %H:%M:%S'):+ super().__init__(title)+ self.format = format++ def get_info(self) -> str:+ return f'The current date, in the format "{self.format}", is {datetime.now().strftime(self.format)}'++class LiveSearchResultsProvider(DynamicInfoProviderBase):+ def __init__(self, title: str, search_query: str):+ super().__init__(title)+ self.search_query = search_query++ def get_info(self) -> str:+ # Placeholder for live search results fetching logic+ # In a real implementation, this would fetch live data from a search API+ return f'Live search results for "{self.search_query}"'class SystemPromptGenerator:- def __init__(self, system_prompt: SystemPrompt, dynamic_info_providers: List[DynamicInfoProvider] = None):+ def __init__(self, system_prompt: SystemPrompt, dynamic_info_providers: List[DynamicInfoProviderBase] = None):self.system_prompt = system_promptself.dynamic_info_providers = dynamic_info_providers or []def generate_prompt(self) -> str:- # Generate the promptsystem_prompt = ''if self.system_prompt.background:@@ -67,7 +83,7 @@ class SystemPromptGenerator:return system_promptclass BasicChatAgent:- def __init__(self, client, model: str = 'gpt-3.5-turbo', system_prompt: SystemPrompt = None, memory: ChatMemory = None, vector_db = None, input_schema = BasicChatAgentInputSchema, output_schema = BasicChatAgentResponse, dynamic_info_providers: List[DynamicInfoProvider] = None):+ def __init__(self, client, model: str = 'gpt-3.5-turbo', system_prompt: SystemPrompt = None, memory: ChatMemory = None, vector_db = None, input_schema = BasicChatAgentInputSchema, output_schema = BasicChatAgentResponse, dynamic_info_providers: List[DynamicInfoProviderBase] = None):self.input_schema = input_schemaself.output_schema = output_schemaself.client = client@@ -104,11 +120,21 @@ class BasicChatAgent:if __name__ == '__main__':console = Console()- openai_client = openai.OpenAI()- client = instructor.from_openai(openai_client)- model = 'gpt-3.5-turbo'- groq_client = instructor.from_groq(Groq(api_key=os.getenv('GROQ_API_KEY')))- groq_model = os.getenv('GROQ_CHAT_MODEL')+ # Configuration dictionary+ config = {+ 'openai': {+ 'client': instructor.from_openai(openai.OpenAI()),+ 'model': 'gpt-3.5-turbo'+ },+ 'groq': {+ 'client': instructor.from_groq(Groq(api_key=os.getenv('GROQ_API_KEY'))),+ 'model': os.getenv('GROQ_CHAT_MODEL')+ }+ }++ # Choose the configuration+ use_groq = False # Set this to True to use Groq, False to use OpenAI+ chosen_config = config['groq'] if use_groq else config['openai']class MyChatAgentResponse(BaseModel):observations: List[str] = Field(..., description='What the agent observes in the current state of the conversation.')@@ -125,37 +151,35 @@ if __name__ == '__main__':system_prompt = SystemPrompt(background=[- 'You are an expert in creating viral video ideas.',- 'Your goal is to generate engaging and creative video concepts that have the potential to go viral on social media platforms.'+ 'You are an expert in brainstorming ideas and creating article outlines.',+ 'Your goal is to generate engaging and creative ideas and structured outlines for articles based on the given topic.'],steps=[- 'Take the input given as the topic for the video ideas.',- 'Brainstorm and create a list of 10 unique and creative video ideas based on the given topic.',- 'Ensure that each idea is designed to capture attention and encourage sharing.'+ 'Take the input given as the topic for the ideas and article outline.',+ 'Brainstorm and create a list of 10 unique and creative ideas based on the given topic.',+ 'Generate a structured article outline based on the given topic and the brainstormed ideas.'],output_instructions=['Ensure each idea is clearly numbered from 1 to 10.',- 'Output the list in human-readable Markdown format.',- 'Do not include any additional text or confirmation messages; only provide the list of 10 viral video ideas.'+ 'Output the list of ideas and the article outline in human-readable Markdown format.',+ 'Do not include any additional text or confirmation messages; only provide the list of ideas and the article outline.'])-memory = ChatMemory()initial_memory = [- {'role': 'assistant', 'content': 'Hello! I\'m an AI assistant that can help you generate viral video ideas. Please provide me with a topic to get started.'}+ {'role': 'assistant', 'content': 'Hello! I\'m an AI assistant that can help you brainstorm ideas and generate article outlines. Please provide me with a topic to get started.'}]memory.load_from_dict_list(initial_memory)# Define dynamic info providers- def current_date_provider(format: str = '%Y-%m-%dT%H:%M:%S') -> str:- return f'The current date, in the format "{format}", is {datetime.now().strftime(format)}'-- dynamic_info_providers = [DynamicInfoProvider(current_date_provider, 'Current date', format='%Y-%m-%d %H:%M:%S')]+ dynamic_info_providers = [+ CurrentDateProvider('Current date', format='%Y-%m-%d %H:%M:%S'),+ ]agent = BasicChatAgent(- client=client,- model=model,+ client=chosen_config['client'],+ model=chosen_config['model'],system_prompt=system_prompt,memory=memory,dynamic_info_providers=dynamic_info_providers,