webscout 5.7__py3-none-any.whl → 5.9__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/Amigo.py +265 -0
- webscout/Provider/ChatGPTES.py +239 -0
- webscout/Provider/Deepinfra.py +1 -1
- webscout/Provider/TTI/WebSimAI.py +142 -0
- webscout/Provider/TTI/__init__.py +5 -1
- webscout/Provider/TTI/aiforce.py +36 -13
- webscout/Provider/TTI/amigo.py +148 -0
- webscout/Provider/TTI/artbit.py +141 -0
- webscout/Provider/TTI/huggingface.py +155 -0
- webscout/Provider/TTS/__init__.py +2 -1
- webscout/Provider/TTS/parler.py +108 -0
- webscout/Provider/__init__.py +18 -0
- webscout/Provider/bixin.py +264 -0
- webscout/Provider/genspark.py +46 -43
- webscout/Provider/learnfastai.py +253 -0
- webscout/Provider/llamatutor.py +222 -0
- webscout/Provider/prefind.py +232 -0
- webscout/Provider/promptrefine.py +191 -0
- webscout/Provider/tutorai.py +354 -0
- webscout/Provider/twitterclone.py +260 -0
- webscout/__init__.py +1 -0
- webscout/version.py +1 -1
- {webscout-5.7.dist-info → webscout-5.9.dist-info}/METADATA +184 -89
- {webscout-5.7.dist-info → webscout-5.9.dist-info}/RECORD +30 -16
- {webscout-5.7.dist-info → webscout-5.9.dist-info}/LICENSE.md +0 -0
- {webscout-5.7.dist-info → webscout-5.9.dist-info}/WHEEL +0 -0
- {webscout-5.7.dist-info → webscout-5.9.dist-info}/entry_points.txt +0 -0
- {webscout-5.7.dist-info → webscout-5.9.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,265 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
import uuid
|
|
4
|
+
import os
|
|
5
|
+
from typing import Any, Dict, Optional, Generator
|
|
6
|
+
|
|
7
|
+
from webscout.AIutel import Optimizers
|
|
8
|
+
from webscout.AIutel import Conversation
|
|
9
|
+
from webscout.AIutel import AwesomePrompts, sanitize_stream
|
|
10
|
+
from webscout.AIbase import Provider, AsyncProvider
|
|
11
|
+
from webscout import exceptions
|
|
12
|
+
|
|
13
|
+
class AmigoChat(Provider):
|
|
14
|
+
"""
|
|
15
|
+
A class to interact with the AmigoChat.io API.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
AVAILABLE_MODELS = [
|
|
19
|
+
"meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo", # Llama 3
|
|
20
|
+
"o1-mini", # OpenAI O1 Mini
|
|
21
|
+
"claude-3-sonnet-20240229", # Claude Sonnet
|
|
22
|
+
"gemini-1.5-pro", # Gemini Pro
|
|
23
|
+
"gemini-1-5-flash", # Gemini Flash
|
|
24
|
+
"o1-preview", # OpenAI O1 Preview
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
is_conversation: bool = True,
|
|
30
|
+
max_tokens: int = 600,
|
|
31
|
+
timeout: int = 30,
|
|
32
|
+
intro: str = None,
|
|
33
|
+
filepath: str = None,
|
|
34
|
+
update_file: bool = True,
|
|
35
|
+
proxies: dict = {},
|
|
36
|
+
history_offset: int = 10250,
|
|
37
|
+
act: str = None,
|
|
38
|
+
model: str = "o1-preview", # Default model
|
|
39
|
+
):
|
|
40
|
+
"""
|
|
41
|
+
Initializes the AmigoChat.io API with given parameters.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
45
|
+
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
46
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
47
|
+
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
48
|
+
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
49
|
+
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
50
|
+
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
51
|
+
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
52
|
+
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
53
|
+
model (str, optional): The AI model to use for text generation. Defaults to "o1-preview".
|
|
54
|
+
Options: "llama-three-point-one", "openai-o-one-mini", "claude",
|
|
55
|
+
"gemini-1.5-pro", "gemini-1.5-flash", "openai-o-one".
|
|
56
|
+
"""
|
|
57
|
+
if model not in self.AVAILABLE_MODELS:
|
|
58
|
+
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
59
|
+
|
|
60
|
+
self.session = requests.Session()
|
|
61
|
+
self.is_conversation = is_conversation
|
|
62
|
+
self.max_tokens_to_sample = max_tokens
|
|
63
|
+
self.api_endpoint = "https://api.amigochat.io/v1/chat/completions"
|
|
64
|
+
self.stream_chunk_size = 64
|
|
65
|
+
self.timeout = timeout
|
|
66
|
+
self.last_response = {}
|
|
67
|
+
self.model = model
|
|
68
|
+
self.headers = {
|
|
69
|
+
"Accept": "*/*",
|
|
70
|
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
71
|
+
"Accept-Language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
72
|
+
"Authorization": "Bearer ", # empty
|
|
73
|
+
"Content-Type": "application/json",
|
|
74
|
+
"DNT": "1",
|
|
75
|
+
"Origin": "https://amigochat.io",
|
|
76
|
+
"Priority": "u=1, i",
|
|
77
|
+
"Referer": "https://amigochat.io/",
|
|
78
|
+
"Sec-CH-UA": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
|
|
79
|
+
"Sec-CH-UA-Mobile": "?0",
|
|
80
|
+
"Sec-CH-UA-Platform": '"Windows"',
|
|
81
|
+
"Sec-Fetch-Dest": "empty",
|
|
82
|
+
"Sec-Fetch-Mode": "cors",
|
|
83
|
+
"Sec-Fetch-Site": "same-site",
|
|
84
|
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
|
85
|
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
86
|
+
"Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
|
|
87
|
+
"X-Device-Language": "en-US",
|
|
88
|
+
"X-Device-Platform": "web",
|
|
89
|
+
"X-Device-UUID": str(uuid.uuid4()),
|
|
90
|
+
"X-Device-Version": "1.0.22"
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
self.__available_optimizers = (
|
|
94
|
+
method
|
|
95
|
+
for method in dir(Optimizers)
|
|
96
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
97
|
+
)
|
|
98
|
+
self.session.headers.update(self.headers)
|
|
99
|
+
Conversation.intro = (
|
|
100
|
+
AwesomePrompts().get_act(
|
|
101
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
102
|
+
)
|
|
103
|
+
if act
|
|
104
|
+
else intro or Conversation.intro
|
|
105
|
+
)
|
|
106
|
+
self.conversation = Conversation(
|
|
107
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
108
|
+
)
|
|
109
|
+
self.conversation.history_offset = history_offset
|
|
110
|
+
self.session.proxies = proxies
|
|
111
|
+
|
|
112
|
+
def ask(
|
|
113
|
+
self,
|
|
114
|
+
prompt: str,
|
|
115
|
+
stream: bool = False,
|
|
116
|
+
raw: bool = False,
|
|
117
|
+
optimizer: str = None,
|
|
118
|
+
conversationally: bool = False,
|
|
119
|
+
) -> Dict[str, Any]:
|
|
120
|
+
"""Chat with AI
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
prompt (str): Prompt to be send.
|
|
124
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
125
|
+
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
126
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
127
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
128
|
+
Returns:
|
|
129
|
+
dict : {}
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"text" : "How may I assist you today?"
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
"""
|
|
136
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
137
|
+
if optimizer:
|
|
138
|
+
if optimizer in self.__available_optimizers:
|
|
139
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
140
|
+
conversation_prompt if conversationally else prompt
|
|
141
|
+
)
|
|
142
|
+
else:
|
|
143
|
+
raise Exception(
|
|
144
|
+
f"Optimizer is not one of {self.__available_optimizers}"
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Define the payload
|
|
148
|
+
payload = {
|
|
149
|
+
"messages": [
|
|
150
|
+
{"role": "system", "content": "Mai hu ba khabr"},
|
|
151
|
+
{"role": "user", "content": conversation_prompt}
|
|
152
|
+
],
|
|
153
|
+
"model": self.model,
|
|
154
|
+
"frequency_penalty": 0,
|
|
155
|
+
"max_tokens": 4000,
|
|
156
|
+
"presence_penalty": 0,
|
|
157
|
+
"stream": stream, # Enable streaming
|
|
158
|
+
"temperature": 0.5,
|
|
159
|
+
"top_p": 0.95
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
def for_stream():
|
|
163
|
+
try:
|
|
164
|
+
# Make the POST request with streaming enabled
|
|
165
|
+
with requests.post(self.api_endpoint, headers=self.headers, json=payload, stream=True) as response:
|
|
166
|
+
# Check if the request was successful
|
|
167
|
+
if response.status_code == 201:
|
|
168
|
+
# Iterate over the streamed response line by line
|
|
169
|
+
for line in response.iter_lines():
|
|
170
|
+
if line:
|
|
171
|
+
# Decode the line from bytes to string
|
|
172
|
+
decoded_line = line.decode('utf-8').strip()
|
|
173
|
+
if decoded_line.startswith("data: "):
|
|
174
|
+
data_str = decoded_line[6:]
|
|
175
|
+
if data_str == "[DONE]":
|
|
176
|
+
break
|
|
177
|
+
try:
|
|
178
|
+
# Load the JSON data
|
|
179
|
+
data_json = json.loads(data_str)
|
|
180
|
+
|
|
181
|
+
# Extract the content from the response
|
|
182
|
+
choices = data_json.get("choices", [])
|
|
183
|
+
if choices:
|
|
184
|
+
delta = choices[0].get("delta", {})
|
|
185
|
+
content = delta.get("content", "")
|
|
186
|
+
if content:
|
|
187
|
+
yield content if raw else dict(text=content)
|
|
188
|
+
except json.JSONDecodeError:
|
|
189
|
+
print(f"Received non-JSON data: {data_str}")
|
|
190
|
+
else:
|
|
191
|
+
print(f"Request failed with status code {response.status_code}")
|
|
192
|
+
print("Response:", response.text)
|
|
193
|
+
|
|
194
|
+
except requests.exceptions.RequestException as e:
|
|
195
|
+
print("An error occurred while making the request:", e)
|
|
196
|
+
|
|
197
|
+
def for_non_stream():
|
|
198
|
+
# Accumulate the streaming response
|
|
199
|
+
full_response = ""
|
|
200
|
+
for chunk in for_stream():
|
|
201
|
+
if not raw: # If not raw, chunk is a dictionary
|
|
202
|
+
full_response += chunk["text"]
|
|
203
|
+
|
|
204
|
+
# Update self.last_response with the full text
|
|
205
|
+
self.last_response.update(dict(text=full_response))
|
|
206
|
+
self.conversation.update_chat_history(
|
|
207
|
+
prompt, self.get_message(self.last_response)
|
|
208
|
+
)
|
|
209
|
+
return self.last_response
|
|
210
|
+
|
|
211
|
+
return for_stream() if stream else for_non_stream()
|
|
212
|
+
|
|
213
|
+
def chat(
|
|
214
|
+
self,
|
|
215
|
+
prompt: str,
|
|
216
|
+
stream: bool = False,
|
|
217
|
+
optimizer: str = None,
|
|
218
|
+
conversationally: bool = False,
|
|
219
|
+
) -> str:
|
|
220
|
+
"""Generate response `str`
|
|
221
|
+
Args:
|
|
222
|
+
prompt (str): Prompt to be send.
|
|
223
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
224
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
225
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
226
|
+
Returns:
|
|
227
|
+
str: Response generated
|
|
228
|
+
"""
|
|
229
|
+
|
|
230
|
+
def for_stream():
|
|
231
|
+
for response in self.ask(
|
|
232
|
+
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
233
|
+
):
|
|
234
|
+
yield self.get_message(response)
|
|
235
|
+
|
|
236
|
+
def for_non_stream():
|
|
237
|
+
return self.get_message(
|
|
238
|
+
self.ask(
|
|
239
|
+
prompt,
|
|
240
|
+
False,
|
|
241
|
+
optimizer=optimizer,
|
|
242
|
+
conversationally=conversationally,
|
|
243
|
+
)
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
return for_stream() if stream else for_non_stream()
|
|
247
|
+
|
|
248
|
+
def get_message(self, response: dict) -> str:
|
|
249
|
+
"""Retrieves message only from response
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
response (dict): Response generated by `self.ask`
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
str: Message extracted
|
|
256
|
+
"""
|
|
257
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
258
|
+
return response["text"]
|
|
259
|
+
|
|
260
|
+
if __name__ == '__main__':
|
|
261
|
+
from rich import print
|
|
262
|
+
ai = AmigoChat(model="o1-preview")
|
|
263
|
+
response = ai.chat(input(">>> "))
|
|
264
|
+
for chunk in response:
|
|
265
|
+
print(chunk, end="", flush=True)
|