webscout 5.6__py3-none-any.whl → 5.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of webscout might be problematic. Click here for more details.
- webscout/AIutel.py +76 -2
- webscout/Agents/Onlinesearcher.py +123 -115
- webscout/Provider/ChatGPTES.py +239 -0
- webscout/Provider/Deepinfra.py +1 -1
- webscout/Provider/TTI/__init__.py +3 -1
- webscout/Provider/TTI/aiforce.py +36 -13
- webscout/Provider/TTI/artbit.py +141 -0
- webscout/Provider/TTI/huggingface.py +155 -0
- webscout/Provider/__init__.py +13 -0
- webscout/Provider/bixin.py +264 -0
- webscout/Provider/genspark.py +46 -43
- webscout/Provider/llamatutor.py +222 -0
- webscout/Provider/meta.py +1 -1
- webscout/Provider/promptrefine.py +191 -0
- webscout/Provider/tutorai.py +354 -0
- webscout/Provider/twitterclone.py +260 -0
- webscout/__init__.py +2 -0
- webscout/requestsHTMLfix.py +775 -0
- webscout/version.py +1 -1
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/METADATA +4 -3
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/RECORD +25 -16
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/LICENSE.md +0 -0
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/WHEEL +0 -0
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/entry_points.txt +0 -0
- {webscout-5.6.dist-info → webscout-5.8.dist-info}/top_level.txt +0 -0
webscout/AIutel.py
CHANGED
|
@@ -3,6 +3,8 @@ import json
|
|
|
3
3
|
import platform
|
|
4
4
|
import subprocess
|
|
5
5
|
import logging
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
6
8
|
import appdirs
|
|
7
9
|
import datetime
|
|
8
10
|
import re
|
|
@@ -11,7 +13,7 @@ import click
|
|
|
11
13
|
from rich.markdown import Markdown
|
|
12
14
|
from rich.console import Console
|
|
13
15
|
import g4f
|
|
14
|
-
from typing import Union
|
|
16
|
+
from typing import List, Tuple, Union
|
|
15
17
|
from typing import NoReturn
|
|
16
18
|
import requests
|
|
17
19
|
from pathlib import Path
|
|
@@ -1053,4 +1055,76 @@ class Audio:
|
|
|
1053
1055
|
"""
|
|
1054
1056
|
if not Path(path_to_audio_file).is_file():
|
|
1055
1057
|
raise FileNotFoundError(f"File does not exist - '{path_to_audio_file}'")
|
|
1056
|
-
playsound(path_to_audio_file)
|
|
1058
|
+
playsound(path_to_audio_file)
|
|
1059
|
+
class ProxyManager:
|
|
1060
|
+
def __init__(self, refresh_interval=60):
|
|
1061
|
+
self.proxies: List[Tuple[str, float]] = [] # Store proxy and its latency
|
|
1062
|
+
self.last_refresh: float = 0
|
|
1063
|
+
self.refresh_interval = refresh_interval
|
|
1064
|
+
self.lock = threading.Lock() # Add a lock for thread safety
|
|
1065
|
+
# Start auto-refresh in a separate thread
|
|
1066
|
+
threading.Thread(target=self.auto_refresh_proxies, daemon=True).start()
|
|
1067
|
+
|
|
1068
|
+
def fetch_proxies(self, max_proxies=50) -> List[str]:
|
|
1069
|
+
try:
|
|
1070
|
+
url = "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all"
|
|
1071
|
+
response = requests.get(url)
|
|
1072
|
+
proxies = response.text.split('\r\n')[:max_proxies] # Extract up to max_proxies
|
|
1073
|
+
return [proxy for proxy in proxies if proxy]
|
|
1074
|
+
except requests.RequestException as e:
|
|
1075
|
+
print(f"Error fetching proxies: {e}")
|
|
1076
|
+
return []
|
|
1077
|
+
|
|
1078
|
+
def test_proxy(self, proxy: str) -> Tuple[str, float] | None:
|
|
1079
|
+
# Test both HTTP and HTTPS
|
|
1080
|
+
for protocol in ['http', 'https']:
|
|
1081
|
+
try:
|
|
1082
|
+
start_time = time.time()
|
|
1083
|
+
response = requests.get('http://httpbin.org/ip', proxies={protocol: f"{protocol}://{proxy}"}, timeout=5)
|
|
1084
|
+
if response.status_code == 200:
|
|
1085
|
+
end_time = time.time()
|
|
1086
|
+
return proxy, end_time - start_time
|
|
1087
|
+
except requests.RequestException:
|
|
1088
|
+
pass
|
|
1089
|
+
return None
|
|
1090
|
+
|
|
1091
|
+
def refresh_proxies(self) -> int:
|
|
1092
|
+
new_proxies = self.fetch_proxies()
|
|
1093
|
+
threads = []
|
|
1094
|
+
working_proxies = []
|
|
1095
|
+
|
|
1096
|
+
# Use threading for faster proxy testing
|
|
1097
|
+
for proxy in new_proxies:
|
|
1098
|
+
thread = threading.Thread(target=self.test_proxy_and_append, args=(proxy, working_proxies))
|
|
1099
|
+
threads.append(thread)
|
|
1100
|
+
thread.start()
|
|
1101
|
+
|
|
1102
|
+
# Wait for all threads to complete
|
|
1103
|
+
for thread in threads:
|
|
1104
|
+
thread.join()
|
|
1105
|
+
|
|
1106
|
+
with self.lock: # Acquire lock before updating proxies list
|
|
1107
|
+
self.proxies = working_proxies
|
|
1108
|
+
self.last_refresh = time.time()
|
|
1109
|
+
|
|
1110
|
+
# print(f"Refreshed proxies at {datetime.now()}. Total working proxies: {len(self.proxies)}")
|
|
1111
|
+
return len(self.proxies)
|
|
1112
|
+
|
|
1113
|
+
def test_proxy_and_append(self, proxy: str, working_proxies: list):
|
|
1114
|
+
result = self.test_proxy(proxy)
|
|
1115
|
+
if result:
|
|
1116
|
+
with self.lock: # Acquire lock before appending to shared list
|
|
1117
|
+
working_proxies.append(result) # Append the proxy and its latency
|
|
1118
|
+
|
|
1119
|
+
def auto_refresh_proxies(self):
|
|
1120
|
+
while True:
|
|
1121
|
+
time.sleep(self.refresh_interval)
|
|
1122
|
+
self.refresh_proxies()
|
|
1123
|
+
|
|
1124
|
+
def get_fastest_proxy(self) -> str | None:
|
|
1125
|
+
with self.lock: # Acquire lock before accessing proxies list
|
|
1126
|
+
if self.proxies:
|
|
1127
|
+
# Sort proxies by latency and return the fastest
|
|
1128
|
+
self.proxies.sort(key=lambda x: x[1]) # Sort by latency
|
|
1129
|
+
return self.proxies[0][0] # Return the fastest proxy
|
|
1130
|
+
return None
|
|
@@ -1,162 +1,170 @@
|
|
|
1
1
|
import json
|
|
2
|
-
import colorlog
|
|
3
|
-
from webscout import WEBS
|
|
4
|
-
from webscout import DeepInfra
|
|
5
2
|
import httpx
|
|
6
3
|
from bs4 import BeautifulSoup
|
|
7
4
|
from typing import List, Dict
|
|
8
|
-
import
|
|
5
|
+
from webscout import WEBS, GEMINIAPI
|
|
6
|
+
import re
|
|
7
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
8
|
+
|
|
9
9
|
|
|
10
10
|
class WebSearchAgent:
|
|
11
11
|
def __init__(self):
|
|
12
12
|
self.webs = WEBS()
|
|
13
|
-
self.ai =
|
|
14
|
-
|
|
15
|
-
def generate_search_queries(self, information, num_queries=
|
|
16
|
-
prompt = f"""
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Your
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
{information}
|
|
43
|
-
|
|
44
|
-
Now, generate the optimal search queries:
|
|
45
|
-
"""
|
|
13
|
+
self.ai = GEMINIAPI(is_conversation=False, api_key='AIzaSyAYlT5-V0MXZwaLYpXCF1Z-Yvy_tx1jylA')
|
|
14
|
+
|
|
15
|
+
def generate_search_queries(self, information: str, num_queries: int = 10) -> List[str]:
|
|
16
|
+
prompt = f""" Task: Generate exactly {num_queries} optimal search queries based on the given information.
|
|
17
|
+
Instructions:
|
|
18
|
+
1. Analyze the provided information thoroughly.
|
|
19
|
+
2. Identify key concepts, entities, and relationships.
|
|
20
|
+
3. Formulate {num_queries} concise and specific search queries that will yield relevant and diverse results.
|
|
21
|
+
4. Each query should focus on a different aspect or angle of the information.
|
|
22
|
+
5. The queries should be in natural language, not in the form of keywords.
|
|
23
|
+
6. Avoid unnecessary words or phrases that might limit the search results.
|
|
24
|
+
7. **Important**: Return the response **ONLY** in JSON format without any additional text or code blocks.
|
|
25
|
+
Your response must be in the following JSON format: {{
|
|
26
|
+
"search_queries": [
|
|
27
|
+
"Your first search query here",
|
|
28
|
+
"Your second search query here",
|
|
29
|
+
"...",
|
|
30
|
+
"Your last search query here"
|
|
31
|
+
]
|
|
32
|
+
}}
|
|
33
|
+
Ensure that:
|
|
34
|
+
- You provide exactly {num_queries} search queries.
|
|
35
|
+
- Each query is unique and focuses on a different aspect of the information.
|
|
36
|
+
- The queries are in plain text, suitable for a web search engine.
|
|
37
|
+
|
|
38
|
+
Information to base the search queries on:
|
|
39
|
+
{information}
|
|
40
|
+
|
|
41
|
+
Now, generate the optimal search queries: """
|
|
46
42
|
|
|
47
43
|
response = ""
|
|
48
44
|
for chunk in self.ai.chat(prompt):
|
|
49
45
|
response += chunk
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
47
|
+
json_match = re.search(r'\{.*\}', response, re.DOTALL)
|
|
48
|
+
if json_match:
|
|
49
|
+
json_str = json_match.group(0)
|
|
50
|
+
try:
|
|
51
|
+
json_response = json.loads(json_str)
|
|
52
|
+
print(json_response['search_queries'])
|
|
53
|
+
return json_response["search_queries"]
|
|
54
|
+
except json.JSONDecodeError:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
queries = re.findall(r'"([^"]+)"', response)
|
|
58
|
+
if len(queries) >= num_queries:
|
|
59
|
+
return queries[:num_queries]
|
|
60
|
+
elif queries:
|
|
61
|
+
return queries
|
|
62
|
+
else:
|
|
63
|
+
return [information]
|
|
64
|
+
|
|
65
|
+
def search(self, information: str, region: str = 'wt-wt', safesearch: str = 'off',
|
|
66
|
+
timelimit: str = 'y', max_results: int = 10) -> List[Dict]:
|
|
67
|
+
search_queries = self.generate_search_queries(information, num_queries=10)
|
|
69
68
|
all_results = []
|
|
70
|
-
|
|
69
|
+
|
|
71
70
|
for query in search_queries:
|
|
72
71
|
results = []
|
|
73
72
|
with self.webs as webs:
|
|
74
|
-
for result in webs.text(query, region=region, safesearch=safesearch,
|
|
73
|
+
for result in webs.text(query, region=region, safesearch=safesearch,
|
|
74
|
+
timelimit=timelimit, max_results=max_results):
|
|
75
75
|
results.append(result)
|
|
76
76
|
all_results.extend(results)
|
|
77
|
-
|
|
77
|
+
|
|
78
78
|
return all_results
|
|
79
79
|
|
|
80
|
-
def extract_urls(self, results):
|
|
81
|
-
urls = []
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if url:
|
|
85
|
-
urls.append(url)
|
|
86
|
-
return list(set(urls))
|
|
80
|
+
def extract_urls(self, results: List[Dict]) -> List[str]:
|
|
81
|
+
urls = [result.get('href') for result in results if result.get('href')]
|
|
82
|
+
unique_urls = list(set(urls))
|
|
83
|
+
return unique_urls
|
|
87
84
|
|
|
88
|
-
def fetch_webpage(self, url: str) -> str:
|
|
85
|
+
def fetch_webpage(self, url: str) -> Dict[str, str]:
|
|
89
86
|
try:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
with httpx.Client(timeout=120) as client:
|
|
88
|
+
response = client.get(url)
|
|
89
|
+
if response.status_code == 200:
|
|
90
|
+
html = response.text
|
|
91
|
+
soup = BeautifulSoup(html, 'html.parser')
|
|
92
|
+
paragraphs = soup.find_all('p')
|
|
93
|
+
text = ' '.join([p.get_text() for p in paragraphs])
|
|
94
|
+
words = text.split()
|
|
95
|
+
if len(words) > 600:
|
|
96
|
+
text = ' '.join(words[:600]) + '...'
|
|
97
|
+
return {"url": url, "content": text}
|
|
98
|
+
else:
|
|
99
|
+
return {"url": url, "content": f"Failed to fetch {url}: HTTP {response.status_code}"}
|
|
102
100
|
except Exception as e:
|
|
103
|
-
return f"Error fetching {url}: {str(e)}"
|
|
101
|
+
return {"url": url, "content": f"Error fetching {url}: {str(e)}"}
|
|
104
102
|
|
|
105
|
-
def fetch_all_webpages(self, urls: List[str]) -> List[Dict[str, str]]:
|
|
103
|
+
def fetch_all_webpages(self, urls: List[str], max_workers: int = 10) -> List[Dict[str, str]]:
|
|
106
104
|
contents = []
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
|
106
|
+
future_to_url = {executor.submit(self.fetch_webpage, url): url for url in urls}
|
|
107
|
+
for future in as_completed(future_to_url):
|
|
108
|
+
result = future.result()
|
|
109
|
+
contents.append(result)
|
|
110
110
|
return contents
|
|
111
111
|
|
|
112
|
+
|
|
112
113
|
class OnlineSearcher:
|
|
113
114
|
def __init__(self):
|
|
114
115
|
self.agent = WebSearchAgent()
|
|
115
|
-
self.ai =
|
|
116
|
+
self.ai = GEMINIAPI(is_conversation=False, api_key='AIzaSyAYlT5-V0MXZwaLYpXCF1Z-Yvy_tx1jylA')
|
|
116
117
|
|
|
117
|
-
def answer_question(self, question: str):
|
|
118
|
-
search_results = self.agent.search(question)
|
|
118
|
+
def answer_question(self, question: str) -> None:
|
|
119
|
+
search_results = self.agent.search(question, max_results=10)
|
|
119
120
|
urls = self.agent.extract_urls(search_results)
|
|
120
121
|
webpage_contents = self.agent.fetch_all_webpages(urls)
|
|
121
122
|
|
|
122
123
|
context = "Web search results and extracted content:\n\n"
|
|
123
124
|
for i, result in enumerate(search_results, 1):
|
|
124
|
-
|
|
125
|
+
title = result.get('title', 'No Title')
|
|
126
|
+
href = result.get('href', 'No URL')
|
|
127
|
+
snippet = result.get('body', 'No Snippet')
|
|
128
|
+
context += f"{i}. **Title:** {title}\n **URL:** {href}\n **Snippet:** {snippet}\n\n"
|
|
125
129
|
|
|
126
130
|
context += "Extracted webpage contents:\n"
|
|
127
|
-
for i, webpage in enumerate(webpage_contents):
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
for i, webpage in enumerate(webpage_contents, 1):
|
|
132
|
+
content = webpage['content']
|
|
133
|
+
content_preview = content[:600] + '...' if len(content) > 600 else content
|
|
134
|
+
context += f"{i}. **URL:** {webpage['url']}\n **Content:** {content_preview}\n\n"
|
|
135
|
+
|
|
136
|
+
prompt = f""" Task: Provide a comprehensive, insightful, and well-structured answer to the given question based on the provided web search results and your general knowledge.
|
|
137
|
+
Question: {question}
|
|
138
|
+
Context: {context}
|
|
139
|
+
Instructions:
|
|
140
|
+
1. Carefully analyze the provided web search results and extracted content.
|
|
141
|
+
2. Synthesize the information to form a coherent and comprehensive answer.
|
|
142
|
+
3. If the search results contain relevant information, incorporate it into your answer seamlessly.
|
|
143
|
+
4. Avoid providing irrelevant information, and do not reference "according to web page".
|
|
144
|
+
5. If the search results don't contain sufficient information, clearly state this and provide the best answer based on your general knowledge.
|
|
145
|
+
6. Ensure your answer is well-structured, factual, and directly addresses the question.
|
|
146
|
+
7. Use clear headings, bullet points, or other formatting tools to enhance readability where appropriate.
|
|
147
|
+
8. Strive for a tone and style similar to that of professional, authoritative sources like Perplexity, ensuring clarity and depth in your response.
|
|
148
|
+
Your response should be informative, accurate, and properly sourced when possible. Begin your answer now: """
|
|
149
|
+
|
|
150
|
+
for chunk in self.ai.chat(prompt, stream=True):
|
|
151
|
+
print(chunk, end='', flush=True) # Print each chunk in real-time
|
|
132
152
|
|
|
133
|
-
Question: {question}
|
|
134
153
|
|
|
135
|
-
Context:
|
|
136
|
-
{context}
|
|
137
|
-
|
|
138
|
-
Instructions:
|
|
139
|
-
1. Carefully analyze the provided web search results and extracted content.
|
|
140
|
-
2. Synthesize the information to form a coherent and comprehensive answer.
|
|
141
|
-
3. If the search results contain relevant information, incorporate it into your answer.
|
|
142
|
-
4. Don't provide irrelevant information, and don't say "according to web page".
|
|
143
|
-
5. If the search results don't contain sufficient information, clearly state this and provide the best answer based on your general knowledge.
|
|
144
|
-
6. Ensure your answer is well-structured, factual, and directly addresses the question.
|
|
145
|
-
7. If appropriate, provide additional context or related information that might be helpful.
|
|
146
|
-
|
|
147
|
-
Your response should be informative, accurate, and properly sourced when possible. Begin your answer now:
|
|
148
|
-
"""
|
|
149
|
-
|
|
150
|
-
return self.ai.chat(prompt)
|
|
151
154
|
|
|
152
155
|
# Usage example
|
|
153
156
|
if __name__ == "__main__":
|
|
154
157
|
assistant = OnlineSearcher()
|
|
155
158
|
while True:
|
|
156
|
-
|
|
157
|
-
|
|
159
|
+
try:
|
|
160
|
+
question = input(">>> ")
|
|
161
|
+
if question.lower() == 'quit':
|
|
162
|
+
break
|
|
163
|
+
print("=" * 50)
|
|
164
|
+
assistant.answer_question(question) # The answer is printed in real-time
|
|
165
|
+
print("=" * 50)
|
|
166
|
+
except KeyboardInterrupt:
|
|
167
|
+
print("\nExiting.")
|
|
158
168
|
break
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
print(chunk, end="", flush=True)
|
|
162
|
-
print("\n" + "="*50)
|
|
169
|
+
except Exception as e:
|
|
170
|
+
print(f"An error occurred: {e}")
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import re
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from typing import List, Dict
|
|
6
|
+
from webscout.AIutel import Optimizers, Conversation, AwesomePrompts
|
|
7
|
+
from webscout.AIbase import Provider
|
|
8
|
+
from webscout import exceptions
|
|
9
|
+
from rich import print
|
|
10
|
+
|
|
11
|
+
class ChatGPTES(Provider):
|
|
12
|
+
"""
|
|
13
|
+
A class to interact with the ChatGPT.es API.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
SUPPORTED_MODELS = {
|
|
17
|
+
'gpt-4o', 'gpt-4o-mini', 'chatgpt-4o-latest'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
is_conversation: bool = True,
|
|
23
|
+
max_tokens: int = 600,
|
|
24
|
+
timeout: int = 30,
|
|
25
|
+
intro: str = None,
|
|
26
|
+
filepath: str = None,
|
|
27
|
+
update_file: bool = True,
|
|
28
|
+
proxies: dict = {},
|
|
29
|
+
history_offset: int = 10250,
|
|
30
|
+
act: str = None,
|
|
31
|
+
model: str = "chatgpt-4o-latest", # Default model
|
|
32
|
+
system_prompt: str = "You are a helpful assistant.",
|
|
33
|
+
):
|
|
34
|
+
"""
|
|
35
|
+
Initializes the ChatGPT.es API with given parameters.
|
|
36
|
+
"""
|
|
37
|
+
if model not in self.SUPPORTED_MODELS:
|
|
38
|
+
raise ValueError(f"Unsupported model: {model}. Choose from: {self.SUPPORTED_MODELS}")
|
|
39
|
+
|
|
40
|
+
self.session = requests.Session()
|
|
41
|
+
self.is_conversation = is_conversation
|
|
42
|
+
self.max_tokens_to_sample = max_tokens
|
|
43
|
+
self.api_endpoint = 'https://chatgpt.es/wp-admin/admin-ajax.php'
|
|
44
|
+
self.stream_chunk_size = 64
|
|
45
|
+
self.timeout = timeout
|
|
46
|
+
self.last_response = {}
|
|
47
|
+
self.system_prompt = system_prompt
|
|
48
|
+
self.model = model
|
|
49
|
+
self.initial_headers = {
|
|
50
|
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
|
|
51
|
+
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
|
|
52
|
+
'Referer': 'https://www.google.com/',
|
|
53
|
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,'
|
|
54
|
+
'image/avif,image/webp,image/apng,*/*;q=0.8,'
|
|
55
|
+
'application/signed-exchange;v=b3;q=0.7',
|
|
56
|
+
}
|
|
57
|
+
self.post_headers = {
|
|
58
|
+
'User-Agent': self.initial_headers['User-Agent'],
|
|
59
|
+
'Referer': 'https://chatgpt.es/',
|
|
60
|
+
'Origin': 'https://chatgpt.es',
|
|
61
|
+
'Accept': '*/*',
|
|
62
|
+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
63
|
+
}
|
|
64
|
+
self.nonce = None
|
|
65
|
+
self.post_id = None
|
|
66
|
+
|
|
67
|
+
self.__available_optimizers = (
|
|
68
|
+
method
|
|
69
|
+
for method in dir(Optimizers)
|
|
70
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Conversation setup
|
|
74
|
+
Conversation.intro = (
|
|
75
|
+
AwesomePrompts().get_act(
|
|
76
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
77
|
+
)
|
|
78
|
+
if act
|
|
79
|
+
else intro or Conversation.intro
|
|
80
|
+
)
|
|
81
|
+
self.conversation = Conversation(
|
|
82
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
83
|
+
)
|
|
84
|
+
self.conversation.history_offset = history_offset
|
|
85
|
+
self.session.proxies = proxies
|
|
86
|
+
|
|
87
|
+
def get_nonce_and_post_id(self):
|
|
88
|
+
"""
|
|
89
|
+
Retrieves the nonce and post ID from the ChatGPT.es website.
|
|
90
|
+
"""
|
|
91
|
+
try:
|
|
92
|
+
response = self.session.get('https://chatgpt.es/', headers=self.initial_headers, timeout=self.timeout)
|
|
93
|
+
response.raise_for_status()
|
|
94
|
+
except requests.RequestException as e:
|
|
95
|
+
raise ConnectionError(f"Failed to retrieve nonce and post_id: {e}")
|
|
96
|
+
|
|
97
|
+
nonce_match = re.search(r'data-nonce="(.+?)"', response.text)
|
|
98
|
+
post_id_match = re.search(r'data-post-id="(.+?)"', response.text)
|
|
99
|
+
|
|
100
|
+
if not nonce_match or not post_id_match:
|
|
101
|
+
raise ValueError("Failed to parse nonce or post_id from the response.")
|
|
102
|
+
|
|
103
|
+
self.nonce = nonce_match.group(1)
|
|
104
|
+
self.post_id = post_id_match.group(1)
|
|
105
|
+
|
|
106
|
+
def ask(
|
|
107
|
+
self,
|
|
108
|
+
prompt: str,
|
|
109
|
+
stream: bool = False,
|
|
110
|
+
raw: bool = False,
|
|
111
|
+
optimizer: str = None,
|
|
112
|
+
conversationally: bool = False,
|
|
113
|
+
) -> dict:
|
|
114
|
+
"""
|
|
115
|
+
Chat with ChatGPT.es
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
prompt (str): Prompt to be sent.
|
|
119
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
120
|
+
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
121
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
122
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
dict: Response dictionary.
|
|
126
|
+
"""
|
|
127
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
128
|
+
if optimizer:
|
|
129
|
+
if optimizer in self.__available_optimizers:
|
|
130
|
+
optimizer_func = getattr(Optimizers, optimizer)
|
|
131
|
+
conversation_prompt = optimizer_func(
|
|
132
|
+
conversation_prompt if conversationally else prompt
|
|
133
|
+
)
|
|
134
|
+
else:
|
|
135
|
+
raise ValueError(f"Optimizer '{optimizer}' is not supported. "
|
|
136
|
+
f"Available optimizers: {list(self.__available_optimizers)}")
|
|
137
|
+
|
|
138
|
+
# Retrieve nonce and post_id if they are not set
|
|
139
|
+
if not self.nonce or not self.post_id:
|
|
140
|
+
self.get_nonce_and_post_id()
|
|
141
|
+
|
|
142
|
+
messages = [
|
|
143
|
+
{"role": "user", "content": conversation_prompt},
|
|
144
|
+
]
|
|
145
|
+
|
|
146
|
+
# Prepare conversation history
|
|
147
|
+
conversation = ["Human: strictly respond in the same language as my prompt, preferably English"]
|
|
148
|
+
for msg in messages:
|
|
149
|
+
role = "Human" if msg['role'] == "user" else "AI"
|
|
150
|
+
conversation.append(f"{role}: {msg['content']}")
|
|
151
|
+
|
|
152
|
+
payload = {
|
|
153
|
+
'_wpnonce': self.nonce,
|
|
154
|
+
'post_id': self.post_id,
|
|
155
|
+
'url': 'https://chatgpt.es',
|
|
156
|
+
'action': 'wpaicg_chat_shortcode_message',
|
|
157
|
+
'message': messages[-1]['content'],
|
|
158
|
+
'bot_id': '0',
|
|
159
|
+
'chatbot_identity': 'shortcode',
|
|
160
|
+
'wpaicg_chat_client_id': os.urandom(5).hex(),
|
|
161
|
+
'wpaicg_chat_history': json.dumps(conversation)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
try:
|
|
165
|
+
response = self.session.post(
|
|
166
|
+
self.api_endpoint,
|
|
167
|
+
headers=self.post_headers,
|
|
168
|
+
data=payload,
|
|
169
|
+
timeout=self.timeout
|
|
170
|
+
)
|
|
171
|
+
response.raise_for_status()
|
|
172
|
+
except requests.RequestException as e:
|
|
173
|
+
raise ConnectionError(f"Failed to send request to ChatGPT.es: {e}")
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
response_data = response.json()
|
|
177
|
+
except json.JSONDecodeError:
|
|
178
|
+
raise ValueError(f"Invalid JSON response: {response.text}")
|
|
179
|
+
|
|
180
|
+
if not isinstance(response_data, dict):
|
|
181
|
+
raise TypeError(f"Expected response_data to be a dict, got {type(response_data)}")
|
|
182
|
+
|
|
183
|
+
# Extract the message directly from the 'data' key
|
|
184
|
+
message = response_data.get('data')
|
|
185
|
+
if not isinstance(message, str):
|
|
186
|
+
raise KeyError("Missing 'data' key in response or 'data' is not a string")
|
|
187
|
+
|
|
188
|
+
self.last_response.update(dict(text=message))
|
|
189
|
+
self.conversation.update_chat_history(
|
|
190
|
+
prompt, self.get_message(self.last_response)
|
|
191
|
+
)
|
|
192
|
+
return self.last_response
|
|
193
|
+
|
|
194
|
+
def chat(
|
|
195
|
+
self,
|
|
196
|
+
prompt: str,
|
|
197
|
+
stream: bool = False,
|
|
198
|
+
optimizer: str = None,
|
|
199
|
+
conversationally: bool = False,
|
|
200
|
+
) -> str:
|
|
201
|
+
"""
|
|
202
|
+
Generate response as a string.
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
prompt (str): Prompt to be sent.
|
|
206
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
207
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
208
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
str: Response generated.
|
|
212
|
+
"""
|
|
213
|
+
response = self.ask(
|
|
214
|
+
prompt,
|
|
215
|
+
stream=stream,
|
|
216
|
+
optimizer=optimizer,
|
|
217
|
+
conversationally=conversationally,
|
|
218
|
+
)
|
|
219
|
+
return self.get_message(response)
|
|
220
|
+
|
|
221
|
+
def get_message(self, response: dict) -> str:
|
|
222
|
+
"""
|
|
223
|
+
Retrieves message only from response.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
response (dict): Response generated by `self.ask`.
|
|
227
|
+
|
|
228
|
+
Returns:
|
|
229
|
+
str: Message extracted.
|
|
230
|
+
"""
|
|
231
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
232
|
+
return response["text"]
|
|
233
|
+
|
|
234
|
+
if __name__ == "__main__":
|
|
235
|
+
ai = ChatGPTES()
|
|
236
|
+
response = ai.chat(input(">>> "))
|
|
237
|
+
for chunk in response:
|
|
238
|
+
print(chunk, end="", flush=True)
|
|
239
|
+
|
webscout/Provider/Deepinfra.py
CHANGED
|
@@ -21,7 +21,7 @@ class DeepInfra(Provider):
|
|
|
21
21
|
proxies: dict = {},
|
|
22
22
|
history_offset: int = 10250,
|
|
23
23
|
act: str = None,
|
|
24
|
-
model: str = "Qwen/Qwen2-72B-Instruct",
|
|
24
|
+
model: str = "Qwen/Qwen2.5-72B-Instruct",
|
|
25
25
|
system_prompt: str = "You are a Helpful AI."
|
|
26
26
|
):
|
|
27
27
|
"""Instantiates DeepInfra
|