Skip to content

Commit

Permalink
Refactor process_request method to yield research and response inform…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
jgravelle committed Oct 13, 2024
1 parent 19c6714 commit 30dea5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions pocketgroq/autonomous_agent.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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."
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 30dea5e

Please sign in to comment.