webscout 7.9__py3-none-any.whl → 8.0__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/Extra/GitToolkit/__init__.py +10 -0
- webscout/Extra/GitToolkit/gitapi/__init__.py +12 -0
- webscout/Extra/GitToolkit/gitapi/repository.py +195 -0
- webscout/Extra/GitToolkit/gitapi/user.py +96 -0
- webscout/Extra/GitToolkit/gitapi/utils.py +62 -0
- webscout/Extra/YTToolkit/ytapi/video.py +232 -103
- webscout/Provider/AISEARCH/__init__.py +5 -1
- webscout/Provider/AISEARCH/hika_search.py +194 -0
- webscout/Provider/AISEARCH/monica_search.py +246 -0
- webscout/Provider/AISEARCH/scira_search.py +320 -0
- webscout/Provider/AISEARCH/webpilotai_search.py +281 -0
- webscout/Provider/AllenAI.py +255 -122
- webscout/Provider/DeepSeek.py +1 -2
- webscout/Provider/Deepinfra.py +17 -9
- webscout/Provider/ExaAI.py +261 -0
- webscout/Provider/ExaChat.py +8 -1
- webscout/Provider/GithubChat.py +2 -1
- webscout/Provider/Netwrck.py +3 -2
- webscout/Provider/OpenGPT.py +199 -0
- webscout/Provider/PI.py +39 -24
- webscout/Provider/Youchat.py +326 -296
- webscout/Provider/__init__.py +10 -0
- webscout/Provider/ai4chat.py +58 -56
- webscout/Provider/akashgpt.py +34 -22
- webscout/Provider/freeaichat.py +1 -1
- webscout/Provider/labyrinth.py +121 -20
- webscout/Provider/llmchatco.py +306 -0
- webscout/Provider/scira_chat.py +271 -0
- webscout/Provider/typefully.py +280 -0
- webscout/version.py +1 -1
- webscout/webscout_search.py +118 -54
- webscout/webscout_search_async.py +109 -45
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/METADATA +2 -2
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/RECORD +38 -24
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/LICENSE.md +0 -0
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/WHEEL +0 -0
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/entry_points.txt +0 -0
- {webscout-7.9.dist-info → webscout-8.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
from typing import Union, Any, Dict, Generator
|
|
2
|
+
from uuid import uuid4
|
|
3
|
+
import requests
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from webscout.AIutel import Optimizers
|
|
8
|
+
from webscout.AIutel import Conversation
|
|
9
|
+
from webscout.AIutel import AwesomePrompts
|
|
10
|
+
from webscout.AIbase import Provider
|
|
11
|
+
from webscout import exceptions
|
|
12
|
+
from webscout.litagent import LitAgent
|
|
13
|
+
|
|
14
|
+
class ExaAI(Provider):
|
|
15
|
+
"""
|
|
16
|
+
A class to interact with the o3minichat.exa.ai API.
|
|
17
|
+
|
|
18
|
+
Attributes:
|
|
19
|
+
system_prompt (str): The system prompt to define the assistant's role.
|
|
20
|
+
|
|
21
|
+
Examples:
|
|
22
|
+
>>> from webscout.Provider.ExaAI import ExaAI
|
|
23
|
+
>>> ai = ExaAI()
|
|
24
|
+
>>> response = ai.chat("What's the weather today?")
|
|
25
|
+
>>> print(response)
|
|
26
|
+
'The weather today depends on your location...'
|
|
27
|
+
"""
|
|
28
|
+
AVAILABLE_MODELS = ["O3-Mini"]
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
is_conversation: bool = True,
|
|
33
|
+
max_tokens: int = 600,
|
|
34
|
+
timeout: int = 30,
|
|
35
|
+
intro: str = None,
|
|
36
|
+
filepath: str = None,
|
|
37
|
+
update_file: bool = True,
|
|
38
|
+
proxies: dict = {},
|
|
39
|
+
history_offset: int = 10250,
|
|
40
|
+
act: str = None,
|
|
41
|
+
# system_prompt: str = "You are a helpful assistant.",
|
|
42
|
+
model: str = "O3-Mini", # >>> THIS FLAG IS NOT USED <<<
|
|
43
|
+
):
|
|
44
|
+
"""
|
|
45
|
+
Initializes the ExaAI API with given parameters.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
is_conversation (bool): Whether the provider is in conversation mode.
|
|
49
|
+
max_tokens (int): Maximum number of tokens to sample.
|
|
50
|
+
timeout (int): Timeout for API requests.
|
|
51
|
+
intro (str): Introduction message for the conversation.
|
|
52
|
+
filepath (str): Filepath for storing conversation history.
|
|
53
|
+
update_file (bool): Whether to update the conversation history file.
|
|
54
|
+
proxies (dict): Proxies for the API requests.
|
|
55
|
+
history_offset (int): Offset for conversation history.
|
|
56
|
+
act (str): Act for the conversation.
|
|
57
|
+
system_prompt (str): The system prompt to define the assistant's role.
|
|
58
|
+
|
|
59
|
+
Examples:
|
|
60
|
+
>>> ai = ExaAI(system_prompt="You are a friendly assistant.")
|
|
61
|
+
>>> print(ai.system_prompt)
|
|
62
|
+
'You are a friendly assistant.'
|
|
63
|
+
"""
|
|
64
|
+
self.session = requests.Session()
|
|
65
|
+
self.is_conversation = is_conversation
|
|
66
|
+
self.max_tokens_to_sample = max_tokens
|
|
67
|
+
self.api_endpoint = "https://o3minichat.exa.ai/api/chat"
|
|
68
|
+
self.timeout = timeout
|
|
69
|
+
self.last_response = {}
|
|
70
|
+
# self.system_prompt = system_prompt
|
|
71
|
+
|
|
72
|
+
# Initialize LitAgent for user agent generation
|
|
73
|
+
self.agent = LitAgent()
|
|
74
|
+
|
|
75
|
+
self.headers = {
|
|
76
|
+
"authority": "o3minichat.exa.ai",
|
|
77
|
+
"accept": "*/*",
|
|
78
|
+
"accept-encoding": "gzip, deflate, br, zstd",
|
|
79
|
+
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
80
|
+
"content-type": "application/json",
|
|
81
|
+
"dnt": "1",
|
|
82
|
+
"origin": "https://o3minichat.exa.ai",
|
|
83
|
+
"priority": "u=1, i",
|
|
84
|
+
"referer": "https://o3minichat.exa.ai/",
|
|
85
|
+
"sec-ch-ua": '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
|
|
86
|
+
"sec-ch-ua-mobile": "?0",
|
|
87
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
88
|
+
"sec-fetch-dest": "empty",
|
|
89
|
+
"sec-fetch-mode": "cors",
|
|
90
|
+
"sec-fetch-site": "same-origin",
|
|
91
|
+
"sec-gpc": "1",
|
|
92
|
+
"user-agent": self.agent.random() # Use LitAgent to generate a random user agent
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
self.__available_optimizers = (
|
|
96
|
+
method
|
|
97
|
+
for method in dir(Optimizers)
|
|
98
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
99
|
+
)
|
|
100
|
+
self.session.headers.update(self.headers)
|
|
101
|
+
Conversation.intro = (
|
|
102
|
+
AwesomePrompts().get_act(
|
|
103
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
104
|
+
)
|
|
105
|
+
if act
|
|
106
|
+
else intro or Conversation.intro
|
|
107
|
+
)
|
|
108
|
+
self.conversation = Conversation(
|
|
109
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
110
|
+
)
|
|
111
|
+
self.conversation.history_offset = history_offset
|
|
112
|
+
self.session.proxies = proxies
|
|
113
|
+
|
|
114
|
+
def ask(
|
|
115
|
+
self,
|
|
116
|
+
prompt: str,
|
|
117
|
+
stream: bool = False,
|
|
118
|
+
raw: bool = False,
|
|
119
|
+
optimizer: str = None,
|
|
120
|
+
conversationally: bool = False,
|
|
121
|
+
) -> Dict[str, Any]:
|
|
122
|
+
"""
|
|
123
|
+
Sends a prompt to the o3minichat.exa.ai API and returns the response.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
prompt (str): The prompt to send to the API.
|
|
127
|
+
stream (bool): Whether to stream the response.
|
|
128
|
+
raw (bool): Whether to return the raw response.
|
|
129
|
+
optimizer (str): Optimizer to use for the prompt.
|
|
130
|
+
conversationally (bool): Whether to generate the prompt conversationally.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
Dict[str, Any]: The API response.
|
|
134
|
+
|
|
135
|
+
Examples:
|
|
136
|
+
>>> ai = ExaAI()
|
|
137
|
+
>>> response = ai.ask("Tell me a joke!")
|
|
138
|
+
>>> print(response)
|
|
139
|
+
{'text': 'Why did the scarecrow win an award? Because he was outstanding in his field!'}
|
|
140
|
+
"""
|
|
141
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
142
|
+
if optimizer:
|
|
143
|
+
if optimizer in self.__available_optimizers:
|
|
144
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
145
|
+
conversation_prompt if conversationally else prompt
|
|
146
|
+
)
|
|
147
|
+
else:
|
|
148
|
+
raise Exception(
|
|
149
|
+
f"Optimizer is not one of {self.__available_optimizers}"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Generate a unique ID for the conversation
|
|
153
|
+
conversation_id = uuid4().hex[:16]
|
|
154
|
+
|
|
155
|
+
payload = {
|
|
156
|
+
"id": conversation_id,
|
|
157
|
+
"messages": [
|
|
158
|
+
# {"role": "system", "content": self.system_prompt},
|
|
159
|
+
{"role": "user", "content": conversation_prompt}
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
def for_stream():
|
|
164
|
+
response = self.session.post(self.api_endpoint, headers=self.headers, json=payload, stream=True, timeout=self.timeout)
|
|
165
|
+
if not response.ok:
|
|
166
|
+
raise exceptions.FailedToGenerateResponseError(
|
|
167
|
+
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
streaming_response = ""
|
|
171
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
172
|
+
if line:
|
|
173
|
+
match = re.search(r'0:"(.*?)"', line)
|
|
174
|
+
if match:
|
|
175
|
+
content = match.group(1)
|
|
176
|
+
streaming_response += content
|
|
177
|
+
yield content if raw else dict(text=content)
|
|
178
|
+
|
|
179
|
+
self.last_response.update(dict(text=streaming_response))
|
|
180
|
+
self.conversation.update_chat_history(
|
|
181
|
+
prompt, self.get_message(self.last_response)
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def for_non_stream():
|
|
185
|
+
for _ in for_stream():
|
|
186
|
+
pass
|
|
187
|
+
return self.last_response
|
|
188
|
+
|
|
189
|
+
return for_stream() if stream else for_non_stream()
|
|
190
|
+
|
|
191
|
+
def chat(
|
|
192
|
+
self,
|
|
193
|
+
prompt: str,
|
|
194
|
+
stream: bool = False,
|
|
195
|
+
optimizer: str = None,
|
|
196
|
+
conversationally: bool = False,
|
|
197
|
+
) -> Union[str, Generator[str, None, None]]:
|
|
198
|
+
"""
|
|
199
|
+
Generates a response from the ExaAI API.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
prompt (str): The prompt to send to the API.
|
|
203
|
+
stream (bool): Whether to stream the response.
|
|
204
|
+
optimizer (str): Optimizer to use for the prompt.
|
|
205
|
+
conversationally (bool): Whether to generate the prompt conversationally.
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
Union[str, Generator[str, None, None]]: The API response as a string or a generator of string chunks.
|
|
209
|
+
|
|
210
|
+
Examples:
|
|
211
|
+
>>> ai = ExaAI()
|
|
212
|
+
>>> response = ai.chat("What's the weather today?")
|
|
213
|
+
>>> print(response)
|
|
214
|
+
'The weather today depends on your location...'
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
def for_stream():
|
|
218
|
+
for response in self.ask(
|
|
219
|
+
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
220
|
+
):
|
|
221
|
+
yield self.get_message(response)
|
|
222
|
+
|
|
223
|
+
def for_non_stream():
|
|
224
|
+
return self.get_message(
|
|
225
|
+
self.ask(
|
|
226
|
+
prompt,
|
|
227
|
+
False,
|
|
228
|
+
optimizer=optimizer,
|
|
229
|
+
conversationally=conversationally,
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
return for_stream() if stream else for_non_stream()
|
|
234
|
+
|
|
235
|
+
def get_message(self, response: dict) -> str:
|
|
236
|
+
"""
|
|
237
|
+
Extracts the message from the API response.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
response (dict): The API response.
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
str: The message content.
|
|
244
|
+
|
|
245
|
+
Examples:
|
|
246
|
+
>>> ai = ExaAI()
|
|
247
|
+
>>> response = ai.ask("Tell me a joke!")
|
|
248
|
+
>>> message = ai.get_message(response)
|
|
249
|
+
>>> print(message)
|
|
250
|
+
'Why did the scarecrow win an award? Because he was outstanding in his field!'
|
|
251
|
+
"""
|
|
252
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
253
|
+
formatted_text = response["text"].replace('\\n', '\n').replace('\\n\\n', '\n\n')
|
|
254
|
+
return formatted_text
|
|
255
|
+
|
|
256
|
+
if __name__ == "__main__":
|
|
257
|
+
from rich import print
|
|
258
|
+
ai = ExaAI(timeout=5000)
|
|
259
|
+
response = ai.chat("Tell me about HelpingAI", stream=True)
|
|
260
|
+
for chunk in response:
|
|
261
|
+
print(chunk, end="", flush=True)
|
webscout/Provider/ExaChat.py
CHANGED
|
@@ -22,6 +22,7 @@ MODEL_CONFIGS = {
|
|
|
22
22
|
"gemini-2.0-flash-thinking-exp-01-21",
|
|
23
23
|
"gemini-2.5-pro-exp-03-25",
|
|
24
24
|
"gemini-2.0-pro-exp-02-05",
|
|
25
|
+
|
|
25
26
|
],
|
|
26
27
|
},
|
|
27
28
|
"openrouter": {
|
|
@@ -31,6 +32,7 @@ MODEL_CONFIGS = {
|
|
|
31
32
|
"deepseek/deepseek-r1:free",
|
|
32
33
|
"deepseek/deepseek-chat-v3-0324:free",
|
|
33
34
|
"google/gemma-3-27b-it:free",
|
|
35
|
+
"meta-llama/llama-4-maverick:free",
|
|
34
36
|
],
|
|
35
37
|
},
|
|
36
38
|
"groq": {
|
|
@@ -49,7 +51,8 @@ MODEL_CONFIGS = {
|
|
|
49
51
|
"llama3-8b-8192",
|
|
50
52
|
"qwen-2.5-32b",
|
|
51
53
|
"qwen-2.5-coder-32b",
|
|
52
|
-
"qwen-qwq-32b"
|
|
54
|
+
"qwen-qwq-32b",
|
|
55
|
+
"meta-llama/llama-4-scout-17b-16e-instruct"
|
|
53
56
|
],
|
|
54
57
|
},
|
|
55
58
|
"cerebras": {
|
|
@@ -71,6 +74,7 @@ class ExaChat(Provider):
|
|
|
71
74
|
|
|
72
75
|
# Gemini Models
|
|
73
76
|
"gemini-2.0-flash",
|
|
77
|
+
"gemini-2.0-flash-exp-image-generation",
|
|
74
78
|
"gemini-2.0-flash-thinking-exp-01-21",
|
|
75
79
|
"gemini-2.5-pro-exp-03-25",
|
|
76
80
|
"gemini-2.0-pro-exp-02-05",
|
|
@@ -80,6 +84,7 @@ class ExaChat(Provider):
|
|
|
80
84
|
"deepseek/deepseek-r1:free",
|
|
81
85
|
"deepseek/deepseek-chat-v3-0324:free",
|
|
82
86
|
"google/gemma-3-27b-it:free",
|
|
87
|
+
"meta-llama/llama-4-maverick:free",
|
|
83
88
|
|
|
84
89
|
# Groq Models
|
|
85
90
|
"deepseek-r1-distill-llama-70b",
|
|
@@ -96,6 +101,8 @@ class ExaChat(Provider):
|
|
|
96
101
|
"qwen-2.5-32b",
|
|
97
102
|
"qwen-2.5-coder-32b",
|
|
98
103
|
"qwen-qwq-32b",
|
|
104
|
+
"meta-llama/llama-4-scout-17b-16e-instruct",
|
|
105
|
+
|
|
99
106
|
|
|
100
107
|
# Cerebras Models
|
|
101
108
|
"llama3.1-8b",
|
webscout/Provider/GithubChat.py
CHANGED
webscout/Provider/Netwrck.py
CHANGED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
from typing import Dict, Generator, Union
|
|
4
|
+
|
|
5
|
+
from webscout.AIutel import Optimizers
|
|
6
|
+
from webscout.AIutel import Conversation
|
|
7
|
+
from webscout.AIutel import AwesomePrompts, sanitize_stream
|
|
8
|
+
from webscout.AIbase import Provider
|
|
9
|
+
from webscout import exceptions
|
|
10
|
+
from webscout.litagent import LitAgent
|
|
11
|
+
|
|
12
|
+
class OpenGPT(Provider):
|
|
13
|
+
"""
|
|
14
|
+
A class to interact with the Open-GPT API.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
is_conversation: bool = True,
|
|
20
|
+
max_tokens: int = 600,
|
|
21
|
+
timeout: int = 30,
|
|
22
|
+
intro: str = None,
|
|
23
|
+
filepath: str = None,
|
|
24
|
+
update_file: bool = True,
|
|
25
|
+
proxies: dict = {},
|
|
26
|
+
history_offset: int = 10250,
|
|
27
|
+
act: str = None,
|
|
28
|
+
app_id: str = "clf3yg8730000ih08ndbdi2v4",
|
|
29
|
+
):
|
|
30
|
+
"""Initializes the OpenGPT API client.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
is_conversation (bool, optional): Whether to maintain conversation history. Defaults to True.
|
|
34
|
+
max_tokens (int, optional): Maximum number of tokens to generate. Defaults to 600.
|
|
35
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
36
|
+
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
37
|
+
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
38
|
+
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
39
|
+
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
40
|
+
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
41
|
+
act (str, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
42
|
+
app_id (str, optional): The OpenGPT application ID. Defaults to "clf3yg8730000ih08ndbdi2v4".
|
|
43
|
+
"""
|
|
44
|
+
self.session = requests.Session()
|
|
45
|
+
self.agent = LitAgent()
|
|
46
|
+
|
|
47
|
+
self.is_conversation = is_conversation
|
|
48
|
+
self.max_tokens_to_sample = max_tokens
|
|
49
|
+
self.timeout = timeout
|
|
50
|
+
self.last_response = {}
|
|
51
|
+
self.app_id = app_id
|
|
52
|
+
|
|
53
|
+
# Set up headers with dynamic user agent
|
|
54
|
+
self.headers = {
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
"User-Agent": self.agent.random(),
|
|
57
|
+
"Referer": f"https://open-gpt.app/id/app/{app_id}"
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
self.session.headers.update(self.headers)
|
|
61
|
+
self.session.proxies.update(proxies)
|
|
62
|
+
|
|
63
|
+
# Initialize optimizers
|
|
64
|
+
self.__available_optimizers = (
|
|
65
|
+
method
|
|
66
|
+
for method in dir(Optimizers)
|
|
67
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Setup conversation
|
|
71
|
+
Conversation.intro = (
|
|
72
|
+
AwesomePrompts().get_act(
|
|
73
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
74
|
+
) if act else intro or Conversation.intro
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
self.conversation = Conversation(
|
|
78
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
79
|
+
)
|
|
80
|
+
self.conversation.history_offset = history_offset
|
|
81
|
+
|
|
82
|
+
def ask(
|
|
83
|
+
self,
|
|
84
|
+
prompt: str,
|
|
85
|
+
stream: bool = False,
|
|
86
|
+
raw: bool = False,
|
|
87
|
+
optimizer: str = None,
|
|
88
|
+
conversationally: bool = False,
|
|
89
|
+
) -> Union[Dict, Generator]:
|
|
90
|
+
"""
|
|
91
|
+
Send a prompt to the OpenGPT API and get a response.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
prompt: The user input/prompt for the API.
|
|
95
|
+
stream: Whether to stream the response.
|
|
96
|
+
raw: Whether to return the raw API response.
|
|
97
|
+
optimizer: Optimizer to use on the prompt.
|
|
98
|
+
conversationally: Whether to apply the optimizer on the full conversation prompt.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
A dictionary or generator with the response.
|
|
102
|
+
"""
|
|
103
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
104
|
+
|
|
105
|
+
if optimizer:
|
|
106
|
+
if optimizer in self.__available_optimizers:
|
|
107
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
108
|
+
conversation_prompt if conversationally else prompt
|
|
109
|
+
)
|
|
110
|
+
else:
|
|
111
|
+
raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
|
|
112
|
+
|
|
113
|
+
# Prepare the request body payload
|
|
114
|
+
payload = {
|
|
115
|
+
"userInput": conversation_prompt,
|
|
116
|
+
"id": self.app_id,
|
|
117
|
+
"userKey": "" # Assuming userKey is meant to be empty as in the original code
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
def for_non_stream():
|
|
121
|
+
try:
|
|
122
|
+
response = self.session.post(
|
|
123
|
+
"https://open-gpt.app/api/generate",
|
|
124
|
+
data=json.dumps(payload),
|
|
125
|
+
timeout=self.timeout
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# Raise an exception for bad status codes
|
|
129
|
+
response.raise_for_status()
|
|
130
|
+
|
|
131
|
+
response_text = response.text
|
|
132
|
+
self.last_response = {"text": response_text}
|
|
133
|
+
self.conversation.update_chat_history(prompt, response_text)
|
|
134
|
+
|
|
135
|
+
return {"text": response_text} if not raw else {"raw": response_text}
|
|
136
|
+
|
|
137
|
+
except requests.exceptions.RequestException as e:
|
|
138
|
+
# Handle potential errors during the request
|
|
139
|
+
error_msg = f"Error fetching data: {e}"
|
|
140
|
+
raise exceptions.FailedToGenerateResponseError(error_msg)
|
|
141
|
+
except Exception as e:
|
|
142
|
+
# Catch any other unexpected errors
|
|
143
|
+
error_msg = f"An unexpected error occurred: {e}"
|
|
144
|
+
raise exceptions.FailedToGenerateResponseError(error_msg)
|
|
145
|
+
|
|
146
|
+
# This provider doesn't support streaming, so just return non-stream
|
|
147
|
+
return for_non_stream()
|
|
148
|
+
|
|
149
|
+
def chat(
|
|
150
|
+
self,
|
|
151
|
+
prompt: str,
|
|
152
|
+
stream: bool = False,
|
|
153
|
+
optimizer: str = None,
|
|
154
|
+
conversationally: bool = False,
|
|
155
|
+
) -> Union[str, Generator[str, None, None]]:
|
|
156
|
+
"""
|
|
157
|
+
Send a prompt to the OpenGPT API and get a text response.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
prompt: The user input/prompt for the API.
|
|
161
|
+
stream: Whether to stream the response (not supported).
|
|
162
|
+
optimizer: Optimizer to use on the prompt.
|
|
163
|
+
conversationally: Whether to apply the optimizer on the full conversation prompt.
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
A string with the response text.
|
|
167
|
+
"""
|
|
168
|
+
response = self.ask(
|
|
169
|
+
prompt, False, optimizer=optimizer, conversationally=conversationally
|
|
170
|
+
)
|
|
171
|
+
return self.get_message(response)
|
|
172
|
+
|
|
173
|
+
def get_message(self, response: dict) -> str:
|
|
174
|
+
"""
|
|
175
|
+
Extract the message from the response dictionary.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
response: Response dictionary from the ask method.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
The text response as a string.
|
|
182
|
+
"""
|
|
183
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
184
|
+
return response["text"]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
if __name__ == "__main__":
|
|
188
|
+
# Test the provider
|
|
189
|
+
print("-" * 80)
|
|
190
|
+
print("Testing OpenGPT provider")
|
|
191
|
+
print("-" * 80)
|
|
192
|
+
|
|
193
|
+
try:
|
|
194
|
+
test_ai = OpenGPT()
|
|
195
|
+
response = test_ai.chat("Explain quantum physics simply.")
|
|
196
|
+
print(response)
|
|
197
|
+
except Exception as e:
|
|
198
|
+
print(f"Error: {e}")
|
|
199
|
+
|
webscout/Provider/PI.py
CHANGED
|
@@ -50,7 +50,7 @@ class PiAI(Provider):
|
|
|
50
50
|
):
|
|
51
51
|
"""
|
|
52
52
|
Initializes PiAI with voice support.
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
Args:
|
|
55
55
|
voice (bool): Enable/disable voice output
|
|
56
56
|
voice_name (str): Name of the voice to use (if None, uses default)
|
|
@@ -66,7 +66,9 @@ class PiAI(Provider):
|
|
|
66
66
|
|
|
67
67
|
# Initialize other attributes
|
|
68
68
|
self.scraper = cloudscraper.create_scraper()
|
|
69
|
-
self.
|
|
69
|
+
self.primary_url = 'https://pi.ai/api/chat'
|
|
70
|
+
self.fallback_url = 'https://pi.ai/api/v2/chat'
|
|
71
|
+
self.url = self.primary_url
|
|
70
72
|
self.headers = {
|
|
71
73
|
'Accept': 'text/event-stream',
|
|
72
74
|
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
|
@@ -115,7 +117,7 @@ class PiAI(Provider):
|
|
|
115
117
|
)
|
|
116
118
|
self.conversation.history_offset = history_offset
|
|
117
119
|
self.session.proxies = proxies
|
|
118
|
-
|
|
120
|
+
|
|
119
121
|
if self.is_conversation:
|
|
120
122
|
self.start_conversation()
|
|
121
123
|
|
|
@@ -130,13 +132,13 @@ class PiAI(Provider):
|
|
|
130
132
|
json={},
|
|
131
133
|
timeout=self.timeout
|
|
132
134
|
)
|
|
133
|
-
|
|
135
|
+
|
|
134
136
|
if not response.ok:
|
|
135
137
|
raise Exception(f"Failed to start conversation: {response.status_code}")
|
|
136
|
-
|
|
138
|
+
|
|
137
139
|
data = response.json()
|
|
138
140
|
self.conversation_id = data['conversations'][0]['sid']
|
|
139
|
-
|
|
141
|
+
|
|
140
142
|
return self.conversation_id
|
|
141
143
|
|
|
142
144
|
def ask(
|
|
@@ -152,7 +154,7 @@ class PiAI(Provider):
|
|
|
152
154
|
) -> dict:
|
|
153
155
|
"""
|
|
154
156
|
Interact with Pi.ai by sending a prompt and receiving a response.
|
|
155
|
-
|
|
157
|
+
|
|
156
158
|
Args:
|
|
157
159
|
prompt (str): The prompt to send
|
|
158
160
|
stream (bool): Whether to stream the response
|
|
@@ -186,15 +188,28 @@ class PiAI(Provider):
|
|
|
186
188
|
}
|
|
187
189
|
|
|
188
190
|
def process_stream():
|
|
191
|
+
# Try primary URL first
|
|
189
192
|
response = self.scraper.post(
|
|
190
|
-
self.url,
|
|
191
|
-
headers=self.headers,
|
|
192
|
-
cookies=self.cookies,
|
|
193
|
-
json=data,
|
|
194
|
-
stream=True,
|
|
193
|
+
self.url,
|
|
194
|
+
headers=self.headers,
|
|
195
|
+
cookies=self.cookies,
|
|
196
|
+
json=data,
|
|
197
|
+
stream=True,
|
|
195
198
|
timeout=self.timeout
|
|
196
199
|
)
|
|
197
|
-
|
|
200
|
+
|
|
201
|
+
# If primary URL fails, try fallback URL
|
|
202
|
+
if not response.ok and self.url == self.primary_url:
|
|
203
|
+
self.url = self.fallback_url
|
|
204
|
+
response = self.scraper.post(
|
|
205
|
+
self.url,
|
|
206
|
+
headers=self.headers,
|
|
207
|
+
cookies=self.cookies,
|
|
208
|
+
json=data,
|
|
209
|
+
stream=True,
|
|
210
|
+
timeout=self.timeout
|
|
211
|
+
)
|
|
212
|
+
|
|
198
213
|
if not response.ok:
|
|
199
214
|
raise Exception(f"API request failed: {response.status_code}")
|
|
200
215
|
|
|
@@ -204,7 +219,7 @@ class PiAI(Provider):
|
|
|
204
219
|
|
|
205
220
|
if voice and voice_name and second_sid:
|
|
206
221
|
threading.Thread(
|
|
207
|
-
target=self.download_audio_threaded,
|
|
222
|
+
target=self.download_audio_threaded,
|
|
208
223
|
args=(voice_name, second_sid, output_file)
|
|
209
224
|
).start()
|
|
210
225
|
|
|
@@ -245,7 +260,7 @@ class PiAI(Provider):
|
|
|
245
260
|
) -> str:
|
|
246
261
|
"""
|
|
247
262
|
Generates a response based on the provided prompt.
|
|
248
|
-
|
|
263
|
+
|
|
249
264
|
Args:
|
|
250
265
|
prompt (str): The prompt to send
|
|
251
266
|
stream (bool): Whether to stream the response
|
|
@@ -300,24 +315,24 @@ class PiAI(Provider):
|
|
|
300
315
|
'voice': f'voice{self.AVAILABLE_VOICES[voice_name]}',
|
|
301
316
|
'messageSid': second_sid,
|
|
302
317
|
}
|
|
303
|
-
|
|
318
|
+
|
|
304
319
|
try:
|
|
305
320
|
audio_response = self.scraper.get(
|
|
306
|
-
'https://pi.ai/api/chat/voice',
|
|
307
|
-
params=params,
|
|
308
|
-
cookies=self.cookies,
|
|
309
|
-
headers=self.headers,
|
|
321
|
+
'https://pi.ai/api/chat/voice',
|
|
322
|
+
params=params,
|
|
323
|
+
cookies=self.cookies,
|
|
324
|
+
headers=self.headers,
|
|
310
325
|
timeout=self.timeout
|
|
311
326
|
)
|
|
312
|
-
|
|
327
|
+
|
|
313
328
|
if not audio_response.ok:
|
|
314
329
|
return
|
|
315
|
-
|
|
330
|
+
|
|
316
331
|
audio_response.raise_for_status()
|
|
317
|
-
|
|
332
|
+
|
|
318
333
|
with open(output_file, "wb") as file:
|
|
319
334
|
file.write(audio_response.content)
|
|
320
|
-
|
|
335
|
+
|
|
321
336
|
except requests.exceptions.RequestException:
|
|
322
337
|
pass
|
|
323
338
|
|