webscout 7.2__py3-none-any.whl → 7.3__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/Bard.py +2 -2
- webscout/Litlogger/core/level.py +3 -0
- webscout/Litlogger/core/logger.py +101 -58
- webscout/Litlogger/handlers/console.py +14 -31
- webscout/Litlogger/handlers/network.py +16 -17
- webscout/Litlogger/styles/colors.py +81 -63
- webscout/Litlogger/styles/formats.py +163 -80
- webscout/Provider/AISEARCH/ISou.py +277 -0
- webscout/Provider/AISEARCH/__init__.py +2 -1
- webscout/Provider/Deepinfra.py +40 -24
- webscout/Provider/TTI/FreeAIPlayground/__init__.py +9 -0
- webscout/Provider/TTI/FreeAIPlayground/async_freeaiplayground.py +206 -0
- webscout/Provider/TTI/FreeAIPlayground/sync_freeaiplayground.py +192 -0
- webscout/Provider/TTI/__init__.py +2 -1
- webscout/Provider/TextPollinationsAI.py +26 -5
- webscout/Provider/__init__.py +2 -0
- webscout/Provider/freeaichat.py +221 -0
- webscout/Provider/yep.py +1 -1
- webscout/__init__.py +1 -0
- webscout/version.py +1 -1
- webscout/webscout_search.py +82 -2
- webscout/webscout_search_async.py +58 -1
- webscout/yep_search.py +297 -0
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/METADATA +59 -20
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/RECORD +29 -23
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/WHEEL +1 -1
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/LICENSE.md +0 -0
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/entry_points.txt +0 -0
- {webscout-7.2.dist-info → webscout-7.3.dist-info}/top_level.txt +0 -0
|
@@ -2,21 +2,51 @@ from enum import Enum
|
|
|
2
2
|
from typing import Dict
|
|
3
3
|
|
|
4
4
|
class LogFormat:
|
|
5
|
-
# Basic formats
|
|
6
|
-
MINIMAL = "
|
|
7
|
-
STANDARD = "
|
|
8
|
-
DETAILED = "
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
# Basic formats with improved styling
|
|
6
|
+
MINIMAL = "│ {time} │ {level} │ {message}"
|
|
7
|
+
STANDARD = "┌─ {time} ─┐\n│ {level} │ {name}: {message}"
|
|
8
|
+
DETAILED = """╭──────────────────────╮
|
|
9
|
+
│ {time}
|
|
10
|
+
│ {level} • {name}
|
|
11
|
+
│ {file}:{line}
|
|
12
|
+
├──────────────────────┤
|
|
13
|
+
│ {message}
|
|
14
|
+
╰──────────────────────╯"""
|
|
15
|
+
SIMPLE = "• {message}"
|
|
16
|
+
COMPACT = "⟦{time}⟧ {level} ⟫ {message}"
|
|
17
|
+
|
|
18
|
+
# Modern Styles with Unicode
|
|
19
|
+
MODERN = """┌────────────────────┐
|
|
20
|
+
│ {time}
|
|
21
|
+
├────────────────────┤
|
|
22
|
+
│ {level} • {name}
|
|
23
|
+
│ {message}
|
|
24
|
+
└────────────────────┘"""
|
|
25
|
+
|
|
26
|
+
MODERN_EMOJI = """╭─ {emoji} {time} ─╮
|
|
27
|
+
│ {level}
|
|
28
|
+
├───────────────────
|
|
29
|
+
│ {message}
|
|
30
|
+
╰───────────────────"""
|
|
31
|
+
|
|
32
|
+
MODERN_CLEAN = """• {time} •
|
|
33
|
+
┌─ {level}
|
|
34
|
+
└→ {message}"""
|
|
35
|
+
|
|
36
|
+
MODERN_BRACKET = """【{time}】
|
|
37
|
+
「{level}」{message}"""
|
|
38
|
+
|
|
39
|
+
MODERN_PLUS = """⊕ {time}
|
|
40
|
+
├─ {level}
|
|
41
|
+
└─ {message}"""
|
|
42
|
+
|
|
43
|
+
MODERN_DOT = """● {time}
|
|
44
|
+
├● {level}
|
|
45
|
+
└● {message}"""
|
|
11
46
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
MODERN_CLEAN = "{time} {level} {message}"
|
|
16
|
-
MODERN_BRACKET = "【{time}】「{level}」{message}"
|
|
17
|
-
MODERN_PLUS = "⊕ {time} ⊕ {level} ⊕ {message}"
|
|
18
|
-
MODERN_DOT = "• {time} • {level} • {message}"
|
|
19
|
-
MODERN_ARROW = "→ {time} → {level} → {message}"
|
|
47
|
+
MODERN_ARROW = """➤ {time}
|
|
48
|
+
├➤ {level}
|
|
49
|
+
└➤ {message}"""
|
|
20
50
|
|
|
21
51
|
# Boxed Styles
|
|
22
52
|
BOXED = """
|
|
@@ -86,36 +116,53 @@ class LogFormat:
|
|
|
86
116
|
💬 Message: {message}
|
|
87
117
|
"""
|
|
88
118
|
|
|
89
|
-
# Error Formats
|
|
90
|
-
ERROR = "
|
|
91
|
-
|
|
92
|
-
{level}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
119
|
+
# Enhanced Error Formats
|
|
120
|
+
ERROR = """╔══ ERROR ══╗
|
|
121
|
+
║ Time: {time}
|
|
122
|
+
║ Level: {level}
|
|
123
|
+
║ Component: {name}
|
|
124
|
+
╟──────────────
|
|
125
|
+
║ {message}
|
|
126
|
+
╚════════════╝"""
|
|
127
|
+
|
|
128
|
+
ERROR_DETAILED = """┏━━ ALERT ━━┓
|
|
129
|
+
┃ Time: {time}
|
|
130
|
+
┃ Level: {level}
|
|
131
|
+
┃ Component: {name}
|
|
132
|
+
┃ Location: {file}:{line}
|
|
133
|
+
┣━━━━━━━━━━━
|
|
134
|
+
┃ {message}
|
|
135
|
+
┗━━━━━━━━━━━┛"""
|
|
136
|
+
|
|
137
|
+
ERROR_COMPACT = "‼ [{time}] {level}: {message}"
|
|
138
|
+
|
|
139
|
+
ERROR_EMOJI = """❌ ERROR REPORT
|
|
140
|
+
⏰ {time}
|
|
141
|
+
⚠️ {level}
|
|
142
|
+
📝 {message}"""
|
|
97
143
|
|
|
98
|
-
|
|
99
|
-
ERROR_EMOJI = "❌ {time} | {level} | {message}"
|
|
100
|
-
ERROR_BLOCK = """
|
|
101
|
-
█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
|
|
144
|
+
ERROR_BLOCK = """█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
|
|
102
145
|
█ ERROR @ {time}
|
|
146
|
+
█ {level}
|
|
103
147
|
█ {message}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
# Status & Progress
|
|
107
|
-
STATUS = """
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
148
|
+
█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█"""
|
|
149
|
+
|
|
150
|
+
# Status & Progress with Better Visual Hierarchy
|
|
151
|
+
STATUS = """┌─── Status Update ───┐
|
|
152
|
+
│ Time: {time}
|
|
153
|
+
│ Level: {level}
|
|
154
|
+
│ Component: {name}
|
|
155
|
+
├───────────────────┤
|
|
156
|
+
│ {message}
|
|
157
|
+
└───────────────────┘"""
|
|
158
|
+
|
|
159
|
+
PROGRESS = """╭── Progress Report ──╮
|
|
160
|
+
│ Time: {time}
|
|
161
|
+
│ Level: {level}
|
|
162
|
+
├────────────────────
|
|
163
|
+
│ Component: {name}
|
|
164
|
+
│ Status: {message}
|
|
165
|
+
╰────────────────────"""
|
|
119
166
|
|
|
120
167
|
PROGRESS_SIMPLE = "► {time} | {level} | {message}"
|
|
121
168
|
PROGRESS_BAR = "[{progress_bar}] {percentage}% - {message}"
|
|
@@ -141,28 +188,29 @@ Response: {message}"""
|
|
|
141
188
|
Details: {message}"""
|
|
142
189
|
|
|
143
190
|
API_COMPACT = "{method} {url} - {status_code} ({time})"
|
|
144
|
-
API_DETAILED = """
|
|
145
|
-
┌── API Call ──────────
|
|
191
|
+
API_DETAILED = """┌── API Request ──┐
|
|
146
192
|
│ Time: {time}
|
|
147
193
|
│ Method: {method}
|
|
148
194
|
│ URL: {url}
|
|
195
|
+
├─────────────────
|
|
149
196
|
│ Status: {status_code}
|
|
150
|
-
│
|
|
151
|
-
|
|
197
|
+
│ {message}
|
|
198
|
+
└─────────────────"""
|
|
152
199
|
|
|
153
200
|
# System & Metrics
|
|
154
|
-
SYSTEM = """
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
{
|
|
158
|
-
{
|
|
159
|
-
{message}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
201
|
+
SYSTEM = """┌─── System Status ───┐
|
|
202
|
+
│ {time}
|
|
203
|
+
├───────────────────────
|
|
204
|
+
│ Component: {name}
|
|
205
|
+
│ Status: {level}
|
|
206
|
+
│ Message: {message}
|
|
207
|
+
└───────────────────────"""
|
|
208
|
+
|
|
209
|
+
METRIC = """┌── Metrics ──┐
|
|
210
|
+
│ Time: {time}
|
|
211
|
+
│ Value: {value}{units}
|
|
212
|
+
│ {message}
|
|
213
|
+
└─────────────"""
|
|
166
214
|
|
|
167
215
|
METRIC_COMPACT = "[METRIC] {name}={value} {units}"
|
|
168
216
|
METRIC_JSON = '{{"metric":"{name}","value":{value},"time":"{time}"}}'
|
|
@@ -182,12 +230,13 @@ Level: {level}
|
|
|
182
230
|
Component: {name}
|
|
183
231
|
Action: {message}"""
|
|
184
232
|
|
|
185
|
-
SECURITY_ALERT = """
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
233
|
+
SECURITY_ALERT = """╔═══ SECURITY ALERT ═══╗
|
|
234
|
+
║ Time: {time}
|
|
235
|
+
║ Level: {level}
|
|
236
|
+
║ Component: {name}
|
|
237
|
+
╟───────────────────────
|
|
238
|
+
║ {message}
|
|
239
|
+
╚═══════════════════════"""
|
|
191
240
|
|
|
192
241
|
# Special Formats
|
|
193
242
|
RAINBOW = " {time} {level} {message}"
|
|
@@ -200,9 +249,9 @@ Details: {message}
|
|
|
200
249
|
# Data Formats
|
|
201
250
|
JSON = '{{"time":"{time}","level":"{level}","name":"{name}","message":"{message}"}}'
|
|
202
251
|
JSON_PRETTY = """{
|
|
203
|
-
"
|
|
252
|
+
"timestamp": "{time}",
|
|
204
253
|
"level": "{level}",
|
|
205
|
-
"
|
|
254
|
+
"component": "{name}",
|
|
206
255
|
"message": "{message}"
|
|
207
256
|
}"""
|
|
208
257
|
|
|
@@ -214,20 +263,21 @@ Details: {message}
|
|
|
214
263
|
</log>"""
|
|
215
264
|
|
|
216
265
|
YAML = """---
|
|
217
|
-
|
|
266
|
+
timestamp: {time}
|
|
218
267
|
level: {level}
|
|
219
|
-
|
|
220
|
-
|
|
268
|
+
component: {name}
|
|
269
|
+
details:
|
|
270
|
+
message: {message}
|
|
221
271
|
"""
|
|
222
272
|
|
|
223
|
-
# Documentation Formats
|
|
224
|
-
MARKDOWN = """
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
"""
|
|
273
|
+
# Modern Documentation Formats
|
|
274
|
+
MARKDOWN = """## Log Entry
|
|
275
|
+
**Time:** `{time}`
|
|
276
|
+
**Level:** `{level}`
|
|
277
|
+
**Component:** `{name}`
|
|
278
|
+
|
|
279
|
+
> {message}
|
|
280
|
+
---"""
|
|
231
281
|
|
|
232
282
|
RST = """
|
|
233
283
|
Log Entry
|
|
@@ -239,12 +289,39 @@ Log Entry
|
|
|
239
289
|
"""
|
|
240
290
|
|
|
241
291
|
HTML = """<div class="log-entry">
|
|
242
|
-
<
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
292
|
+
<div class="log-header">
|
|
293
|
+
<span class="time">{time}</span>
|
|
294
|
+
<span class="level">{level}</span>
|
|
295
|
+
</div>
|
|
296
|
+
<div class="log-body">
|
|
297
|
+
<span class="name">{name}</span>
|
|
298
|
+
<span class="message">{message}</span>
|
|
299
|
+
</div>
|
|
246
300
|
</div>"""
|
|
247
301
|
|
|
302
|
+
# Rich Console Formats
|
|
303
|
+
RICH = """╭── {name} ──╮
|
|
304
|
+
│ {time} │ {level_colored}
|
|
305
|
+
├──────────┴───────────
|
|
306
|
+
│ {message}
|
|
307
|
+
│ {context}{exception}
|
|
308
|
+
╰────────────────────"""
|
|
309
|
+
|
|
310
|
+
RICH_DETAILED = """╔══════════════════════════╗
|
|
311
|
+
║ {name}
|
|
312
|
+
╟──────────────────────────
|
|
313
|
+
║ Time: {time}
|
|
314
|
+
║ Level: {level_colored}
|
|
315
|
+
║ Thread: {thread_info}
|
|
316
|
+
╟──────────────────────────
|
|
317
|
+
║ {message}
|
|
318
|
+
║ {context}{exception}
|
|
319
|
+
╚══════════════════════════╝"""
|
|
320
|
+
|
|
321
|
+
RICH_MINIMAL = "│ {time} │ {level_colored} │ {message}"
|
|
322
|
+
|
|
323
|
+
RICH_COMPACT = "⟦{time}⟧ {level_colored} [{name}] ⟫ {message}"
|
|
324
|
+
|
|
248
325
|
# Template registry
|
|
249
326
|
TEMPLATES = {
|
|
250
327
|
# Basic formats
|
|
@@ -327,6 +404,12 @@ Log Entry
|
|
|
327
404
|
"markdown": MARKDOWN,
|
|
328
405
|
"rst": RST,
|
|
329
406
|
"html": HTML,
|
|
407
|
+
|
|
408
|
+
# Rich-like formats
|
|
409
|
+
"rich": RICH,
|
|
410
|
+
"rich_detailed": RICH_DETAILED,
|
|
411
|
+
"rich_minimal": RICH_MINIMAL,
|
|
412
|
+
"rich_compact": RICH_COMPACT,
|
|
330
413
|
}
|
|
331
414
|
|
|
332
415
|
@staticmethod
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
import re
|
|
4
|
+
from typing import Dict, Optional, Generator, Union, Any
|
|
5
|
+
from webscout import LitAgent
|
|
6
|
+
from webscout import exceptions
|
|
7
|
+
from webscout.AIbase import AISearch
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Response:
|
|
11
|
+
"""A wrapper class for Felo API responses.
|
|
12
|
+
|
|
13
|
+
This class automatically converts response objects to their text representation
|
|
14
|
+
when printed or converted to string.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
text (str): The text content of the response
|
|
18
|
+
|
|
19
|
+
Example:
|
|
20
|
+
>>> response = Response("Hello, world!")
|
|
21
|
+
>>> print(response)
|
|
22
|
+
Hello, world!
|
|
23
|
+
>>> str(response)
|
|
24
|
+
'Hello, world!'
|
|
25
|
+
"""
|
|
26
|
+
def __init__(self, text: str):
|
|
27
|
+
self.text = text
|
|
28
|
+
|
|
29
|
+
def __str__(self):
|
|
30
|
+
return self.text
|
|
31
|
+
|
|
32
|
+
def __repr__(self):
|
|
33
|
+
return self.text
|
|
34
|
+
class Isou(AISearch):
|
|
35
|
+
"""A class to interact with the Isou AI search API.
|
|
36
|
+
|
|
37
|
+
Isou provides a powerful search interface that returns AI-generated responses
|
|
38
|
+
based on web content. It supports both streaming and non-streaming responses.
|
|
39
|
+
|
|
40
|
+
Basic Usage:
|
|
41
|
+
>>> from webscout import Isou
|
|
42
|
+
>>> ai = Isou()
|
|
43
|
+
>>> # Non-streaming example
|
|
44
|
+
>>> response = ai.search("What is Python?")
|
|
45
|
+
>>> print(response)
|
|
46
|
+
Python is a high-level programming language...
|
|
47
|
+
|
|
48
|
+
>>> # Streaming example
|
|
49
|
+
>>> for chunk in ai.search("Tell me about AI", stream=True):
|
|
50
|
+
... print(chunk, end="", flush=True)
|
|
51
|
+
Artificial Intelligence is...
|
|
52
|
+
|
|
53
|
+
>>> # Raw response format
|
|
54
|
+
>>> for chunk in ai.search("Hello", stream=True, raw=True):
|
|
55
|
+
... print(chunk)
|
|
56
|
+
{'text': 'Hello', 'links': ['http://...']}
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
timeout (int, optional): Request timeout in seconds. Defaults to 120.
|
|
60
|
+
proxies (dict, optional): Proxy configuration for requests. Defaults to None.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
timeout: int = 120,
|
|
66
|
+
proxies: Optional[dict] = None,
|
|
67
|
+
model: str = "siliconflow:deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
|
68
|
+
logging: bool = False
|
|
69
|
+
):
|
|
70
|
+
"""Initialize the Isou API client.
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
timeout (int, optional): Request timeout in seconds. Defaults to 120.
|
|
74
|
+
proxies (dict, optional): Proxy configuration for requests. Defaults to None.
|
|
75
|
+
model (str, optional): Model to use for search. Defaults to DeepSeek-R1.
|
|
76
|
+
logging (bool, optional): Enable logging. Defaults to False.
|
|
77
|
+
"""
|
|
78
|
+
self.available_models = [
|
|
79
|
+
"siliconflow:deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
|
80
|
+
"siliconflow:Qwen/Qwen2.5-72B-Instruct-128K",
|
|
81
|
+
"deepseek-reasoner"
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
if model not in self.available_models:
|
|
85
|
+
raise ValueError(
|
|
86
|
+
f"Invalid model: {model}. Choose from: {self.available_models}"
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
self.session = requests.Session()
|
|
90
|
+
self.api_endpoint = "https://isou.chat/api/search"
|
|
91
|
+
self.stream_chunk_size = 64
|
|
92
|
+
self.timeout = timeout
|
|
93
|
+
self.last_response = {}
|
|
94
|
+
self.model = model
|
|
95
|
+
self.provider = "siliconflow" if model in self.available_models[:2] else "deepseek"
|
|
96
|
+
self.mode = "simple" # or "deep"
|
|
97
|
+
self.categories = "general" # or "science"
|
|
98
|
+
self.reload = False
|
|
99
|
+
|
|
100
|
+
self.headers = {
|
|
101
|
+
"accept": "text/event-stream",
|
|
102
|
+
"pragma": "no-cache",
|
|
103
|
+
"referer": "https://isou.chat/search?q=hi",
|
|
104
|
+
"accept-language": "en-US,en;q=0.9",
|
|
105
|
+
"content-type": "application/json",
|
|
106
|
+
"sec-ch-ua": '"Not(A:Brand";v="99", "Brave";v="133", "Chromium";v="133"',
|
|
107
|
+
"sec-ch-ua-mobile": "?0",
|
|
108
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
109
|
+
"sec-fetch-dest": "empty",
|
|
110
|
+
"sec-fetch-mode": "cors",
|
|
111
|
+
"sec-fetch-site": "same-origin",
|
|
112
|
+
"sec-gpc": "1",
|
|
113
|
+
"user-agent": LitAgent().random(),
|
|
114
|
+
}
|
|
115
|
+
self.session.headers.update(self.headers)
|
|
116
|
+
self.proxies = proxies
|
|
117
|
+
|
|
118
|
+
# Initialize logger if enabled
|
|
119
|
+
if logging:
|
|
120
|
+
from webscout.Litlogger import Logger, LogFormat, ConsoleHandler
|
|
121
|
+
from webscout.Litlogger.core.level import LogLevel
|
|
122
|
+
|
|
123
|
+
console_handler = ConsoleHandler(
|
|
124
|
+
level=LogLevel.DEBUG,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
self.logger = Logger(
|
|
128
|
+
name="Isou",
|
|
129
|
+
level=LogLevel.DEBUG,
|
|
130
|
+
handlers=[console_handler]
|
|
131
|
+
)
|
|
132
|
+
self.logger.info("Isou initialized successfully ✨")
|
|
133
|
+
else:
|
|
134
|
+
self.logger = None
|
|
135
|
+
|
|
136
|
+
def search(
|
|
137
|
+
self,
|
|
138
|
+
prompt: str,
|
|
139
|
+
stream: bool = False,
|
|
140
|
+
raw: bool = False,
|
|
141
|
+
) -> Dict[str, Any] | Generator[Dict[str, Any], None, None]:
|
|
142
|
+
"""Search using the Isou API and get AI-generated responses.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
prompt (str): The search query or prompt to send to the API.
|
|
146
|
+
stream (bool, optional): If True, yields response chunks as they arrive.
|
|
147
|
+
If False, returns complete response. Defaults to False.
|
|
148
|
+
raw (bool, optional): If True, returns raw response dictionaries.
|
|
149
|
+
If False, returns Response objects. Defaults to False.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
Union[Dict[str, Any], Generator[Dict[str, Any], None, None]]:
|
|
153
|
+
- If stream=False: Returns complete response
|
|
154
|
+
- If stream=True: Yields response chunks as they arrive
|
|
155
|
+
|
|
156
|
+
Raises:
|
|
157
|
+
APIConnectionError: If the API request fails
|
|
158
|
+
"""
|
|
159
|
+
self.provider = "siliconflow" if self.model in self.available_models[:2] else "deepseek"
|
|
160
|
+
|
|
161
|
+
payload = {
|
|
162
|
+
"categories": [self.categories],
|
|
163
|
+
"engine": "SEARXNG",
|
|
164
|
+
"language": "all",
|
|
165
|
+
"mode": self.mode,
|
|
166
|
+
"model": self.model,
|
|
167
|
+
"provider": self.provider,
|
|
168
|
+
"reload": self.reload,
|
|
169
|
+
"stream": stream,
|
|
170
|
+
}
|
|
171
|
+
params = {"q": prompt}
|
|
172
|
+
|
|
173
|
+
def for_stream() -> Generator[Dict[str, Any], None, None]:
|
|
174
|
+
full_text = ""
|
|
175
|
+
links = []
|
|
176
|
+
try:
|
|
177
|
+
with self.session.post(
|
|
178
|
+
self.api_endpoint,
|
|
179
|
+
params=params,
|
|
180
|
+
json=payload,
|
|
181
|
+
stream=True,
|
|
182
|
+
timeout=self.timeout,
|
|
183
|
+
) as response:
|
|
184
|
+
response.raise_for_status()
|
|
185
|
+
for line in response.iter_lines(chunk_size=self.stream_chunk_size, decode_unicode=True):
|
|
186
|
+
if not line or not line.startswith('data:'):
|
|
187
|
+
continue
|
|
188
|
+
try:
|
|
189
|
+
data = json.loads(line[5:].strip())
|
|
190
|
+
|
|
191
|
+
# Handle nested data structure
|
|
192
|
+
if 'data' in data and isinstance(data['data'], str):
|
|
193
|
+
try:
|
|
194
|
+
nested_data = json.loads(data['data'])
|
|
195
|
+
data.update(nested_data)
|
|
196
|
+
except json.JSONDecodeError:
|
|
197
|
+
pass
|
|
198
|
+
|
|
199
|
+
# Extract content and ensure it's properly decoded
|
|
200
|
+
if 'content' in data:
|
|
201
|
+
content = data['content']
|
|
202
|
+
if isinstance(content, str):
|
|
203
|
+
# Get only the new text (delta)
|
|
204
|
+
delta = content[len(full_text):]
|
|
205
|
+
full_text = content
|
|
206
|
+
|
|
207
|
+
# Yield the chunk
|
|
208
|
+
if raw:
|
|
209
|
+
yield {"text": delta, "links": links}
|
|
210
|
+
else:
|
|
211
|
+
yield Response(delta)
|
|
212
|
+
|
|
213
|
+
# Extract links
|
|
214
|
+
if 'links' in data and isinstance(data['links'], list):
|
|
215
|
+
links.extend(data['links'])
|
|
216
|
+
|
|
217
|
+
except json.JSONDecodeError:
|
|
218
|
+
continue
|
|
219
|
+
|
|
220
|
+
except requests.exceptions.RequestException as e:
|
|
221
|
+
raise exceptions.APIConnectionError(f"Request failed: {e}")
|
|
222
|
+
|
|
223
|
+
def for_non_stream():
|
|
224
|
+
full_response = ""
|
|
225
|
+
all_links = []
|
|
226
|
+
for chunk in for_stream():
|
|
227
|
+
if raw:
|
|
228
|
+
yield chunk
|
|
229
|
+
else:
|
|
230
|
+
full_response += str(chunk)
|
|
231
|
+
if isinstance(chunk, dict):
|
|
232
|
+
all_links.extend(chunk.get("links", []))
|
|
233
|
+
|
|
234
|
+
if not raw:
|
|
235
|
+
self.last_response = Response(full_response)
|
|
236
|
+
return self.last_response
|
|
237
|
+
|
|
238
|
+
return for_stream() if stream else for_non_stream()
|
|
239
|
+
|
|
240
|
+
@staticmethod
|
|
241
|
+
def format_response(text: str, links: list) -> str:
|
|
242
|
+
"""Format the response text with numbered citations and link list.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
text (str): The response text with citation markers
|
|
246
|
+
links (list): List of reference links
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
str: Formatted text with numbered citations and link list
|
|
250
|
+
"""
|
|
251
|
+
# Clean up text
|
|
252
|
+
text = re.sub(r'\s+', ' ', text).strip()
|
|
253
|
+
|
|
254
|
+
# Replace citations with numbers
|
|
255
|
+
link_map = {f"citation:{i}]]": f"[{i}]" for i, _ in enumerate(links, start=1)}
|
|
256
|
+
for key, value in link_map.items():
|
|
257
|
+
text = text.replace(key, value)
|
|
258
|
+
text = text.replace("[[[", "[")
|
|
259
|
+
|
|
260
|
+
# Format link list
|
|
261
|
+
link_list = "\n".join([f"{i}. {link}" for i, link in enumerate(links, start=1)])
|
|
262
|
+
|
|
263
|
+
return f"{text}\n\nLinks:\n{link_list}"
|
|
264
|
+
|
|
265
|
+
if __name__ == "__main__":
|
|
266
|
+
from rich import print
|
|
267
|
+
|
|
268
|
+
# Initialize with specific model and logging
|
|
269
|
+
ai = Isou(
|
|
270
|
+
model="siliconflow:deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
|
271
|
+
logging=False
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
response = ai.search(input(">>> "), stream=True, raw=False)
|
|
275
|
+
for chunk in response:
|
|
276
|
+
print(chunk, end="", flush=True)
|
|
277
|
+
|