diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-02-22 00:16:58 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-02-22 00:16:58 +0100 |
commit | e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6 (patch) | |
tree | 70e9f87cb18f165428492d53a1c6e28c0828490a /g4f/gui/server/internet.py | |
parent | Update docs / readme, Improve Gemini auth (diff) | |
download | gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar.gz gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar.bz2 gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar.lz gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar.xz gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.tar.zst gpt4free-e5b7f72b719814ffa2748e8e8ed1c6713a24e1a6.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/gui/server/internet.py | 18 |
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 |