webscout 2025.10.19.2__py3-none-any.whl → 2025.10.22.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.

@@ -1,378 +0,0 @@
1
- from curl_cffi.requests import Session
2
- from curl_cffi import CurlError
3
- import json
4
- import re
5
- from typing import Any, Dict, Optional, Generator, Union, List
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
11
- from webscout import exceptions
12
- from webscout.litagent import LitAgent
13
-
14
-
15
- class DeepSeekAssistant(Provider):
16
- """
17
- A class to interact with the DeepSeek Assistant API.
18
-
19
- This provider interfaces with the deepseek-assistant.com API to provide
20
- AI chat completions using the V3 model.
21
-
22
- Attributes:
23
- AVAILABLE_MODELS (list): List of available models for the provider.
24
-
25
- Examples:
26
- >>> from webscout.Provider.deepseek_assistant import DeepSeekAssistant
27
- >>> ai = DeepSeekAssistant()
28
- >>> response = ai.chat("What's the weather today?")
29
- >>> print(response)
30
- 'I can help you with weather information...'
31
- """
32
-
33
- AVAILABLE_MODELS = ["V3 model", "R1 model"]
34
- required_auth = False
35
- @staticmethod
36
- def _deepseek_assistant_extractor(chunk: Union[str, Dict[str, Any]]) -> Optional[str]:
37
- """Extracts content from DeepSeek Assistant stream JSON objects."""
38
- if isinstance(chunk, dict):
39
- return chunk.get("choices", [{}])[0].get("delta", {}).get("content")
40
- return None
41
-
42
- def __init__(
43
- self,
44
- is_conversation: bool = True,
45
- max_tokens: int = 2049,
46
- timeout: int = 30,
47
- intro: str = None,
48
- filepath: str = None,
49
- update_file: bool = True,
50
- proxies: dict = {},
51
- history_offset: int = 10250,
52
- act: str = None,
53
- model: str = "V3 model",
54
- system_prompt: str = "You are a helpful assistant.",
55
- browser: str = "chrome"
56
- ):
57
- """
58
- Initializes the DeepSeek Assistant API client.
59
-
60
- Args:
61
- is_conversation (bool): Whether the provider is in conversation mode.
62
- max_tokens (int): Maximum number of tokens to sample.
63
- timeout (int): Timeout for API requests.
64
- intro (str): Introduction message for the conversation.
65
- filepath (str): Filepath for storing conversation history.
66
- update_file (bool): Whether to update the conversation history file.
67
- proxies (dict): Proxies for the API requests.
68
- history_offset (int): Offset for conversation history.
69
- act (str): Act for the conversation.
70
- model (str): The model to use for completions.
71
- system_prompt (str): The system prompt to define the assistant's role.
72
- browser (str): Browser type for fingerprinting.
73
-
74
- Examples:
75
- >>> ai = DeepSeekAssistant(model="V3 model")
76
- >>> print(ai.model)
77
- 'V3 model'
78
- """
79
- if model not in self.AVAILABLE_MODELS:
80
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
81
-
82
- self.url = "https://deepseek-assistant.com/api/search-stream-deep-chat-testing.php"
83
-
84
- # Initialize LitAgent for user agent generation
85
- self.agent = LitAgent()
86
- self.fingerprint = self.agent.generate_fingerprint(browser)
87
-
88
- # Headers based on the JavaScript code
89
- self.headers = {
90
- "accept": "*/*",
91
- "accept-language": "id-ID,id;q=0.9",
92
- "cache-control": "no-cache",
93
- "content-type": "application/json",
94
- "cookie": "click_id=OS3Hz0E1yKfu4YnZNwedESMEdKEgMTzL; organic_user_deepseek_assistant_ch=%7B%22pixel%22%3A%22OS3Hz0E1yKfu4YnZNwedESMEdKEgMTzL%22%2C%22cc%22%3A%22ID%22%2C%22channel%22%3A%22organic_flag%22%7D",
95
- "origin": "https://deepseek-assistant.com",
96
- **self.fingerprint
97
-
98
- }
99
-
100
- # Initialize curl_cffi Session
101
- self.session = Session()
102
- self.session.headers.update(self.headers)
103
- self.session.proxies = proxies
104
-
105
- self.system_prompt = system_prompt
106
- self.is_conversation = is_conversation
107
- self.max_tokens_to_sample = max_tokens
108
- self.timeout = timeout
109
- self.last_response = {}
110
- self.model = model
111
-
112
- self.__available_optimizers = (
113
- method
114
- for method in dir(Optimizers)
115
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
116
- )
117
-
118
- Conversation.intro = (
119
- AwesomePrompts().get_act(
120
- act, raise_not_found=True, default=None, case_insensitive=True
121
- )
122
- if act
123
- else intro or Conversation.intro
124
- )
125
-
126
- self.conversation = Conversation(
127
- is_conversation, self.max_tokens_to_sample, filepath, update_file
128
- )
129
- self.conversation.history_offset = history_offset
130
-
131
- def refresh_identity(self, browser: str = None):
132
- """
133
- Refreshes the browser identity fingerprint.
134
-
135
- Args:
136
- browser: Specific browser to use for the new fingerprint
137
- """
138
- browser = browser or self.fingerprint.get("browser_type", "chrome")
139
- self.fingerprint = self.agent.generate_fingerprint(browser)
140
-
141
- # Update user-agent header with new fingerprint
142
- self.headers.update({
143
- "user-agent": self.fingerprint.get("user_agent", self.headers["user-agent"])
144
- })
145
-
146
- # Update session headers
147
- self.session.headers.update(self.headers)
148
-
149
- return self.fingerprint
150
-
151
- def _parse_chat_response(self, input_text: str) -> str:
152
- """
153
- Parses the chat response from the API, similar to the JavaScript parseChatResponse method.
154
-
155
- Args:
156
- input_text (str): The raw response text from the API
157
-
158
- Returns:
159
- str: The parsed content from the response
160
- """
161
- lines = input_text.strip().split("\n")
162
- result = ""
163
-
164
- for line in lines:
165
- trimmed_line = line.strip()
166
- if trimmed_line.startswith("data: {") and trimmed_line.endswith("}"):
167
- try:
168
- # Extract JSON from the line
169
- json_start = trimmed_line.find("{")
170
- if json_start != -1:
171
- json_str = trimmed_line[json_start:]
172
- parsed_data = json.loads(json_str)
173
-
174
- # Extract content from the parsed data
175
- content = parsed_data.get("choices", [{}])[0].get("delta", {}).get("content")
176
- if content is not None:
177
- result += content
178
- except (json.JSONDecodeError, KeyError, IndexError):
179
- # Skip malformed JSON or missing keys
180
- continue
181
-
182
- return result.strip()
183
-
184
- def ask(
185
- self,
186
- prompt: str,
187
- stream: bool = False,
188
- raw: bool = False,
189
- optimizer: str = None,
190
- conversationally: bool = False,
191
- ) -> Union[Dict[str, Any], Generator]:
192
- """
193
- Sends a prompt to the DeepSeek Assistant API and returns the response.
194
-
195
- Args:
196
- prompt (str): The prompt to send to the API.
197
- stream (bool): Whether to stream the response.
198
- raw (bool): Whether to return the raw response.
199
- optimizer (str): Optimizer to use for the prompt.
200
- conversationally (bool): Whether to generate the prompt conversationally.
201
-
202
- Returns:
203
- Union[Dict[str, Any], Generator]: The API response.
204
-
205
- Examples:
206
- >>> ai = DeepSeekAssistant()
207
- >>> response = ai.ask("Tell me a joke!")
208
- >>> print(response)
209
- {'text': 'Why did the scarecrow win an award? Because he was outstanding in his field!'}
210
- """
211
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
212
- if optimizer:
213
- if optimizer in self.__available_optimizers:
214
- conversation_prompt = getattr(Optimizers, optimizer)(
215
- conversation_prompt if conversationally else prompt
216
- )
217
- else:
218
- raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
219
-
220
- payload = {
221
- "model": self.model,
222
- "messages": [
223
- {"role": "system", "content": self.system_prompt}, # Add system role
224
- {"role": "user", "content": conversation_prompt}
225
- ]
226
- }
227
-
228
- def for_stream():
229
- streaming_text = ""
230
- try:
231
- response = self.session.post(
232
- self.url,
233
- data=json.dumps(payload),
234
- stream=True,
235
- timeout=self.timeout,
236
- impersonate="chrome110"
237
- )
238
- response.raise_for_status()
239
-
240
- # Use sanitize_stream to process the response
241
- processed_stream = sanitize_stream(
242
- data=response.iter_content(chunk_size=None),
243
- intro_value="data:",
244
- to_json=True,
245
- skip_markers=["[DONE]"],
246
- content_extractor=self._deepseek_assistant_extractor,
247
- yield_raw_on_error=False
248
- )
249
-
250
- for content_chunk in processed_stream:
251
- if content_chunk and isinstance(content_chunk, str):
252
- streaming_text += content_chunk
253
- resp = dict(text=content_chunk)
254
- yield resp if not raw else content_chunk
255
-
256
- except CurlError as e:
257
- raise exceptions.FailedToGenerateResponseError(f"Request failed (CurlError): {str(e)}") from e
258
- except Exception as e:
259
- raise exceptions.FailedToGenerateResponseError(f"Request failed ({type(e).__name__}): {str(e)}") from e
260
- finally:
261
- # Update history after stream finishes or fails
262
- if streaming_text:
263
- self.last_response = {"text": streaming_text}
264
- self.conversation.update_chat_history(prompt, streaming_text)
265
-
266
- def for_non_stream():
267
- try:
268
- response = self.session.post(
269
- self.url,
270
- data=json.dumps(payload),
271
- timeout=self.timeout,
272
- impersonate="chrome110"
273
- )
274
- response.raise_for_status()
275
-
276
- # Parse the response using the custom parser
277
- content = self._parse_chat_response(response.text)
278
-
279
- self.last_response = {"text": content}
280
- self.conversation.update_chat_history(prompt, content)
281
- return self.last_response if not raw else content
282
-
283
- except CurlError as e:
284
- raise exceptions.FailedToGenerateResponseError(f"Request failed (CurlError): {e}") from e
285
- except Exception as e:
286
- err_text = getattr(e, 'response', None) and getattr(e.response, 'text', '')
287
- raise exceptions.FailedToGenerateResponseError(f"Request failed ({type(e).__name__}): {e} - {err_text}") from e
288
-
289
- return for_stream() if stream else for_non_stream()
290
-
291
- def chat(
292
- self,
293
- prompt: str,
294
- stream: bool = False,
295
- optimizer: str = None,
296
- conversationally: bool = False,
297
- ) -> Union[str, Generator[str, None, None]]:
298
- """
299
- Initiates a chat with the DeepSeek Assistant API using the provided prompt.
300
-
301
- Args:
302
- prompt (str): The prompt to send to the API.
303
- stream (bool): Whether to stream the response.
304
- optimizer (str): Optimizer to use for the prompt.
305
- conversationally (bool): Whether to generate the prompt conversationally.
306
-
307
- Returns:
308
- Union[str, Generator[str, None, None]]: The chat response.
309
-
310
- Examples:
311
- >>> ai = DeepSeekAssistant()
312
- >>> response = ai.chat("Tell me a joke")
313
- >>> print(response)
314
- 'Why did the scarecrow win an award? Because he was outstanding in his field!'
315
- """
316
- def for_stream_chat():
317
- gen = self.ask(
318
- prompt, stream=True, raw=False,
319
- optimizer=optimizer, conversationally=conversationally
320
- )
321
- for response_dict in gen:
322
- yield self.get_message(response_dict)
323
-
324
- def for_non_stream_chat():
325
- response_data = self.ask(
326
- prompt, stream=False, raw=False,
327
- optimizer=optimizer, conversationally=conversationally
328
- )
329
- return self.get_message(response_data)
330
-
331
- return for_stream_chat() if stream else for_non_stream_chat()
332
-
333
- def get_message(self, response: dict) -> str:
334
- """
335
- Extracts the message content from the API response.
336
-
337
- Args:
338
- response (dict): The API response.
339
-
340
- Returns:
341
- str: The message content.
342
-
343
- Examples:
344
- >>> ai = DeepSeekAssistant()
345
- >>> response = ai.ask("Tell me a joke!")
346
- >>> message = ai.get_message(response)
347
- >>> print(message)
348
- 'Why did the scarecrow win an award? Because he was outstanding in his field!'
349
- """
350
- assert isinstance(response, dict), "Response should be of dict data-type only"
351
- return response["text"]
352
-
353
-
354
- if __name__ == "__main__":
355
- # Test the provider
356
- print("-" * 80)
357
- print(f"{'Model':<50} {'Status':<10} {'Response'}")
358
- print("-" * 80)
359
-
360
- for model in DeepSeekAssistant.AVAILABLE_MODELS:
361
- try:
362
- test_ai = DeepSeekAssistant(model=model, timeout=60)
363
- response = test_ai.chat("Say 'Hello' in one word", stream=True)
364
- response_text = ""
365
- for chunk in response:
366
- response_text += chunk
367
-
368
- if response_text and len(response_text.strip()) > 0:
369
- status = "✓"
370
- # Clean and truncate response
371
- clean_text = response_text.strip().encode('utf-8', errors='ignore').decode('utf-8')
372
- display_text = clean_text[:50] + "..." if len(clean_text) > 50 else clean_text
373
- else:
374
- status = "✗"
375
- display_text = "Empty or invalid response"
376
- print(f"\r{model:<50} {status:<10} {display_text}")
377
- except Exception as e:
378
- print(f"\r{model:<50} {'✗':<10} {str(e)}")