summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/internet.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/gui/server/internet.py')
-rw-r--r--g4f/gui/server/internet.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/g4f/gui/server/internet.py b/g4f/gui/server/internet.py
index a6bfc885..e784e52d 100644
--- a/g4f/gui/server/internet.py
+++ b/g4f/gui/server/internet.py
@@ -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 \ No newline at end of file