Move some modules, create providers dir

Set min version for duckduckgo
Make duckduckgo search async
Remove get_lastet_version
This commit is contained in:
Heiner Lohaus 2024-02-22 00:16:58 +01:00
parent 0a0698c7f3
commit e5b7f72b71
31 changed files with 448 additions and 435 deletions

View file

@ -2,7 +2,7 @@ from __future__ import annotations
from aiohttp import ClientSession, ClientTimeout
try:
from duckduckgo_search import DDGS
from duckduckgo_search.duckduckgo_search_async import AsyncDDGS
from bs4 import BeautifulSoup
has_requirements = True
except ImportError:
@ -30,7 +30,10 @@ class SearchResults():
search += result.snippet
search += f"\n\nSource: [[{idx}]]({result.url})"
return search
def __len__(self) -> int:
return len(self.results)
class SearchResultEntry():
def __init__(self, title: str, url: str, snippet: str, text: str = None):
self.title = title
@ -96,21 +99,20 @@ async def fetch_and_scrape(session: ClientSession, url: str, max_words: int = No
async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text: bool = True) -> SearchResults:
if not has_requirements:
raise MissingRequirementsError('Install "duckduckgo-search" and "beautifulsoup4" package')
with DDGS() as ddgs:
async with AsyncDDGS() as ddgs:
results = []
for result in ddgs.text(
async for result in ddgs.text(
query,
region="wt-wt",
safesearch="moderate",
timelimit="y",
max_results=n_results
):
results.append(SearchResultEntry(
result["title"],
result["href"],
result["body"]
))
if len(results) >= n_results:
break
if add_text:
requests = []
@ -136,7 +138,6 @@ async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text
return SearchResults(formatted_results)
def get_search_message(prompt) -> str:
try:
search_results = asyncio.run(search(prompt))
@ -146,7 +147,6 @@ def get_search_message(prompt) -> str:
Instruction: Using the provided web search results, to write a comprehensive reply to the user request.
Make sure to add the sources of cites using [[Number]](Url) notation after the reference. Example: [[0]](http://google.com)
If the provided search results refer to multiple subjects with the same name, write separate answers for each subject.
User request:
{prompt}
@ -154,4 +154,4 @@ User request:
return message
except Exception as e:
print("Couldn't do web search:", e)
return prompt
return prompt