webscout 7.3__py3-none-any.whl → 7.4__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/Provider/AISEARCH/__init__.py +4 -3
- webscout/Provider/AISEARCH/genspark_search.py +208 -0
- webscout/Provider/AllenAI.py +282 -0
- webscout/Provider/Deepinfra.py +43 -44
- webscout/Provider/ElectronHub.py +634 -0
- webscout/Provider/Glider.py +7 -41
- webscout/Provider/HeckAI.py +200 -0
- webscout/Provider/Jadve.py +49 -63
- webscout/Provider/PI.py +106 -93
- webscout/Provider/Perplexitylabs.py +395 -0
- webscout/Provider/QwenLM.py +7 -61
- webscout/Provider/TTI/__init__.py +1 -0
- webscout/Provider/TTI/piclumen/__init__.py +23 -0
- webscout/Provider/TTI/piclumen/async_piclumen.py +268 -0
- webscout/Provider/TTI/piclumen/sync_piclumen.py +233 -0
- webscout/Provider/TextPollinationsAI.py +3 -2
- webscout/Provider/TwoAI.py +200 -0
- webscout/Provider/Venice.py +200 -0
- webscout/Provider/WiseCat.py +1 -18
- webscout/Provider/__init__.py +12 -0
- webscout/Provider/akashgpt.py +312 -0
- webscout/Provider/chatglm.py +5 -5
- webscout/Provider/freeaichat.py +251 -221
- webscout/Provider/koala.py +9 -1
- webscout/Provider/yep.py +4 -24
- webscout/version.py +1 -1
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/METADATA +44 -49
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/RECORD +32 -21
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/LICENSE.md +0 -0
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/WHEEL +0 -0
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/entry_points.txt +0 -0
- {webscout-7.3.dist-info → webscout-7.4.dist-info}/top_level.txt +0 -0
webscout/Provider/Glider.py
CHANGED
|
@@ -5,12 +5,11 @@ from typing import Any, Dict, Generator, Optional
|
|
|
5
5
|
from webscout.AIutel import Optimizers, Conversation, AwesomePrompts
|
|
6
6
|
from webscout.AIbase import Provider
|
|
7
7
|
from webscout import exceptions
|
|
8
|
-
from webscout.Litlogger import Logger, LogFormat
|
|
9
8
|
from webscout import LitAgent as Lit
|
|
10
9
|
|
|
11
10
|
class GliderAI(Provider):
|
|
12
11
|
"""
|
|
13
|
-
A class to interact with the Glider.so API
|
|
12
|
+
A class to interact with the Glider.so API.
|
|
14
13
|
"""
|
|
15
14
|
|
|
16
15
|
AVAILABLE_MODELS = {
|
|
@@ -32,22 +31,12 @@ class GliderAI(Provider):
|
|
|
32
31
|
history_offset: int = 10250,
|
|
33
32
|
act: Optional[str] = None,
|
|
34
33
|
model: str = "chat-llama-3-1-70b",
|
|
35
|
-
system_prompt: str = "You are a helpful AI assistant."
|
|
36
|
-
logging: bool = False
|
|
34
|
+
system_prompt: str = "You are a helpful AI assistant."
|
|
37
35
|
):
|
|
38
|
-
"""Initializes the GliderAI API client
|
|
36
|
+
"""Initializes the GliderAI API client."""
|
|
39
37
|
if model not in self.AVAILABLE_MODELS:
|
|
40
38
|
raise ValueError(f"Invalid model: {model}. Choose from: {', '.join(self.AVAILABLE_MODELS)}")
|
|
41
39
|
|
|
42
|
-
self.logger = Logger(
|
|
43
|
-
name="GliderAI",
|
|
44
|
-
format=LogFormat.MODERN_EMOJI,
|
|
45
|
-
|
|
46
|
-
) if logging else None
|
|
47
|
-
|
|
48
|
-
if self.logger:
|
|
49
|
-
self.logger.info(f"Initializing GliderAI with model: {model}")
|
|
50
|
-
|
|
51
40
|
self.session = requests.Session()
|
|
52
41
|
self.is_conversation = is_conversation
|
|
53
42
|
self.max_tokens_to_sample = max_tokens
|
|
@@ -85,9 +74,6 @@ class GliderAI(Provider):
|
|
|
85
74
|
)
|
|
86
75
|
self.conversation.history_offset = history_offset
|
|
87
76
|
|
|
88
|
-
if self.logger:
|
|
89
|
-
self.logger.info("GliderAI initialized successfully")
|
|
90
|
-
|
|
91
77
|
def ask(
|
|
92
78
|
self,
|
|
93
79
|
prompt: str,
|
|
@@ -96,7 +82,7 @@ class GliderAI(Provider):
|
|
|
96
82
|
optimizer: Optional[str] = None,
|
|
97
83
|
conversationally: bool = False,
|
|
98
84
|
) -> Dict[str, Any] | Generator[Dict[str, Any], None, None]:
|
|
99
|
-
"""Chat with AI
|
|
85
|
+
"""Chat with AI.
|
|
100
86
|
|
|
101
87
|
Args:
|
|
102
88
|
prompt (str): Prompt to be sent.
|
|
@@ -107,21 +93,13 @@ class GliderAI(Provider):
|
|
|
107
93
|
Returns:
|
|
108
94
|
dict or Generator[dict, None, None]: The response from the API.
|
|
109
95
|
"""
|
|
110
|
-
if self.logger:
|
|
111
|
-
self.logger.debug(f"Processing request - Prompt: {prompt[:50]}...")
|
|
112
|
-
self.logger.debug(f"Stream: {stream}, Optimizer: {optimizer}")
|
|
113
|
-
|
|
114
96
|
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
115
97
|
if optimizer:
|
|
116
98
|
if optimizer in self.__available_optimizers:
|
|
117
99
|
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
118
100
|
conversation_prompt if conversationally else prompt
|
|
119
101
|
)
|
|
120
|
-
if self.logger:
|
|
121
|
-
self.logger.debug(f"Applied optimizer: {optimizer}")
|
|
122
102
|
else:
|
|
123
|
-
if self.logger:
|
|
124
|
-
self.logger.error(f"Invalid optimizer requested: {optimizer}")
|
|
125
103
|
raise Exception(f"Optimizer is not one of {list(self.__available_optimizers)}")
|
|
126
104
|
|
|
127
105
|
payload = {
|
|
@@ -133,16 +111,10 @@ class GliderAI(Provider):
|
|
|
133
111
|
}
|
|
134
112
|
|
|
135
113
|
def for_stream():
|
|
136
|
-
if self.logger:
|
|
137
|
-
self.logger.debug("Initiating streaming request to API")
|
|
138
114
|
response = self.session.post(
|
|
139
115
|
self.api_endpoint, json=payload, stream=True, timeout=self.timeout
|
|
140
116
|
)
|
|
141
117
|
if not response.ok:
|
|
142
|
-
if self.logger:
|
|
143
|
-
self.logger.error(
|
|
144
|
-
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
145
|
-
)
|
|
146
118
|
raise exceptions.FailedToGenerateResponseError(
|
|
147
119
|
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
148
120
|
)
|
|
@@ -161,12 +133,8 @@ class GliderAI(Provider):
|
|
|
161
133
|
break
|
|
162
134
|
self.last_response.update(dict(text=streaming_text))
|
|
163
135
|
self.conversation.update_chat_history(prompt, self.get_message(self.last_response))
|
|
164
|
-
if self.logger:
|
|
165
|
-
self.logger.debug("Response processing completed")
|
|
166
136
|
|
|
167
137
|
def for_non_stream():
|
|
168
|
-
if self.logger:
|
|
169
|
-
self.logger.debug("Processing non-streaming request")
|
|
170
138
|
for _ in for_stream():
|
|
171
139
|
pass
|
|
172
140
|
return self.last_response
|
|
@@ -180,7 +148,7 @@ class GliderAI(Provider):
|
|
|
180
148
|
optimizer: Optional[str] = None,
|
|
181
149
|
conversationally: bool = False,
|
|
182
150
|
) -> str | Generator[str, None, None]:
|
|
183
|
-
"""Generate response as a string
|
|
151
|
+
"""Generate response as a string.
|
|
184
152
|
|
|
185
153
|
Args:
|
|
186
154
|
prompt (str): Prompt to be sent.
|
|
@@ -190,8 +158,6 @@ class GliderAI(Provider):
|
|
|
190
158
|
Returns:
|
|
191
159
|
str or Generator[str, None, None]: The response generated.
|
|
192
160
|
"""
|
|
193
|
-
if self.logger:
|
|
194
|
-
self.logger.debug(f"Chat request initiated - Prompt: {prompt[:50]}...")
|
|
195
161
|
def for_stream():
|
|
196
162
|
for response in self.ask(
|
|
197
163
|
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
@@ -215,8 +181,8 @@ class GliderAI(Provider):
|
|
|
215
181
|
|
|
216
182
|
if __name__ == "__main__":
|
|
217
183
|
from rich import print
|
|
218
|
-
# For testing
|
|
219
|
-
ai = GliderAI(model="chat-llama-3-1-70b"
|
|
184
|
+
# For testing
|
|
185
|
+
ai = GliderAI(model="chat-llama-3-1-70b")
|
|
220
186
|
response = ai.chat("Meaning of Life", stream=True)
|
|
221
187
|
for chunk in response:
|
|
222
188
|
print(chunk, end="", flush=True)
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
import uuid
|
|
4
|
+
import sys
|
|
5
|
+
from typing import Any, Dict, Optional, Generator, Union
|
|
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
|
+
from webscout import LitAgent
|
|
13
|
+
|
|
14
|
+
class HeckAI(Provider):
|
|
15
|
+
"""
|
|
16
|
+
A class to interact with the HeckAI API with LitAgent user-agent.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
AVAILABLE_MODELS = [
|
|
20
|
+
"deepseek/deepseek-chat",
|
|
21
|
+
"openai/gpt-4o-mini",
|
|
22
|
+
"deepseek/deepseek-r1",
|
|
23
|
+
"google/gemini-2.0-flash-001"
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
is_conversation: bool = True,
|
|
29
|
+
max_tokens: int = 2049,
|
|
30
|
+
timeout: int = 30,
|
|
31
|
+
intro: str = None,
|
|
32
|
+
filepath: str = None,
|
|
33
|
+
update_file: bool = True,
|
|
34
|
+
proxies: dict = {},
|
|
35
|
+
history_offset: int = 10250,
|
|
36
|
+
act: str = None,
|
|
37
|
+
model: str = "google/gemini-2.0-flash-001",
|
|
38
|
+
language: str = "English"
|
|
39
|
+
):
|
|
40
|
+
"""Initializes the HeckAI API client."""
|
|
41
|
+
if model not in self.AVAILABLE_MODELS:
|
|
42
|
+
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
43
|
+
|
|
44
|
+
self.url = "https://api.heckai.weight-wave.com/api/ha/v1/chat"
|
|
45
|
+
self.session_id = str(uuid.uuid4())
|
|
46
|
+
self.language = language
|
|
47
|
+
|
|
48
|
+
# Use LitAgent for user-agent
|
|
49
|
+
self.headers = {
|
|
50
|
+
'User-Agent': LitAgent().random(),
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
'Origin': 'https://heck.ai',
|
|
53
|
+
'Referer': 'https://heck.ai/',
|
|
54
|
+
'Connection': 'keep-alive'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
self.session = requests.Session()
|
|
58
|
+
self.session.headers.update(self.headers)
|
|
59
|
+
self.session.proxies.update(proxies)
|
|
60
|
+
|
|
61
|
+
self.is_conversation = is_conversation
|
|
62
|
+
self.max_tokens_to_sample = max_tokens
|
|
63
|
+
self.timeout = timeout
|
|
64
|
+
self.last_response = {}
|
|
65
|
+
self.model = model
|
|
66
|
+
self.previous_question = None
|
|
67
|
+
self.previous_answer = None
|
|
68
|
+
|
|
69
|
+
self.__available_optimizers = (
|
|
70
|
+
method
|
|
71
|
+
for method in dir(Optimizers)
|
|
72
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
73
|
+
)
|
|
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
|
+
|
|
82
|
+
self.conversation = Conversation(
|
|
83
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
84
|
+
)
|
|
85
|
+
self.conversation.history_offset = history_offset
|
|
86
|
+
|
|
87
|
+
def ask(
|
|
88
|
+
self,
|
|
89
|
+
prompt: str,
|
|
90
|
+
stream: bool = False,
|
|
91
|
+
raw: bool = False,
|
|
92
|
+
optimizer: str = None,
|
|
93
|
+
conversationally: bool = False,
|
|
94
|
+
) -> Union[Dict[str, Any], Generator]:
|
|
95
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
96
|
+
if optimizer:
|
|
97
|
+
if optimizer in self.__available_optimizers:
|
|
98
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
99
|
+
conversation_prompt if conversationally else prompt
|
|
100
|
+
)
|
|
101
|
+
else:
|
|
102
|
+
raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
|
|
103
|
+
|
|
104
|
+
# Payload construction
|
|
105
|
+
payload = {
|
|
106
|
+
"model": self.model,
|
|
107
|
+
"question": conversation_prompt,
|
|
108
|
+
"language": self.language,
|
|
109
|
+
"sessionId": self.session_id,
|
|
110
|
+
"previousQuestion": self.previous_question,
|
|
111
|
+
"previousAnswer": self.previous_answer,
|
|
112
|
+
"imgUrls": []
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
# Store this message as previous for next request
|
|
116
|
+
self.previous_question = conversation_prompt
|
|
117
|
+
|
|
118
|
+
def for_stream():
|
|
119
|
+
try:
|
|
120
|
+
with requests.post(self.url, headers=self.headers, data=json.dumps(payload), stream=True, timeout=self.timeout) as response:
|
|
121
|
+
if response.status_code != 200:
|
|
122
|
+
raise exceptions.FailedToGenerateResponseError(
|
|
123
|
+
f"Request failed with status code {response.status_code}"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
streaming_text = ""
|
|
127
|
+
in_answer = False
|
|
128
|
+
|
|
129
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
130
|
+
if not line:
|
|
131
|
+
continue
|
|
132
|
+
|
|
133
|
+
# Remove "data: " prefix
|
|
134
|
+
if line.startswith("data: "):
|
|
135
|
+
data = line[6:]
|
|
136
|
+
else:
|
|
137
|
+
continue
|
|
138
|
+
|
|
139
|
+
# Check for control markers
|
|
140
|
+
if data == "[ANSWER_START]":
|
|
141
|
+
in_answer = True
|
|
142
|
+
continue
|
|
143
|
+
|
|
144
|
+
if data == "[ANSWER_DONE]":
|
|
145
|
+
in_answer = False
|
|
146
|
+
continue
|
|
147
|
+
|
|
148
|
+
if data == "[RELATE_Q_START]" or data == "[RELATE_Q_DONE]":
|
|
149
|
+
continue
|
|
150
|
+
|
|
151
|
+
# Process content if we're in an answer section
|
|
152
|
+
if in_answer:
|
|
153
|
+
streaming_text += data
|
|
154
|
+
resp = dict(text=data)
|
|
155
|
+
yield resp if raw else resp
|
|
156
|
+
|
|
157
|
+
self.previous_answer = streaming_text
|
|
158
|
+
self.conversation.update_chat_history(prompt, streaming_text)
|
|
159
|
+
|
|
160
|
+
except requests.RequestException as e:
|
|
161
|
+
raise exceptions.FailedToGenerateResponseError(f"Request failed: {str(e)}")
|
|
162
|
+
|
|
163
|
+
def for_non_stream():
|
|
164
|
+
full_text = ""
|
|
165
|
+
for chunk in for_stream():
|
|
166
|
+
if isinstance(chunk, dict) and "text" in chunk:
|
|
167
|
+
full_text += chunk["text"]
|
|
168
|
+
self.last_response = {"text": full_text}
|
|
169
|
+
return self.last_response
|
|
170
|
+
|
|
171
|
+
return for_stream() if stream else for_non_stream()
|
|
172
|
+
|
|
173
|
+
def chat(
|
|
174
|
+
self,
|
|
175
|
+
prompt: str,
|
|
176
|
+
stream: bool = False,
|
|
177
|
+
optimizer: str = None,
|
|
178
|
+
conversationally: bool = False,
|
|
179
|
+
) -> str:
|
|
180
|
+
def for_stream():
|
|
181
|
+
for response in self.ask(prompt, True, optimizer=optimizer, conversationally=conversationally):
|
|
182
|
+
yield self.get_message(response)
|
|
183
|
+
|
|
184
|
+
def for_non_stream():
|
|
185
|
+
return self.get_message(
|
|
186
|
+
self.ask(prompt, False, optimizer=optimizer, conversationally=conversationally)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
return for_stream() if stream else for_non_stream()
|
|
190
|
+
|
|
191
|
+
def get_message(self, response: dict) -> str:
|
|
192
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
193
|
+
return response["text"]
|
|
194
|
+
|
|
195
|
+
if __name__ == "__main__":
|
|
196
|
+
from rich import print
|
|
197
|
+
ai = HeckAI(timeout=120)
|
|
198
|
+
response = ai.chat("Write a short poem about artificial intelligence", stream=True)
|
|
199
|
+
for chunk in response:
|
|
200
|
+
print(chunk, end="", flush=True)
|
webscout/Provider/Jadve.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
import requests
|
|
3
2
|
import json
|
|
4
3
|
import re
|
|
@@ -8,15 +7,13 @@ from webscout.AIutel import Optimizers, Conversation, AwesomePrompts
|
|
|
8
7
|
from webscout.AIbase import Provider
|
|
9
8
|
from webscout import exceptions
|
|
10
9
|
from webscout.litagent import LitAgent
|
|
11
|
-
from webscout.Litlogger import Logger, LogFormat
|
|
12
10
|
|
|
13
11
|
class JadveOpenAI(Provider):
|
|
14
12
|
"""
|
|
15
13
|
A class to interact with the OpenAI API through jadve.com using the streaming endpoint.
|
|
16
|
-
Includes optional logging capabilities.
|
|
17
14
|
"""
|
|
18
15
|
|
|
19
|
-
AVAILABLE_MODELS = ["gpt-4o", "gpt-4o-mini"]
|
|
16
|
+
AVAILABLE_MODELS = ["gpt-4o", "gpt-4o-mini", "claude-3-7-sonnet-20250219", "claude-3-5-sonnet-20240620", "o1-mini", "deepseek-chat", "o1-mini", "claude-3-5-haiku-20241022"]
|
|
20
17
|
|
|
21
18
|
def __init__(
|
|
22
19
|
self,
|
|
@@ -29,12 +26,11 @@ class JadveOpenAI(Provider):
|
|
|
29
26
|
proxies: dict = {},
|
|
30
27
|
history_offset: int = 10250,
|
|
31
28
|
act: str = None,
|
|
32
|
-
model: str = "
|
|
33
|
-
system_prompt: str = "You are a helpful AI assistant."
|
|
34
|
-
logging: bool = False
|
|
29
|
+
model: str = "claude-3-7-sonnet-20250219",
|
|
30
|
+
system_prompt: str = "You are a helpful AI assistant."
|
|
35
31
|
):
|
|
36
32
|
"""
|
|
37
|
-
Initializes the JadveOpenAI client
|
|
33
|
+
Initializes the JadveOpenAI client.
|
|
38
34
|
|
|
39
35
|
Args:
|
|
40
36
|
is_conversation (bool, optional): Enable conversational mode. Defaults to True.
|
|
@@ -48,24 +44,13 @@ class JadveOpenAI(Provider):
|
|
|
48
44
|
act (str|int, optional): Act key for AwesomePrompts. Defaults to None.
|
|
49
45
|
model (str, optional): AI model to be used. Defaults to "gpt-4o-mini".
|
|
50
46
|
system_prompt (str, optional): System prompt text. Defaults to "You are a helpful AI assistant."
|
|
51
|
-
logging (bool, optional): Enable logging functionality. Defaults to False.
|
|
52
47
|
"""
|
|
53
48
|
if model not in self.AVAILABLE_MODELS:
|
|
54
49
|
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
55
50
|
|
|
56
|
-
self.logger = Logger(
|
|
57
|
-
name="JadveOpenAI",
|
|
58
|
-
format=LogFormat.MODERN_EMOJI,
|
|
59
|
-
|
|
60
|
-
) if logging else None
|
|
61
|
-
|
|
62
|
-
if self.logger:
|
|
63
|
-
self.logger.info(f"Initializing JadveOpenAI with model: {model}")
|
|
64
|
-
|
|
65
51
|
self.session = requests.Session()
|
|
66
52
|
self.is_conversation = is_conversation
|
|
67
53
|
self.max_tokens_to_sample = max_tokens
|
|
68
|
-
# Streaming endpoint for jadve.com
|
|
69
54
|
self.api_endpoint = "https://openai.jadve.com/stream"
|
|
70
55
|
self.stream_chunk_size = 64
|
|
71
56
|
self.timeout = timeout
|
|
@@ -73,17 +58,17 @@ class JadveOpenAI(Provider):
|
|
|
73
58
|
self.model = model
|
|
74
59
|
self.system_prompt = system_prompt
|
|
75
60
|
|
|
76
|
-
#
|
|
61
|
+
# Headers for API requests
|
|
77
62
|
self.headers = {
|
|
78
63
|
"accept": "*/*",
|
|
79
64
|
"accept-encoding": "gzip, deflate, br, zstd",
|
|
80
|
-
"accept-language": "en",
|
|
65
|
+
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
81
66
|
"content-type": "application/json",
|
|
82
67
|
"dnt": "1",
|
|
83
68
|
"origin": "https://jadve.com",
|
|
84
69
|
"priority": "u=1, i",
|
|
85
70
|
"referer": "https://jadve.com/",
|
|
86
|
-
"sec-ch-ua": '"
|
|
71
|
+
"sec-ch-ua": '"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"',
|
|
87
72
|
"sec-ch-ua-mobile": "?0",
|
|
88
73
|
"sec-ch-ua-platform": '"Windows"',
|
|
89
74
|
"sec-fetch-dest": "empty",
|
|
@@ -113,9 +98,6 @@ class JadveOpenAI(Provider):
|
|
|
113
98
|
)
|
|
114
99
|
self.conversation.history_offset = history_offset
|
|
115
100
|
|
|
116
|
-
if self.logger:
|
|
117
|
-
self.logger.info("JadveOpenAI initialized successfully.")
|
|
118
|
-
|
|
119
101
|
def ask(
|
|
120
102
|
self,
|
|
121
103
|
prompt: str,
|
|
@@ -136,21 +118,13 @@ class JadveOpenAI(Provider):
|
|
|
136
118
|
Returns:
|
|
137
119
|
dict or generator: A dictionary with the generated text or a generator yielding text chunks.
|
|
138
120
|
"""
|
|
139
|
-
if self.logger:
|
|
140
|
-
self.logger.debug(f"Processing request - Prompt: {prompt[:50]}...")
|
|
141
|
-
self.logger.debug(f"Stream: {stream}, Optimizer: {optimizer}")
|
|
142
|
-
|
|
143
121
|
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
144
122
|
if optimizer:
|
|
145
123
|
if optimizer in self.__available_optimizers:
|
|
146
124
|
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
147
125
|
conversation_prompt if conversationally else prompt
|
|
148
126
|
)
|
|
149
|
-
if self.logger:
|
|
150
|
-
self.logger.debug(f"Applied optimizer: {optimizer}")
|
|
151
127
|
else:
|
|
152
|
-
if self.logger:
|
|
153
|
-
self.logger.error(f"Invalid optimizer requested: {optimizer}")
|
|
154
128
|
raise Exception(
|
|
155
129
|
f"Optimizer is not one of {list(self.__available_optimizers)}"
|
|
156
130
|
)
|
|
@@ -169,43 +143,59 @@ class JadveOpenAI(Provider):
|
|
|
169
143
|
}
|
|
170
144
|
|
|
171
145
|
def for_stream():
|
|
172
|
-
if self.logger:
|
|
173
|
-
self.logger.debug("Initiating streaming request to API")
|
|
174
146
|
response = self.session.post(
|
|
175
147
|
self.api_endpoint, headers=self.headers, json=payload, stream=True, timeout=self.timeout
|
|
176
148
|
)
|
|
177
149
|
|
|
178
150
|
if not response.ok:
|
|
179
|
-
if self.logger:
|
|
180
|
-
self.logger.error(f"API request failed. Status: {response.status_code}, Reason: {response.reason}")
|
|
181
151
|
raise exceptions.FailedToGenerateResponseError(
|
|
182
152
|
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
183
153
|
)
|
|
184
154
|
|
|
185
|
-
|
|
186
|
-
self.logger.info(f"API connection established successfully. Status: {response.status_code}")
|
|
187
|
-
|
|
188
|
-
# Read the entire response text.
|
|
189
|
-
response_text = response.text
|
|
155
|
+
# Pattern to match the streaming chunks format: 0:"text"
|
|
190
156
|
pattern = r'0:"(.*?)"'
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
157
|
+
full_response_text = ""
|
|
158
|
+
|
|
159
|
+
# Process the response as it comes in
|
|
160
|
+
buffer = ""
|
|
161
|
+
|
|
162
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
163
|
+
if not line:
|
|
164
|
+
continue
|
|
165
|
+
|
|
166
|
+
buffer += line
|
|
167
|
+
|
|
168
|
+
# Try to match chunks in the current buffer
|
|
169
|
+
matches = re.findall(pattern, buffer)
|
|
170
|
+
if matches:
|
|
171
|
+
for chunk in matches:
|
|
172
|
+
full_response_text += chunk
|
|
173
|
+
# Return the current chunk
|
|
174
|
+
yield chunk if raw else dict(text=chunk)
|
|
175
|
+
|
|
176
|
+
# Remove matched parts from the buffer
|
|
177
|
+
matched_parts = [f'0:"{match}"' for match in matches]
|
|
178
|
+
for part in matched_parts:
|
|
179
|
+
buffer = buffer.replace(part, '', 1)
|
|
180
|
+
|
|
181
|
+
# Check if we've reached the end of the response
|
|
182
|
+
if 'e:' in line or 'd:' in line:
|
|
183
|
+
# No need to process usage data without logging
|
|
184
|
+
break
|
|
185
|
+
|
|
186
|
+
self.last_response.update(dict(text=full_response_text))
|
|
199
187
|
self.conversation.update_chat_history(prompt, self.get_message(self.last_response))
|
|
200
188
|
|
|
201
|
-
if self.logger:
|
|
202
|
-
self.logger.debug("Response processing completed.")
|
|
203
|
-
|
|
204
189
|
def for_non_stream():
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
for
|
|
208
|
-
|
|
190
|
+
# For non-streaming requests, we collect all chunks and return the complete response
|
|
191
|
+
collected_text = ""
|
|
192
|
+
for chunk in for_stream():
|
|
193
|
+
if raw:
|
|
194
|
+
collected_text += chunk
|
|
195
|
+
else:
|
|
196
|
+
collected_text += chunk.get("text", "")
|
|
197
|
+
|
|
198
|
+
self.last_response = {"text": collected_text}
|
|
209
199
|
return self.last_response
|
|
210
200
|
|
|
211
201
|
return for_stream() if stream else for_non_stream()
|
|
@@ -228,9 +218,6 @@ class JadveOpenAI(Provider):
|
|
|
228
218
|
Returns:
|
|
229
219
|
str or generator: Generated response string or generator yielding response chunks.
|
|
230
220
|
"""
|
|
231
|
-
if self.logger:
|
|
232
|
-
self.logger.debug(f"Chat request initiated - Prompt: {prompt[:50]}...")
|
|
233
|
-
|
|
234
221
|
def for_stream():
|
|
235
222
|
for response in self.ask(
|
|
236
223
|
prompt, stream=True, optimizer=optimizer, conversationally=conversationally
|
|
@@ -258,8 +245,7 @@ class JadveOpenAI(Provider):
|
|
|
258
245
|
|
|
259
246
|
if __name__ == "__main__":
|
|
260
247
|
from rich import print
|
|
261
|
-
ai = JadveOpenAI(timeout=5000
|
|
262
|
-
|
|
263
|
-
response = ai.chat("yo what's up", stream=True)
|
|
248
|
+
ai = JadveOpenAI(timeout=5000)
|
|
249
|
+
response = ai.chat("Who made u?", stream=True)
|
|
264
250
|
for chunk in response:
|
|
265
251
|
print(chunk, end="", flush=True)
|