webscout 8.2.4__py3-none-any.whl → 8.2.5__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/gguf.py +2 -0
- webscout/Provider/AISEARCH/scira_search.py +2 -5
- webscout/Provider/Aitopia.py +75 -51
- webscout/Provider/AllenAI.py +64 -67
- webscout/Provider/ChatGPTClone.py +33 -34
- webscout/Provider/ChatSandbox.py +342 -0
- webscout/Provider/Cloudflare.py +79 -32
- webscout/Provider/Deepinfra.py +69 -56
- webscout/Provider/ElectronHub.py +48 -39
- webscout/Provider/ExaChat.py +36 -20
- webscout/Provider/GPTWeb.py +24 -18
- webscout/Provider/GithubChat.py +52 -49
- webscout/Provider/GizAI.py +283 -0
- webscout/Provider/Glider.py +39 -28
- webscout/Provider/Groq.py +48 -20
- webscout/Provider/HeckAI.py +18 -36
- webscout/Provider/Jadve.py +30 -37
- webscout/Provider/LambdaChat.py +36 -59
- webscout/Provider/MCPCore.py +18 -21
- webscout/Provider/Marcus.py +23 -14
- webscout/Provider/Netwrck.py +35 -26
- webscout/Provider/OPENAI/__init__.py +1 -1
- webscout/Provider/OPENAI/exachat.py +4 -0
- webscout/Provider/OPENAI/scirachat.py +2 -4
- webscout/Provider/OPENAI/textpollinations.py +20 -22
- webscout/Provider/OPENAI/toolbaz.py +1 -0
- webscout/Provider/PI.py +22 -13
- webscout/Provider/StandardInput.py +42 -30
- webscout/Provider/TeachAnything.py +16 -7
- webscout/Provider/TextPollinationsAI.py +78 -76
- webscout/Provider/TwoAI.py +120 -88
- webscout/Provider/TypliAI.py +305 -0
- webscout/Provider/Venice.py +24 -22
- webscout/Provider/VercelAI.py +31 -12
- webscout/Provider/__init__.py +7 -7
- webscout/Provider/asksteve.py +53 -44
- webscout/Provider/cerebras.py +77 -31
- webscout/Provider/chatglm.py +47 -37
- webscout/Provider/elmo.py +38 -32
- webscout/Provider/granite.py +24 -21
- webscout/Provider/hermes.py +27 -20
- webscout/Provider/learnfastai.py +25 -20
- webscout/Provider/llmchatco.py +48 -78
- webscout/Provider/multichat.py +13 -3
- webscout/Provider/scira_chat.py +49 -30
- webscout/Provider/scnet.py +23 -20
- webscout/Provider/searchchat.py +16 -24
- webscout/Provider/sonus.py +37 -39
- webscout/Provider/toolbaz.py +24 -46
- webscout/Provider/turboseek.py +37 -41
- webscout/Provider/typefully.py +30 -22
- webscout/Provider/typegpt.py +47 -51
- webscout/Provider/uncovr.py +46 -40
- webscout/cli.py +256 -0
- webscout/conversation.py +0 -2
- webscout/exceptions.py +3 -0
- webscout/version.py +1 -1
- {webscout-8.2.4.dist-info → webscout-8.2.5.dist-info}/METADATA +166 -45
- {webscout-8.2.4.dist-info → webscout-8.2.5.dist-info}/RECORD +63 -76
- {webscout-8.2.4.dist-info → webscout-8.2.5.dist-info}/WHEEL +1 -1
- webscout-8.2.5.dist-info/entry_points.txt +3 -0
- {webscout-8.2.4.dist-info → webscout-8.2.5.dist-info}/top_level.txt +0 -1
- inferno/__init__.py +0 -6
- inferno/__main__.py +0 -9
- inferno/cli.py +0 -6
- inferno/lol.py +0 -589
- webscout/Local/__init__.py +0 -12
- webscout/Local/__main__.py +0 -9
- webscout/Local/api.py +0 -576
- webscout/Local/cli.py +0 -516
- webscout/Local/config.py +0 -75
- webscout/Local/llm.py +0 -287
- webscout/Local/model_manager.py +0 -253
- webscout/Local/server.py +0 -721
- webscout/Local/utils.py +0 -93
- webscout/Provider/Chatify.py +0 -175
- webscout/Provider/askmyai.py +0 -158
- webscout/Provider/gaurish.py +0 -244
- webscout-8.2.4.dist-info/entry_points.txt +0 -5
- {webscout-8.2.4.dist-info → webscout-8.2.5.dist-info}/licenses/LICENSE.md +0 -0
webscout/Local/utils.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Utility functions for webscout.local
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
import base64
|
|
6
|
-
import logging
|
|
7
|
-
|
|
8
|
-
logger = logging.getLogger(__name__)
|
|
9
|
-
|
|
10
|
-
def parse_duration(duration_str: str) -> float:
|
|
11
|
-
"""
|
|
12
|
-
Parse a duration string into seconds.
|
|
13
|
-
|
|
14
|
-
Args:
|
|
15
|
-
duration_str (str): Duration string (e.g., '5m', '1h', '30s', '500ms', '0').
|
|
16
|
-
Returns:
|
|
17
|
-
float: Duration in seconds.
|
|
18
|
-
"""
|
|
19
|
-
if not duration_str:
|
|
20
|
-
return 300.0 # Default 5 minutes
|
|
21
|
-
if duration_str.endswith("ms"):
|
|
22
|
-
return int(duration_str[:-2]) / 1000.0
|
|
23
|
-
elif duration_str.endswith("s"):
|
|
24
|
-
return int(duration_str[:-1])
|
|
25
|
-
elif duration_str.endswith("m"):
|
|
26
|
-
return int(duration_str[:-1]) * 60
|
|
27
|
-
elif duration_str.endswith("h"):
|
|
28
|
-
return int(duration_str[:-1]) * 3600
|
|
29
|
-
elif duration_str == "0":
|
|
30
|
-
return 0.0
|
|
31
|
-
else:
|
|
32
|
-
try:
|
|
33
|
-
return float(duration_str)
|
|
34
|
-
except ValueError:
|
|
35
|
-
return 300.0 # Default 5 minutes
|
|
36
|
-
|
|
37
|
-
def format_duration(seconds: float) -> str:
|
|
38
|
-
"""
|
|
39
|
-
Format seconds into a human-readable duration string.
|
|
40
|
-
Args:
|
|
41
|
-
seconds (float): Duration in seconds.
|
|
42
|
-
Returns:
|
|
43
|
-
str: Human-readable duration string.
|
|
44
|
-
"""
|
|
45
|
-
if seconds < 1:
|
|
46
|
-
return f"{int(seconds * 1000)}ms"
|
|
47
|
-
elif seconds < 60:
|
|
48
|
-
return f"{int(seconds)}s"
|
|
49
|
-
elif seconds < 3600:
|
|
50
|
-
return f"{int(seconds / 60)}m"
|
|
51
|
-
else:
|
|
52
|
-
return f"{int(seconds / 3600)}h"
|
|
53
|
-
|
|
54
|
-
def decode_image(image_str: str) -> bytes:
|
|
55
|
-
"""
|
|
56
|
-
Decode a base64 image string to bytes.
|
|
57
|
-
Args:
|
|
58
|
-
image_str (str): Base64-encoded image string (optionally with data URI prefix).
|
|
59
|
-
Returns:
|
|
60
|
-
bytes: Decoded image bytes.
|
|
61
|
-
"""
|
|
62
|
-
if image_str.startswith("data:"):
|
|
63
|
-
image_str = image_str.split(",", 1)[1]
|
|
64
|
-
return base64.b64decode(image_str)
|
|
65
|
-
|
|
66
|
-
def encode_image(image_bytes: bytes, mime_type: str = "image/png") -> str:
|
|
67
|
-
"""
|
|
68
|
-
Encode image bytes to a base64 data URI.
|
|
69
|
-
Args:
|
|
70
|
-
image_bytes (bytes): Image data.
|
|
71
|
-
mime_type (str): MIME type for the image.
|
|
72
|
-
Returns:
|
|
73
|
-
str: Base64-encoded data URI string.
|
|
74
|
-
"""
|
|
75
|
-
encoded = base64.b64encode(image_bytes).decode("utf-8")
|
|
76
|
-
return f"data:{mime_type};base64,{encoded}"
|
|
77
|
-
|
|
78
|
-
def get_file_size_str(size_bytes: int) -> str:
|
|
79
|
-
"""
|
|
80
|
-
Convert file size in bytes to a human-readable string.
|
|
81
|
-
Args:
|
|
82
|
-
size_bytes (int): File size in bytes.
|
|
83
|
-
Returns:
|
|
84
|
-
str: Human-readable file size string.
|
|
85
|
-
"""
|
|
86
|
-
if size_bytes < 1024:
|
|
87
|
-
return f"{size_bytes} B"
|
|
88
|
-
elif size_bytes < 1024 * 1024:
|
|
89
|
-
return f"{size_bytes / 1024:.2f} KB"
|
|
90
|
-
elif size_bytes < 1024 * 1024 * 1024:
|
|
91
|
-
return f"{size_bytes / (1024 * 1024):.2f} MB"
|
|
92
|
-
else:
|
|
93
|
-
return f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
|
webscout/Provider/Chatify.py
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
from webscout.AIutel import Optimizers
|
|
2
|
-
from webscout.AIutel import Conversation
|
|
3
|
-
from webscout.AIutel import AwesomePrompts
|
|
4
|
-
from webscout.AIbase import Provider
|
|
5
|
-
from webscout import exceptions
|
|
6
|
-
import requests
|
|
7
|
-
from webscout.litagent import LitAgent
|
|
8
|
-
class Chatify(Provider):
|
|
9
|
-
"""
|
|
10
|
-
A class to interact with the Chatify AI 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 and informative AI assistant.",
|
|
25
|
-
):
|
|
26
|
-
"""
|
|
27
|
-
Initializes the Chatify AI API with given parameters.
|
|
28
|
-
"""
|
|
29
|
-
self.session = requests.Session()
|
|
30
|
-
self.is_conversation = is_conversation
|
|
31
|
-
self.max_tokens_to_sample = max_tokens
|
|
32
|
-
self.api_endpoint = "https://chatify-ai.vercel.app/api/chat"
|
|
33
|
-
self.timeout = timeout
|
|
34
|
-
self.last_response = {}
|
|
35
|
-
self.headers = {
|
|
36
|
-
'Accept': '*/*',
|
|
37
|
-
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
|
38
|
-
'Accept-Language': 'en-US,en;q=0.9,en-IN;q=0.8',
|
|
39
|
-
'Content-Type': 'application/json',
|
|
40
|
-
'DNT': '1',
|
|
41
|
-
'Origin': 'https://chatify-ai.vercel.app',
|
|
42
|
-
'Referer': 'https://chatify-ai.vercel.app/',
|
|
43
|
-
'Sec-CH-UA': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
|
|
44
|
-
'Sec-CH-UA-Mobile': '?0',
|
|
45
|
-
'Sec-CH-UA-Platform': '"Windows"',
|
|
46
|
-
'Sec-Fetch-Dest': 'empty',
|
|
47
|
-
'Sec-Fetch-Mode': 'cors',
|
|
48
|
-
'Sec-Fetch-Site': 'same-origin',
|
|
49
|
-
'User-Agent': LitAgent().random(),
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
self.__available_optimizers = (
|
|
53
|
-
method
|
|
54
|
-
for method in dir(Optimizers)
|
|
55
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
56
|
-
)
|
|
57
|
-
self.session.headers.update(self.headers)
|
|
58
|
-
Conversation.intro = (
|
|
59
|
-
AwesomePrompts().get_act(
|
|
60
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
61
|
-
)
|
|
62
|
-
if act
|
|
63
|
-
else intro or Conversation.intro
|
|
64
|
-
)
|
|
65
|
-
self.conversation = Conversation(
|
|
66
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
67
|
-
)
|
|
68
|
-
self.conversation.history_offset = history_offset
|
|
69
|
-
self.session.proxies = proxies
|
|
70
|
-
self.system_prompt = system_prompt
|
|
71
|
-
|
|
72
|
-
def ask(
|
|
73
|
-
self,
|
|
74
|
-
prompt: str,
|
|
75
|
-
stream: bool = False,
|
|
76
|
-
raw: bool = False,
|
|
77
|
-
optimizer: str = None,
|
|
78
|
-
conversationally: bool = False,
|
|
79
|
-
) -> dict:
|
|
80
|
-
"""
|
|
81
|
-
Sends a prompt to the Chatify API and returns the response.
|
|
82
|
-
"""
|
|
83
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
84
|
-
if optimizer:
|
|
85
|
-
if optimizer in self.__available_optimizers:
|
|
86
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
87
|
-
conversation_prompt if conversationally else prompt
|
|
88
|
-
)
|
|
89
|
-
else:
|
|
90
|
-
raise Exception(
|
|
91
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
messages = []
|
|
95
|
-
if self.system_prompt:
|
|
96
|
-
messages.append({"role": "system", "content": self.system_prompt})
|
|
97
|
-
messages.append({"role": "user", "content": conversation_prompt})
|
|
98
|
-
|
|
99
|
-
payload = {
|
|
100
|
-
"messages": messages
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
def for_stream():
|
|
104
|
-
response = self.session.post(self.api_endpoint, headers=self.headers, json=payload, stream=True, timeout=self.timeout)
|
|
105
|
-
if not response.ok:
|
|
106
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
107
|
-
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
streaming_text = ""
|
|
111
|
-
for line in response.iter_lines():
|
|
112
|
-
if line:
|
|
113
|
-
decoded_line = line.decode('utf-8') # Decode the line
|
|
114
|
-
parts = decoded_line.split(':', 1)
|
|
115
|
-
if len(parts) > 1:
|
|
116
|
-
content = parts[1].strip().strip('"')
|
|
117
|
-
streaming_text += content
|
|
118
|
-
yield content if raw else dict(text=content)
|
|
119
|
-
self.last_response.update(dict(text=streaming_text))
|
|
120
|
-
self.conversation.update_chat_history(
|
|
121
|
-
prompt, self.get_message(self.last_response)
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
def for_non_stream():
|
|
125
|
-
for _ in for_stream():
|
|
126
|
-
pass
|
|
127
|
-
return self.last_response
|
|
128
|
-
|
|
129
|
-
return for_stream() if stream else for_non_stream()
|
|
130
|
-
|
|
131
|
-
def chat(
|
|
132
|
-
self,
|
|
133
|
-
prompt: str,
|
|
134
|
-
stream: bool = False,
|
|
135
|
-
optimizer: str = None,
|
|
136
|
-
conversationally: bool = False,
|
|
137
|
-
) -> str:
|
|
138
|
-
"""
|
|
139
|
-
Generates a response from the Chatify API.
|
|
140
|
-
"""
|
|
141
|
-
|
|
142
|
-
def for_stream():
|
|
143
|
-
for response in self.ask(
|
|
144
|
-
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
145
|
-
):
|
|
146
|
-
yield self.get_message(response)
|
|
147
|
-
|
|
148
|
-
def for_non_stream():
|
|
149
|
-
return self.get_message(
|
|
150
|
-
self.ask(
|
|
151
|
-
prompt,
|
|
152
|
-
False,
|
|
153
|
-
optimizer=optimizer,
|
|
154
|
-
conversationally=conversationally,
|
|
155
|
-
)
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
return for_stream() if stream else for_non_stream()
|
|
159
|
-
|
|
160
|
-
def get_message(self, response: dict) -> str:
|
|
161
|
-
"""
|
|
162
|
-
Extracts the message from the API response and formats it.
|
|
163
|
-
"""
|
|
164
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
165
|
-
return response["text"].replace('\\n', '\n').replace('\\n\\n', '\n\n')
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
# Example usage
|
|
169
|
-
if __name__ == "__main__":
|
|
170
|
-
from rich import print
|
|
171
|
-
|
|
172
|
-
ai = Chatify(timeout=5000)
|
|
173
|
-
response = ai.chat("write a poem about AI", stream=True)
|
|
174
|
-
for chunk in response:
|
|
175
|
-
print(chunk, end="", flush=True)
|
webscout/Provider/askmyai.py
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
import re
|
|
4
|
-
from typing import Union, Any, Dict, Optional, Generator
|
|
5
|
-
|
|
6
|
-
from webscout.AIutel import Optimizers
|
|
7
|
-
from webscout.AIutel import Conversation
|
|
8
|
-
from webscout.AIutel import AwesomePrompts
|
|
9
|
-
from webscout.AIbase import Provider
|
|
10
|
-
from webscout import exceptions
|
|
11
|
-
from webscout.litagent import LitAgent as UserAgent
|
|
12
|
-
|
|
13
|
-
class AskMyAI(Provider):
|
|
14
|
-
"""
|
|
15
|
-
A class to interact with the askmyai.chat API. Improved to match webscout standards.
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
def __init__(
|
|
19
|
-
self,
|
|
20
|
-
is_conversation: bool = True,
|
|
21
|
-
max_tokens: int = 2048, # Added max_tokens parameter
|
|
22
|
-
timeout: int = 30,
|
|
23
|
-
intro: str = None,
|
|
24
|
-
filepath: str = None,
|
|
25
|
-
update_file: bool = True,
|
|
26
|
-
proxies: dict = {},
|
|
27
|
-
history_offset: int = 10250,
|
|
28
|
-
act: str = None,
|
|
29
|
-
system_prompt: str = "You are a helpful assistant.", # Added system prompt
|
|
30
|
-
):
|
|
31
|
-
"""Initializes the AskMyAI API."""
|
|
32
|
-
self.session = requests.Session()
|
|
33
|
-
self.is_conversation = is_conversation
|
|
34
|
-
self.max_tokens_to_sample = max_tokens
|
|
35
|
-
self.api_endpoint = "https://www.askmyai.chat/api/chat"
|
|
36
|
-
self.timeout = timeout
|
|
37
|
-
self.last_response = {}
|
|
38
|
-
self.system_prompt = system_prompt # Use system prompt
|
|
39
|
-
self.headers = {
|
|
40
|
-
"Content-Type": "application/json",
|
|
41
|
-
"Accept": "*/*",
|
|
42
|
-
"Accept-Encoding": "gzip, deflate, br",
|
|
43
|
-
"Accept-Language": "en-US,en;q=0.9",
|
|
44
|
-
'user-agent': UserAgent().random()
|
|
45
|
-
}
|
|
46
|
-
self.__available_optimizers = (
|
|
47
|
-
method
|
|
48
|
-
for method in dir(Optimizers)
|
|
49
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
50
|
-
)
|
|
51
|
-
Conversation.intro = (
|
|
52
|
-
AwesomePrompts().get_act(
|
|
53
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
54
|
-
)
|
|
55
|
-
if act
|
|
56
|
-
else intro or Conversation.intro
|
|
57
|
-
)
|
|
58
|
-
self.conversation = Conversation(
|
|
59
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
60
|
-
)
|
|
61
|
-
self.conversation.history_offset = history_offset
|
|
62
|
-
self.session.proxies = proxies
|
|
63
|
-
|
|
64
|
-
def ask(
|
|
65
|
-
self,
|
|
66
|
-
prompt: str,
|
|
67
|
-
stream: bool = False,
|
|
68
|
-
raw: bool = False,
|
|
69
|
-
optimizer: str = None,
|
|
70
|
-
conversationally: bool = False,
|
|
71
|
-
) -> Union[Dict[str, Any], Generator[Any, None, None]]:
|
|
72
|
-
"""Sends a prompt to the askmyai.chat API and returns the response."""
|
|
73
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
74
|
-
if optimizer:
|
|
75
|
-
if optimizer in self.__available_optimizers:
|
|
76
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
77
|
-
conversation_prompt if conversationally else prompt
|
|
78
|
-
)
|
|
79
|
-
else:
|
|
80
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
81
|
-
f"Optimizer is not one of {self.__available_optimizers}"
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
payload = {
|
|
85
|
-
"messages": [
|
|
86
|
-
{"role": "system", "content": self.system_prompt},
|
|
87
|
-
{"role": "user", "content": conversation_prompt}
|
|
88
|
-
],
|
|
89
|
-
"data": {"datasource": "thucpn"}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
def for_stream():
|
|
93
|
-
response = self.session.post(
|
|
94
|
-
self.api_endpoint, json=payload, stream=True, timeout=self.timeout
|
|
95
|
-
)
|
|
96
|
-
if not response.ok:
|
|
97
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
98
|
-
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
streaming_response = ""
|
|
102
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
103
|
-
if line:
|
|
104
|
-
match = re.search(r'0:"(.*?)"', line)
|
|
105
|
-
if match:
|
|
106
|
-
content = match.group(1)
|
|
107
|
-
streaming_response += content
|
|
108
|
-
yield content if raw else {"text": content}
|
|
109
|
-
self.last_response.update({"text": streaming_response})
|
|
110
|
-
self.conversation.update_chat_history(
|
|
111
|
-
prompt, self.get_message(self.last_response)
|
|
112
|
-
)
|
|
113
|
-
|
|
114
|
-
def for_non_stream():
|
|
115
|
-
full_response = ""
|
|
116
|
-
for chunk in for_stream():
|
|
117
|
-
full_response += chunk if raw else chunk['text']
|
|
118
|
-
return {"text": full_response}
|
|
119
|
-
|
|
120
|
-
return for_stream() if stream else for_non_stream()
|
|
121
|
-
|
|
122
|
-
def chat(
|
|
123
|
-
self,
|
|
124
|
-
prompt: str,
|
|
125
|
-
stream: bool = False,
|
|
126
|
-
optimizer: str = None,
|
|
127
|
-
conversationally: bool = False,
|
|
128
|
-
) -> Union[str, Generator[str, None, None]]:
|
|
129
|
-
"""Generates a response from the AskMyAI API."""
|
|
130
|
-
|
|
131
|
-
def for_stream():
|
|
132
|
-
for response in self.ask(
|
|
133
|
-
prompt, stream=True, optimizer=optimizer, conversationally=conversationally
|
|
134
|
-
):
|
|
135
|
-
yield self.get_message(response)
|
|
136
|
-
|
|
137
|
-
def for_non_stream():
|
|
138
|
-
return self.get_message(
|
|
139
|
-
self.ask(
|
|
140
|
-
prompt, stream=False, optimizer=optimizer, conversationally=conversationally
|
|
141
|
-
)
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
return for_stream() if stream else for_non_stream()
|
|
145
|
-
|
|
146
|
-
def get_message(self, response: Dict[str, Any]) -> str:
|
|
147
|
-
"""Extracts the message from the API response."""
|
|
148
|
-
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
149
|
-
return response["text"].replace('\\n', '\n').replace('\\n\\n', '\n\n')
|
|
150
|
-
|
|
151
|
-
if __name__ == "__main__":
|
|
152
|
-
from rich import print
|
|
153
|
-
|
|
154
|
-
ai = AskMyAI(timeout=30)
|
|
155
|
-
response = ai.chat("write a poem about AI", stream=True)
|
|
156
|
-
|
|
157
|
-
for chunk in response:
|
|
158
|
-
print(chunk, end="", flush=True)
|
webscout/Provider/gaurish.py
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import requests
|
|
2
|
-
import json
|
|
3
|
-
from typing import Dict, Generator, Union
|
|
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
|
-
from webscout.Litlogger import Logger, LogFormat
|
|
11
|
-
|
|
12
|
-
class GaurishCerebras(Provider):
|
|
13
|
-
"""
|
|
14
|
-
A class to interact with the Gaurish Cerebras API with comprehensive logging.
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
def __init__(
|
|
18
|
-
self,
|
|
19
|
-
is_conversation: bool = True,
|
|
20
|
-
max_tokens: int = 2049,
|
|
21
|
-
timeout: int = 30,
|
|
22
|
-
intro: str = None,
|
|
23
|
-
filepath: str = None,
|
|
24
|
-
update_file: bool = True,
|
|
25
|
-
proxies: dict = {},
|
|
26
|
-
history_offset: int = 10250,
|
|
27
|
-
act: str = None,
|
|
28
|
-
system_prompt: str = "You are a helpful assistant.",
|
|
29
|
-
logging: bool = False
|
|
30
|
-
):
|
|
31
|
-
"""Initializes the Gaurish Cerebras API client with logging capabilities."""
|
|
32
|
-
self.logger = Logger(
|
|
33
|
-
name="GaurishCerebras",
|
|
34
|
-
format=LogFormat.MODERN_EMOJI,
|
|
35
|
-
) if logging else None
|
|
36
|
-
|
|
37
|
-
if self.logger:
|
|
38
|
-
self.logger.info("Initializing GaurishCerebras client")
|
|
39
|
-
|
|
40
|
-
self.chat_endpoint = "https://proxy.gaurish.xyz/api/cerebras/v1/chat/completions"
|
|
41
|
-
|
|
42
|
-
self.headers = {
|
|
43
|
-
"Content-Type": "application/json",
|
|
44
|
-
"Accept": "application/json",
|
|
45
|
-
"authorization": "Bearer 123",
|
|
46
|
-
"accept-encoding": "gzip, deflate, br, zstd",
|
|
47
|
-
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
48
|
-
"dnt": "1",
|
|
49
|
-
"origin": "https://chat.gaurish.xyz",
|
|
50
|
-
"priority": "u=1, i",
|
|
51
|
-
"referer": "https://chat.gaurish.xyz/",
|
|
52
|
-
"sec-ch-ua": '"Not A(Brand";v="8", "Chromium";v="132", "Microsoft Edge";v="132"',
|
|
53
|
-
"sec-ch-ua-mobile": "?0",
|
|
54
|
-
"sec-ch-ua-platform": '"Windows"',
|
|
55
|
-
"sec-fetch-dest": "empty",
|
|
56
|
-
"sec-fetch-mode": "cors",
|
|
57
|
-
"sec-fetch-site": "same-site",
|
|
58
|
-
"user-agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
|
59
|
-
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
60
|
-
"Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0"),
|
|
61
|
-
}
|
|
62
|
-
self.session = requests.Session()
|
|
63
|
-
self.session.headers.update(self.headers)
|
|
64
|
-
self.session.proxies.update(proxies)
|
|
65
|
-
self.timeout = timeout
|
|
66
|
-
self.last_response = {}
|
|
67
|
-
|
|
68
|
-
self.is_conversation = is_conversation
|
|
69
|
-
self.max_tokens_to_sample = max_tokens
|
|
70
|
-
|
|
71
|
-
if self.logger:
|
|
72
|
-
self.logger.debug(f"Session configured with timeout: {timeout}")
|
|
73
|
-
self.logger.debug(f"Max tokens set to: {max_tokens}")
|
|
74
|
-
|
|
75
|
-
self.__available_optimizers = (
|
|
76
|
-
method
|
|
77
|
-
for method in dir(Optimizers)
|
|
78
|
-
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
79
|
-
)
|
|
80
|
-
Conversation.intro = (
|
|
81
|
-
AwesomePrompts().get_act(
|
|
82
|
-
act, raise_not_found=True, default=None, case_insensitive=True
|
|
83
|
-
)
|
|
84
|
-
if act
|
|
85
|
-
else intro or system_prompt or Conversation.intro
|
|
86
|
-
)
|
|
87
|
-
self.conversation = Conversation(
|
|
88
|
-
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
89
|
-
)
|
|
90
|
-
self.conversation.history_offset = history_offset
|
|
91
|
-
self.system_prompt = system_prompt
|
|
92
|
-
|
|
93
|
-
if self.logger:
|
|
94
|
-
self.logger.info("GaurishCerebras initialization completed successfully")
|
|
95
|
-
|
|
96
|
-
def ask(
|
|
97
|
-
self,
|
|
98
|
-
prompt: str,
|
|
99
|
-
stream: bool = False,
|
|
100
|
-
raw: bool = False,
|
|
101
|
-
optimizer: str = None,
|
|
102
|
-
conversationally: bool = False,
|
|
103
|
-
) -> Union[Dict, Generator]:
|
|
104
|
-
"""
|
|
105
|
-
Sends a prompt to the API and returns the response with logging.
|
|
106
|
-
If stream is True, returns a generator for streamed responses.
|
|
107
|
-
"""
|
|
108
|
-
if self.logger:
|
|
109
|
-
self.logger.debug(f"Processing request - Prompt: {prompt[:50]}...")
|
|
110
|
-
self.logger.debug(f"Stream: {stream}, Optimizer: {optimizer}")
|
|
111
|
-
|
|
112
|
-
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
113
|
-
if optimizer:
|
|
114
|
-
if optimizer in self.__available_optimizers:
|
|
115
|
-
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
116
|
-
conversation_prompt if conversationally else prompt
|
|
117
|
-
)
|
|
118
|
-
if self.logger:
|
|
119
|
-
self.logger.debug(f"Applied optimizer: {optimizer}")
|
|
120
|
-
else:
|
|
121
|
-
if self.logger:
|
|
122
|
-
self.logger.error(f"Invalid optimizer requested: {optimizer}")
|
|
123
|
-
raise Exception(f"Optimizer is not one of {list(self.__available_optimizers)}")
|
|
124
|
-
|
|
125
|
-
payload = {
|
|
126
|
-
"messages": [
|
|
127
|
-
{"role": "system", "content": self.system_prompt},
|
|
128
|
-
{"role": "user", "content": conversation_prompt},
|
|
129
|
-
],
|
|
130
|
-
"model": "llama3.3-70b",
|
|
131
|
-
"max_tokens": self.max_tokens_to_sample,
|
|
132
|
-
"temperature": 0.75,
|
|
133
|
-
"stream": stream,
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
def for_stream():
|
|
137
|
-
try:
|
|
138
|
-
if self.logger:
|
|
139
|
-
self.logger.debug("Initiating streaming request to API")
|
|
140
|
-
|
|
141
|
-
with self.session.post(self.chat_endpoint, json=payload, stream=True, timeout=self.timeout) as response:
|
|
142
|
-
if response.status_code != 200:
|
|
143
|
-
if self.logger:
|
|
144
|
-
self.logger.error(f"API request failed. Status: {response.status_code}")
|
|
145
|
-
raise exceptions.FailedToGenerateResponseError(
|
|
146
|
-
f"Request failed with status code {response.status_code}"
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
if self.logger:
|
|
150
|
-
self.logger.info(f"API connection established successfully. Status: {response.status_code}")
|
|
151
|
-
|
|
152
|
-
streaming_text = ""
|
|
153
|
-
for line in response.iter_lines(decode_unicode=True):
|
|
154
|
-
if line:
|
|
155
|
-
line = line.strip()
|
|
156
|
-
if line.startswith("data: "):
|
|
157
|
-
json_str = line[6:]
|
|
158
|
-
if json_str == "[DONE]":
|
|
159
|
-
if self.logger:
|
|
160
|
-
self.logger.debug("Stream completed")
|
|
161
|
-
break
|
|
162
|
-
try:
|
|
163
|
-
json_data = json.loads(json_str)
|
|
164
|
-
if 'choices' in json_data:
|
|
165
|
-
choice = json_data['choices'][0]
|
|
166
|
-
if 'delta' in choice and 'content' in choice['delta']:
|
|
167
|
-
content = choice['delta']['content']
|
|
168
|
-
streaming_text += content
|
|
169
|
-
yield dict(text=content) if raw else dict(text=content)
|
|
170
|
-
except json.JSONDecodeError as e:
|
|
171
|
-
if self.logger:
|
|
172
|
-
self.logger.error(f"JSON parsing error: {str(e)}")
|
|
173
|
-
pass
|
|
174
|
-
|
|
175
|
-
self.conversation.update_chat_history(prompt, streaming_text)
|
|
176
|
-
if self.logger:
|
|
177
|
-
self.logger.debug("Response processing completed")
|
|
178
|
-
|
|
179
|
-
except requests.RequestException as e:
|
|
180
|
-
if self.logger:
|
|
181
|
-
self.logger.error(f"Request failed: {str(e)}")
|
|
182
|
-
raise exceptions.FailedToGenerateResponseError(f"Request failed: {e}")
|
|
183
|
-
|
|
184
|
-
def for_non_stream():
|
|
185
|
-
if self.logger:
|
|
186
|
-
self.logger.debug("Processing non-streaming request")
|
|
187
|
-
for _ in for_stream():
|
|
188
|
-
pass
|
|
189
|
-
return self.last_response
|
|
190
|
-
|
|
191
|
-
return for_stream() if stream else for_non_stream()
|
|
192
|
-
|
|
193
|
-
def chat(
|
|
194
|
-
self,
|
|
195
|
-
prompt: str,
|
|
196
|
-
stream: bool = False,
|
|
197
|
-
optimizer: str = None,
|
|
198
|
-
conversationally: bool = False,
|
|
199
|
-
) -> Union[str, Generator]:
|
|
200
|
-
"""
|
|
201
|
-
A convenience method to return just the text message from the response with logging.
|
|
202
|
-
"""
|
|
203
|
-
if self.logger:
|
|
204
|
-
self.logger.debug(f"Chat request initiated - Prompt: {prompt[:50]}...")
|
|
205
|
-
|
|
206
|
-
def for_stream():
|
|
207
|
-
for response in self.ask(
|
|
208
|
-
prompt, stream=True, optimizer=optimizer, conversationally=conversationally
|
|
209
|
-
):
|
|
210
|
-
yield response if isinstance(response, str) else self.get_message(response)
|
|
211
|
-
|
|
212
|
-
def for_non_stream():
|
|
213
|
-
resp = self.ask(
|
|
214
|
-
prompt, stream=False, optimizer=optimizer, conversationally=conversationally
|
|
215
|
-
)
|
|
216
|
-
return resp if isinstance(resp, str) else self.get_message(resp)
|
|
217
|
-
|
|
218
|
-
return for_stream() if stream else for_non_stream()
|
|
219
|
-
|
|
220
|
-
def get_message(self, response: dict) -> str:
|
|
221
|
-
"""
|
|
222
|
-
Retrieve the message text from the API response with logging.
|
|
223
|
-
"""
|
|
224
|
-
if not isinstance(response, dict):
|
|
225
|
-
if self.logger:
|
|
226
|
-
self.logger.warning("Invalid response format received")
|
|
227
|
-
return ""
|
|
228
|
-
|
|
229
|
-
if "text" in response and response["text"]:
|
|
230
|
-
return response["text"]
|
|
231
|
-
|
|
232
|
-
if self.logger:
|
|
233
|
-
self.logger.warning("No valid message content found in response")
|
|
234
|
-
return ""
|
|
235
|
-
|
|
236
|
-
if __name__ == "__main__":
|
|
237
|
-
from rich import print
|
|
238
|
-
bot = GaurishCerebras(logging=True)
|
|
239
|
-
try:
|
|
240
|
-
response = bot.chat("what is meaning of life", stream=True)
|
|
241
|
-
for chunk in response:
|
|
242
|
-
print(chunk, end="", flush=True)
|
|
243
|
-
except Exception as e:
|
|
244
|
-
print(f"An error occurred: {e}")
|
|
File without changes
|