summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/gui/server')
-rw-r--r--g4f/gui/server/backend.py6
-rw-r--r--g4f/gui/server/internet.py18
2 files changed, 12 insertions, 12 deletions
diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py
index 6847be34..454ed1c6 100644
--- a/g4f/gui/server/backend.py
+++ b/g4f/gui/server/backend.py
@@ -8,8 +8,6 @@ from g4f.image import is_allowed_extension, to_image
from g4f.errors import VersionNotFoundError
from g4f.Provider import __providers__
from g4f.Provider.bing.create_images import patch_provider
-from .internet import get_search_message
-
class Backend_Api:
"""
@@ -97,7 +95,7 @@ class Backend_Api:
current_version = None
return {
"version": current_version,
- "latest_version": version.get_latest_version(),
+ "latest_version": version.utils.latest_version,
}
def generate_title(self):
@@ -157,6 +155,8 @@ class Backend_Api:
if provider == "Bing":
kwargs['web_search'] = True
else:
+ # ResourceWarning: unclosed event loop
+ from .internet import get_search_message
messages[-1]["content"] = get_search_message(messages[-1]["content"])
model = json_data.get('model')
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