diff --git a/pocketgroq/autonomous_agent.py b/pocketgroq/autonomous_agent.py index f8d3688..2f27a87 100644 --- a/pocketgroq/autonomous_agent.py +++ b/pocketgroq/autonomous_agent.py @@ -1,6 +1,6 @@ import time import logging -from typing import List, Dict, Any +from typing import List, Dict, Any, Generator from pocketgroq import GroqProvider from pocketgroq.exceptions import GroqAPIError @@ -13,55 +13,73 @@ def __init__(self, groq_provider: GroqProvider, max_sources: int = 5, search_del self.max_sources = max_sources self.search_delay = search_delay - def process_request(self, request: str, max_sources: int = None) -> str: + def process_request(self, request: str, max_sources: int = None) -> Generator[Dict[str, str], None, None]: if max_sources is not None: self.max_sources = max_sources self._inform_user(f"Processing request: '{request}'") + yield {"type": "research", "content": f"Processing request: '{request}'"} initial_response = self.groq.generate(request) self._inform_user(f"Initial response: {initial_response}") + yield {"type": "research", "content": f"Initial response: {initial_response}"} if self.groq.evaluate_response(request, initial_response): self._inform_user("Initial response was satisfactory.") - return initial_response + yield {"type": "research", "content": "Initial response was satisfactory."} + yield {"type": "response", "content": initial_response} + return self._inform_user("Initial response was not satisfactory. I'll search for information online.") + yield {"type": "research", "content": "Initial response was not satisfactory. I'll search for information online."} search_query = self._generate_search_query(request) self._inform_user(f"Generated search query: '{search_query}'") + yield {"type": "research", "content": f"Generated search query: '{search_query}'"} search_results = self.groq.web_search(search_query) self._inform_user(f"Found {len(search_results)} search results.") + yield {"type": "research", "content": f"Found {len(search_results)} search results."} for i, result in enumerate(search_results[:self.max_sources]): if i > 0: time.sleep(self.search_delay) self._inform_user(f"Checking source {i+1}: {result['url']}") + yield {"type": "research", "content": f"Checking source {i+1}: {result['url']}"} try: content = self.groq.get_web_content(result['url']) self._inform_user(f"Retrieved content from {result['url']} (length: {len(content)} characters)") + yield {"type": "research", "content": f"Retrieved content from {result['url']} (length: {len(content)} characters)"} response = self._generate_response_from_content(request, content) self._inform_user(f"Generated response from content: {response}") + yield {"type": "research", "content": f"Generated response from content: {response}"} if self.groq.evaluate_response(request, response): self._inform_user("This response is satisfactory.") - return response + yield {"type": "research", "content": "This response is satisfactory."} + yield {"type": "response", "content": response} + return else: self._inform_user("This response was not satisfactory. I'll check another source.") + yield {"type": "research", "content": "This response was not satisfactory. I'll check another source."} except GroqAPIError as e: if e.status_code == 429: self._inform_user("I've encountered a rate limit. I'll wait for a minute before trying again.") + yield {"type": "research", "content": "I've encountered a rate limit. I'll wait for a minute before trying again."} time.sleep(60) else: self._inform_user(f"I encountered an error while processing {result['url']}: {str(e)}") + yield {"type": "research", "content": f"I encountered an error while processing {result['url']}: {str(e)}"} except Exception as e: self._inform_user(f"An unexpected error occurred while processing {result['url']}: {str(e)}") + yield {"type": "research", "content": f"An unexpected error occurred while processing {result['url']}: {str(e)}"} - return "I'm sorry, but after checking multiple sources, I couldn't find a satisfactory answer to your request." + final_message = "I'm sorry, but after checking multiple sources, I couldn't find a satisfactory answer to your request." + self._inform_user(final_message) + yield {"type": "response", "content": final_message} def _generate_search_query(self, request: str) -> str: prompt = f"Generate a single, concise search query (no more than 6 words) to find information for: '{request}'. Respond with only the search query, no other text." diff --git a/setup.py b/setup.py index deaf21a..0608c76 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="pocketgroq", - version="0.5.0", + version="0.5.1", author="PocketGroq Team", author_email="j@gravelle.us", description="A library for easy integration with Groq API, including web scraping, image handling, and Chain of Thought reasoning",