Spaces:
Paused
Paused
Update webscout.py
Browse files- webscout.py +16 -11
webscout.py
CHANGED
|
@@ -12,6 +12,13 @@ from typing import Dict, List, Optional, Tuple, Type, Union, cast
|
|
| 12 |
import asyncio
|
| 13 |
import json
|
| 14 |
import aiohttp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
import pyreqwest_impersonate as pri
|
| 17 |
|
|
@@ -178,6 +185,7 @@ class WEBS:
|
|
| 178 |
verify=False,
|
| 179 |
)
|
| 180 |
self._exception_event = Event()
|
|
|
|
| 181 |
self._chat_vqd: str = ""
|
| 182 |
|
| 183 |
def __enter__(self) -> "WEBS":
|
|
@@ -226,7 +234,7 @@ class WEBS:
|
|
| 226 |
resp_content = self._get_url("POST", "https://duckduckgo.com", data={"q": keywords})
|
| 227 |
return _extract_vqd(resp_content, keywords)
|
| 228 |
|
| 229 |
-
def chat(self,
|
| 230 |
"""Initiates a chat session with DuckDuckGo AI.
|
| 231 |
|
| 232 |
Args:
|
|
@@ -241,7 +249,7 @@ class WEBS:
|
|
| 241 |
models = {
|
| 242 |
"claude-3-haiku": "claude-3-haiku-20240307",
|
| 243 |
"gpt-3.5": "gpt-3.5-turbo-0125",
|
| 244 |
-
"llama-3-70b": "meta-llama/Llama-3-
|
| 245 |
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 246 |
"gpt-4o-mini": "gpt-4o-mini",
|
| 247 |
}
|
|
@@ -250,9 +258,11 @@ class WEBS:
|
|
| 250 |
resp = self.client.get("https://duckduckgo.com/duckchat/v1/status", headers={"x-vqd-accept": "1"})
|
| 251 |
self._chat_vqd = resp.headers.get("x-vqd-4", "")
|
| 252 |
|
|
|
|
|
|
|
| 253 |
json_data = {
|
| 254 |
"model": models[model],
|
| 255 |
-
"messages":
|
| 256 |
}
|
| 257 |
resp = self.client.post(
|
| 258 |
"https://duckduckgo.com/duckchat/v1/chat",
|
|
@@ -264,6 +274,8 @@ class WEBS:
|
|
| 264 |
|
| 265 |
data = ",".join(x for line in resp.text.rstrip("[DONE]\n").split("data:") if (x := line.strip()))
|
| 266 |
result = "".join(x.get("message", "") for x in json_loads("[" + data + "]"))
|
|
|
|
|
|
|
| 267 |
return result
|
| 268 |
|
| 269 |
def text(
|
|
@@ -1169,15 +1181,8 @@ class WEBS:
|
|
| 1169 |
raise e
|
| 1170 |
|
| 1171 |
return results
|
| 1172 |
-
import requests
|
| 1173 |
-
import http.cookiejar as cookiejar
|
| 1174 |
-
import json
|
| 1175 |
-
from xml.etree import ElementTree
|
| 1176 |
-
import re
|
| 1177 |
-
import html.parser
|
| 1178 |
-
from typing import List, Dict, Union, Optional
|
| 1179 |
|
| 1180 |
-
html_parser = html.parser.HTMLParser()
|
| 1181 |
|
| 1182 |
|
| 1183 |
def unescape(string):
|
|
|
|
| 12 |
import asyncio
|
| 13 |
import json
|
| 14 |
import aiohttp
|
| 15 |
+
import requests
|
| 16 |
+
import http.cookiejar as cookiejar
|
| 17 |
+
import json
|
| 18 |
+
from xml.etree import ElementTree
|
| 19 |
+
import re
|
| 20 |
+
import html.parser
|
| 21 |
+
from typing import List, Dict, Union, Optional
|
| 22 |
|
| 23 |
import pyreqwest_impersonate as pri
|
| 24 |
|
|
|
|
| 185 |
verify=False,
|
| 186 |
)
|
| 187 |
self._exception_event = Event()
|
| 188 |
+
self._chat_messages: List[Dict[str, str]] = []
|
| 189 |
self._chat_vqd: str = ""
|
| 190 |
|
| 191 |
def __enter__(self) -> "WEBS":
|
|
|
|
| 234 |
resp_content = self._get_url("POST", "https://duckduckgo.com", data={"q": keywords})
|
| 235 |
return _extract_vqd(resp_content, keywords)
|
| 236 |
|
| 237 |
+
def chat(self, keywords: str, model: str = "gpt-3.5", timeout: int = 20) -> str:
|
| 238 |
"""Initiates a chat session with DuckDuckGo AI.
|
| 239 |
|
| 240 |
Args:
|
|
|
|
| 249 |
models = {
|
| 250 |
"claude-3-haiku": "claude-3-haiku-20240307",
|
| 251 |
"gpt-3.5": "gpt-3.5-turbo-0125",
|
| 252 |
+
"llama-3-70b": "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
|
| 253 |
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 254 |
"gpt-4o-mini": "gpt-4o-mini",
|
| 255 |
}
|
|
|
|
| 258 |
resp = self.client.get("https://duckduckgo.com/duckchat/v1/status", headers={"x-vqd-accept": "1"})
|
| 259 |
self._chat_vqd = resp.headers.get("x-vqd-4", "")
|
| 260 |
|
| 261 |
+
self._chat_messages.append({"role": "user", "content": keywords})
|
| 262 |
+
|
| 263 |
json_data = {
|
| 264 |
"model": models[model],
|
| 265 |
+
"messages": self._chat_messages,
|
| 266 |
}
|
| 267 |
resp = self.client.post(
|
| 268 |
"https://duckduckgo.com/duckchat/v1/chat",
|
|
|
|
| 274 |
|
| 275 |
data = ",".join(x for line in resp.text.rstrip("[DONE]\n").split("data:") if (x := line.strip()))
|
| 276 |
result = "".join(x.get("message", "") for x in json_loads("[" + data + "]"))
|
| 277 |
+
|
| 278 |
+
self._chat_messages.append({"role": "assistant", "content": result})
|
| 279 |
return result
|
| 280 |
|
| 281 |
def text(
|
|
|
|
| 1181 |
raise e
|
| 1182 |
|
| 1183 |
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1184 |
|
| 1185 |
+
html_parser = html.parser.HTMLParser()html_parser = html.parser.HTMLParser()
|
| 1186 |
|
| 1187 |
|
| 1188 |
def unescape(string):
|