webscout 8.3__py3-none-any.whl → 8.3.1__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.

Files changed (62) hide show
  1. webscout/AIauto.py +4 -4
  2. webscout/AIbase.py +61 -1
  3. webscout/Extra/YTToolkit/ytapi/patterns.py +45 -45
  4. webscout/Extra/YTToolkit/ytapi/stream.py +1 -1
  5. webscout/Extra/YTToolkit/ytapi/video.py +10 -10
  6. webscout/Extra/autocoder/autocoder_utiles.py +1 -1
  7. webscout/Litlogger/formats.py +9 -0
  8. webscout/Litlogger/handlers.py +18 -0
  9. webscout/Litlogger/logger.py +43 -1
  10. webscout/Provider/AISEARCH/scira_search.py +3 -2
  11. webscout/Provider/LambdaChat.py +7 -1
  12. webscout/Provider/OPENAI/BLACKBOXAI.py +1049 -1017
  13. webscout/Provider/OPENAI/Qwen3.py +303 -303
  14. webscout/Provider/OPENAI/README.md +3 -0
  15. webscout/Provider/OPENAI/TogetherAI.py +355 -0
  16. webscout/Provider/OPENAI/__init__.py +2 -1
  17. webscout/Provider/OPENAI/api.py +298 -13
  18. webscout/Provider/OPENAI/autoproxy.py +39 -0
  19. webscout/Provider/OPENAI/base.py +89 -12
  20. webscout/Provider/OPENAI/chatgpt.py +15 -2
  21. webscout/Provider/OPENAI/chatgptclone.py +14 -3
  22. webscout/Provider/OPENAI/deepinfra.py +339 -328
  23. webscout/Provider/OPENAI/e2b.py +295 -73
  24. webscout/Provider/OPENAI/opkfc.py +18 -6
  25. webscout/Provider/OPENAI/scirachat.py +3 -2
  26. webscout/Provider/OPENAI/toolbaz.py +0 -1
  27. webscout/Provider/OPENAI/writecream.py +166 -166
  28. webscout/Provider/OPENAI/x0gpt.py +367 -367
  29. webscout/Provider/OPENAI/yep.py +383 -383
  30. webscout/Provider/STT/__init__.py +3 -0
  31. webscout/Provider/STT/base.py +281 -0
  32. webscout/Provider/STT/elevenlabs.py +265 -0
  33. webscout/Provider/TTI/__init__.py +3 -1
  34. webscout/Provider/TTI/aiarta.py +399 -365
  35. webscout/Provider/TTI/base.py +74 -2
  36. webscout/Provider/TTI/fastflux.py +63 -30
  37. webscout/Provider/TTI/gpt1image.py +149 -0
  38. webscout/Provider/TTI/imagen.py +196 -0
  39. webscout/Provider/TTI/magicstudio.py +60 -29
  40. webscout/Provider/TTI/piclumen.py +43 -32
  41. webscout/Provider/TTI/pixelmuse.py +232 -225
  42. webscout/Provider/TTI/pollinations.py +43 -32
  43. webscout/Provider/TTI/together.py +287 -0
  44. webscout/Provider/TTI/utils.py +2 -1
  45. webscout/Provider/TTS/README.md +1 -0
  46. webscout/Provider/TTS/__init__.py +2 -1
  47. webscout/Provider/TTS/freetts.py +140 -0
  48. webscout/Provider/UNFINISHED/ChutesAI.py +314 -0
  49. webscout/Provider/UNFINISHED/fetch_together_models.py +95 -0
  50. webscout/Provider/__init__.py +3 -0
  51. webscout/Provider/scira_chat.py +3 -2
  52. webscout/Provider/toolbaz.py +0 -1
  53. webscout/litagent/Readme.md +12 -3
  54. webscout/litagent/agent.py +99 -62
  55. webscout/version.py +1 -1
  56. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/METADATA +1 -1
  57. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/RECORD +61 -51
  58. webscout/Provider/TTI/artbit.py +0 -0
  59. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/WHEEL +0 -0
  60. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/entry_points.txt +0 -0
  61. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/licenses/LICENSE.md +0 -0
  62. {webscout-8.3.dist-info → webscout-8.3.1.dist-info}/top_level.txt +0 -0
@@ -1,328 +1,339 @@
1
- import requests
2
- import json
3
- import time
4
- import uuid
5
- from typing import List, Dict, Optional, Union, Generator, Any
6
-
7
- # Import base classes and utility structures
8
- from .base import OpenAICompatibleProvider, BaseChat, BaseCompletions
9
- from .utils import (
10
- ChatCompletionChunk, ChatCompletion, Choice, ChoiceDelta,
11
- ChatCompletionMessage, CompletionUsage
12
- )
13
-
14
- # Attempt to import LitAgent, fallback if not available
15
- try:
16
- from webscout.litagent import LitAgent
17
- except ImportError:
18
- pass
19
-
20
- # --- DeepInfra Client ---
21
-
22
- class Completions(BaseCompletions):
23
- def __init__(self, client: 'DeepInfra'):
24
- self._client = client
25
-
26
- def create(
27
- self,
28
- *,
29
- model: str,
30
- messages: List[Dict[str, str]],
31
- max_tokens: Optional[int] = 2049,
32
- stream: bool = False,
33
- temperature: Optional[float] = None,
34
- top_p: Optional[float] = None,
35
- timeout: Optional[int] = None,
36
- proxies: Optional[Dict[str, str]] = None,
37
- **kwargs: Any
38
- ) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
39
- """
40
- Creates a model response for the given chat conversation.
41
- Mimics openai.chat.completions.create
42
- """
43
- payload = {
44
- "model": model,
45
- "messages": messages,
46
- "max_tokens": max_tokens,
47
- "stream": stream,
48
- }
49
- if temperature is not None:
50
- payload["temperature"] = temperature
51
- if top_p is not None:
52
- payload["top_p"] = top_p
53
-
54
- payload.update(kwargs)
55
-
56
- request_id = f"chatcmpl-{uuid.uuid4()}"
57
- created_time = int(time.time())
58
-
59
- if stream:
60
- return self._create_stream(request_id, created_time, model, payload, timeout, proxies)
61
- else:
62
- return self._create_non_stream(request_id, created_time, model, payload, timeout, proxies)
63
-
64
- def _create_stream(
65
- self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
66
- timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None
67
- ) -> Generator[ChatCompletionChunk, None, None]:
68
- try:
69
- response = self._client.session.post(
70
- self._client.base_url,
71
- headers=self._client.headers,
72
- json=payload,
73
- stream=True,
74
- timeout=timeout or self._client.timeout,
75
- proxies=proxies
76
- )
77
- response.raise_for_status()
78
-
79
- # Track token usage across chunks
80
- prompt_tokens = 0
81
- completion_tokens = 0
82
- total_tokens = 0
83
-
84
- for line in response.iter_lines():
85
- if line:
86
- decoded_line = line.decode('utf-8').strip()
87
-
88
- if decoded_line.startswith("data: "):
89
- json_str = decoded_line[6:]
90
- if json_str == "[DONE]":
91
- # Format the final [DONE] marker in OpenAI format
92
- # print("data: [DONE]")
93
- break
94
-
95
- try:
96
- data = json.loads(json_str)
97
- choice_data = data.get('choices', [{}])[0]
98
- delta_data = choice_data.get('delta', {})
99
- finish_reason = choice_data.get('finish_reason')
100
-
101
- # Update token counts if available
102
- usage_data = data.get('usage', {})
103
- if usage_data:
104
- prompt_tokens = usage_data.get('prompt_tokens', prompt_tokens)
105
- completion_tokens = usage_data.get('completion_tokens', completion_tokens)
106
- total_tokens = usage_data.get('total_tokens', total_tokens)
107
-
108
- # Create the delta object
109
- delta = ChoiceDelta(
110
- content=delta_data.get('content'),
111
- role=delta_data.get('role'),
112
- tool_calls=delta_data.get('tool_calls')
113
- )
114
-
115
- # Create the choice object
116
- choice = Choice(
117
- index=choice_data.get('index', 0),
118
- delta=delta,
119
- finish_reason=finish_reason,
120
- logprobs=choice_data.get('logprobs')
121
- )
122
-
123
- # Create the chunk object
124
- chunk = ChatCompletionChunk(
125
- id=request_id,
126
- choices=[choice],
127
- created=created_time,
128
- model=model,
129
- system_fingerprint=data.get('system_fingerprint')
130
- )
131
-
132
- # Convert chunk to dict using Pydantic's API
133
- if hasattr(chunk, "model_dump"):
134
- chunk_dict = chunk.model_dump(exclude_none=True)
135
- else:
136
- chunk_dict = chunk.dict(exclude_none=True)
137
-
138
- # Add usage information to match OpenAI format
139
- # Even if we don't have real token counts, include estimated usage
140
- # This matches the format in the examples
141
- usage_dict = {
142
- "prompt_tokens": prompt_tokens or 10,
143
- "completion_tokens": completion_tokens or (len(delta_data.get('content', '')) if delta_data.get('content') else 0),
144
- "total_tokens": total_tokens or (10 + (len(delta_data.get('content', '')) if delta_data.get('content') else 0)),
145
- "estimated_cost": None
146
- }
147
-
148
- # Update completion_tokens and total_tokens as we receive more content
149
- if delta_data.get('content'):
150
- completion_tokens += 1
151
- total_tokens = prompt_tokens + completion_tokens
152
- usage_dict["completion_tokens"] = completion_tokens
153
- usage_dict["total_tokens"] = total_tokens
154
-
155
- chunk_dict["usage"] = usage_dict
156
-
157
- # Format the response in OpenAI format exactly as requested
158
- # We need to print the raw string and also yield the chunk object
159
- # This ensures both the console output and the returned object are correct
160
- # print(f"data: {json.dumps(chunk_dict)}")
161
-
162
- # Return the chunk object for internal processing
163
- yield chunk
164
- except json.JSONDecodeError:
165
- print(f"Warning: Could not decode JSON line: {json_str}")
166
- continue
167
- except requests.exceptions.RequestException as e:
168
- print(f"Error during DeepInfra stream request: {e}")
169
- raise IOError(f"DeepInfra request failed: {e}") from e
170
- except Exception as e:
171
- print(f"Error processing DeepInfra stream: {e}")
172
- raise
173
-
174
- def _create_non_stream(
175
- self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
176
- timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None
177
- ) -> ChatCompletion:
178
- try:
179
- response = self._client.session.post(
180
- self._client.base_url,
181
- headers=self._client.headers,
182
- json=payload,
183
- timeout=timeout or self._client.timeout,
184
- proxies=proxies
185
- )
186
- response.raise_for_status()
187
- data = response.json()
188
-
189
- choices_data = data.get('choices', [])
190
- usage_data = data.get('usage', {})
191
-
192
- choices = []
193
- for choice_d in choices_data:
194
- message_d = choice_d.get('message', {})
195
- message = ChatCompletionMessage(
196
- role=message_d.get('role', 'assistant'),
197
- content=message_d.get('content', '')
198
- )
199
- choice = Choice(
200
- index=choice_d.get('index', 0),
201
- message=message,
202
- finish_reason=choice_d.get('finish_reason', 'stop')
203
- )
204
- choices.append(choice)
205
-
206
- usage = CompletionUsage(
207
- prompt_tokens=usage_data.get('prompt_tokens', 0),
208
- completion_tokens=usage_data.get('completion_tokens', 0),
209
- total_tokens=usage_data.get('total_tokens', 0)
210
- )
211
-
212
- completion = ChatCompletion(
213
- id=request_id,
214
- choices=choices,
215
- created=created_time,
216
- model=data.get('model', model),
217
- usage=usage,
218
- )
219
- return completion
220
-
221
- except requests.exceptions.RequestException as e:
222
- print(f"Error during DeepInfra non-stream request: {e}")
223
- raise IOError(f"DeepInfra request failed: {e}") from e
224
- except Exception as e:
225
- print(f"Error processing DeepInfra response: {e}")
226
- raise
227
-
228
- class Chat(BaseChat):
229
- def __init__(self, client: 'DeepInfra'):
230
- self.completions = Completions(client)
231
-
232
- class DeepInfra(OpenAICompatibleProvider):
233
-
234
- AVAILABLE_MODELS = [
235
- # "anthropic/claude-3-7-sonnet-latest", # >>>> NOT WORKING
236
- "deepseek-ai/DeepSeek-R1-0528",
237
- "deepseek-ai/DeepSeek-R1",
238
- "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
239
- "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
240
- "deepseek-ai/DeepSeek-R1-Turbo",
241
- "deepseek-ai/DeepSeek-V3",
242
- "deepseek-ai/DeepSeek-Prover-V2-671B",
243
- "google/gemma-2-27b-it",
244
- "google/gemma-2-9b-it",
245
- "google/gemma-3-12b-it",
246
- "google/gemma-3-27b-it",
247
- "google/gemma-3-4b-it",
248
- "meta-llama/Llama-3.3-70B-Instruct",
249
- "meta-llama/Llama-3.3-70B-Instruct-Turbo",
250
- "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
251
- "meta-llama/Llama-4-Scout-17B-16E-Instruct",
252
- "meta-llama/Llama-Guard-4-12B",
253
- "meta-llama/Meta-Llama-3.1-8B-Instruct",
254
- "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
255
- "microsoft/Phi-4-multimodal-instruct",
256
- "microsoft/WizardLM-2-8x22B",
257
- "microsoft/phi-4",
258
- "microsoft/phi-4-reasoning-plus",
259
- "mistralai/Mistral-Small-24B-Instruct-2501",
260
- "nvidia/Llama-3.1-Nemotron-70B-Instruct",
261
- "Qwen/QwQ-32B",
262
- "Qwen/Qwen2.5-72B-Instruct",
263
- "Qwen/Qwen2.5-Coder-32B-Instruct",
264
- "Qwen/Qwen3-14B",
265
- "Qwen/Qwen3-30B-A3B",
266
- "Qwen/Qwen3-32B",
267
- "Qwen/Qwen3-235B-A22B",
268
- # "google/gemini-1.5-flash", # >>>> NOT WORKING
269
- # "google/gemini-1.5-flash-8b", # >>>> NOT WORKING
270
- # "google/gemini-2.0-flash-001", # >>>> NOT WORKING
271
-
272
- # "Gryphe/MythoMax-L2-13b", # >>>> NOT WORKING
273
-
274
- # "meta-llama/Llama-3.2-1B-Instruct", # >>>> NOT WORKING
275
- # "meta-llama/Llama-3.2-3B-Instruct", # >>>> NOT WORKING
276
- # "meta-llama/Llama-3.2-90B-Vision-Instruct", # >>>> NOT WORKING
277
- # "meta-llama/Llama-3.2-11B-Vision-Instruct", # >>>> NOT WORKING
278
- # "meta-llama/Meta-Llama-3-70B-Instruct", # >>>> NOT WORKING
279
- # "meta-llama/Meta-Llama-3-8B-Instruct", # >>>> NOT WORKING
280
- # "meta-llama/Meta-Llama-3.1-70B-Instruct", # >>>> NOT WORKING
281
- # "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", # >>>> NOT WORKING
282
- # "meta-llama/Meta-Llama-3.1-405B-Instruct", # >>>> NOT WORKING
283
- # "mistralai/Mixtral-8x7B-Instruct-v0.1", # >>>> NOT WORKING
284
- # "mistralai/Mistral-7B-Instruct-v0.3", # >>>> NOT WORKING
285
- # "mistralai/Mistral-Nemo-Instruct-2407", # >>>> NOT WORKING
286
- # "NousResearch/Hermes-3-Llama-3.1-405B", # >>>> NOT WORKING
287
- # "NovaSky-AI/Sky-T1-32B-Preview", # >>>> NOT WORKING
288
- # "Qwen/Qwen2.5-7B-Instruct", # >>>> NOT WORKING
289
- # "Sao10K/L3.1-70B-Euryale-v2.2", # >>>> NOT WORKING
290
- # "Sao10K/L3.3-70B-Euryale-v2.3", # >>>> NOT WORKING
291
- ]
292
-
293
- def __init__(self, browser: str = "chrome"):
294
- self.timeout = None # Default timeout
295
- self.base_url = "https://api.deepinfra.com/v1/openai/chat/completions"
296
- self.session = requests.Session()
297
-
298
- agent = LitAgent()
299
- fingerprint = agent.generate_fingerprint(browser)
300
-
301
- self.headers = {
302
- "Accept": fingerprint["accept"],
303
- "Accept-Encoding": "gzip, deflate, br, zstd",
304
- "Accept-Language": fingerprint["accept_language"],
305
- "Content-Type": "application/json",
306
- "Cache-Control": "no-cache",
307
- "Connection": "keep-alive",
308
- "Origin": "https://deepinfra.com",
309
- "Pragma": "no-cache",
310
- "Referer": "https://deepinfra.com/",
311
- "Sec-Fetch-Dest": "empty",
312
- "Sec-Fetch-Mode": "cors",
313
- "Sec-Fetch-Site": "same-site",
314
- "X-Deepinfra-Source": "web-embed",
315
- "Sec-CH-UA": fingerprint["sec_ch_ua"] or '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
316
- "Sec-CH-UA-Mobile": "?0",
317
- "Sec-CH-UA-Platform": f'"{fingerprint["platform"]}"',
318
- "User-Agent": fingerprint["user_agent"],
319
- }
320
- self.session.headers.update(self.headers)
321
- self.chat = Chat(self)
322
-
323
- @property
324
- def models(self):
325
- class _ModelList:
326
- def list(inner_self):
327
- return type(self).AVAILABLE_MODELS
328
- return _ModelList()
1
+ import requests
2
+ import json
3
+ import time
4
+ import uuid
5
+ from typing import List, Dict, Optional, Union, Generator, Any
6
+
7
+ # Import base classes and utility structures
8
+ from webscout.Provider.OPENAI.base import OpenAICompatibleProvider, BaseChat, BaseCompletions
9
+ from webscout.Provider.OPENAI.utils import (
10
+ ChatCompletionChunk, ChatCompletion, Choice, ChoiceDelta,
11
+ ChatCompletionMessage, CompletionUsage
12
+ )
13
+
14
+ # Attempt to import LitAgent, fallback if not available
15
+ try:
16
+ from webscout.litagent import LitAgent
17
+ except ImportError:
18
+ pass
19
+
20
+ # --- DeepInfra Client ---
21
+
22
+ class Completions(BaseCompletions):
23
+ def __init__(self, client: 'DeepInfra'):
24
+ self._client = client
25
+
26
+ def create(
27
+ self,
28
+ *,
29
+ model: str,
30
+ messages: List[Dict[str, str]],
31
+ max_tokens: Optional[int] = 2049,
32
+ stream: bool = False,
33
+ temperature: Optional[float] = None,
34
+ top_p: Optional[float] = None,
35
+ timeout: Optional[int] = None,
36
+ proxies: Optional[Dict[str, str]] = None,
37
+ **kwargs: Any
38
+ ) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
39
+ """
40
+ Creates a model response for the given chat conversation.
41
+ Mimics openai.chat.completions.create
42
+ """
43
+ payload = {
44
+ "model": model,
45
+ "messages": messages,
46
+ "max_tokens": max_tokens,
47
+ "stream": stream,
48
+ }
49
+ if temperature is not None:
50
+ payload["temperature"] = temperature
51
+ if top_p is not None:
52
+ payload["top_p"] = top_p
53
+
54
+ payload.update(kwargs)
55
+
56
+ request_id = f"chatcmpl-{uuid.uuid4()}"
57
+ created_time = int(time.time())
58
+
59
+ if stream:
60
+ return self._create_stream(request_id, created_time, model, payload, timeout, proxies)
61
+ else:
62
+ return self._create_non_stream(request_id, created_time, model, payload, timeout, proxies)
63
+
64
+ def _create_stream(
65
+ self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
66
+ timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None
67
+ ) -> Generator[ChatCompletionChunk, None, None]:
68
+ try:
69
+ response = self._client.session.post(
70
+ self._client.base_url,
71
+ headers=self._client.headers,
72
+ json=payload,
73
+ stream=True,
74
+ timeout=timeout or self._client.timeout,
75
+ proxies=proxies
76
+ )
77
+ response.raise_for_status()
78
+
79
+ # Track token usage across chunks
80
+ prompt_tokens = 0
81
+ completion_tokens = 0
82
+ total_tokens = 0
83
+
84
+ for line in response.iter_lines():
85
+ if line:
86
+ decoded_line = line.decode('utf-8').strip()
87
+
88
+ if decoded_line.startswith("data: "):
89
+ json_str = decoded_line[6:]
90
+ if json_str == "[DONE]":
91
+ # Format the final [DONE] marker in OpenAI format
92
+ # print("data: [DONE]")
93
+ break
94
+
95
+ try:
96
+ data = json.loads(json_str)
97
+ choice_data = data.get('choices', [{}])[0]
98
+ delta_data = choice_data.get('delta', {})
99
+ finish_reason = choice_data.get('finish_reason')
100
+
101
+ # Update token counts if available
102
+ usage_data = data.get('usage', {})
103
+ if usage_data:
104
+ prompt_tokens = usage_data.get('prompt_tokens', prompt_tokens)
105
+ completion_tokens = usage_data.get('completion_tokens', completion_tokens)
106
+ total_tokens = usage_data.get('total_tokens', total_tokens)
107
+
108
+ # Create the delta object
109
+ delta = ChoiceDelta(
110
+ content=delta_data.get('content'),
111
+ role=delta_data.get('role'),
112
+ tool_calls=delta_data.get('tool_calls')
113
+ )
114
+
115
+ # Create the choice object
116
+ choice = Choice(
117
+ index=choice_data.get('index', 0),
118
+ delta=delta,
119
+ finish_reason=finish_reason,
120
+ logprobs=choice_data.get('logprobs')
121
+ )
122
+
123
+ # Create the chunk object
124
+ chunk = ChatCompletionChunk(
125
+ id=request_id,
126
+ choices=[choice],
127
+ created=created_time,
128
+ model=model,
129
+ system_fingerprint=data.get('system_fingerprint')
130
+ )
131
+
132
+ # Convert chunk to dict using Pydantic's API
133
+ if hasattr(chunk, "model_dump"):
134
+ chunk_dict = chunk.model_dump(exclude_none=True)
135
+ else:
136
+ chunk_dict = chunk.dict(exclude_none=True)
137
+
138
+ # Add usage information to match OpenAI format
139
+ # Even if we don't have real token counts, include estimated usage
140
+ # This matches the format in the examples
141
+ usage_dict = {
142
+ "prompt_tokens": prompt_tokens or 10,
143
+ "completion_tokens": completion_tokens or (len(delta_data.get('content', '')) if delta_data.get('content') else 0),
144
+ "total_tokens": total_tokens or (10 + (len(delta_data.get('content', '')) if delta_data.get('content') else 0)),
145
+ "estimated_cost": None
146
+ }
147
+
148
+ # Update completion_tokens and total_tokens as we receive more content
149
+ if delta_data.get('content'):
150
+ completion_tokens += 1
151
+ total_tokens = prompt_tokens + completion_tokens
152
+ usage_dict["completion_tokens"] = completion_tokens
153
+ usage_dict["total_tokens"] = total_tokens
154
+
155
+ chunk_dict["usage"] = usage_dict
156
+
157
+ # Format the response in OpenAI format exactly as requested
158
+ # We need to print the raw string and also yield the chunk object
159
+ # This ensures both the console output and the returned object are correct
160
+ # print(f"data: {json.dumps(chunk_dict)}")
161
+
162
+ # Return the chunk object for internal processing
163
+ yield chunk
164
+ except json.JSONDecodeError:
165
+ print(f"Warning: Could not decode JSON line: {json_str}")
166
+ continue
167
+ except requests.exceptions.RequestException as e:
168
+ print(f"Error during DeepInfra stream request: {e}")
169
+ raise IOError(f"DeepInfra request failed: {e}") from e
170
+ except Exception as e:
171
+ print(f"Error processing DeepInfra stream: {e}")
172
+ raise
173
+
174
+ def _create_non_stream(
175
+ self, request_id: str, created_time: int, model: str, payload: Dict[str, Any],
176
+ timeout: Optional[int] = None, proxies: Optional[Dict[str, str]] = None
177
+ ) -> ChatCompletion:
178
+ try:
179
+ response = self._client.session.post(
180
+ self._client.base_url,
181
+ headers=self._client.headers,
182
+ json=payload,
183
+ timeout=timeout or self._client.timeout,
184
+ proxies=proxies
185
+ )
186
+ response.raise_for_status()
187
+ data = response.json()
188
+
189
+ choices_data = data.get('choices', [])
190
+ usage_data = data.get('usage', {})
191
+
192
+ choices = []
193
+ for choice_d in choices_data:
194
+ message_d = choice_d.get('message', {})
195
+ message = ChatCompletionMessage(
196
+ role=message_d.get('role', 'assistant'),
197
+ content=message_d.get('content', '')
198
+ )
199
+ choice = Choice(
200
+ index=choice_d.get('index', 0),
201
+ message=message,
202
+ finish_reason=choice_d.get('finish_reason', 'stop')
203
+ )
204
+ choices.append(choice)
205
+
206
+ usage = CompletionUsage(
207
+ prompt_tokens=usage_data.get('prompt_tokens', 0),
208
+ completion_tokens=usage_data.get('completion_tokens', 0),
209
+ total_tokens=usage_data.get('total_tokens', 0)
210
+ )
211
+
212
+ completion = ChatCompletion(
213
+ id=request_id,
214
+ choices=choices,
215
+ created=created_time,
216
+ model=data.get('model', model),
217
+ usage=usage,
218
+ )
219
+ return completion
220
+
221
+ except requests.exceptions.RequestException as e:
222
+ print(f"Error during DeepInfra non-stream request: {e}")
223
+ raise IOError(f"DeepInfra request failed: {e}") from e
224
+ except Exception as e:
225
+ print(f"Error processing DeepInfra response: {e}")
226
+ raise
227
+
228
+ class Chat(BaseChat):
229
+ def __init__(self, client: 'DeepInfra'):
230
+ self.completions = Completions(client)
231
+
232
+ class DeepInfra(OpenAICompatibleProvider):
233
+
234
+ AVAILABLE_MODELS = [
235
+ # "anthropic/claude-3-7-sonnet-latest", # >>>> NOT WORKING
236
+ "deepseek-ai/DeepSeek-R1-0528",
237
+ "deepseek-ai/DeepSeek-R1",
238
+ "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
239
+ "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
240
+ "deepseek-ai/DeepSeek-R1-Turbo",
241
+ "deepseek-ai/DeepSeek-V3",
242
+ "deepseek-ai/DeepSeek-Prover-V2-671B",
243
+ "google/gemma-2-27b-it",
244
+ "google/gemma-2-9b-it",
245
+ "google/gemma-3-12b-it",
246
+ "google/gemma-3-27b-it",
247
+ "google/gemma-3-4b-it",
248
+ "meta-llama/Llama-3.3-70B-Instruct",
249
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo",
250
+ "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
251
+ "meta-llama/Llama-4-Scout-17B-16E-Instruct",
252
+ "meta-llama/Llama-Guard-4-12B",
253
+ "meta-llama/Meta-Llama-3.1-8B-Instruct",
254
+ "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
255
+ "microsoft/Phi-4-multimodal-instruct",
256
+ "microsoft/WizardLM-2-8x22B",
257
+ "microsoft/phi-4",
258
+ "microsoft/phi-4-reasoning-plus",
259
+ "mistralai/Mistral-Small-24B-Instruct-2501",
260
+ "nvidia/Llama-3.1-Nemotron-70B-Instruct",
261
+ "Qwen/QwQ-32B",
262
+ "Qwen/Qwen2.5-72B-Instruct",
263
+ "Qwen/Qwen2.5-Coder-32B-Instruct",
264
+ "Qwen/Qwen3-14B",
265
+ "Qwen/Qwen3-30B-A3B",
266
+ "Qwen/Qwen3-32B",
267
+ "Qwen/Qwen3-235B-A22B",
268
+ # "google/gemini-1.5-flash", # >>>> NOT WORKING
269
+ # "google/gemini-1.5-flash-8b", # >>>> NOT WORKING
270
+ # "google/gemini-2.0-flash-001", # >>>> NOT WORKING
271
+
272
+ # "Gryphe/MythoMax-L2-13b", # >>>> NOT WORKING
273
+
274
+ # "meta-llama/Llama-3.2-1B-Instruct", # >>>> NOT WORKING
275
+ # "meta-llama/Llama-3.2-3B-Instruct", # >>>> NOT WORKING
276
+ # "meta-llama/Llama-3.2-90B-Vision-Instruct", # >>>> NOT WORKING
277
+ # "meta-llama/Llama-3.2-11B-Vision-Instruct", # >>>> NOT WORKING
278
+ # "meta-llama/Meta-Llama-3-70B-Instruct", # >>>> NOT WORKING
279
+ # "meta-llama/Meta-Llama-3-8B-Instruct", # >>>> NOT WORKING
280
+ # "meta-llama/Meta-Llama-3.1-70B-Instruct", # >>>> NOT WORKING
281
+ # "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", # >>>> NOT WORKING
282
+ # "meta-llama/Meta-Llama-3.1-405B-Instruct", # >>>> NOT WORKING
283
+ # "mistralai/Mixtral-8x7B-Instruct-v0.1", # >>>> NOT WORKING
284
+ # "mistralai/Mistral-7B-Instruct-v0.3", # >>>> NOT WORKING
285
+ # "mistralai/Mistral-Nemo-Instruct-2407", # >>>> NOT WORKING
286
+ # "NousResearch/Hermes-3-Llama-3.1-405B", # >>>> NOT WORKING
287
+ # "NovaSky-AI/Sky-T1-32B-Preview", # >>>> NOT WORKING
288
+ # "Qwen/Qwen2.5-7B-Instruct", # >>>> NOT WORKING
289
+ # "Sao10K/L3.1-70B-Euryale-v2.2", # >>>> NOT WORKING
290
+ # "Sao10K/L3.3-70B-Euryale-v2.3", # >>>> NOT WORKING
291
+ ]
292
+
293
+ def __init__(self, browser: str = "chrome"):
294
+ self.timeout = None # Default timeout
295
+ self.base_url = "https://api.deepinfra.com/v1/openai/chat/completions"
296
+ self.session = requests.Session()
297
+
298
+ agent = LitAgent()
299
+ fingerprint = agent.generate_fingerprint(browser)
300
+
301
+ self.headers = {
302
+ "Accept": fingerprint["accept"],
303
+ "Accept-Encoding": "gzip, deflate, br, zstd",
304
+ "Accept-Language": fingerprint["accept_language"],
305
+ "Content-Type": "application/json",
306
+ "Cache-Control": "no-cache",
307
+ "Connection": "keep-alive",
308
+ "Origin": "https://deepinfra.com",
309
+ "Pragma": "no-cache",
310
+ "Referer": "https://deepinfra.com/",
311
+ "Sec-Fetch-Dest": "empty",
312
+ "Sec-Fetch-Mode": "cors",
313
+ "Sec-Fetch-Site": "same-site",
314
+ "X-Deepinfra-Source": "web-embed",
315
+ "Sec-CH-UA": fingerprint["sec_ch_ua"] or '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
316
+ "Sec-CH-UA-Mobile": "?0",
317
+ "Sec-CH-UA-Platform": f'"{fingerprint["platform"]}"',
318
+ "User-Agent": fingerprint["user_agent"],
319
+ }
320
+ self.session.headers.update(self.headers)
321
+ self.chat = Chat(self)
322
+
323
+ @property
324
+ def models(self):
325
+ class _ModelList:
326
+ def list(inner_self):
327
+ return type(self).AVAILABLE_MODELS
328
+ return _ModelList()
329
+
330
+ if __name__ == "__main__":
331
+ # Example usage
332
+ client = DeepInfra()
333
+ response = client.chat.completions.create(
334
+ model="deepseek-ai/DeepSeek-R1-0528",
335
+ messages=[{"role": "user", "content": "Hello, how are you?"}],
336
+ max_tokens=100,
337
+ stream=False
338
+ )
339
+ print(response)