diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-11-20 19:58:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-20 19:58:16 +0100 |
commit | ffb4b0d162cc29b1caa4539d854de37206804225 (patch) | |
tree | ea565c46ed908d2a6b4ef3fef1f100e68428aec3 /g4f/gui/server/internet.py | |
parent | Update api.py (diff) | |
download | gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar.gz gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar.bz2 gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar.lz gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar.xz gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.tar.zst gpt4free-ffb4b0d162cc29b1caa4539d854de37206804225.zip |
Diffstat (limited to 'g4f/gui/server/internet.py')
-rw-r--r-- | g4f/gui/server/internet.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/g4f/gui/server/internet.py b/g4f/gui/server/internet.py index b41b5eae..bafa3af7 100644 --- a/g4f/gui/server/internet.py +++ b/g4f/gui/server/internet.py @@ -8,12 +8,14 @@ try: except ImportError: has_requirements = False from ...errors import MissingRequirementsError - +from ... import debug + import asyncio class SearchResults(): - def __init__(self, results: list): + def __init__(self, results: list, used_words: int): self.results = results + self.used_words = used_words def __iter__(self): yield from self.results @@ -104,7 +106,8 @@ async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text region="wt-wt", safesearch="moderate", timelimit="y", - max_results=n_results + max_results=n_results, + backend="html" ): results.append(SearchResultEntry( result["title"], @@ -120,6 +123,7 @@ async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text texts = await asyncio.gather(*requests) formatted_results = [] + used_words = 0 left_words = max_words for i, entry in enumerate(results): if add_text: @@ -132,13 +136,14 @@ async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text left_words -= entry.snippet.count(" ") if 0 > left_words: break + used_words = max_words - left_words formatted_results.append(entry) - return SearchResults(formatted_results) + return SearchResults(formatted_results, used_words) -def get_search_message(prompt) -> str: +def get_search_message(prompt, n_results: int = 5, max_words: int = 2500) -> str: try: - search_results = asyncio.run(search(prompt)) + search_results = asyncio.run(search(prompt, n_results, max_words)) message = f""" {search_results} @@ -149,7 +154,8 @@ Make sure to add the sources of cites using [[Number]](Url) notation after the r User request: {prompt} """ + debug.log(f"Web search: '{prompt.strip()[:50]}...' {search_results.used_words} Words") return message except Exception as e: - print("Couldn't do web search:", e) + debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}") return prompt
\ No newline at end of file |