From d719860e2af8a825a9e2eaf6dc97c375ff535cbb Mon Sep 17 00:00:00 2001 From: Commenter123321 <36051603+Commenter123321@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:47:41 +0200 Subject: make internet searching better --- etc/testing/test_gui.py | 6 ++++++ g4f/gui/server/internet.py | 48 ++++++++++++++++++++++++++++++++++++---------- g4f/gui/server/provider.py | 4 +++- requirements.txt | 3 +-- 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 etc/testing/test_gui.py diff --git a/etc/testing/test_gui.py b/etc/testing/test_gui.py new file mode 100644 index 00000000..cc3ae379 --- /dev/null +++ b/etc/testing/test_gui.py @@ -0,0 +1,6 @@ +import sys +from pathlib import Path +sys.path.append(str(Path(__file__).parent.parent.parent)) + +from g4f.gui import run_gui +run_gui() diff --git a/g4f/gui/server/internet.py b/g4f/gui/server/internet.py index 0d9636a3..75edb9cb 100644 --- a/g4f/gui/server/internet.py +++ b/g4f/gui/server/internet.py @@ -1,28 +1,56 @@ -from requests import get from datetime import datetime +from duckduckgo_search import DDGS + +ddgs = DDGS(timeout=20) + def search(internet_access, prompt): print(prompt) try: - if internet_access == False: + if not internet_access: return [] - search = get('https://ddg-api.herokuapp.com/search', params={ - 'query': prompt['content'], - 'limit': 3 - }) + results = duckduckgo_search(q=prompt) + + if not search: + return [] blob = '' - for index, result in enumerate(search.json()): - blob += f'[{index}] "{result["snippet"]}"\nURL:{result["link"]}\n\n' + for index, result in enumerate(results): + blob += f'[{index}] "{result["body"]}"\nURL:{result["href"]}\n\n' date = datetime.now().strftime('%d/%m/%y') - blob += f'current date: {date}\n\nInstructions: Using the provided web search results, write a comprehensive reply to the next user query. Make sure to cite results using [[number](URL)] notation after the reference. If the provided search results refer to multiple subjects with the same name, write separate answers for each subject. Ignore your previous response if any.' + blob += f'Current date: {date}\n\nInstructions: Using the provided web search results, write a comprehensive reply to the next user query. Make sure to cite results using [[number](URL)] notation after the reference. If the provided search results refer to multiple subjects with the same name, write separate answers for each subject. Ignore your previous response if any.' return [{'role': 'user', 'content': blob}] except Exception as e: - return [] \ No newline at end of file + print("Couldn't search DuckDuckGo:", e) + print(e.__traceback__.tb_next) + return [] + + +def duckduckgo_search(q: str, max_results: int = 3, safesearch: str = "moderate", region: str = "us-en") -> list | None: + if region is None: + region = "us-en" + + if safesearch is None: + safesearch = "moderate" + + if q is None: + return None + + results = [] + + try: + for r in ddgs.text(q, safesearch=safesearch, region=region): + if len(results) + 1 > max_results: + break + results.append(r) + except Exception as e: + print(e) + + return results diff --git a/g4f/gui/server/provider.py b/g4f/gui/server/provider.py index 286f881b..230b5f5f 100644 --- a/g4f/gui/server/provider.py +++ b/g4f/gui/server/provider.py @@ -1,6 +1,8 @@ import g4f +from g4f import BaseProvider -def get_provider(provider: str) -> g4f.Provider.BaseProvider: + +def get_provider(provider: str) -> BaseProvider | None: if isinstance(provider, str): print(provider) diff --git a/requirements.txt b/requirements.txt index 88084aad..75549d68 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,5 +10,4 @@ flask flask-cors typing-extensions PyExecJS -flask -flask-cors \ No newline at end of file +duckduckgo-search \ No newline at end of file -- cgit v1.2.3