summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/internet.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/gui/server/internet.py20
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