webscout 8.3.6__py3-none-any.whl → 8.3.7__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 +2 -0
- webscout/Provider/AISEARCH/__init__.py +18 -11
- webscout/Provider/AISEARCH/scira_search.py +3 -1
- webscout/Provider/Aitopia.py +2 -3
- webscout/Provider/Andi.py +3 -3
- webscout/Provider/ChatGPTClone.py +1 -1
- webscout/Provider/ChatSandbox.py +1 -0
- webscout/Provider/Cloudflare.py +1 -1
- webscout/Provider/Cohere.py +1 -0
- webscout/Provider/Deepinfra.py +7 -10
- webscout/Provider/ExaAI.py +1 -1
- webscout/Provider/ExaChat.py +1 -80
- webscout/Provider/Flowith.py +1 -1
- webscout/Provider/Gemini.py +7 -5
- webscout/Provider/GeminiProxy.py +1 -0
- webscout/Provider/GithubChat.py +3 -1
- webscout/Provider/Groq.py +1 -1
- webscout/Provider/HeckAI.py +8 -4
- webscout/Provider/Jadve.py +23 -38
- webscout/Provider/K2Think.py +308 -0
- webscout/Provider/Koboldai.py +8 -186
- webscout/Provider/LambdaChat.py +2 -4
- webscout/Provider/Nemotron.py +3 -4
- webscout/Provider/Netwrck.py +3 -2
- webscout/Provider/OLLAMA.py +1 -0
- webscout/Provider/OPENAI/Cloudflare.py +6 -7
- webscout/Provider/OPENAI/FalconH1.py +2 -7
- webscout/Provider/OPENAI/FreeGemini.py +6 -8
- webscout/Provider/OPENAI/{monochat.py → K2Think.py} +180 -77
- webscout/Provider/OPENAI/NEMOTRON.py +3 -6
- webscout/Provider/OPENAI/PI.py +5 -4
- webscout/Provider/OPENAI/Qwen3.py +2 -3
- webscout/Provider/OPENAI/TogetherAI.py +2 -2
- webscout/Provider/OPENAI/TwoAI.py +3 -4
- webscout/Provider/OPENAI/__init__.py +17 -58
- webscout/Provider/OPENAI/ai4chat.py +313 -303
- webscout/Provider/OPENAI/base.py +9 -29
- webscout/Provider/OPENAI/chatgpt.py +7 -2
- webscout/Provider/OPENAI/chatgptclone.py +4 -7
- webscout/Provider/OPENAI/chatsandbox.py +84 -59
- webscout/Provider/OPENAI/deepinfra.py +6 -6
- webscout/Provider/OPENAI/heckai.py +4 -1
- webscout/Provider/OPENAI/netwrck.py +1 -0
- webscout/Provider/OPENAI/scirachat.py +6 -0
- webscout/Provider/OPENAI/textpollinations.py +3 -11
- webscout/Provider/OPENAI/toolbaz.py +14 -11
- webscout/Provider/OpenGPT.py +1 -1
- webscout/Provider/Openai.py +150 -402
- webscout/Provider/PI.py +1 -0
- webscout/Provider/Perplexitylabs.py +1 -2
- webscout/Provider/QwenLM.py +107 -89
- webscout/Provider/STT/__init__.py +17 -2
- webscout/Provider/{Llama3.py → Sambanova.py} +9 -10
- webscout/Provider/StandardInput.py +1 -1
- webscout/Provider/TTI/__init__.py +18 -12
- webscout/Provider/TTS/__init__.py +18 -10
- webscout/Provider/TeachAnything.py +1 -0
- webscout/Provider/TextPollinationsAI.py +5 -12
- webscout/Provider/TogetherAI.py +86 -87
- webscout/Provider/TwoAI.py +53 -309
- webscout/Provider/TypliAI.py +2 -1
- webscout/Provider/{GizAI.py → UNFINISHED/GizAI.py} +1 -1
- webscout/Provider/Venice.py +2 -1
- webscout/Provider/VercelAI.py +1 -0
- webscout/Provider/WiseCat.py +2 -1
- webscout/Provider/WrDoChat.py +2 -1
- webscout/Provider/__init__.py +18 -86
- webscout/Provider/ai4chat.py +1 -1
- webscout/Provider/akashgpt.py +7 -10
- webscout/Provider/cerebras.py +115 -9
- webscout/Provider/chatglm.py +170 -83
- webscout/Provider/cleeai.py +1 -2
- webscout/Provider/deepseek_assistant.py +1 -1
- webscout/Provider/elmo.py +1 -1
- webscout/Provider/geminiapi.py +1 -1
- webscout/Provider/granite.py +1 -1
- webscout/Provider/hermes.py +1 -3
- webscout/Provider/julius.py +1 -0
- webscout/Provider/learnfastai.py +1 -1
- webscout/Provider/llama3mitril.py +1 -1
- webscout/Provider/llmchat.py +1 -1
- webscout/Provider/llmchatco.py +1 -1
- webscout/Provider/meta.py +3 -3
- webscout/Provider/oivscode.py +2 -2
- webscout/Provider/scira_chat.py +51 -124
- webscout/Provider/searchchat.py +1 -0
- webscout/Provider/sonus.py +1 -1
- webscout/Provider/toolbaz.py +15 -12
- webscout/Provider/turboseek.py +31 -22
- webscout/Provider/typefully.py +2 -1
- webscout/Provider/x0gpt.py +1 -0
- webscout/Provider/yep.py +2 -1
- webscout/tempid.py +6 -0
- webscout/version.py +1 -1
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/METADATA +2 -1
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/RECORD +103 -129
- webscout/Provider/AllenAI.py +0 -440
- webscout/Provider/Blackboxai.py +0 -793
- webscout/Provider/FreeGemini.py +0 -250
- webscout/Provider/GptOss.py +0 -207
- webscout/Provider/Hunyuan.py +0 -283
- webscout/Provider/Kimi.py +0 -445
- webscout/Provider/MCPCore.py +0 -322
- webscout/Provider/MiniMax.py +0 -207
- webscout/Provider/OPENAI/BLACKBOXAI.py +0 -1045
- webscout/Provider/OPENAI/MiniMax.py +0 -298
- webscout/Provider/OPENAI/autoproxy.py +0 -1067
- webscout/Provider/OPENAI/copilot.py +0 -321
- webscout/Provider/OPENAI/gptoss.py +0 -288
- webscout/Provider/OPENAI/kimi.py +0 -469
- webscout/Provider/OPENAI/mcpcore.py +0 -431
- webscout/Provider/OPENAI/multichat.py +0 -378
- webscout/Provider/Reka.py +0 -214
- webscout/Provider/UNFINISHED/fetch_together_models.py +0 -90
- webscout/Provider/asksteve.py +0 -220
- webscout/Provider/copilot.py +0 -441
- webscout/Provider/freeaichat.py +0 -294
- webscout/Provider/koala.py +0 -182
- webscout/Provider/lmarena.py +0 -198
- webscout/Provider/monochat.py +0 -275
- webscout/Provider/multichat.py +0 -375
- webscout/Provider/scnet.py +0 -244
- webscout/Provider/talkai.py +0 -194
- /webscout/Provider/{Marcus.py → UNFINISHED/Marcus.py} +0 -0
- /webscout/Provider/{Qodo.py → UNFINISHED/Qodo.py} +0 -0
- /webscout/Provider/{XenAI.py → UNFINISHED/XenAI.py} +0 -0
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/WHEEL +0 -0
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/entry_points.txt +0 -0
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/licenses/LICENSE.md +0 -0
- {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/top_level.txt +0 -0
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
import uuid
|
|
3
|
-
import time
|
|
4
|
-
from typing import List, Dict, Optional, Union, Generator, Any
|
|
5
|
-
from urllib.parse import quote
|
|
6
|
-
from curl_cffi.requests import Session, CurlWsFlag
|
|
7
|
-
|
|
8
|
-
# Import base classes and utility structures
|
|
9
|
-
from .base import OpenAICompatibleProvider, BaseChat, BaseCompletions
|
|
10
|
-
from .utils import (
|
|
11
|
-
ChatCompletionChunk, ChatCompletion, Choice, ChoiceDelta,
|
|
12
|
-
ChatCompletionMessage, CompletionUsage, format_prompt, count_tokens
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
# Attempt to import LitAgent, fallback if not available
|
|
16
|
-
try:
|
|
17
|
-
from webscout.litagent import LitAgent
|
|
18
|
-
except ImportError:
|
|
19
|
-
pass
|
|
20
|
-
|
|
21
|
-
# --- Microsoft Copilot Client ---
|
|
22
|
-
|
|
23
|
-
class Completions(BaseCompletions):
|
|
24
|
-
def __init__(self, client: 'Copilot'):
|
|
25
|
-
self._client = client
|
|
26
|
-
|
|
27
|
-
def create(
|
|
28
|
-
self,
|
|
29
|
-
*,
|
|
30
|
-
model: str,
|
|
31
|
-
messages: List[Dict[str, str]],
|
|
32
|
-
max_tokens: Optional[int] = None,
|
|
33
|
-
stream: bool = False,
|
|
34
|
-
temperature: Optional[float] = None,
|
|
35
|
-
top_p: Optional[float] = None,
|
|
36
|
-
timeout: Optional[int] = None,
|
|
37
|
-
proxies: Optional[dict] = None,
|
|
38
|
-
**kwargs: Any
|
|
39
|
-
) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
|
|
40
|
-
"""
|
|
41
|
-
Creates a model response for the given chat conversation.
|
|
42
|
-
Mimics openai.chat.completions.create
|
|
43
|
-
"""
|
|
44
|
-
# Format the entire conversation using the utility function
|
|
45
|
-
formatted_prompt = format_prompt(messages, add_special_tokens=True, include_system=True, do_continue=True)
|
|
46
|
-
|
|
47
|
-
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
48
|
-
created_time = int(time.time())
|
|
49
|
-
|
|
50
|
-
# Handle image if provided
|
|
51
|
-
image = kwargs.get("image")
|
|
52
|
-
|
|
53
|
-
if stream:
|
|
54
|
-
return self._create_stream(request_id, created_time, model, formatted_prompt, image, timeout=timeout, proxies=proxies)
|
|
55
|
-
else:
|
|
56
|
-
return self._create_non_stream(request_id, created_time, model, formatted_prompt, image, timeout=timeout, proxies=proxies)
|
|
57
|
-
|
|
58
|
-
def _create_stream(
|
|
59
|
-
self, request_id: str, created_time: int, model: str, prompt_text: str, image: Optional[bytes] = None,
|
|
60
|
-
timeout: Optional[int] = None, proxies: Optional[dict] = None
|
|
61
|
-
) -> Generator[ChatCompletionChunk, None, None]:
|
|
62
|
-
original_proxies = self._client.session.proxies
|
|
63
|
-
if proxies is not None:
|
|
64
|
-
self._client.session.proxies = proxies
|
|
65
|
-
else:
|
|
66
|
-
self._client.session.proxies = {}
|
|
67
|
-
try:
|
|
68
|
-
timeout_val = timeout if timeout is not None else self._client.timeout
|
|
69
|
-
s = self._client.session
|
|
70
|
-
# Create a new conversation if needed
|
|
71
|
-
r = s.post(self._client.conversation_url, timeout=timeout_val)
|
|
72
|
-
if r.status_code != 200:
|
|
73
|
-
raise RuntimeError(f"Failed to create conversation: {r.text}")
|
|
74
|
-
conv_id = r.json().get("id")
|
|
75
|
-
|
|
76
|
-
# Handle image upload if provided
|
|
77
|
-
images = []
|
|
78
|
-
if image:
|
|
79
|
-
r = s.post(
|
|
80
|
-
f"{self._client.url}/c/api/attachments",
|
|
81
|
-
headers={"content-type": "image/jpeg"},
|
|
82
|
-
data=image,
|
|
83
|
-
timeout=timeout_val
|
|
84
|
-
)
|
|
85
|
-
if r.status_code != 200:
|
|
86
|
-
raise RuntimeError(f"Image upload failed: {r.text}")
|
|
87
|
-
images.append({"type": "image", "url": r.json().get("url")})
|
|
88
|
-
|
|
89
|
-
ws = s.ws_connect(self._client.websocket_url)
|
|
90
|
-
# Map alias to real model name if needed
|
|
91
|
-
real_model = Copilot.MODEL_ALIASES.get(model, model)
|
|
92
|
-
if real_model not in Copilot.AVAILABLE_MODELS:
|
|
93
|
-
raise RuntimeError(f"Invalid model: {model}. Choose from: {Copilot.AVAILABLE_MODELS}")
|
|
94
|
-
if real_model == "Smart":
|
|
95
|
-
mode = "smart"
|
|
96
|
-
elif "Think" in real_model:
|
|
97
|
-
mode = "reasoning"
|
|
98
|
-
else:
|
|
99
|
-
mode = "chat"
|
|
100
|
-
ws.send(json.dumps({
|
|
101
|
-
"event": "send",
|
|
102
|
-
"conversationId": conv_id,
|
|
103
|
-
"content": images + [{"type": "text", "text": prompt_text}],
|
|
104
|
-
"mode": mode,
|
|
105
|
-
"model": real_model
|
|
106
|
-
}).encode(), CurlWsFlag.TEXT)
|
|
107
|
-
|
|
108
|
-
prompt_tokens = count_tokens(prompt_text)
|
|
109
|
-
completion_tokens = 0
|
|
110
|
-
total_tokens = prompt_tokens
|
|
111
|
-
started = False
|
|
112
|
-
image_prompt = None
|
|
113
|
-
while True:
|
|
114
|
-
try:
|
|
115
|
-
msg = json.loads(ws.recv()[0])
|
|
116
|
-
except Exception:
|
|
117
|
-
break
|
|
118
|
-
|
|
119
|
-
event = msg.get("event")
|
|
120
|
-
if event not in ["appendText", "done", "error", "generatingImage", "imageGenerated", "suggestedFollowups", "replaceText"]:
|
|
121
|
-
print(f"[Copilot] Unhandled event: {event} | msg: {msg}")
|
|
122
|
-
|
|
123
|
-
if event == "appendText":
|
|
124
|
-
started = True
|
|
125
|
-
content = msg.get("text", "")
|
|
126
|
-
content_tokens = count_tokens(content)
|
|
127
|
-
completion_tokens += content_tokens
|
|
128
|
-
total_tokens = prompt_tokens + completion_tokens
|
|
129
|
-
delta = ChoiceDelta(
|
|
130
|
-
content=content,
|
|
131
|
-
role="assistant"
|
|
132
|
-
)
|
|
133
|
-
choice = Choice(
|
|
134
|
-
index=0,
|
|
135
|
-
delta=delta,
|
|
136
|
-
finish_reason=None
|
|
137
|
-
)
|
|
138
|
-
chunk = ChatCompletionChunk(
|
|
139
|
-
id=request_id,
|
|
140
|
-
choices=[choice],
|
|
141
|
-
created=created_time,
|
|
142
|
-
model=model
|
|
143
|
-
)
|
|
144
|
-
yield chunk
|
|
145
|
-
elif event == "replaceText":
|
|
146
|
-
# treat as appendText for OpenAI compatibility
|
|
147
|
-
content = msg.get("text", "")
|
|
148
|
-
content_tokens = count_tokens(content)
|
|
149
|
-
completion_tokens += content_tokens
|
|
150
|
-
total_tokens = prompt_tokens + completion_tokens
|
|
151
|
-
delta = ChoiceDelta(
|
|
152
|
-
content=content,
|
|
153
|
-
role="assistant"
|
|
154
|
-
)
|
|
155
|
-
choice = Choice(
|
|
156
|
-
index=0,
|
|
157
|
-
delta=delta,
|
|
158
|
-
finish_reason=None
|
|
159
|
-
)
|
|
160
|
-
chunk = ChatCompletionChunk(
|
|
161
|
-
id=request_id,
|
|
162
|
-
choices=[choice],
|
|
163
|
-
created=created_time,
|
|
164
|
-
model=model
|
|
165
|
-
)
|
|
166
|
-
yield chunk
|
|
167
|
-
elif event == "generatingImage":
|
|
168
|
-
image_prompt = msg.get("prompt")
|
|
169
|
-
elif event == "imageGenerated":
|
|
170
|
-
# Yield a chunk with image metadata in the delta (custom extension)
|
|
171
|
-
delta = ChoiceDelta(
|
|
172
|
-
content=None,
|
|
173
|
-
role=None
|
|
174
|
-
)
|
|
175
|
-
choice = Choice(
|
|
176
|
-
index=0,
|
|
177
|
-
delta=delta,
|
|
178
|
-
finish_reason=None
|
|
179
|
-
)
|
|
180
|
-
chunk = ChatCompletionChunk(
|
|
181
|
-
id=request_id,
|
|
182
|
-
choices=[choice],
|
|
183
|
-
created=created_time,
|
|
184
|
-
model=model
|
|
185
|
-
)
|
|
186
|
-
chunk.image_url = msg.get("url")
|
|
187
|
-
chunk.image_prompt = image_prompt
|
|
188
|
-
chunk.image_preview = msg.get("thumbnailUrl")
|
|
189
|
-
yield chunk
|
|
190
|
-
elif event == "suggestedFollowups":
|
|
191
|
-
# Yield a chunk with followups in the delta (custom extension)
|
|
192
|
-
delta = ChoiceDelta(
|
|
193
|
-
content=None,
|
|
194
|
-
role=None
|
|
195
|
-
)
|
|
196
|
-
choice = Choice(
|
|
197
|
-
index=0,
|
|
198
|
-
delta=delta,
|
|
199
|
-
finish_reason=None
|
|
200
|
-
)
|
|
201
|
-
chunk = ChatCompletionChunk(
|
|
202
|
-
id=request_id,
|
|
203
|
-
choices=[choice],
|
|
204
|
-
created=created_time,
|
|
205
|
-
model=model
|
|
206
|
-
)
|
|
207
|
-
chunk.suggested_followups = msg.get("suggestions")
|
|
208
|
-
yield chunk
|
|
209
|
-
elif event == "done":
|
|
210
|
-
delta = ChoiceDelta(
|
|
211
|
-
content=None,
|
|
212
|
-
role=None
|
|
213
|
-
)
|
|
214
|
-
choice = Choice(
|
|
215
|
-
index=0,
|
|
216
|
-
delta=delta,
|
|
217
|
-
finish_reason="stop"
|
|
218
|
-
)
|
|
219
|
-
chunk = ChatCompletionChunk(
|
|
220
|
-
id=request_id,
|
|
221
|
-
choices=[choice],
|
|
222
|
-
created=created_time,
|
|
223
|
-
model=model
|
|
224
|
-
)
|
|
225
|
-
yield chunk
|
|
226
|
-
break
|
|
227
|
-
elif event == "error":
|
|
228
|
-
print(f"[Copilot] Error event: {msg}")
|
|
229
|
-
raise RuntimeError(f"Copilot error: {msg}")
|
|
230
|
-
|
|
231
|
-
ws.close()
|
|
232
|
-
if not started:
|
|
233
|
-
raise RuntimeError("No response received from Copilot")
|
|
234
|
-
except Exception as e:
|
|
235
|
-
raise RuntimeError(f"Stream error: {e}") from e
|
|
236
|
-
finally:
|
|
237
|
-
self._client.session.proxies = original_proxies
|
|
238
|
-
|
|
239
|
-
def _create_non_stream(
|
|
240
|
-
self, request_id: str, created_time: int, model: str, prompt_text: str, image: Optional[bytes] = None,
|
|
241
|
-
timeout: Optional[int] = None, proxies: Optional[dict] = None
|
|
242
|
-
) -> ChatCompletion:
|
|
243
|
-
result = ""
|
|
244
|
-
# Pass timeout and proxies to the underlying _create_stream call
|
|
245
|
-
for chunk in self._create_stream(request_id, created_time, model, prompt_text, image, timeout=timeout, proxies=proxies):
|
|
246
|
-
if hasattr(chunk, 'choices') and chunk.choices and hasattr(chunk.choices[0], 'delta') and chunk.choices[0].delta.content:
|
|
247
|
-
result += chunk.choices[0].delta.content
|
|
248
|
-
|
|
249
|
-
# Create the message object
|
|
250
|
-
message = ChatCompletionMessage(
|
|
251
|
-
role="assistant",
|
|
252
|
-
content=result
|
|
253
|
-
)
|
|
254
|
-
|
|
255
|
-
# Create the choice object
|
|
256
|
-
choice = Choice(
|
|
257
|
-
index=0,
|
|
258
|
-
message=message,
|
|
259
|
-
finish_reason="stop"
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
# Estimate token usage using count_tokens
|
|
263
|
-
prompt_tokens = count_tokens(prompt_text)
|
|
264
|
-
completion_tokens = count_tokens(result)
|
|
265
|
-
total_tokens = prompt_tokens + completion_tokens
|
|
266
|
-
|
|
267
|
-
# Create usage object
|
|
268
|
-
usage = CompletionUsage(
|
|
269
|
-
prompt_tokens=prompt_tokens,
|
|
270
|
-
completion_tokens=completion_tokens,
|
|
271
|
-
total_tokens=total_tokens
|
|
272
|
-
)
|
|
273
|
-
|
|
274
|
-
# Create the completion object
|
|
275
|
-
completion = ChatCompletion(
|
|
276
|
-
id=request_id,
|
|
277
|
-
choices=[choice],
|
|
278
|
-
created=created_time,
|
|
279
|
-
model=model,
|
|
280
|
-
usage=usage
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
return completion
|
|
284
|
-
|
|
285
|
-
class Chat(BaseChat):
|
|
286
|
-
def __init__(self, client: 'Copilot'):
|
|
287
|
-
self.completions = Completions(client)
|
|
288
|
-
|
|
289
|
-
class Copilot(OpenAICompatibleProvider):
|
|
290
|
-
|
|
291
|
-
url = "https://copilot.microsoft.com"
|
|
292
|
-
conversation_url = f"{url}/c/api/conversations"
|
|
293
|
-
websocket_url = "wss://copilot.microsoft.com/c/api/chat?api-version=2"
|
|
294
|
-
|
|
295
|
-
AVAILABLE_MODELS = ["Copilot", "Think Deeper", "Smart"]
|
|
296
|
-
MODEL_ALIASES = {
|
|
297
|
-
"gpt-4o": "Copilot",
|
|
298
|
-
"o4-mini": "Think Deeper",
|
|
299
|
-
"gpt-5": "Smart",
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
def __init__(self, browser: str = "chrome", tools: Optional[List] = None, **kwargs):
|
|
304
|
-
self.timeout = 900
|
|
305
|
-
self.session = Session(impersonate=browser)
|
|
306
|
-
self.session.proxies = {}
|
|
307
|
-
|
|
308
|
-
# Initialize tools
|
|
309
|
-
self.available_tools = {}
|
|
310
|
-
if tools:
|
|
311
|
-
self.register_tools(tools)
|
|
312
|
-
|
|
313
|
-
# Set up the chat interface
|
|
314
|
-
self.chat = Chat(self)
|
|
315
|
-
|
|
316
|
-
@property
|
|
317
|
-
def models(self):
|
|
318
|
-
class _ModelList:
|
|
319
|
-
def list(inner_self):
|
|
320
|
-
return self.AVAILABLE_MODELS
|
|
321
|
-
return _ModelList()
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
import time
|
|
4
|
-
import uuid
|
|
5
|
-
from typing import List, Dict, Optional, Union, Generator, Any
|
|
6
|
-
|
|
7
|
-
from webscout.Provider.OPENAI.base import OpenAICompatibleProvider, BaseChat, BaseCompletions
|
|
8
|
-
from webscout.Provider.OPENAI.utils import (
|
|
9
|
-
ChatCompletionChunk, ChatCompletion, Choice, ChoiceDelta,
|
|
10
|
-
ChatCompletionMessage, CompletionUsage, format_prompt, count_tokens
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
try:
|
|
14
|
-
from webscout.litagent import LitAgent
|
|
15
|
-
except ImportError:
|
|
16
|
-
pass
|
|
17
|
-
|
|
18
|
-
class Completions(BaseCompletions):
|
|
19
|
-
def __init__(self, client: 'GptOss'):
|
|
20
|
-
self._client = client
|
|
21
|
-
|
|
22
|
-
def create(
|
|
23
|
-
self,
|
|
24
|
-
*,
|
|
25
|
-
model: str,
|
|
26
|
-
messages: List[Dict[str, str]],
|
|
27
|
-
max_tokens: Optional[int] = 600,
|
|
28
|
-
stream: bool = False,
|
|
29
|
-
temperature: Optional[float] = None,
|
|
30
|
-
top_p: Optional[float] = None,
|
|
31
|
-
timeout: Optional[int] = None,
|
|
32
|
-
proxies: Optional[Dict[str, str]] = None,
|
|
33
|
-
**kwargs: Any
|
|
34
|
-
) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
|
|
35
|
-
# Format messages into conversation prompt
|
|
36
|
-
conversation_prompt = format_prompt(messages, add_special_tokens=False, do_continue=True)
|
|
37
|
-
|
|
38
|
-
# Count tokens for usage tracking
|
|
39
|
-
prompt_tokens = count_tokens(conversation_prompt)
|
|
40
|
-
|
|
41
|
-
payload = {
|
|
42
|
-
"op": "threads.create",
|
|
43
|
-
"params": {
|
|
44
|
-
"input": {
|
|
45
|
-
"text": conversation_prompt,
|
|
46
|
-
"content": [{"type": "input_text", "text": conversation_prompt}],
|
|
47
|
-
"quoted_text": "",
|
|
48
|
-
"attachments": []
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
54
|
-
created_time = int(time.time())
|
|
55
|
-
|
|
56
|
-
if stream:
|
|
57
|
-
return self._create_stream(request_id, created_time, model, payload, timeout, proxies, prompt_tokens)
|
|
58
|
-
else:
|
|
59
|
-
return self._create_non_stream(request_id, created_time, model, payload, timeout, proxies, prompt_tokens)
|
|
60
|
-
|
|
61
|
-
def _create_stream(
|
|
62
|
-
self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
|
|
63
|
-
timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None,
|
|
64
|
-
prompt_tokens: int = 0
|
|
65
|
-
) -> Generator[ChatCompletionChunk, None, None]:
|
|
66
|
-
try:
|
|
67
|
-
response = self._client.session.post(
|
|
68
|
-
self._client.base_url,
|
|
69
|
-
headers=self._client.headers,
|
|
70
|
-
json=payload,
|
|
71
|
-
stream=True,
|
|
72
|
-
timeout=timeout or self._client.timeout,
|
|
73
|
-
proxies=proxies
|
|
74
|
-
)
|
|
75
|
-
response.raise_for_status()
|
|
76
|
-
|
|
77
|
-
completion_tokens = 0
|
|
78
|
-
total_tokens = prompt_tokens
|
|
79
|
-
|
|
80
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
81
|
-
if line and line.startswith("data: "):
|
|
82
|
-
json_str = line[6:]
|
|
83
|
-
if json_str == "[DONE]":
|
|
84
|
-
break
|
|
85
|
-
try:
|
|
86
|
-
data = json.loads(json_str)
|
|
87
|
-
|
|
88
|
-
# Extract content from GptOss response format
|
|
89
|
-
content = None
|
|
90
|
-
if (data.get('type') == 'thread.item_updated' and
|
|
91
|
-
data.get('update', {}).get('type') == 'assistant_message.content_part.text_delta'):
|
|
92
|
-
content = data.get('update', {}).get('delta')
|
|
93
|
-
|
|
94
|
-
if content:
|
|
95
|
-
# Count tokens in the content chunk
|
|
96
|
-
chunk_tokens = count_tokens(content)
|
|
97
|
-
completion_tokens += chunk_tokens
|
|
98
|
-
total_tokens = prompt_tokens + completion_tokens
|
|
99
|
-
|
|
100
|
-
delta = ChoiceDelta(
|
|
101
|
-
content=content,
|
|
102
|
-
role="assistant"
|
|
103
|
-
)
|
|
104
|
-
choice = Choice(
|
|
105
|
-
index=0,
|
|
106
|
-
delta=delta,
|
|
107
|
-
finish_reason=None
|
|
108
|
-
)
|
|
109
|
-
chunk = ChatCompletionChunk(
|
|
110
|
-
id=request_id,
|
|
111
|
-
choices=[choice],
|
|
112
|
-
created=created_time,
|
|
113
|
-
model=model
|
|
114
|
-
)
|
|
115
|
-
chunk.usage = {
|
|
116
|
-
"prompt_tokens": prompt_tokens,
|
|
117
|
-
"completion_tokens": completion_tokens,
|
|
118
|
-
"total_tokens": total_tokens,
|
|
119
|
-
"estimated_cost": None
|
|
120
|
-
}
|
|
121
|
-
yield chunk
|
|
122
|
-
except json.JSONDecodeError:
|
|
123
|
-
continue
|
|
124
|
-
|
|
125
|
-
# Final chunk with finish_reason="stop"
|
|
126
|
-
delta = ChoiceDelta(content=None, role=None)
|
|
127
|
-
choice = Choice(index=0, delta=delta, finish_reason="stop")
|
|
128
|
-
chunk = ChatCompletionChunk(
|
|
129
|
-
id=request_id,
|
|
130
|
-
choices=[choice],
|
|
131
|
-
created=created_time,
|
|
132
|
-
model=model
|
|
133
|
-
)
|
|
134
|
-
chunk.usage = {
|
|
135
|
-
"prompt_tokens": prompt_tokens,
|
|
136
|
-
"completion_tokens": completion_tokens,
|
|
137
|
-
"total_tokens": total_tokens,
|
|
138
|
-
"estimated_cost": None
|
|
139
|
-
}
|
|
140
|
-
yield chunk
|
|
141
|
-
|
|
142
|
-
except Exception as e:
|
|
143
|
-
print(f"Error during GptOss stream request: {e}")
|
|
144
|
-
raise IOError(f"GptOss request failed: {e}") from e
|
|
145
|
-
|
|
146
|
-
def _create_non_stream(
|
|
147
|
-
self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
|
|
148
|
-
timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None,
|
|
149
|
-
prompt_tokens: int = 0
|
|
150
|
-
) -> ChatCompletion:
|
|
151
|
-
try:
|
|
152
|
-
response = self._client.session.post(
|
|
153
|
-
self._client.base_url,
|
|
154
|
-
headers=self._client.headers,
|
|
155
|
-
json=payload,
|
|
156
|
-
stream=True, # GptOss API is event-stream only
|
|
157
|
-
timeout=timeout or self._client.timeout,
|
|
158
|
-
proxies=proxies
|
|
159
|
-
)
|
|
160
|
-
response.raise_for_status()
|
|
161
|
-
|
|
162
|
-
# Collect all chunks to form complete response
|
|
163
|
-
full_content = ""
|
|
164
|
-
completion_tokens = 0
|
|
165
|
-
|
|
166
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
167
|
-
if line and line.startswith("data: "):
|
|
168
|
-
json_str = line[6:]
|
|
169
|
-
if json_str == "[DONE]":
|
|
170
|
-
break
|
|
171
|
-
try:
|
|
172
|
-
data = json.loads(json_str)
|
|
173
|
-
|
|
174
|
-
# Extract content from GptOss response format
|
|
175
|
-
if (data.get('type') == 'thread.item_updated' and
|
|
176
|
-
data.get('update', {}).get('type') == 'assistant_message.content_part.text_delta'):
|
|
177
|
-
content = data.get('update', {}).get('delta')
|
|
178
|
-
if content:
|
|
179
|
-
full_content += content
|
|
180
|
-
except json.JSONDecodeError:
|
|
181
|
-
continue
|
|
182
|
-
|
|
183
|
-
# Count tokens in the complete response
|
|
184
|
-
completion_tokens = count_tokens(full_content)
|
|
185
|
-
total_tokens = prompt_tokens + completion_tokens
|
|
186
|
-
|
|
187
|
-
message = ChatCompletionMessage(
|
|
188
|
-
role="assistant",
|
|
189
|
-
content=full_content
|
|
190
|
-
)
|
|
191
|
-
choice = Choice(
|
|
192
|
-
index=0,
|
|
193
|
-
message=message,
|
|
194
|
-
finish_reason="stop"
|
|
195
|
-
)
|
|
196
|
-
usage = CompletionUsage(
|
|
197
|
-
prompt_tokens=prompt_tokens,
|
|
198
|
-
completion_tokens=completion_tokens,
|
|
199
|
-
total_tokens=total_tokens
|
|
200
|
-
)
|
|
201
|
-
completion = ChatCompletion(
|
|
202
|
-
id=request_id,
|
|
203
|
-
choices=[choice],
|
|
204
|
-
created=created_time,
|
|
205
|
-
model=model,
|
|
206
|
-
usage=usage
|
|
207
|
-
)
|
|
208
|
-
return completion
|
|
209
|
-
|
|
210
|
-
except Exception as e:
|
|
211
|
-
print(f"Error during GptOss non-stream request: {e}")
|
|
212
|
-
raise IOError(f"GptOss request failed: {e}") from e
|
|
213
|
-
|
|
214
|
-
class Chat(BaseChat):
|
|
215
|
-
def __init__(self, client: 'GptOss'):
|
|
216
|
-
self.completions = Completions(client)
|
|
217
|
-
|
|
218
|
-
class GptOss(OpenAICompatibleProvider):
|
|
219
|
-
AVAILABLE_MODELS = ["gpt-oss-20b", "gpt-oss-120b"]
|
|
220
|
-
|
|
221
|
-
def __init__(
|
|
222
|
-
self,
|
|
223
|
-
browser: str = "chrome",
|
|
224
|
-
api_key: str = None,
|
|
225
|
-
model: str = "gpt-oss-120b",
|
|
226
|
-
reasoning_effort: str = "high",
|
|
227
|
-
timeout: int = 30,
|
|
228
|
-
**kwargs
|
|
229
|
-
):
|
|
230
|
-
super().__init__(api_key=api_key, **kwargs)
|
|
231
|
-
self.timeout = timeout
|
|
232
|
-
self.base_url = "https://api.gpt-oss.com/chatkit"
|
|
233
|
-
self.model = model if model in self.AVAILABLE_MODELS else self.AVAILABLE_MODELS[0]
|
|
234
|
-
self.reasoning_effort = reasoning_effort
|
|
235
|
-
self.session = requests.Session()
|
|
236
|
-
|
|
237
|
-
# Generate headers using LitAgent
|
|
238
|
-
try:
|
|
239
|
-
agent = LitAgent()
|
|
240
|
-
fingerprint = agent.generate_fingerprint(browser)
|
|
241
|
-
self.headers = {
|
|
242
|
-
"Accept": "text/event-stream",
|
|
243
|
-
"Accept-Encoding": fingerprint.get("accept_encoding", "gzip, deflate, br"),
|
|
244
|
-
"Accept-Language": fingerprint.get("accept_language", "en-US,en;q=0.9"),
|
|
245
|
-
"Content-Type": "application/json",
|
|
246
|
-
"Cache-Control": "no-cache",
|
|
247
|
-
"Connection": "keep-alive",
|
|
248
|
-
"Pragma": "no-cache",
|
|
249
|
-
"User-Agent": fingerprint.get("user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"),
|
|
250
|
-
"x-reasoning-effort": self.reasoning_effort,
|
|
251
|
-
"x-selected-model": self.model,
|
|
252
|
-
"x-show-reasoning": "true"
|
|
253
|
-
}
|
|
254
|
-
except:
|
|
255
|
-
# Fallback headers if LitAgent fails
|
|
256
|
-
self.headers = {
|
|
257
|
-
"Accept": "text/event-stream",
|
|
258
|
-
"Accept-Encoding": "gzip, deflate, br",
|
|
259
|
-
"Accept-Language": "en-US,en;q=0.9",
|
|
260
|
-
"Content-Type": "application/json",
|
|
261
|
-
"Cache-Control": "no-cache",
|
|
262
|
-
"Connection": "keep-alive",
|
|
263
|
-
"Pragma": "no-cache",
|
|
264
|
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
|
265
|
-
"x-reasoning-effort": self.reasoning_effort,
|
|
266
|
-
"x-selected-model": self.model,
|
|
267
|
-
"x-show-reasoning": "true"
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
self.session.headers.update(self.headers)
|
|
271
|
-
self.chat = Chat(self)
|
|
272
|
-
|
|
273
|
-
@property
|
|
274
|
-
def models(self):
|
|
275
|
-
class _ModelList:
|
|
276
|
-
def list(inner_self):
|
|
277
|
-
return type(self).AVAILABLE_MODELS
|
|
278
|
-
return _ModelList()
|
|
279
|
-
|
|
280
|
-
if __name__ == "__main__":
|
|
281
|
-
client = GptOss()
|
|
282
|
-
response = client.chat.completions.create(
|
|
283
|
-
model="gpt-oss-120b",
|
|
284
|
-
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
285
|
-
max_tokens=100,
|
|
286
|
-
stream=False
|
|
287
|
-
)
|
|
288
|
-
print(response)
|