summaryrefslogtreecommitdiffstats
path: root/g4f/gui/server/internet.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-10-10 16:29:51 +0200
committerGitHub <noreply@github.com>2023-10-10 16:29:51 +0200
commit26cd71c7f48f7d8417d1f0348d2d1f89636e54d6 (patch)
tree891c9e1aaf8fec34bac0b4031cfeb1ca8354e402 /g4f/gui/server/internet.py
parent ~ | g4f `v-0.1.5.7` (diff)
parentfix html (diff)
downloadgpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar.gz
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar.bz2
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar.lz
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar.xz
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.tar.zst
gpt4free-26cd71c7f48f7d8417d1f0348d2d1f89636e54d6.zip
Diffstat (limited to '')
-rw-r--r--g4f/gui/server/internet.py48
1 files changed, 38 insertions, 10 deletions
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