Skip to content

Commit

Permalink
Merge pull request assafelovic#21 from assafelovic/agent_logs
Browse files Browse the repository at this point in the history
added additional agent task logs for better progress visibility
  • Loading branch information
rotemweiss57 authored Jul 11, 2023
2 parents 72d734c + 643f6b9 commit a245af7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion actions/web_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from selenium.webdriver.safari.options import Options as SafariOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from fastapi import WebSocket

import processing.text as summary

Expand All @@ -32,25 +33,32 @@
CFG = Config()


async def async_browse(url: str, question: str) -> str:
async def async_browse(url: str, question: str, websocket: WebSocket) -> str:
"""Browse a website and return the answer and links to the user
Args:
url (str): The url of the website to browse
question (str): The question asked by the user
websocket (WebSocketManager): The websocket manager
Returns:
str: The answer and links to the user
"""
loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor(max_workers=8)

print(f"Scraping url {url} with question {question}")
await websocket.send_json(
{"type": "logs", "output": f"🔎 Browsing the {url} for relevant about: {question}..."})

try:
driver, text = await loop.run_in_executor(executor, scrape_text_with_selenium, url)
await loop.run_in_executor(executor, add_header, driver)
summary_text = await loop.run_in_executor(executor, summary.summarize_text, url, text, question, driver)

await websocket.send_json(
{"type": "logs", "output": f"📝 Information gathered from url {url}: {summary_text}"})

return f"Information gathered from url {url}: {summary_text}"
except Exception as e:
print(f"An error occurred while processing the url {url}: {e}")
Expand Down
6 changes: 4 additions & 2 deletions agent/research_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async def get_new_urls(self, url_set_input):
self.visited_urls.add(url)
new_urls.append(url)

await self.websocket.send_json({"type": "logs", "output": f"📝 Summarizing sources..."})
return new_urls

async def call_agent(self, action, stream=False, websocket=None):
Expand Down Expand Up @@ -99,8 +98,11 @@ async def async_search(self, query):
search_results = json.loads(web_search(query))
new_search_urls = self.get_new_urls([url.get("href") for url in search_results])

await self.websocket.send_json(
{"type": "logs", "output": f"🌐 Browsing the following sites for relevant information: {new_search_urls}..."})

# Create a list to hold the coroutine objects
tasks = [async_browse(url, query,) for url in await new_search_urls]
tasks = [async_browse(url, query, self.websocket) for url in await new_search_urls]

# Gather the results as they become available
responses = await asyncio.gather(*tasks, return_exceptions=True)
Expand Down

0 comments on commit a245af7

Please sign in to comment.