webscout 6.5__py3-none-any.whl → 6.6__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/Extra/autocoder/autocoder_utiles.py +119 -101
- webscout/Provider/AISEARCH/__init__.py +2 -0
- webscout/Provider/AISEARCH/ooai.py +155 -0
- webscout/Provider/Amigo.py +70 -85
- webscout/Provider/{prefind.py → Jadve.py} +72 -70
- webscout/Provider/Netwrck.py +235 -0
- webscout/Provider/Openai.py +4 -3
- webscout/Provider/PI.py +2 -2
- webscout/Provider/PizzaGPT.py +3 -3
- webscout/Provider/TeachAnything.py +15 -2
- webscout/Provider/Youchat.py +42 -8
- webscout/Provider/__init__.py +134 -147
- webscout/Provider/multichat.py +230 -0
- webscout/Provider/promptrefine.py +2 -2
- webscout/Provider/talkai.py +10 -13
- webscout/Provider/turboseek.py +5 -4
- webscout/Provider/tutorai.py +8 -112
- webscout/Provider/typegpt.py +4 -5
- webscout/Provider/x0gpt.py +81 -9
- webscout/Provider/yep.py +123 -361
- webscout/__init__.py +10 -1
- webscout/conversation.py +24 -9
- webscout/exceptions.py +188 -20
- webscout/litprinter/__init__.py +4 -117
- webscout/litprinter/colors.py +54 -0
- webscout/optimizers.py +335 -185
- webscout/scout/__init__.py +2 -5
- webscout/scout/core/__init__.py +7 -0
- webscout/scout/core/crawler.py +140 -0
- webscout/scout/core/scout.py +571 -0
- webscout/scout/core/search_result.py +96 -0
- webscout/scout/core/text_analyzer.py +63 -0
- webscout/scout/core/text_utils.py +277 -0
- webscout/scout/core/web_analyzer.py +52 -0
- webscout/scout/element.py +6 -5
- webscout/update_checker.py +117 -58
- webscout/version.py +1 -1
- webscout/zeroart/base.py +15 -16
- webscout/zeroart/effects.py +1 -1
- webscout/zeroart/fonts.py +1 -1
- {webscout-6.5.dist-info → webscout-6.6.dist-info}/METADATA +8 -165
- {webscout-6.5.dist-info → webscout-6.6.dist-info}/RECORD +59 -41
- webscout-6.6.dist-info/top_level.txt +2 -0
- webstoken/__init__.py +30 -0
- webstoken/classifier.py +189 -0
- webstoken/keywords.py +216 -0
- webstoken/language.py +128 -0
- webstoken/ner.py +164 -0
- webstoken/normalizer.py +35 -0
- webstoken/processor.py +77 -0
- webstoken/sentiment.py +206 -0
- webstoken/stemmer.py +73 -0
- webstoken/t.py +75 -0
- webstoken/tagger.py +60 -0
- webstoken/tokenizer.py +158 -0
- webscout/Provider/Perplexity.py +0 -591
- webscout/Provider/RoboCoders.py +0 -206
- webscout/Provider/genspark.py +0 -225
- webscout/Provider/perplexitylabs.py +0 -265
- webscout/Provider/twitterclone.py +0 -251
- webscout/Provider/upstage.py +0 -230
- webscout-6.5.dist-info/top_level.txt +0 -1
- /webscout/Provider/{felo_search.py → AISEARCH/felo_search.py} +0 -0
- {webscout-6.5.dist-info → webscout-6.6.dist-info}/LICENSE.md +0 -0
- {webscout-6.5.dist-info → webscout-6.6.dist-info}/WHEEL +0 -0
- {webscout-6.5.dist-info → webscout-6.6.dist-info}/entry_points.txt +0 -0
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
import random
|
|
4
|
-
from webscout.AIutel import Optimizers, Conversation, AwesomePrompts
|
|
5
|
-
from webscout.AIbase import Provider
|
|
6
|
-
from webscout import exceptions
|
|
7
|
-
|
|
8
|
-
class AIUncensored(Provider):
|
|
9
|
-
"""
|
|
10
|
-
A class to interact with the AIUncensored.info API.
|
|
11
|
-
"""
|
|
12
|
-
|
|
13
|
-
def __init__(
|
|
14
|
-
self,
|
|
15
|
-
is_conversation: bool = True,
|
|
16
|
-
max_tokens: int = 600,
|
|
17
|
-
timeout: int = 30,
|
|
18
|
-
intro: str = None,
|
|
19
|
-
filepath: str = None,
|
|
20
|
-
update_file: bool = True,
|
|
21
|
-
proxies: dict = {},
|
|
22
|
-
history_offset: int = 10250,
|
|
23
|
-
act: str = None,
|
|
24
|
-
system_prompt: str = "You are a helpful AI assistant.",
|
|
25
|
-
):
|
|
26
|
-
"""
|
|
27
|
-
Initializes the AIUncensored.info API with given parameters.
|
|
28
|
-
|
|
29
|
-
Args:
|
|
30
|
-
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
31
|
-
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
32
|
-
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
33
|
-
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
34
|
-
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
35
|
-
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
36
|
-
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
37
|
-
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
38
|
-
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
39
|
-
system_prompt (str, optional): System prompt for AIUncensored.
|
|
40
|
-
Defaults to "You are a helpful AI assistant.".
|
|
41
|
-
"""
|
|
42
|
-
self.session = requests.Session()
|
|
43
|
-
self.is_conversation = is_conversation
|
|
44
|
-
self.max_tokens_to_sample = max_tokens
|
|
45
|
-
self.api_endpoint = ['https://twitterclone-i0wr.onrender.com/api/chat', 'https://twitterclone-4e8t.onrender.com/api/chat', 'https://twitterclone-8wd1.onrender.com/api/chat']
|
|
46
|
-
self.endpoint_index = 0
|
|
47
|
-
self.stream_chunk_size = 64
|
|
48
|
-
self.timeout = timeout
|
|
49
|
-
self.last_response = {}
|
|
50
|
-
self.system_prompt = system_prompt
|
|
51
|
-
self.headers = {
|
|
52
|
-
"authority": "twitterclone-i0wr.onrender.com",
|
|
53
|
-
"accept": "*/*",
|
|
54
|
-
"accept-encoding": "gzip, deflate, br, zstd",
|
|
55
|
-
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
56
|
-
"content-type": "application/json",
|
|
57
|
-
"dnt": "1",
|
|
58
|
-
"origin": "https://www.aiuncensored.info",
|
|
59
|
-
"priority": "u=1, i",
|
|
60
|
-
"referer": "https://www.aiuncensored.info/",
|
|
61
|
-
"sec-ch-ua": '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"',
|
|
62
|
-
"sec-ch-ua-mobile": "?0",
|
|
63
|
-
"sec-ch-ua-platform": '"Windows"',
|
|
64
|
-
"sec-fetch-dest": "empty",
|
|
65
|
-
"sec-fetch-mode": "cors",
|
|
66
|
-
"sec-fetch-site": "cross-site",
|
|
67
|
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
self.__available_optimizers = [
|
|
71
|
-
method for method in dir(Optimizers)
|
|
72
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
73
|
-
]
|
|
74
|
-
self.session.headers.update(self.headers)
|
|
75
|
-
Conversation.intro = (
|
|
76
|
-
AwesomePrompts().get_act(
|
|
77
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
78
|
-
)
|
|
79
|
-
if act
|
|
80
|
-
else intro or Conversation.intro
|
|
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
|
-
self.session.proxies = proxies
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def ask(
|
|
90
|
-
self,
|
|
91
|
-
prompt: str,
|
|
92
|
-
stream: bool = False,
|
|
93
|
-
raw: bool = False,
|
|
94
|
-
optimizer: str = None,
|
|
95
|
-
conversationally: bool = False,
|
|
96
|
-
):
|
|
97
|
-
"""
|
|
98
|
-
Chat with AI
|
|
99
|
-
|
|
100
|
-
Args:
|
|
101
|
-
prompt (str): Prompt to be sent.
|
|
102
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
103
|
-
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
104
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
105
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
106
|
-
|
|
107
|
-
Returns:
|
|
108
|
-
dict or generator:
|
|
109
|
-
If stream is False, returns a dict:
|
|
110
|
-
```json
|
|
111
|
-
{
|
|
112
|
-
"text" : "How may I assist you today?"
|
|
113
|
-
}
|
|
114
|
-
```
|
|
115
|
-
If stream is True, yields dicts with incremental text.
|
|
116
|
-
"""
|
|
117
|
-
|
|
118
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if optimizer:
|
|
122
|
-
|
|
123
|
-
if optimizer in self.__available_optimizers:
|
|
124
|
-
try:
|
|
125
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
126
|
-
conversation_prompt if conversationally else prompt
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
except Exception as e:
|
|
130
|
-
|
|
131
|
-
raise
|
|
132
|
-
else:
|
|
133
|
-
|
|
134
|
-
raise Exception(
|
|
135
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
payload = {
|
|
139
|
-
"messages": [
|
|
140
|
-
{
|
|
141
|
-
"role": "system",
|
|
142
|
-
"content": self.system_prompt
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
"role": "user",
|
|
146
|
-
"content": conversation_prompt
|
|
147
|
-
}
|
|
148
|
-
],
|
|
149
|
-
"cipher": "24040024"
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
def for_stream():
|
|
155
|
-
full_content = ''
|
|
156
|
-
with requests.post(self.api_endpoint[self.endpoint_index], headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
|
|
157
|
-
if response.status_code == 200:
|
|
158
|
-
for line in response.iter_lines():
|
|
159
|
-
decoded_line = line.decode('utf-8').strip()
|
|
160
|
-
if decoded_line:
|
|
161
|
-
|
|
162
|
-
if decoded_line == "data: [DONE]":
|
|
163
|
-
|
|
164
|
-
break
|
|
165
|
-
if decoded_line.startswith("data: "):
|
|
166
|
-
data_str = decoded_line[len("data: "):]
|
|
167
|
-
try:
|
|
168
|
-
data_json = json.loads(data_str)
|
|
169
|
-
content = data_json.get("data", "")
|
|
170
|
-
if content:
|
|
171
|
-
full_content += content
|
|
172
|
-
yield content if raw else dict(text=content)
|
|
173
|
-
except json.JSONDecodeError:
|
|
174
|
-
raise Exception
|
|
175
|
-
self.last_response.update(dict(text=full_content))
|
|
176
|
-
self.conversation.update_chat_history(
|
|
177
|
-
prompt, self.get_message(self.last_response)
|
|
178
|
-
)
|
|
179
|
-
self.endpoint_index = (self.endpoint_index + 1) % len(self.api_endpoint)
|
|
180
|
-
def for_non_stream():
|
|
181
|
-
full_content = ''
|
|
182
|
-
for _ in for_stream():
|
|
183
|
-
pass
|
|
184
|
-
return self.last_response
|
|
185
|
-
|
|
186
|
-
return for_stream() if stream else for_non_stream()
|
|
187
|
-
|
|
188
|
-
def chat(
|
|
189
|
-
self,
|
|
190
|
-
prompt: str,
|
|
191
|
-
stream: bool = False,
|
|
192
|
-
optimizer: str = None,
|
|
193
|
-
conversationally: bool = False,
|
|
194
|
-
):
|
|
195
|
-
"""
|
|
196
|
-
Generate response `str`
|
|
197
|
-
Args:
|
|
198
|
-
prompt (str): Prompt to be sent.
|
|
199
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
200
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
201
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
202
|
-
Returns:
|
|
203
|
-
str or generator:
|
|
204
|
-
If stream is False, returns a string.
|
|
205
|
-
If stream is True, yields incremental strings.
|
|
206
|
-
"""
|
|
207
|
-
|
|
208
|
-
def for_stream():
|
|
209
|
-
|
|
210
|
-
for response in self.ask(
|
|
211
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
212
|
-
):
|
|
213
|
-
|
|
214
|
-
yield self.get_message(response)
|
|
215
|
-
|
|
216
|
-
def for_non_stream():
|
|
217
|
-
|
|
218
|
-
response = self.ask(
|
|
219
|
-
prompt,
|
|
220
|
-
False,
|
|
221
|
-
optimizer=optimizer,
|
|
222
|
-
conversationally=conversationally,
|
|
223
|
-
)
|
|
224
|
-
|
|
225
|
-
return self.get_message(response)
|
|
226
|
-
|
|
227
|
-
return for_stream() if stream else for_non_stream()
|
|
228
|
-
|
|
229
|
-
@staticmethod
|
|
230
|
-
def generate_cipher() -> str:
|
|
231
|
-
"""Generate a cipher in format like '3221229284179118'"""
|
|
232
|
-
return ''.join([str(random.randint(0, 9)) for _ in range(16)])
|
|
233
|
-
|
|
234
|
-
def get_message(self, response: dict) -> str:
|
|
235
|
-
"""Retrieves message only from response
|
|
236
|
-
|
|
237
|
-
Args:
|
|
238
|
-
response (dict): Response generated by `self.ask`
|
|
239
|
-
|
|
240
|
-
Returns:
|
|
241
|
-
str: Message extracted
|
|
242
|
-
"""
|
|
243
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
244
|
-
return response["text"]
|
|
245
|
-
|
|
246
|
-
if __name__ == "__main__":
|
|
247
|
-
from rich import print
|
|
248
|
-
ai = AIUncensored(timeout=5000)
|
|
249
|
-
response = ai.chat("write a poem about AI", stream=True)
|
|
250
|
-
for chunk in response:
|
|
251
|
-
print(chunk, end="", flush=True)
|
webscout/Provider/upstage.py
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
from typing import Any, Dict, Optional
|
|
4
|
-
|
|
5
|
-
from webscout.AIutel import Optimizers
|
|
6
|
-
from webscout.AIutel import Conversation
|
|
7
|
-
from webscout.AIutel import AwesomePrompts, sanitize_stream
|
|
8
|
-
from webscout.AIbase import Provider
|
|
9
|
-
from webscout import exceptions
|
|
10
|
-
|
|
11
|
-
class Upstage(Provider):
|
|
12
|
-
"""
|
|
13
|
-
A class to interact with the Upstage API.
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
AVAILABLE_MODELS = [
|
|
17
|
-
"upstage/solar-1-mini-chat",
|
|
18
|
-
"upstage/solar-1-mini-chat-ja",
|
|
19
|
-
"solar-pro"
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
def __init__(
|
|
23
|
-
self,
|
|
24
|
-
is_conversation: bool = True,
|
|
25
|
-
max_tokens: int = 600,
|
|
26
|
-
timeout: int = 30,
|
|
27
|
-
intro: str = None,
|
|
28
|
-
filepath: str = None,
|
|
29
|
-
update_file: bool = True,
|
|
30
|
-
proxies: dict = {},
|
|
31
|
-
history_offset: int = 10250,
|
|
32
|
-
act: str = None,
|
|
33
|
-
model: str = "upstage/solar-1-mini-chat",
|
|
34
|
-
) -> None:
|
|
35
|
-
"""
|
|
36
|
-
Initializes the Upstage API with given parameters.
|
|
37
|
-
|
|
38
|
-
Args:
|
|
39
|
-
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
40
|
-
max_tokens (int, optional): Maximum number of tokens to be generated upon completion.
|
|
41
|
-
Defaults to 600.
|
|
42
|
-
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
43
|
-
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
44
|
-
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
45
|
-
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
46
|
-
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
47
|
-
history_offset (int, optional): Limit conversation history to this number of last texts.
|
|
48
|
-
Defaults to 10250.
|
|
49
|
-
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
50
|
-
model (str, optional): AI model to use. Defaults to "upstage/solar-1-mini-chat".
|
|
51
|
-
Available models: "upstage/solar-1-mini-chat",
|
|
52
|
-
"upstage/solar-1-mini-chat-ja",
|
|
53
|
-
"solar-pro"
|
|
54
|
-
"""
|
|
55
|
-
if model not in self.AVAILABLE_MODELS:
|
|
56
|
-
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
57
|
-
|
|
58
|
-
self.session = requests.Session()
|
|
59
|
-
self.is_conversation = is_conversation
|
|
60
|
-
self.max_tokens_to_sample = max_tokens
|
|
61
|
-
self.api_endpoint = "https://ap-northeast-2.apistage.ai/v1/web/demo/chat/completions"
|
|
62
|
-
self.stream_chunk_size = 64
|
|
63
|
-
self.timeout = timeout
|
|
64
|
-
self.last_response = {}
|
|
65
|
-
self.model = model
|
|
66
|
-
self.headers = {
|
|
67
|
-
"Content-Type": "application/json",
|
|
68
|
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
|
|
69
|
-
"Origin": "https://console.upstage.ai",
|
|
70
|
-
"Referer": "https://console.upstage.ai/"
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
self.__available_optimizers = (
|
|
74
|
-
method
|
|
75
|
-
for method in dir(Optimizers)
|
|
76
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
77
|
-
)
|
|
78
|
-
self.session.headers.update(self.headers)
|
|
79
|
-
Conversation.intro = (
|
|
80
|
-
AwesomePrompts().get_act(
|
|
81
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
82
|
-
)
|
|
83
|
-
if act
|
|
84
|
-
else intro or Conversation.intro
|
|
85
|
-
)
|
|
86
|
-
self.conversation = Conversation(
|
|
87
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
88
|
-
)
|
|
89
|
-
self.conversation.history_offset = history_offset
|
|
90
|
-
self.session.proxies = proxies
|
|
91
|
-
|
|
92
|
-
def ask(
|
|
93
|
-
self,
|
|
94
|
-
prompt: str,
|
|
95
|
-
stream: bool = False,
|
|
96
|
-
raw: bool = False,
|
|
97
|
-
optimizer: str = None,
|
|
98
|
-
conversationally: bool = False,
|
|
99
|
-
) -> Dict[str, Any]:
|
|
100
|
-
"""
|
|
101
|
-
Sends a prompt to the Upstage API and returns the response.
|
|
102
|
-
|
|
103
|
-
Args:
|
|
104
|
-
prompt: The text prompt to generate text from.
|
|
105
|
-
stream (bool, optional): Whether to stream the response. Defaults to False.
|
|
106
|
-
raw (bool, optional): Whether to return the raw response. Defaults to False.
|
|
107
|
-
optimizer (str, optional): The name of the optimizer to use. Defaults to None.
|
|
108
|
-
conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
|
|
109
|
-
|
|
110
|
-
Returns:
|
|
111
|
-
The response from the API.
|
|
112
|
-
"""
|
|
113
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
114
|
-
if optimizer:
|
|
115
|
-
if optimizer in self.__available_optimizers:
|
|
116
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
117
|
-
conversation_prompt if conversationally else prompt
|
|
118
|
-
)
|
|
119
|
-
else:
|
|
120
|
-
raise Exception(
|
|
121
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
payload = {
|
|
125
|
-
"stream": True,
|
|
126
|
-
"messages": [
|
|
127
|
-
{
|
|
128
|
-
"role": "user",
|
|
129
|
-
"content": conversation_prompt
|
|
130
|
-
}
|
|
131
|
-
],
|
|
132
|
-
"model": self.model
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
def for_stream():
|
|
136
|
-
response = self.session.post(
|
|
137
|
-
self.api_endpoint, headers=self.headers, json=payload, stream=True, timeout=self.timeout
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
if not response.ok:
|
|
141
|
-
# If 'solar-pro' fails, try mini-chat model
|
|
142
|
-
if self.model == "solar-pro":
|
|
143
|
-
print("solar-pro failed. Trying 'upstage/solar-1-mini-chat'...")
|
|
144
|
-
self.model = "upstage/solar-1-mini-chat"
|
|
145
|
-
return self.ask(prompt, stream, raw, optimizer, conversationally) # Retry with mini-chat
|
|
146
|
-
|
|
147
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
148
|
-
f"Failed to generate response - ({response.status_code}, {response.reason})"
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
streaming_response = ""
|
|
152
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
153
|
-
if line:
|
|
154
|
-
if line.startswith("data: "):
|
|
155
|
-
data = line[6:] # Remove 'data: ' prefix
|
|
156
|
-
if data != "[DONE]":
|
|
157
|
-
try:
|
|
158
|
-
json_data = json.loads(data)
|
|
159
|
-
content = json_data['choices'][0]['delta'].get('content', '')
|
|
160
|
-
if content:
|
|
161
|
-
streaming_response += content
|
|
162
|
-
yield content if raw else dict(text=content)
|
|
163
|
-
except json.JSONDecodeError:
|
|
164
|
-
print(f"Error decoding JSON: {data}")
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
self.last_response.update(dict(text=streaming_response))
|
|
168
|
-
self.conversation.update_chat_history(
|
|
169
|
-
prompt, self.get_message(self.last_response)
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
def for_non_stream():
|
|
173
|
-
for _ in for_stream():
|
|
174
|
-
pass
|
|
175
|
-
return self.last_response
|
|
176
|
-
|
|
177
|
-
return for_stream() if stream else for_non_stream()
|
|
178
|
-
|
|
179
|
-
def chat(
|
|
180
|
-
self,
|
|
181
|
-
prompt: str,
|
|
182
|
-
stream: bool = False,
|
|
183
|
-
optimizer: str = None,
|
|
184
|
-
conversationally: bool = False,
|
|
185
|
-
) -> str:
|
|
186
|
-
"""Generate response `str`
|
|
187
|
-
Args:
|
|
188
|
-
prompt (str): Prompt to be send.
|
|
189
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
190
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
191
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
192
|
-
Returns:
|
|
193
|
-
str: Response generated
|
|
194
|
-
"""
|
|
195
|
-
|
|
196
|
-
def for_stream():
|
|
197
|
-
for response in self.ask(
|
|
198
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
199
|
-
):
|
|
200
|
-
yield self.get_message(response)
|
|
201
|
-
|
|
202
|
-
def for_non_stream():
|
|
203
|
-
return self.get_message(
|
|
204
|
-
self.ask(
|
|
205
|
-
prompt,
|
|
206
|
-
False,
|
|
207
|
-
optimizer=optimizer,
|
|
208
|
-
conversationally=conversationally,
|
|
209
|
-
)
|
|
210
|
-
)
|
|
211
|
-
|
|
212
|
-
return for_stream() if stream else for_non_stream()
|
|
213
|
-
|
|
214
|
-
def get_message(self, response: dict) -> str:
|
|
215
|
-
"""Retrieves message only from response
|
|
216
|
-
|
|
217
|
-
Args:
|
|
218
|
-
response (dict): Response generated by `self.ask`
|
|
219
|
-
|
|
220
|
-
Returns:
|
|
221
|
-
str: Message extracted
|
|
222
|
-
"""
|
|
223
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
224
|
-
return response["text"]
|
|
225
|
-
if __name__ == '__main__':
|
|
226
|
-
from rich import print
|
|
227
|
-
ai = Upstage(timeout=5000)
|
|
228
|
-
response = ai.chat("write a poem about AI", stream=True)
|
|
229
|
-
for chunk in response:
|
|
230
|
-
print(chunk, end="", flush=True)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
webscout
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|