webscout 6.7__py3-none-any.whl → 6.8__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/Provider/Cloudflare.py +2 -1
- webscout/Provider/DARKAI.py +2 -2
- webscout/Provider/Free2GPT.py +5 -5
- webscout/Provider/Marcus.py +3 -3
- webscout/Provider/PI.py +113 -47
- webscout/Provider/Phind.py +6 -0
- webscout/Provider/PizzaGPT.py +62 -53
- webscout/Provider/RUBIKSAI.py +93 -38
- webscout/Provider/__init__.py +0 -6
- webscout/Provider/cleeai.py +2 -2
- webscout/Provider/elmo.py +2 -2
- webscout/Provider/gaurish.py +2 -2
- webscout/Provider/geminiprorealtime.py +2 -2
- webscout/Provider/lepton.py +2 -2
- webscout/Provider/llama3mitril.py +3 -3
- webscout/Provider/llamatutor.py +2 -2
- webscout/Provider/llmchat.py +3 -2
- webscout/Provider/meta.py +2 -2
- webscout/Provider/tutorai.py +1 -1
- webscout/version.py +1 -1
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/METADATA +1 -1
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/RECORD +26 -30
- webscout/Provider/Farfalle.py +0 -227
- webscout/Provider/NinjaChat.py +0 -200
- webscout/Provider/mhystical.py +0 -176
- webstoken/t.py +0 -75
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/LICENSE.md +0 -0
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/WHEEL +0 -0
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/entry_points.txt +0 -0
- {webscout-6.7.dist-info → webscout-6.8.dist-info}/top_level.txt +0 -0
webscout/Provider/Farfalle.py
DELETED
|
@@ -1,227 +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
|
|
8
|
-
from webscout.AIbase import Provider, AsyncProvider
|
|
9
|
-
from webscout import exceptions
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Farfalle(Provider):
|
|
13
|
-
"""
|
|
14
|
-
A class to interact with the Farfalle.dev API.
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
AVAILABLE_MODELS = [
|
|
18
|
-
"gpt-3.5-turbo",
|
|
19
|
-
|
|
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 = "gpt-3.5-turbo",
|
|
34
|
-
) -> None:
|
|
35
|
-
"""
|
|
36
|
-
Initializes the Farfalle.dev 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 "gpt-3.5-turbo".
|
|
51
|
-
Options: "gpt-3.5-turbo", "gpt-4"
|
|
52
|
-
"""
|
|
53
|
-
if model not in self.AVAILABLE_MODELS:
|
|
54
|
-
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
55
|
-
|
|
56
|
-
self.session = requests.Session()
|
|
57
|
-
self.is_conversation = is_conversation
|
|
58
|
-
self.max_tokens_to_sample = max_tokens
|
|
59
|
-
self.api_endpoint = "https://api.farfalle.dev/chat"
|
|
60
|
-
self.stream_chunk_size = 64
|
|
61
|
-
self.timeout = timeout
|
|
62
|
-
self.last_response = {}
|
|
63
|
-
self.model = model
|
|
64
|
-
self.headers = {
|
|
65
|
-
"accept": "text/event-stream",
|
|
66
|
-
"accept-encoding": "gzip, deflate, br, zstd",
|
|
67
|
-
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
68
|
-
"content-type": "application/json",
|
|
69
|
-
"dnt": "1",
|
|
70
|
-
"origin": "https://www.farfalle.dev",
|
|
71
|
-
"priority": "u=1, i",
|
|
72
|
-
"referer": "https://www.farfalle.dev/",
|
|
73
|
-
"sec-ch-ua": '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
|
|
74
|
-
"sec-ch-ua-mobile": "?0",
|
|
75
|
-
"sec-ch-ua-platform": '"Windows"',
|
|
76
|
-
"sec-fetch-dest": "empty",
|
|
77
|
-
"sec-fetch-mode": "cors",
|
|
78
|
-
"sec-fetch-site": "same-site",
|
|
79
|
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0"
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
self.__available_optimizers = (
|
|
83
|
-
method
|
|
84
|
-
for method in dir(Optimizers)
|
|
85
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
86
|
-
)
|
|
87
|
-
self.session.headers.update(self.headers)
|
|
88
|
-
Conversation.intro = (
|
|
89
|
-
AwesomePrompts().get_act(
|
|
90
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
91
|
-
)
|
|
92
|
-
if act
|
|
93
|
-
else intro or Conversation.intro
|
|
94
|
-
)
|
|
95
|
-
self.conversation = Conversation(
|
|
96
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
97
|
-
)
|
|
98
|
-
self.conversation.history_offset = history_offset
|
|
99
|
-
self.session.proxies = proxies
|
|
100
|
-
|
|
101
|
-
def ask(
|
|
102
|
-
self,
|
|
103
|
-
prompt: str,
|
|
104
|
-
stream: bool = False,
|
|
105
|
-
raw: bool = False,
|
|
106
|
-
optimizer: str = None,
|
|
107
|
-
conversationally: bool = False,
|
|
108
|
-
) -> Dict[str, Any]:
|
|
109
|
-
"""
|
|
110
|
-
Sends a prompt to the Farfalle.dev API and returns the response.
|
|
111
|
-
|
|
112
|
-
Args:
|
|
113
|
-
prompt: The text prompt to generate text from.
|
|
114
|
-
stream (bool, optional): Whether to stream the response. Defaults to False.
|
|
115
|
-
raw (bool, optional): Whether to return the raw response. Defaults to False.
|
|
116
|
-
optimizer (str, optional): The name of the optimizer to use. Defaults to None.
|
|
117
|
-
conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
|
|
118
|
-
|
|
119
|
-
Returns:
|
|
120
|
-
The response from the API.
|
|
121
|
-
"""
|
|
122
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
123
|
-
if optimizer:
|
|
124
|
-
if optimizer in self.__available_optimizers:
|
|
125
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
126
|
-
conversation_prompt if conversationally else prompt
|
|
127
|
-
)
|
|
128
|
-
else:
|
|
129
|
-
raise Exception(
|
|
130
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
131
|
-
)
|
|
132
|
-
|
|
133
|
-
payload = {
|
|
134
|
-
"query": conversation_prompt,
|
|
135
|
-
"model": self.model
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
def for_stream():
|
|
139
|
-
response = self.session.post(
|
|
140
|
-
self.api_endpoint, json=payload, headers=self.headers, stream=True, timeout=self.timeout
|
|
141
|
-
)
|
|
142
|
-
|
|
143
|
-
if not response.ok:
|
|
144
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
145
|
-
f"Failed to generate response - ({response.status_code}, {response.reason})"
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
streaming_response = ""
|
|
149
|
-
for line in response.iter_lines():
|
|
150
|
-
if line:
|
|
151
|
-
decoded_line = line.decode('utf-8')
|
|
152
|
-
if decoded_line.startswith("data:"):
|
|
153
|
-
data = decoded_line[len("data:"):].strip()
|
|
154
|
-
if data:
|
|
155
|
-
try:
|
|
156
|
-
event = json.loads(data)
|
|
157
|
-
if event.get("event") == "final-response":
|
|
158
|
-
message = event['data'].get('message', '')
|
|
159
|
-
streaming_response += message
|
|
160
|
-
yield message if raw else dict(text=streaming_response)
|
|
161
|
-
except json.decoder.JSONDecodeError:
|
|
162
|
-
continue
|
|
163
|
-
self.last_response.update(dict(text=streaming_response))
|
|
164
|
-
self.conversation.update_chat_history(
|
|
165
|
-
prompt, self.get_message(self.last_response)
|
|
166
|
-
)
|
|
167
|
-
def for_non_stream():
|
|
168
|
-
for _ in for_stream():
|
|
169
|
-
pass
|
|
170
|
-
return self.last_response
|
|
171
|
-
|
|
172
|
-
return for_stream() if stream else for_non_stream()
|
|
173
|
-
|
|
174
|
-
def chat(
|
|
175
|
-
self,
|
|
176
|
-
prompt: str,
|
|
177
|
-
stream: bool = False,
|
|
178
|
-
optimizer: str = None,
|
|
179
|
-
conversationally: bool = False,
|
|
180
|
-
) -> str:
|
|
181
|
-
"""Generate response `str`
|
|
182
|
-
Args:
|
|
183
|
-
prompt (str): Prompt to be send.
|
|
184
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
185
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
186
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
187
|
-
Returns:
|
|
188
|
-
str: Response generated
|
|
189
|
-
"""
|
|
190
|
-
|
|
191
|
-
def for_stream():
|
|
192
|
-
for response in self.ask(
|
|
193
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
194
|
-
):
|
|
195
|
-
yield self.get_message(response)
|
|
196
|
-
|
|
197
|
-
def for_non_stream():
|
|
198
|
-
return self.get_message(
|
|
199
|
-
self.ask(
|
|
200
|
-
prompt,
|
|
201
|
-
False,
|
|
202
|
-
optimizer=optimizer,
|
|
203
|
-
conversationally=conversationally,
|
|
204
|
-
)
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
return for_stream() if stream else for_non_stream()
|
|
208
|
-
|
|
209
|
-
def get_message(self, response: dict) -> str:
|
|
210
|
-
"""Retrieves message only from response
|
|
211
|
-
|
|
212
|
-
Args:
|
|
213
|
-
response (dict): Response generated by `self.ask`
|
|
214
|
-
|
|
215
|
-
Returns:
|
|
216
|
-
str: Message extracted
|
|
217
|
-
"""
|
|
218
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
219
|
-
return response["text"]
|
|
220
|
-
if __name__ == "__main__":
|
|
221
|
-
from rich import print
|
|
222
|
-
|
|
223
|
-
ai = Farfalle()
|
|
224
|
-
# Stream the response
|
|
225
|
-
response = ai.chat("who is pm of india")
|
|
226
|
-
for chunk in response:
|
|
227
|
-
print(chunk, end="", flush=True)
|
webscout/Provider/NinjaChat.py
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
import os
|
|
4
|
-
from typing import Any, Dict, Optional, Generator, List, Union
|
|
5
|
-
|
|
6
|
-
from webscout.AIutel import Optimizers
|
|
7
|
-
from webscout.AIutel import Conversation
|
|
8
|
-
from webscout.AIutel import AwesomePrompts, sanitize_stream
|
|
9
|
-
from webscout.AIbase import Provider, AsyncProvider
|
|
10
|
-
from webscout import exceptions
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class NinjaChat(Provider):
|
|
14
|
-
"""
|
|
15
|
-
A class to interact with the NinjaChat API.
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
AVAILABLE_MODELS = {
|
|
19
|
-
"mistral": "https://www.ninjachat.ai/api/mistral",
|
|
20
|
-
"perplexity": "https://www.ninjachat.ai/api/perplexity",
|
|
21
|
-
"claude-3.5": "https://www.ninjachat.ai/api/claude-pro",
|
|
22
|
-
"gemini-1.5-pro": "https://www.ninjachat.ai/api/gemini",
|
|
23
|
-
"llama": "https://www.ninjachat.ai/api/llama-pro",
|
|
24
|
-
"o1-mini": "https://www.ninjachat.ai/api/o1-mini",
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
def __init__(
|
|
28
|
-
self,
|
|
29
|
-
is_conversation: bool = True,
|
|
30
|
-
max_tokens: int = 2049,
|
|
31
|
-
timeout: int = 30,
|
|
32
|
-
intro: str = None, # System message/intro prompt
|
|
33
|
-
filepath: str = None,
|
|
34
|
-
update_file: bool = True,
|
|
35
|
-
proxies: dict = {},
|
|
36
|
-
history_offset: int = 10250,
|
|
37
|
-
act: str = None,
|
|
38
|
-
model: str = "llama", # Default model
|
|
39
|
-
system_message: str = "You are a helpful AI assistant.", # Default system message
|
|
40
|
-
):
|
|
41
|
-
"""Initializes the NinjaChat API client."""
|
|
42
|
-
|
|
43
|
-
self.headers = {
|
|
44
|
-
"Accept": "*/*",
|
|
45
|
-
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
46
|
-
"Accept-Language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
47
|
-
"Content-Type": "application/json",
|
|
48
|
-
"Cookie": "_ga=GA1.1.298084589.1727859540; _ga_11N4NZX9WP=GS1.1.1727859539.1.0.1727859552.0.0.0; __stripe_mid=4f63db68-c41d-45b4-9111-2457a6cf1b538696a9; __Host-next-auth.csrf-token=a5cb5a40c73df3e808ebc072dcb116fe7dd4b9b8d39d8002ef7e54153e6aa665%7Cbffe3f934f2db43330d281453af2cd0b4757f439b958f2d1a06a36cea63e9cc8; __stripe_sid=118678d1-403a-43f9-b3b9-d80ed9392a0d2ac131; __Secure-next-auth.callback-url=https%3A%2F%2Fwww.ninjachat.ai%2Fdashboard; __Secure-next-auth.session-token=eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..l34CIFGTJCtstUqU.VjEYgaUUPpgp-49wueXFlFYvbm8csuyX0HichHrPNH45nX4s_LeZX2VhK1ZvwmUpfdlsMD4bi8VzFfQUEgs8FLPhkbKnoZDP939vobV7K_2Q9CA8PgC0oXEsQf_azWmILZ8rOE37uYzTu1evCnOjCucDYrC1ONXzl9NbGNPVa8AQr7hXvatuqtqe-lBUQXWdrw3QLulbqxvh6yLoxJj04gqC-nPudGciU-_-3TZJYr98u8o7KtUGio1ZX9vHNFfv8djWM1NCkji3Kl9eUhiyMj71.6uhUS39UcCVRa6tFzHxz2g; ph_phc_wWUtqcGWqyyochfPvwKlXMkMjIoIQKUwcnHE3KMKm8K_posthog=%7B%22distinct_id%22%3A%2201924c74-2926-7042-a1fb-5b5debdbcd1c%22%2C%22%24sesid%22%3A%5B1727966419499%2C%22019252bb-9de4-75db-9f85-a389fb401670%22%2C1727964880355%5D%7D",
|
|
49
|
-
"DNT": "1",
|
|
50
|
-
"Origin": "https://www.ninjachat.ai",
|
|
51
|
-
"Priority": "u=1, i",
|
|
52
|
-
"Referer": "https://www.ninjachat.ai/dashboard",
|
|
53
|
-
"Sec-CH-UA": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
|
|
54
|
-
"Sec-CH-UA-Mobile": "?0",
|
|
55
|
-
"Sec-CH-UA-Platform": '"Windows"',
|
|
56
|
-
"Sec-Fetch-Dest": "empty",
|
|
57
|
-
"Sec-Fetch-Mode": "cors",
|
|
58
|
-
"Sec-Fetch-Site": "same-origin",
|
|
59
|
-
"User-Agent": (
|
|
60
|
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
|
61
|
-
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
62
|
-
"Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
self.session = requests.Session()
|
|
66
|
-
self.session.headers.update(self.headers)
|
|
67
|
-
self.session.proxies.update(proxies)
|
|
68
|
-
self.timeout = timeout
|
|
69
|
-
self.last_response = {}
|
|
70
|
-
self.system_message = system_message
|
|
71
|
-
|
|
72
|
-
self.is_conversation = is_conversation
|
|
73
|
-
self.max_tokens_to_sample = max_tokens
|
|
74
|
-
self.__available_optimizers = (
|
|
75
|
-
method
|
|
76
|
-
for method in dir(Optimizers)
|
|
77
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
#Set the intro/system message
|
|
81
|
-
Conversation.intro = (
|
|
82
|
-
AwesomePrompts().get_act(
|
|
83
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
84
|
-
)
|
|
85
|
-
if act
|
|
86
|
-
else intro or system_message or Conversation.intro #Priority: act > intro > system_message > Conversation.intro
|
|
87
|
-
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
self.conversation = Conversation(
|
|
92
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
93
|
-
)
|
|
94
|
-
self.conversation.history_offset = history_offset
|
|
95
|
-
|
|
96
|
-
if model not in self.AVAILABLE_MODELS:
|
|
97
|
-
raise ValueError(f"Invalid model: {model}. Choose from: {', '.join(self.AVAILABLE_MODELS)}")
|
|
98
|
-
self.model_url = self.AVAILABLE_MODELS[model]
|
|
99
|
-
self.headers["Referer"] = self.model_url # Set initial referer
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def ask(
|
|
104
|
-
self,
|
|
105
|
-
prompt: str,
|
|
106
|
-
stream: bool = False,
|
|
107
|
-
raw: bool = False,
|
|
108
|
-
optimizer: str = None,
|
|
109
|
-
conversationally: bool = False,
|
|
110
|
-
) -> Union[Dict, Generator]:
|
|
111
|
-
|
|
112
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt, intro=Conversation.intro)
|
|
113
|
-
|
|
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(f"Optimizer is not one of {self.__available_optimizers}")
|
|
121
|
-
|
|
122
|
-
#Include the system message in the payload
|
|
123
|
-
payload = {
|
|
124
|
-
"messages": [
|
|
125
|
-
{"role": "system", "content": self.system_message}, # System message here
|
|
126
|
-
{"role": "user", "content": conversation_prompt},
|
|
127
|
-
],
|
|
128
|
-
"stream": stream # Now passed dynamically
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
def for_stream():
|
|
132
|
-
try:
|
|
133
|
-
with requests.post(self.model_url, headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
|
|
134
|
-
response.raise_for_status()
|
|
135
|
-
streaming_text = ""
|
|
136
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
137
|
-
if line:
|
|
138
|
-
if line.startswith("0:"):
|
|
139
|
-
try:
|
|
140
|
-
text = json.loads(line[2:]) # Extract streaming text
|
|
141
|
-
streaming_text += text #Accumulate for history
|
|
142
|
-
resp = dict(text=text)
|
|
143
|
-
yield resp if raw else resp
|
|
144
|
-
except json.JSONDecodeError:
|
|
145
|
-
print("\n[Error] Failed to decode JSON content.")
|
|
146
|
-
|
|
147
|
-
elif line.startswith("d:"):
|
|
148
|
-
break #End of stream
|
|
149
|
-
self.conversation.update_chat_history(prompt, streaming_text)
|
|
150
|
-
self.last_response.update({"text": streaming_text})
|
|
151
|
-
except requests.exceptions.RequestException as e:
|
|
152
|
-
print("An error occurred:", e)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
def for_non_stream():
|
|
157
|
-
|
|
158
|
-
for _ in for_stream():
|
|
159
|
-
pass
|
|
160
|
-
return self.last_response
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return for_stream() if stream else for_non_stream()
|
|
164
|
-
|
|
165
|
-
def chat(
|
|
166
|
-
self,
|
|
167
|
-
prompt: str,
|
|
168
|
-
stream: bool = False,
|
|
169
|
-
optimizer: str = None,
|
|
170
|
-
conversationally: bool = False,
|
|
171
|
-
) -> Union[str, Generator]:
|
|
172
|
-
|
|
173
|
-
def for_stream():
|
|
174
|
-
for response in self.ask(
|
|
175
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
176
|
-
):
|
|
177
|
-
yield self.get_message(response)
|
|
178
|
-
|
|
179
|
-
def for_non_stream():
|
|
180
|
-
return self.get_message(
|
|
181
|
-
self.ask(
|
|
182
|
-
prompt, False, optimizer=optimizer, conversationally=conversationally
|
|
183
|
-
)
|
|
184
|
-
)
|
|
185
|
-
return for_stream() if stream else for_non_stream()
|
|
186
|
-
|
|
187
|
-
def get_message(self, response: dict) -> str:
|
|
188
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
189
|
-
return response["text"]
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if __name__ == "__main__":
|
|
194
|
-
from rich import print
|
|
195
|
-
bot = NinjaChat(model="perplexity", system_message="You are a creative writer.")
|
|
196
|
-
|
|
197
|
-
response = bot.chat("tell me about Abhay koul, HelpingAI ", stream=True)
|
|
198
|
-
|
|
199
|
-
for chunk in response:
|
|
200
|
-
print(chunk, end="", flush=True)
|
webscout/Provider/mhystical.py
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
from typing import Any, Dict, Optional, Generator, List
|
|
4
|
-
|
|
5
|
-
from webscout.AIutel import Optimizers
|
|
6
|
-
from webscout.AIutel import Conversation
|
|
7
|
-
from webscout.AIutel import AwesomePrompts
|
|
8
|
-
from webscout.AIbase import Provider
|
|
9
|
-
from webscout import exceptions
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Mhystical(Provider):
|
|
13
|
-
"""
|
|
14
|
-
A class to interact with the Mhystical API. Improved to meet webscout provider standards.
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
AVAILABLE_MODELS = ["gpt-4", "gpt-3.5-turbo"] # Add available models
|
|
18
|
-
|
|
19
|
-
def __init__(
|
|
20
|
-
self,
|
|
21
|
-
is_conversation: bool = True,
|
|
22
|
-
max_tokens: int = 2048,
|
|
23
|
-
timeout: int = 30,
|
|
24
|
-
intro: str = None,
|
|
25
|
-
filepath: str = None,
|
|
26
|
-
update_file: bool = True,
|
|
27
|
-
proxies: dict = {},
|
|
28
|
-
history_offset: int = 10250,
|
|
29
|
-
act: str = None,
|
|
30
|
-
model: str = "gpt-4", # Default model
|
|
31
|
-
system_prompt: str = "You are a helpful AI assistant." # Default system prompt
|
|
32
|
-
):
|
|
33
|
-
"""Initializes the Mhystical API."""
|
|
34
|
-
if model not in self.AVAILABLE_MODELS:
|
|
35
|
-
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
36
|
-
|
|
37
|
-
self.session = requests.Session()
|
|
38
|
-
self.is_conversation = is_conversation
|
|
39
|
-
self.max_tokens_to_sample = max_tokens
|
|
40
|
-
self.api_endpoint = "https://api.mhystical.cc/v1/completions"
|
|
41
|
-
self.timeout = timeout
|
|
42
|
-
self.last_response = {}
|
|
43
|
-
self.model = model
|
|
44
|
-
self.system_prompt = system_prompt # Store system prompt
|
|
45
|
-
self.headers = {
|
|
46
|
-
"x-api-key": "mhystical", # Set API key in header (or better, in __init__ from parameter)
|
|
47
|
-
"Content-Type": "application/json",
|
|
48
|
-
"accept": "*/*",
|
|
49
|
-
"user-agent": "Mozilla/5.0"
|
|
50
|
-
}
|
|
51
|
-
self.__available_optimizers = (
|
|
52
|
-
method
|
|
53
|
-
for method in dir(Optimizers)
|
|
54
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
55
|
-
)
|
|
56
|
-
Conversation.intro = (
|
|
57
|
-
AwesomePrompts().get_act(
|
|
58
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
59
|
-
)
|
|
60
|
-
if act
|
|
61
|
-
else intro or Conversation.intro
|
|
62
|
-
)
|
|
63
|
-
self.conversation = Conversation(
|
|
64
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
65
|
-
)
|
|
66
|
-
self.conversation.history_offset = history_offset
|
|
67
|
-
self.session.proxies = proxies
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def ask(
|
|
71
|
-
self,
|
|
72
|
-
prompt: str,
|
|
73
|
-
stream: bool = False,
|
|
74
|
-
raw: bool = False,
|
|
75
|
-
optimizer: str = None,
|
|
76
|
-
conversationally: bool = False,
|
|
77
|
-
) -> Dict[str, Any] | Generator[Dict[str, Any], None, None]:
|
|
78
|
-
|
|
79
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
80
|
-
if optimizer:
|
|
81
|
-
if optimizer in self.__available_optimizers:
|
|
82
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
83
|
-
conversation_prompt if conversationally else prompt
|
|
84
|
-
)
|
|
85
|
-
else:
|
|
86
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
87
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
messages = [
|
|
91
|
-
{"role": "system", "content": self.system_prompt}, # Include system prompt
|
|
92
|
-
{"role": "user", "content": conversation_prompt},
|
|
93
|
-
]
|
|
94
|
-
|
|
95
|
-
data = {
|
|
96
|
-
"model": self.model, # Now using self.model
|
|
97
|
-
"messages": messages # Pass messages to API
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
def for_stream():
|
|
101
|
-
try:
|
|
102
|
-
with requests.post(self.api_endpoint, headers=self.headers, json=data, stream=True, timeout=self.timeout) as response:
|
|
103
|
-
response.raise_for_status() # Raise exceptions for HTTP errors
|
|
104
|
-
|
|
105
|
-
# Emulate streaming for this API
|
|
106
|
-
full_response = "" # Accumulate the full response
|
|
107
|
-
for chunk in response.iter_content(decode_unicode=True, chunk_size=self.stream_chunk_size):
|
|
108
|
-
if chunk:
|
|
109
|
-
full_response += chunk
|
|
110
|
-
yield chunk if raw else {"text": chunk}
|
|
111
|
-
|
|
112
|
-
self.last_response.update({"text": full_response})
|
|
113
|
-
self.conversation.update_chat_history(prompt, full_response)
|
|
114
|
-
except requests.exceptions.RequestException as e:
|
|
115
|
-
raise exceptions.ProviderConnectionError(f"Network error: {str(e)}")
|
|
116
|
-
|
|
117
|
-
def for_non_stream():
|
|
118
|
-
try:
|
|
119
|
-
response = self.session.post(self.api_endpoint, headers=self.headers, json=data, timeout=self.timeout)
|
|
120
|
-
response.raise_for_status()
|
|
121
|
-
|
|
122
|
-
full_response = self._parse_response(response.text)
|
|
123
|
-
self.last_response.update({"text": full_response})
|
|
124
|
-
|
|
125
|
-
# Yield the entire response as a single chunk
|
|
126
|
-
yield {"text": full_response}
|
|
127
|
-
|
|
128
|
-
except requests.exceptions.RequestException as e:
|
|
129
|
-
raise exceptions.ProviderConnectionError(f"Network error: {str(e)}")
|
|
130
|
-
|
|
131
|
-
return for_stream() if stream else for_non_stream()
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def chat(
|
|
136
|
-
self,
|
|
137
|
-
prompt: str,
|
|
138
|
-
stream: bool = False,
|
|
139
|
-
optimizer: str = None,
|
|
140
|
-
conversationally: bool = False,
|
|
141
|
-
) -> str | Generator[str, None, None]:
|
|
142
|
-
|
|
143
|
-
def for_stream():
|
|
144
|
-
for response in self.ask(
|
|
145
|
-
prompt, stream=True, optimizer=optimizer, conversationally=conversationally
|
|
146
|
-
):
|
|
147
|
-
yield self.get_message(response)
|
|
148
|
-
|
|
149
|
-
def for_non_stream():
|
|
150
|
-
response = next(self.ask(
|
|
151
|
-
prompt, stream=False, optimizer=optimizer, conversationally=conversationally
|
|
152
|
-
))
|
|
153
|
-
return self.get_message(response)
|
|
154
|
-
return for_stream() if stream else for_non_stream()
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
def get_message(self, response: dict) -> str:
|
|
159
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
160
|
-
return response["text"]
|
|
161
|
-
|
|
162
|
-
@staticmethod
|
|
163
|
-
def _parse_response(response_text: str) -> str:
|
|
164
|
-
"""Parse and validate API response."""
|
|
165
|
-
try:
|
|
166
|
-
data = json.loads(response_text)
|
|
167
|
-
return data["choices"][0]["message"]["content"].strip()
|
|
168
|
-
except (json.JSONDecodeError, KeyError, IndexError) as e:
|
|
169
|
-
raise exceptions.InvalidResponseError(f"Failed to parse response: {str(e)}")
|
|
170
|
-
|
|
171
|
-
if __name__ == "__main__":
|
|
172
|
-
from rich import print
|
|
173
|
-
ai = Mhystical()
|
|
174
|
-
response = ai.chat(input(">>> "))
|
|
175
|
-
for chunk in response:
|
|
176
|
-
print(chunk, end="", flush=True)
|
webstoken/t.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
from webstoken import (
|
|
2
|
-
process_text, NamedEntityRecognizer, TextClassifier,
|
|
3
|
-
TopicClassifier, LanguageDetector, SentimentAnalyzer,
|
|
4
|
-
KeywordExtractor
|
|
5
|
-
)
|
|
6
|
-
from rich import print
|
|
7
|
-
# Example text
|
|
8
|
-
text = """
|
|
9
|
-
Dr. John Smith from Microsoft Corporation visited New York City on January 15th, 2024.
|
|
10
|
-
He presented an excellent paper about artificial intelligence and machine learning at
|
|
11
|
-
the International Technology Conference. The research was incredibly well-received,
|
|
12
|
-
and many attendees were excited about its potential applications in healthcare.
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
print("1. Basic Text Processing")
|
|
16
|
-
print("-" * 50)
|
|
17
|
-
result = process_text(text)
|
|
18
|
-
for sentence_data in result['sentences']:
|
|
19
|
-
print("Original:", sentence_data['original'])
|
|
20
|
-
print("Tokens:", sentence_data['tokens'])
|
|
21
|
-
print("POS Tags:", sentence_data['pos_tags'])
|
|
22
|
-
print("Stems:", sentence_data['stems'])
|
|
23
|
-
print()
|
|
24
|
-
|
|
25
|
-
print("\n2. Named Entity Recognition")
|
|
26
|
-
print("-" * 50)
|
|
27
|
-
ner = NamedEntityRecognizer()
|
|
28
|
-
entities = ner.extract_entities(text)
|
|
29
|
-
for entity_type, entity_list in entities.items():
|
|
30
|
-
if entity_list:
|
|
31
|
-
print(f"{entity_type}:", entity_list)
|
|
32
|
-
|
|
33
|
-
print("\n3. Topic Classification")
|
|
34
|
-
print("-" * 50)
|
|
35
|
-
topic_classifier = TopicClassifier()
|
|
36
|
-
topics = topic_classifier.classify(text)
|
|
37
|
-
print("Topics (with confidence):")
|
|
38
|
-
for topic, confidence in topics[:3]: # Top 3 topics
|
|
39
|
-
print(f"{topic}: {confidence:.2f}")
|
|
40
|
-
|
|
41
|
-
print("\n4. Language Detection")
|
|
42
|
-
print("-" * 50)
|
|
43
|
-
lang_detector = LanguageDetector()
|
|
44
|
-
languages = lang_detector.detect(text)
|
|
45
|
-
print("Detected Languages (with confidence):")
|
|
46
|
-
for lang, confidence in languages:
|
|
47
|
-
print(f"{lang}: {confidence:.2f}")
|
|
48
|
-
|
|
49
|
-
print("\n5. Sentiment Analysis")
|
|
50
|
-
print("-" * 50)
|
|
51
|
-
sentiment_analyzer = SentimentAnalyzer()
|
|
52
|
-
sentiment = sentiment_analyzer.analyze_sentiment(text)
|
|
53
|
-
print("Sentiment Scores:")
|
|
54
|
-
print(f"Polarity: {sentiment['polarity']:.2f}")
|
|
55
|
-
print(f"Subjectivity: {sentiment['subjectivity']:.2f}")
|
|
56
|
-
print(f"Confidence: {sentiment['confidence']:.2f}")
|
|
57
|
-
|
|
58
|
-
print("\nEmotions:")
|
|
59
|
-
emotions = sentiment_analyzer.analyze_emotions(text)
|
|
60
|
-
for emotion, score in emotions:
|
|
61
|
-
if score > 0.1: # Only show significant emotions
|
|
62
|
-
print(f"{emotion}: {score:.2f}")
|
|
63
|
-
|
|
64
|
-
print("\n6. Keyword Extraction")
|
|
65
|
-
print("-" * 50)
|
|
66
|
-
keyword_extractor = KeywordExtractor()
|
|
67
|
-
print("Keywords:")
|
|
68
|
-
keywords = keyword_extractor.extract_keywords(text, num_keywords=5)
|
|
69
|
-
for keyword, score in keywords:
|
|
70
|
-
print(f"{keyword}: {score:.2f}")
|
|
71
|
-
|
|
72
|
-
print("\nKey Phrases:")
|
|
73
|
-
keyphrases = keyword_extractor.extract_keyphrases(text, num_phrases=3)
|
|
74
|
-
for phrase, score in keyphrases:
|
|
75
|
-
print(f"{phrase}: {score:.2f}")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|