webscout 7.3__py3-none-any.whl → 7.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webscout might be problematic. Click here for more details.

@@ -1,221 +1,251 @@
1
- import requests
2
- import json
3
- import time
4
- from typing import Any, Dict, Optional, Generator, 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
- from webscout import LitAgent
12
- from webscout.Litlogger import Logger, LogFormat
13
-
14
- class FreeAIChat(Provider):
15
- """
16
- A class to interact with the FreeAIChat API with logging and LitAgent user-agent.
17
- """
18
-
19
- AVAILABLE_MODELS = [
20
- "mistral-nemo",
21
- "mistral-large",
22
- "llama3.1-70b-fast",
23
- "gemini-2.0-flash",
24
- "gemini-1.5-pro",
25
- "gemini-1.5-flash",
26
- "gemini-2.0-pro-exp-02-05",
27
- "deepseek-r1",
28
- "deepseek-v3",
29
- "Deepseek r1 14B",
30
- "Deepseek r1 32B",
31
- "o3-mini-high",
32
- "o3-mini-medium",
33
- "o3-mini-low",
34
- "o3-mini",
35
- "GPT-4o-mini",
36
- "o1",
37
- "o1-mini",
38
- "GPT-4o"
39
- ]
40
-
41
- def __init__(
42
- self,
43
- is_conversation: bool = True,
44
- max_tokens: int = 2049,
45
- timeout: int = 30,
46
- intro: str = None,
47
- filepath: str = None,
48
- update_file: bool = True,
49
- proxies: dict = {},
50
- history_offset: int = 10250,
51
- act: str = None,
52
- model: str = "GPT-4o",
53
- system_prompt: str = "You are a helpful AI assistant.",
54
- logging: bool = False
55
- ):
56
- """Initializes the FreeAIChat API client with logging support."""
57
- if model not in self.AVAILABLE_MODELS:
58
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
59
-
60
- self.url = "https://freeaichatplayground.com/api/v1/chat/completions"
61
- self.headers = {
62
- 'User-Agent': LitAgent().random(),
63
- 'Accept': '*/*',
64
- 'Content-Type': 'application/json',
65
- 'Origin': 'https://freeaichatplayground.com',
66
- 'Referer': 'https://freeaichatplayground.com/',
67
- 'Sec-Fetch-Mode': 'cors',
68
- 'Sec-Fetch-Site': 'same-origin'
69
- }
70
- self.session = requests.Session()
71
- self.session.headers.update(self.headers)
72
- self.session.proxies.update(proxies)
73
-
74
- self.is_conversation = is_conversation
75
- self.max_tokens_to_sample = max_tokens
76
- self.timeout = timeout
77
- self.last_response = {}
78
- self.model = model
79
- self.system_prompt = system_prompt
80
-
81
- self.__available_optimizers = (
82
- method
83
- for method in dir(Optimizers)
84
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
85
- )
86
- Conversation.intro = (
87
- AwesomePrompts().get_act(
88
- act, raise_not_found=True, default=None, case_insensitive=True
89
- )
90
- if act
91
- else intro or Conversation.intro
92
- )
93
-
94
- self.conversation = Conversation(
95
- is_conversation, self.max_tokens_to_sample, filepath, update_file
96
- )
97
- self.conversation.history_offset = history_offset
98
-
99
- self.logger = Logger(
100
- name="FreeAIChat",
101
- format=LogFormat.MODERN_EMOJI,
102
- ) if logging else None
103
-
104
- if self.logger:
105
- self.logger.info(f"FreeAIChat initialized successfully with model: {model}")
106
-
107
- def ask(
108
- self,
109
- prompt: str,
110
- stream: bool = False,
111
- raw: bool = False,
112
- optimizer: str = None,
113
- conversationally: bool = False,
114
- ) -> Union[Dict[str, Any], Generator]:
115
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
116
- if optimizer:
117
- if optimizer in self.__available_optimizers:
118
- conversation_prompt = getattr(Optimizers, optimizer)(
119
- conversation_prompt if conversationally else prompt
120
- )
121
- if self.logger:
122
- self.logger.debug(f"Applied optimizer: {optimizer}")
123
- else:
124
- if self.logger:
125
- self.logger.error(f"Invalid optimizer requested: {optimizer}")
126
- raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
127
-
128
- messages = [
129
- {
130
- "role": "system",
131
- "content": self.system_prompt
132
- },
133
- {
134
- "role": "user",
135
- "content": conversation_prompt
136
- }
137
- ]
138
-
139
- payload = {
140
- "model": self.model,
141
- "messages": messages
142
- }
143
-
144
- def for_stream():
145
- if self.logger:
146
- self.logger.debug("Sending streaming request to FreeAIChat API...")
147
- try:
148
- with requests.post(self.url, headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
149
- if response.status_code != 200:
150
- if self.logger:
151
- self.logger.error(f"Request failed with status code {response.status_code}")
152
- raise exceptions.FailedToGenerateResponseError(
153
- f"Request failed with status code {response.status_code}"
154
- )
155
-
156
- streaming_text = ""
157
- for line in response.iter_lines(decode_unicode=True):
158
- if line:
159
- line = line.strip()
160
- if line.startswith("data: "):
161
- json_str = line[6:] # Remove "data: " prefix
162
- if json_str == "[DONE]":
163
- break
164
- try:
165
- json_data = json.loads(json_str)
166
- if 'choices' in json_data:
167
- choice = json_data['choices'][0]
168
- if 'delta' in choice and 'content' in choice['delta']:
169
- content = choice['delta']['content']
170
- streaming_text += content
171
- resp = dict(text=content)
172
- yield resp if raw else resp
173
- except json.JSONDecodeError:
174
- if self.logger:
175
- self.logger.error("JSON decode error in streaming data")
176
- pass
177
-
178
- self.conversation.update_chat_history(prompt, streaming_text)
179
- if self.logger:
180
- self.logger.info("Streaming response completed successfully")
181
-
182
- except requests.RequestException as e:
183
- if self.logger:
184
- self.logger.error(f"Request failed: {e}")
185
- raise exceptions.FailedToGenerateResponseError(f"Request failed: {e}")
186
-
187
- def for_non_stream():
188
- for _ in for_stream():
189
- pass
190
- return self.last_response
191
-
192
- return for_stream() if stream else for_non_stream()
193
-
194
- def chat(
195
- self,
196
- prompt: str,
197
- stream: bool = False,
198
- optimizer: str = None,
199
- conversationally: bool = False,
200
- ) -> str:
201
- def for_stream():
202
- for response in self.ask(prompt, True, optimizer=optimizer, conversationally=conversationally):
203
- yield self.get_message(response)
204
-
205
- def for_non_stream():
206
- return self.get_message(
207
- self.ask(prompt, False, optimizer=optimizer, conversationally=conversationally)
208
- )
209
-
210
- return for_stream() if stream else for_non_stream()
211
-
212
- def get_message(self, response: dict) -> str:
213
- assert isinstance(response, dict), "Response should be of dict data-type only"
214
- return response["text"]
215
-
216
- if __name__ == "__main__":
217
- from rich import print
218
- ai = FreeAIChat(model="GPT-4o", logging=True)
219
- response = ai.chat("Write a hello world program in Python", stream=True)
220
- for chunk in response:
221
- print(chunk, end="", flush=True)
1
+ import requests
2
+ import json
3
+ import time
4
+ from typing import Any, Dict, Optional, Generator, 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
+ from webscout import LitAgent
12
+ from webscout.Litlogger import Logger, LogFormat
13
+
14
+ class FreeAIChat(Provider):
15
+ """
16
+ A class to interact with the FreeAIChat API with logging and LitAgent user-agent.
17
+ """
18
+
19
+ AVAILABLE_MODELS = [
20
+ "mistral-nemo",
21
+ "mistral-large",
22
+ "gemini-2.0-flash",
23
+ "gemini-1.5-pro",
24
+ "gemini-1.5-flash",
25
+ "gemini-2.0-pro-exp-02-05",
26
+ "deepseek-r1",
27
+ "deepseek-v3",
28
+ "Deepseek r1 14B",
29
+ "Deepseek r1 32B",
30
+ "o3-mini-high",
31
+ "o3-mini-medium",
32
+ "o3-mini-low",
33
+ "o3-mini",
34
+ "GPT-4o-mini",
35
+ "o1",
36
+ "o1-mini",
37
+ "GPT-4o",
38
+ "Qwen coder",
39
+ "Qwen 2.5 72B",
40
+ "Llama 3.1 405B",
41
+ "llama3.1-70b-fast",
42
+ "Llama 3.3 70B",
43
+ "claude 3.5 haiku",
44
+ "claude 3.5 sonnet",
45
+ ]
46
+
47
+ def __init__(
48
+ self,
49
+ is_conversation: bool = True,
50
+ max_tokens: int = 2049,
51
+ timeout: int = 30,
52
+ intro: str = None,
53
+ filepath: str = None,
54
+ update_file: bool = True,
55
+ proxies: dict = {},
56
+ history_offset: int = 10250,
57
+ act: str = None,
58
+ model: str = "GPT-4o",
59
+ system_prompt: str = "You are a helpful AI assistant.",
60
+ logging: bool = False
61
+ ):
62
+ """Initializes the FreeAIChat API client with logging support."""
63
+ if model not in self.AVAILABLE_MODELS:
64
+ raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
65
+
66
+ self.url = "https://freeaichatplayground.com/api/v1/chat/completions"
67
+ self.headers = {
68
+ 'User-Agent': LitAgent().random(),
69
+ 'Accept': '*/*',
70
+ 'Content-Type': 'application/json',
71
+ 'Origin': 'https://freeaichatplayground.com',
72
+ 'Referer': 'https://freeaichatplayground.com/',
73
+ 'Sec-Fetch-Mode': 'cors',
74
+ 'Sec-Fetch-Site': 'same-origin'
75
+ }
76
+ self.session = requests.Session()
77
+ self.session.headers.update(self.headers)
78
+ self.session.proxies.update(proxies)
79
+
80
+ self.is_conversation = is_conversation
81
+ self.max_tokens_to_sample = max_tokens
82
+ self.timeout = timeout
83
+ self.last_response = {}
84
+ self.model = model
85
+ self.system_prompt = system_prompt
86
+
87
+ self.__available_optimizers = (
88
+ method
89
+ for method in dir(Optimizers)
90
+ if callable(getattr(Optimizers, method)) and not method.startswith("__")
91
+ )
92
+ Conversation.intro = (
93
+ AwesomePrompts().get_act(
94
+ act, raise_not_found=True, default=None, case_insensitive=True
95
+ )
96
+ if act
97
+ else intro or Conversation.intro
98
+ )
99
+
100
+ self.conversation = Conversation(
101
+ is_conversation, self.max_tokens_to_sample, filepath, update_file
102
+ )
103
+ self.conversation.history_offset = history_offset
104
+
105
+ self.logger = Logger(
106
+ name="FreeAIChat",
107
+ format=LogFormat.MODERN_EMOJI,
108
+ ) if logging else None
109
+
110
+ if self.logger:
111
+ self.logger.info(f"FreeAIChat initialized successfully with model: {model}")
112
+
113
+ def ask(
114
+ self,
115
+ prompt: str,
116
+ stream: bool = False,
117
+ raw: bool = False,
118
+ optimizer: str = None,
119
+ conversationally: bool = False,
120
+ ) -> Union[Dict[str, Any], Generator]:
121
+ conversation_prompt = self.conversation.gen_complete_prompt(prompt)
122
+ if optimizer:
123
+ if optimizer in self.__available_optimizers:
124
+ conversation_prompt = getattr(Optimizers, optimizer)(
125
+ conversation_prompt if conversationally else prompt
126
+ )
127
+ if self.logger:
128
+ self.logger.debug(f"Applied optimizer: {optimizer}")
129
+ else:
130
+ if self.logger:
131
+ self.logger.error(f"Invalid optimizer requested: {optimizer}")
132
+ raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
133
+
134
+ messages = [
135
+ {
136
+ "role": "system",
137
+ "content": self.system_prompt
138
+ },
139
+ {
140
+ "role": "user",
141
+ "content": conversation_prompt
142
+ }
143
+ ]
144
+
145
+ payload = {
146
+ "model": self.model,
147
+ "messages": messages
148
+ }
149
+
150
+ def for_stream():
151
+ if self.logger:
152
+ self.logger.debug("Sending streaming request to FreeAIChat API...")
153
+ try:
154
+ with requests.post(self.url, headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
155
+ if response.status_code != 200:
156
+ if self.logger:
157
+ self.logger.error(f"Request failed with status code {response.status_code}")
158
+ raise exceptions.FailedToGenerateResponseError(
159
+ f"Request failed with status code {response.status_code}"
160
+ )
161
+
162
+ streaming_text = ""
163
+ for line in response.iter_lines(decode_unicode=True):
164
+ if line:
165
+ line = line.strip()
166
+ if line.startswith("data: "):
167
+ json_str = line[6:] # Remove "data: " prefix
168
+ if json_str == "[DONE]":
169
+ break
170
+ try:
171
+ json_data = json.loads(json_str)
172
+ if 'choices' in json_data:
173
+ choice = json_data['choices'][0]
174
+ if 'delta' in choice and 'content' in choice['delta']:
175
+ content = choice['delta']['content']
176
+ streaming_text += content
177
+ resp = dict(text=content)
178
+ yield resp if raw else resp
179
+ except json.JSONDecodeError:
180
+ if self.logger:
181
+ self.logger.error("JSON decode error in streaming data")
182
+ pass
183
+
184
+ self.conversation.update_chat_history(prompt, streaming_text)
185
+ if self.logger:
186
+ self.logger.info("Streaming response completed successfully")
187
+
188
+ except requests.RequestException as e:
189
+ if self.logger:
190
+ self.logger.error(f"Request failed: {e}")
191
+ raise exceptions.FailedToGenerateResponseError(f"Request failed: {e}")
192
+
193
+ def for_non_stream():
194
+ full_text = ""
195
+ for chunk in for_stream():
196
+ full_text += chunk["text"]
197
+ return {"text": full_text}
198
+
199
+ return for_stream() if stream else for_non_stream()
200
+
201
+ def chat(
202
+ self,
203
+ prompt: str,
204
+ stream: bool = False,
205
+ optimizer: str = None,
206
+ conversationally: bool = False,
207
+ ) -> str:
208
+ def for_stream():
209
+ for response in self.ask(prompt, True, optimizer=optimizer, conversationally=conversationally):
210
+ yield self.get_message(response)
211
+
212
+ def for_non_stream():
213
+ return self.get_message(
214
+ self.ask(prompt, False, optimizer=optimizer, conversationally=conversationally)
215
+ )
216
+
217
+ return for_stream() if stream else for_non_stream()
218
+
219
+ def get_message(self, response: dict) -> str:
220
+ assert isinstance(response, dict), "Response should be of dict data-type only"
221
+ return response["text"]
222
+
223
+ @staticmethod
224
+ def fix_encoding(text):
225
+ if isinstance(text, dict) and "text" in text:
226
+ try:
227
+ text["text"] = text["text"].encode("latin1").decode("utf-8")
228
+ return text
229
+ except (UnicodeError, AttributeError) as e:
230
+ return text
231
+ elif isinstance(text, str):
232
+ try:
233
+ return text.encode("latin1").decode("utf-8")
234
+ except (UnicodeError, AttributeError) as e:
235
+ return text
236
+ return text
237
+
238
+
239
+ if __name__ == "__main__":
240
+ from rich import print
241
+ ai = FreeAIChat(model="GPT-4o", logging=True)
242
+ # response = ai.chat(input(">>>"), stream=True)
243
+ # full_text = ""
244
+
245
+ # for chunk in response:
246
+ # corrected_chunk = ai.fix_encoding(chunk)
247
+ # full_text += corrected_chunk
248
+
249
+ response = ai.chat(input(">>>"), stream=False)
250
+ response = ai.fix_encoding(response)
251
+ print(response)
@@ -12,6 +12,11 @@ class KOALA(Provider):
12
12
  A class to interact with the Koala.sh API.
13
13
  """
14
14
 
15
+ AVAILABLE_MODELS = [
16
+ "gpt-4o-mini",
17
+ "gpt-4o",
18
+ ]
19
+
15
20
  def __init__(
16
21
  self,
17
22
  is_conversation: bool = True,
@@ -23,7 +28,7 @@ class KOALA(Provider):
23
28
  proxies: dict = {},
24
29
  history_offset: int = 10250,
25
30
  act: str = None,
26
- model: str = "gpt-4o-mini",
31
+ model: str = "gpt-4o",
27
32
  web_search: bool = True,
28
33
 
29
34
  ) -> None:
@@ -44,6 +49,9 @@ class KOALA(Provider):
44
49
  act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
45
50
  model (str, optional): AI model to use. Defaults to "gpt-4o-mini".
46
51
  """
52
+ if model not in self.AVAILABLE_MODELS:
53
+ raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
54
+
47
55
  self.session = requests.Session()
48
56
  self.is_conversation = is_conversation
49
57
  self.max_tokens_to_sample = max_tokens
webscout/Provider/yep.py CHANGED
@@ -12,7 +12,6 @@ from webscout.AIutel import Conversation
12
12
  from webscout.AIutel import AwesomePrompts
13
13
  from webscout.AIbase import Provider
14
14
  from webscout import WEBS, exceptions
15
- from webscout.Litlogger import Logger, LogFormat
16
15
  from webscout.litagent import LitAgent
17
16
 
18
17
 
@@ -39,14 +38,13 @@ class YEPCHAT(Provider):
39
38
  act: str = None,
40
39
  model: str = "DeepSeek-R1-Distill-Qwen-32B",
41
40
  temperature: float = 0.6,
42
- top_p: float = 0.7,
43
- logging: bool = False,
41
+ top_p: float = 0.7
44
42
  ):
45
43
  """
46
44
  Initializes the YEPCHAT provider with the specified parameters.
47
45
 
48
46
  Examples:
49
- >>> ai = YEPCHAT(logging=True)
47
+ >>> ai = YEPCHAT()
50
48
  >>> ai.ask("What's the weather today?")
51
49
  Sends a prompt to the Yep API and returns the response.
52
50
 
@@ -85,7 +83,7 @@ class YEPCHAT(Provider):
85
83
  "Sec-CH-UA-Platform": '"Windows"',
86
84
  "User-Agent": self.agent.random(), # Use LitAgent to generate a random user agent
87
85
  }
88
- self.cookies = {"__Host-session": uuid.uuid4().hex}
86
+ self.cookies = {"__Host-session": uuid.uuid4().hex, '__cf_bm': uuid.uuid4().hex}
89
87
 
90
88
  self.__available_optimizers = (
91
89
  method
@@ -106,9 +104,6 @@ class YEPCHAT(Provider):
106
104
 
107
105
  self.knowledge_cutoff = "December 2023"
108
106
 
109
- # Initialize logger
110
- self.logger = Logger(name="YEPCHAT", format=LogFormat.MODERN_EMOJI) if logging else None
111
-
112
107
  def ask(
113
108
  self,
114
109
  prompt: str,
@@ -128,9 +123,6 @@ class YEPCHAT(Provider):
128
123
  >>> ai.ask("Tell me a joke", stream=True)
129
124
  Streams the response from the Yep API.
130
125
  """
131
- if self.logger:
132
- self.logger.debug(f"ask() called with prompt: {prompt}")
133
-
134
126
  conversation_prompt = self.conversation.gen_complete_prompt(prompt)
135
127
  if optimizer:
136
128
  if optimizer in self.__available_optimizers:
@@ -138,8 +130,6 @@ class YEPCHAT(Provider):
138
130
  conversation_prompt if conversationally else prompt
139
131
  )
140
132
  else:
141
- if self.logger:
142
- self.logger.error(f"Invalid optimizer: {optimizer}")
143
133
  raise Exception(
144
134
  f"Optimizer is not one of {self.__available_optimizers}"
145
135
  )
@@ -157,8 +147,6 @@ class YEPCHAT(Provider):
157
147
  try:
158
148
  with self.session.post(self.chat_endpoint, headers=self.headers, cookies=self.cookies, json=data, stream=True, timeout=self.timeout) as response:
159
149
  if not response.ok:
160
- if self.logger:
161
- self.logger.error(f"Failed to generate response: {response.status_code} {response.reason}")
162
150
  raise exceptions.FailedToGenerateResponseError(
163
151
  f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
164
152
  )
@@ -183,13 +171,9 @@ class YEPCHAT(Provider):
183
171
  resp = dict(text=content)
184
172
  yield resp if raw else resp
185
173
  except json.JSONDecodeError:
186
- if self.logger:
187
- self.logger.warning("JSONDecodeError encountered.")
188
174
  pass
189
175
  self.conversation.update_chat_history(prompt, streaming_text)
190
176
  except Exception as e:
191
- if self.logger:
192
- self.logger.error(f"Request failed: {e}")
193
177
  raise exceptions.FailedToGenerateResponseError(f"Request failed: {e}")
194
178
 
195
179
  def for_non_stream():
@@ -217,9 +201,6 @@ class YEPCHAT(Provider):
217
201
  >>> ai.chat("What's the weather today?", stream=True)
218
202
  Streams the chat response from the Yep API.
219
203
  """
220
- if self.logger:
221
- self.logger.debug(f"chat() called with prompt: {prompt}")
222
-
223
204
  def for_stream():
224
205
  for response in self.ask(
225
206
  prompt, True, optimizer=optimizer, conversationally=conversationally
@@ -255,8 +236,7 @@ class YEPCHAT(Provider):
255
236
  if __name__ == "__main__":
256
237
  from rich import print
257
238
 
258
- ai = YEPCHAT(logging=False)
259
-
239
+ ai = YEPCHAT(model="DeepSeek-R1-Distill-Qwen-32B")
260
240
  response = ai.chat("how many r in 'strawberry'", stream=True)
261
241
  for chunk in response:
262
242
  print(chunk, end="", flush=True)
webscout/version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "7.3"
1
+ __version__ = "7.4"
2
2
  __prog__ = "webscout"