webscout 8.3.6__py3-none-any.whl → 8.3.7__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.

Files changed (130) hide show
  1. webscout/AIutel.py +2 -0
  2. webscout/Provider/AISEARCH/__init__.py +18 -11
  3. webscout/Provider/AISEARCH/scira_search.py +3 -1
  4. webscout/Provider/Aitopia.py +2 -3
  5. webscout/Provider/Andi.py +3 -3
  6. webscout/Provider/ChatGPTClone.py +1 -1
  7. webscout/Provider/ChatSandbox.py +1 -0
  8. webscout/Provider/Cloudflare.py +1 -1
  9. webscout/Provider/Cohere.py +1 -0
  10. webscout/Provider/Deepinfra.py +7 -10
  11. webscout/Provider/ExaAI.py +1 -1
  12. webscout/Provider/ExaChat.py +1 -80
  13. webscout/Provider/Flowith.py +1 -1
  14. webscout/Provider/Gemini.py +7 -5
  15. webscout/Provider/GeminiProxy.py +1 -0
  16. webscout/Provider/GithubChat.py +3 -1
  17. webscout/Provider/Groq.py +1 -1
  18. webscout/Provider/HeckAI.py +8 -4
  19. webscout/Provider/Jadve.py +23 -38
  20. webscout/Provider/K2Think.py +308 -0
  21. webscout/Provider/Koboldai.py +8 -186
  22. webscout/Provider/LambdaChat.py +2 -4
  23. webscout/Provider/Nemotron.py +3 -4
  24. webscout/Provider/Netwrck.py +3 -2
  25. webscout/Provider/OLLAMA.py +1 -0
  26. webscout/Provider/OPENAI/Cloudflare.py +6 -7
  27. webscout/Provider/OPENAI/FalconH1.py +2 -7
  28. webscout/Provider/OPENAI/FreeGemini.py +6 -8
  29. webscout/Provider/OPENAI/{monochat.py → K2Think.py} +180 -77
  30. webscout/Provider/OPENAI/NEMOTRON.py +3 -6
  31. webscout/Provider/OPENAI/PI.py +5 -4
  32. webscout/Provider/OPENAI/Qwen3.py +2 -3
  33. webscout/Provider/OPENAI/TogetherAI.py +2 -2
  34. webscout/Provider/OPENAI/TwoAI.py +3 -4
  35. webscout/Provider/OPENAI/__init__.py +17 -58
  36. webscout/Provider/OPENAI/ai4chat.py +313 -303
  37. webscout/Provider/OPENAI/base.py +9 -29
  38. webscout/Provider/OPENAI/chatgpt.py +7 -2
  39. webscout/Provider/OPENAI/chatgptclone.py +4 -7
  40. webscout/Provider/OPENAI/chatsandbox.py +84 -59
  41. webscout/Provider/OPENAI/deepinfra.py +6 -6
  42. webscout/Provider/OPENAI/heckai.py +4 -1
  43. webscout/Provider/OPENAI/netwrck.py +1 -0
  44. webscout/Provider/OPENAI/scirachat.py +6 -0
  45. webscout/Provider/OPENAI/textpollinations.py +3 -11
  46. webscout/Provider/OPENAI/toolbaz.py +14 -11
  47. webscout/Provider/OpenGPT.py +1 -1
  48. webscout/Provider/Openai.py +150 -402
  49. webscout/Provider/PI.py +1 -0
  50. webscout/Provider/Perplexitylabs.py +1 -2
  51. webscout/Provider/QwenLM.py +107 -89
  52. webscout/Provider/STT/__init__.py +17 -2
  53. webscout/Provider/{Llama3.py → Sambanova.py} +9 -10
  54. webscout/Provider/StandardInput.py +1 -1
  55. webscout/Provider/TTI/__init__.py +18 -12
  56. webscout/Provider/TTS/__init__.py +18 -10
  57. webscout/Provider/TeachAnything.py +1 -0
  58. webscout/Provider/TextPollinationsAI.py +5 -12
  59. webscout/Provider/TogetherAI.py +86 -87
  60. webscout/Provider/TwoAI.py +53 -309
  61. webscout/Provider/TypliAI.py +2 -1
  62. webscout/Provider/{GizAI.py → UNFINISHED/GizAI.py} +1 -1
  63. webscout/Provider/Venice.py +2 -1
  64. webscout/Provider/VercelAI.py +1 -0
  65. webscout/Provider/WiseCat.py +2 -1
  66. webscout/Provider/WrDoChat.py +2 -1
  67. webscout/Provider/__init__.py +18 -86
  68. webscout/Provider/ai4chat.py +1 -1
  69. webscout/Provider/akashgpt.py +7 -10
  70. webscout/Provider/cerebras.py +115 -9
  71. webscout/Provider/chatglm.py +170 -83
  72. webscout/Provider/cleeai.py +1 -2
  73. webscout/Provider/deepseek_assistant.py +1 -1
  74. webscout/Provider/elmo.py +1 -1
  75. webscout/Provider/geminiapi.py +1 -1
  76. webscout/Provider/granite.py +1 -1
  77. webscout/Provider/hermes.py +1 -3
  78. webscout/Provider/julius.py +1 -0
  79. webscout/Provider/learnfastai.py +1 -1
  80. webscout/Provider/llama3mitril.py +1 -1
  81. webscout/Provider/llmchat.py +1 -1
  82. webscout/Provider/llmchatco.py +1 -1
  83. webscout/Provider/meta.py +3 -3
  84. webscout/Provider/oivscode.py +2 -2
  85. webscout/Provider/scira_chat.py +51 -124
  86. webscout/Provider/searchchat.py +1 -0
  87. webscout/Provider/sonus.py +1 -1
  88. webscout/Provider/toolbaz.py +15 -12
  89. webscout/Provider/turboseek.py +31 -22
  90. webscout/Provider/typefully.py +2 -1
  91. webscout/Provider/x0gpt.py +1 -0
  92. webscout/Provider/yep.py +2 -1
  93. webscout/tempid.py +6 -0
  94. webscout/version.py +1 -1
  95. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/METADATA +2 -1
  96. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/RECORD +103 -129
  97. webscout/Provider/AllenAI.py +0 -440
  98. webscout/Provider/Blackboxai.py +0 -793
  99. webscout/Provider/FreeGemini.py +0 -250
  100. webscout/Provider/GptOss.py +0 -207
  101. webscout/Provider/Hunyuan.py +0 -283
  102. webscout/Provider/Kimi.py +0 -445
  103. webscout/Provider/MCPCore.py +0 -322
  104. webscout/Provider/MiniMax.py +0 -207
  105. webscout/Provider/OPENAI/BLACKBOXAI.py +0 -1045
  106. webscout/Provider/OPENAI/MiniMax.py +0 -298
  107. webscout/Provider/OPENAI/autoproxy.py +0 -1067
  108. webscout/Provider/OPENAI/copilot.py +0 -321
  109. webscout/Provider/OPENAI/gptoss.py +0 -288
  110. webscout/Provider/OPENAI/kimi.py +0 -469
  111. webscout/Provider/OPENAI/mcpcore.py +0 -431
  112. webscout/Provider/OPENAI/multichat.py +0 -378
  113. webscout/Provider/Reka.py +0 -214
  114. webscout/Provider/UNFINISHED/fetch_together_models.py +0 -90
  115. webscout/Provider/asksteve.py +0 -220
  116. webscout/Provider/copilot.py +0 -441
  117. webscout/Provider/freeaichat.py +0 -294
  118. webscout/Provider/koala.py +0 -182
  119. webscout/Provider/lmarena.py +0 -198
  120. webscout/Provider/monochat.py +0 -275
  121. webscout/Provider/multichat.py +0 -375
  122. webscout/Provider/scnet.py +0 -244
  123. webscout/Provider/talkai.py +0 -194
  124. /webscout/Provider/{Marcus.py → UNFINISHED/Marcus.py} +0 -0
  125. /webscout/Provider/{Qodo.py → UNFINISHED/Qodo.py} +0 -0
  126. /webscout/Provider/{XenAI.py → UNFINISHED/XenAI.py} +0 -0
  127. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/WHEEL +0 -0
  128. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/entry_points.txt +0 -0
  129. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/licenses/LICENSE.md +0 -0
  130. {webscout-8.3.6.dist-info → webscout-8.3.7.dist-info}/top_level.txt +0 -0
@@ -1,294 +0,0 @@
1
- import re
2
- import requests
3
- import json
4
- import uuid
5
- import random
6
- import string
7
- from typing import Any, Dict, Optional, Generator, Union
8
-
9
- from webscout.AIutel import Optimizers
10
- from webscout.AIutel import Conversation
11
- from webscout.AIutel import AwesomePrompts, sanitize_stream
12
- from webscout.AIbase import Provider, AsyncProvider
13
- from webscout import exceptions
14
- from webscout.litagent import LitAgent
15
-
16
- class FreeAIChat(Provider):
17
- """
18
- A class to interact with the FreeAIChat API
19
- """
20
-
21
- AVAILABLE_MODELS = [
22
- # OpenAI Models
23
- "Deepseek R1 Latest",
24
- "GPT 4o",
25
- "O4 Mini",
26
- "O4 Mini High",
27
- "QwQ Plus",
28
- "Llama 4 Maverick",
29
- "Grok 3",
30
- "GPT 4o mini",
31
- "Deepseek v3 0324",
32
- "Grok 3 Mini",
33
- "GPT 4.1",
34
- "GPT 4.1 Mini",
35
- "Claude 3.7 Sonnet (Thinking)",
36
- "Llama 4 Scout",
37
- "O3 High",
38
- "Gemini 2.5 Pro",
39
- "Magistral Medium 2506",
40
- "O3",
41
- "Gemini 2.5 Flash",
42
- "Qwen 3 235B A22B",
43
- "Claude 4 Sonnet",
44
- "Claude 4 Sonnet (Thinking)",
45
- "Claude 4 Opus",
46
- "Claude 4 Opus (Thinking)",
47
- "Google: Gemini 2.5 Pro (thinking)",
48
- ]
49
-
50
- def _auto_fetch_api_key(self, proxies=None, timeout=30):
51
- """
52
- Automatically register a new user and fetch an API key from FreeAIChat Playground.
53
- """
54
- session = requests.Session()
55
- if proxies:
56
- session.proxies.update(proxies)
57
- def random_email():
58
- user = ''.join(random.choices(string.ascii_lowercase + string.digits, k=12))
59
- return f"{user}@bltiwd.com"
60
- email = random_email()
61
- payload = {"email": email, "password": email}
62
- headers = {
63
- 'User-Agent': LitAgent().random(),
64
- 'Accept': '*/*',
65
- 'Content-Type': 'application/json',
66
- 'Origin': 'https://freeaichatplayground.com',
67
- 'Referer': 'https://freeaichatplayground.com/register',
68
- }
69
- try:
70
- resp = session.post(
71
- "https://freeaichatplayground.com/api/v1/auth/register",
72
- headers=headers,
73
- json=payload,
74
- timeout=timeout
75
- )
76
- if resp.status_code == 201:
77
- data = resp.json()
78
- apikey = data.get("user", {}).get("apikey")
79
- if apikey:
80
- return apikey
81
- else:
82
- raise exceptions.FailedToGenerateResponseError("API key not found in registration response.")
83
- else:
84
- raise exceptions.FailedToGenerateResponseError(f"Registration failed: {resp.status_code} {resp.text}")
85
- except Exception as e:
86
- raise exceptions.FailedToGenerateResponseError(f"API key auto-fetch failed: {e}")
87
-
88
- def __init__(
89
- self,
90
- api_key: str = None,
91
- is_conversation: bool = True,
92
- max_tokens: int = 150,
93
- timeout: int = 30,
94
- intro: str = None,
95
- filepath: str = None,
96
- update_file: bool = True,
97
- proxies: dict = {},
98
- history_offset: int = 10250,
99
- act: str = None,
100
- model: str = "GPT 4o",
101
- system_prompt: str = "You are a helpful AI assistant.",
102
- temperature: float = 0.7,
103
- ):
104
- """Initializes the FreeAIChat API client. If api_key is not provided, auto-register and fetch one."""
105
- if model not in self.AVAILABLE_MODELS:
106
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
107
-
108
- self.url = "https://freeaichatplayground.com/api/v1/chat/completions"
109
- self.headers = {
110
- 'User-Agent': LitAgent().random(),
111
- 'Accept': '*/*',
112
- 'Content-Type': 'application/json',
113
- 'Origin': 'https://freeaichatplayground.com',
114
- 'Referer': 'https://freeaichatplayground.com/',
115
- 'Sec-Fetch-Mode': 'cors',
116
- 'Sec-Fetch-Site': 'same-origin'
117
- }
118
- self.session = requests.Session()
119
- self.session.headers.update(self.headers)
120
- self.session.proxies.update(proxies)
121
-
122
- self.is_conversation = is_conversation
123
- self.max_tokens = max_tokens
124
- self.timeout = timeout
125
- self.last_response = {}
126
- self.model = model
127
- self.system_prompt = system_prompt
128
- self.temperature = temperature
129
- if not api_key:
130
- self.api_key = self._auto_fetch_api_key(proxies=proxies, timeout=timeout)
131
- else:
132
- self.api_key = api_key
133
-
134
- self.__available_optimizers = (
135
- method
136
- for method in dir(Optimizers)
137
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
138
- )
139
- Conversation.intro = (
140
- AwesomePrompts().get_act(
141
- act, raise_not_found=True, default=None, case_insensitive=True
142
- )
143
- if act
144
- else intro or Conversation.intro
145
- )
146
-
147
- self.conversation = Conversation(
148
- is_conversation, self.max_tokens, filepath, update_file
149
- )
150
- self.conversation.history_offset = history_offset
151
-
152
- @staticmethod
153
- def _extractor(chunk: Union[str, Dict[str, Any]]) -> Optional[str]:
154
- """Extracts content from the x0gpt stream format '0:"..."'."""
155
- if isinstance(chunk, str):
156
- match = re.search(r'0:"(.*?)"', chunk)
157
- if match:
158
- # Decode potential unicode escapes like \u00e9
159
- content = match.group(1).encode().decode('unicode_escape')
160
- return content.replace('\\\\', '\\').replace('\\"', '"') # Handle escaped backslashes and quotes
161
- return None
162
-
163
- def ask(
164
- self,
165
- prompt: str,
166
- stream: bool = False,
167
- raw: bool = False,
168
- optimizer: str = None,
169
- conversationally: bool = False,
170
- ) -> Union[Dict[str, Any], Generator]:
171
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
172
- if optimizer:
173
- if optimizer in self.__available_optimizers:
174
- conversation_prompt = getattr(Optimizers, optimizer)(
175
- conversation_prompt if conversationally else prompt
176
- )
177
- else:
178
- raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
179
-
180
- payload = {
181
- "id": str(uuid.uuid4()),
182
- "messages": [{
183
- "role": "user",
184
- "content": conversation_prompt,
185
- "parts": [{"type": "text", "text": conversation_prompt}]
186
- }],
187
- "model": self.model,
188
- "config": {
189
- "temperature": self.temperature,
190
- "maxTokens": self.max_tokens
191
- },
192
- "apiKey": self.api_key
193
- }
194
-
195
- def for_stream():
196
- try:
197
- with requests.post(self.url, headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
198
- if response.status_code != 200:
199
- raise exceptions.FailedToGenerateResponseError(
200
- f"Request failed with status code {response.status_code}"
201
- )
202
-
203
- streaming_response = ""
204
- processed_stream = sanitize_stream(
205
- data=response.iter_lines(decode_unicode=True),
206
- intro_value=None,
207
- to_json=False,
208
- content_extractor=self._extractor,
209
- skip_markers=None
210
- )
211
-
212
- for content_chunk in processed_stream:
213
- if content_chunk and isinstance(content_chunk, str):
214
- streaming_response += content_chunk
215
- yield dict(text=content_chunk) if raw else dict(text=content_chunk)
216
-
217
- self.last_response.update(dict(text=streaming_response))
218
- self.conversation.update_chat_history(
219
- prompt, self.get_message(self.last_response)
220
- )
221
-
222
- except requests.RequestException as e:
223
- raise exceptions.FailedToGenerateResponseError(f"Request failed: {e}")
224
-
225
- def for_non_stream():
226
- full_text = ""
227
- for chunk in for_stream():
228
- full_text += chunk["text"]
229
- return {"text": full_text}
230
-
231
- return for_stream() if stream else for_non_stream()
232
-
233
- def chat(
234
- self,
235
- prompt: str,
236
- stream: bool = False,
237
- optimizer: str = None,
238
- conversationally: bool = False,
239
- ) -> str:
240
- def for_stream():
241
- for response in self.ask(prompt, True, optimizer=optimizer, conversationally=conversationally):
242
- yield self.get_message(response)
243
-
244
- def for_non_stream():
245
- return self.get_message(
246
- self.ask(prompt, False, optimizer=optimizer, conversationally=conversationally)
247
- )
248
-
249
- return for_stream() if stream else for_non_stream()
250
-
251
- def get_message(self, response: dict) -> str:
252
- assert isinstance(response, dict), "Response should be of dict data-type only"
253
- return response["text"]
254
-
255
- @staticmethod
256
- def fix_encoding(text):
257
- if isinstance(text, dict) and "text" in text:
258
- try:
259
- text["text"] = text["text"].encode("latin1").decode("utf-8")
260
- return text
261
- except (UnicodeError, AttributeError) as e:
262
- return text
263
- elif isinstance(text, str):
264
- try:
265
- return text.encode("latin1").decode("utf-8")
266
- except (UnicodeError, AttributeError) as e:
267
- return text
268
- return text
269
-
270
- if __name__ == "__main__":
271
- print("-" * 80)
272
- print(f"{'Model':<50} {'Status':<10} {'Response'}")
273
- print("-" * 80)
274
-
275
- for model in FreeAIChat.AVAILABLE_MODELS:
276
- try:
277
- test_ai = FreeAIChat(model=model, timeout=60)
278
- response = test_ai.chat("Say 'Hello' in one word", stream=True)
279
- response_text = ""
280
- for chunk in response:
281
- response_text += chunk
282
- print(f"\r{model:<50} {'Testing...':<10}", end="", flush=True)
283
-
284
- if response_text and len(response_text.strip()) > 0:
285
- status = "✓"
286
- # Clean and truncate response
287
- clean_text = response_text.strip().encode('utf-8', errors='ignore').decode('utf-8')
288
- display_text = clean_text[:50] + "..." if len(clean_text) > 50 else clean_text
289
- else:
290
- status = "✗"
291
- display_text = "Empty or invalid response"
292
- print(f"\r{model:<50} {status:<10} {display_text}")
293
- except Exception as e:
294
- print(f"\r{model:<50} {'✗':<10} {str(e)}")
@@ -1,182 +0,0 @@
1
- import requests
2
- import re
3
- from typing import Optional, Union, Any, Dict, Generator
4
- from uuid import uuid4
5
-
6
- from webscout.AIutel import Optimizers, Conversation, AwesomePrompts, sanitize_stream
7
- from webscout.AIbase import Provider
8
- from webscout import exceptions
9
-
10
- class KOALA(Provider):
11
- """
12
- A class to interact with the Koala.sh API, X0GPT-style, without sanitize_stream.
13
- """
14
- AVAILABLE_MODELS = [
15
- "gpt-4.1-mini",
16
- "gpt-4.1",
17
- ]
18
-
19
- def __init__(
20
- self,
21
- is_conversation: bool = True,
22
- max_tokens: int = 600,
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.1",
31
- web_search: bool = True,
32
- ):
33
- if model not in self.AVAILABLE_MODELS:
34
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
35
- self.session = requests.Session()
36
- self.is_conversation = is_conversation
37
- self.max_tokens_to_sample = max_tokens
38
- self.api_endpoint = "https://koala.sh/api/gpt/"
39
- self.timeout = timeout
40
- self.last_response = {}
41
- self.model = model
42
- self.headers = {
43
- "accept": "text/event-stream",
44
- "accept-encoding": "gzip, deflate, br, zstd",
45
- "accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
46
- "content-type": "application/json",
47
- "dnt": "1",
48
- "flag-real-time-data": "true" if web_search else "false",
49
- "origin": "https://koala.sh",
50
- "referer": "https://koala.sh/chat",
51
- "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",
52
- }
53
- self.session.headers.update(self.headers)
54
- Conversation.intro = (
55
- AwesomePrompts().get_act(
56
- act, raise_not_found=True, default=None, case_insensitive=True
57
- )
58
- if act
59
- else intro or Conversation.intro
60
- )
61
- self.conversation = Conversation(
62
- is_conversation, self.max_tokens_to_sample, filepath, update_file
63
- )
64
- self.conversation.history_offset = history_offset
65
- self.session.proxies = proxies
66
-
67
- @staticmethod
68
- def _koala_extractor(line: str) -> Optional[str]:
69
- # Koala returns lines like: data: "Hello" or data: "..."
70
- match = re.match(r'data:\s*"(.*)"', line)
71
- if match:
72
- return match.group(1)
73
- return None
74
-
75
- def ask(
76
- self,
77
- prompt: str,
78
- stream: bool = False,
79
- raw: bool = False,
80
- optimizer: str = None,
81
- conversationally: bool = False,
82
- ) -> Union[Dict[str, Any], Generator[Any, None, None]]:
83
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
84
- if optimizer:
85
- if hasattr(Optimizers, optimizer):
86
- conversation_prompt = getattr(Optimizers, optimizer)(
87
- conversation_prompt if conversationally else prompt
88
- )
89
- else:
90
- raise Exception(f"Optimizer is not valid.")
91
- payload = {
92
- "input": conversation_prompt,
93
- "inputHistory": [],
94
- "outputHistory": [],
95
- "model": self.model
96
- }
97
- response = self.session.post(
98
- self.api_endpoint, json=payload, headers=self.headers, stream=True, timeout=self.timeout
99
- )
100
- if not response.ok:
101
- raise exceptions.FailedToGenerateResponseError(
102
- f"Failed to generate response - ({response.status_code}, {response.reason})"
103
- )
104
- # Use sanitize_stream with content_extractor and intro_value like YEPCHAT/X0GPT
105
- processed_stream = sanitize_stream(
106
- data=response.iter_lines(decode_unicode=True),
107
- intro_value="data:",
108
- to_json=False,
109
- content_extractor=self._koala_extractor,
110
- raw=raw
111
- )
112
- if stream:
113
- streaming_response = ""
114
- for content_chunk in processed_stream:
115
- if raw:
116
- if content_chunk and isinstance(content_chunk, str) and content_chunk.strip():
117
- streaming_response += content_chunk
118
- yield content_chunk
119
- else:
120
- if content_chunk and isinstance(content_chunk, str) and content_chunk.strip():
121
- streaming_response += content_chunk
122
- yield dict(text=content_chunk)
123
- if streaming_response.strip():
124
- self.last_response = dict(text=streaming_response)
125
- self.conversation.update_chat_history(
126
- prompt, self.get_message(self.last_response)
127
- )
128
- else:
129
- full_text = ""
130
- for content_chunk in processed_stream:
131
- if raw:
132
- if content_chunk and isinstance(content_chunk, str):
133
- full_text += content_chunk
134
- else:
135
- if content_chunk and isinstance(content_chunk, str):
136
- full_text += content_chunk
137
- if full_text.strip():
138
- self.last_response = dict(text=full_text)
139
- self.conversation.update_chat_history(
140
- prompt, self.get_message(self.last_response)
141
- )
142
- return self.last_response
143
-
144
- def chat(
145
- self,
146
- prompt: str,
147
- stream: bool = False,
148
- raw: bool = False,
149
- optimizer: str = None,
150
- conversationally: bool = False,
151
- ) -> Union[str, Generator[str, None, None]]:
152
- def for_stream():
153
- for response in self.ask(
154
- prompt, True, raw=raw, optimizer=optimizer, conversationally=conversationally
155
- ):
156
- if raw:
157
- yield response
158
- else:
159
- yield self.get_message(response)
160
- def for_non_stream():
161
- result = self.ask(
162
- prompt,
163
- False,
164
- raw=raw,
165
- optimizer=optimizer,
166
- conversationally=conversationally,
167
- )
168
- if raw:
169
- return result.get("text", "") if isinstance(result, dict) else str(result)
170
- return self.get_message(result)
171
- return for_stream() if stream else for_non_stream()
172
-
173
- def get_message(self, response: dict) -> str:
174
- assert isinstance(response, dict), "Response should be of dict data-type only"
175
- return response.get("text", "")
176
-
177
- if __name__ == "__main__":
178
- from rich import print
179
- ai = KOALA(timeout=60)
180
- response = ai.chat("tell me about humans", stream=True, raw=False)
181
- for chunk in response:
182
- print(chunk)
@@ -1,198 +0,0 @@
1
- from typing import Any, Dict, Optional, Union, Generator
2
- from uuid import uuid4
3
- import re
4
- from curl_cffi.requests import Session
5
- from curl_cffi import CurlError
6
- from webscout.AIbase import Provider
7
- from webscout.AIutel import Optimizers, Conversation, AwesomePrompts, sanitize_stream
8
- from webscout.litagent import LitAgent
9
- from webscout import exceptions
10
-
11
- class lmarena(Provider):
12
- """
13
- Provider for the Arena API (battle mode).
14
- """
15
- AVAILABLE_MODELS = ["arena-battle"]
16
-
17
- def __init__(
18
- self,
19
- is_conversation: bool = True,
20
- max_tokens: int = 2048,
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
- ):
29
- self.url = "https://arena-api-stable.vercel.app/evaluation"
30
- self.session = Session()
31
- self.is_conversation = is_conversation
32
- self.max_tokens_to_sample = max_tokens
33
- self.timeout = timeout
34
- self.last_response = {}
35
- self.supabase_jwt = "base64-eyJhY2Nlc3NfdG9rZW4iOiJleUpoYkdjaU9pSklVekkxTmlJc0ltdHBaQ0k2SWtOVFQwNHhkM05uU0hkRlNFTkNNbGNpTENKMGVYQWlPaUpLVjFRaWZRLmV5SnBjM01pT2lKb2RIUndjem92TDJoMWIyZDZiMlZ4ZW1OeVpIWnJkM1IyYjJScExuTjFjR0ZpWVhObExtTnZMMkYxZEdndmRqRWlMQ0p6ZFdJaU9pSXhNV0kxT1ROaE9DMHpZak5sTFRSak1UQXRPRE13TUMwMk16QTBNMk15TW1VeU1qWWlMQ0poZFdRaU9pSmhkWFJvWlc1MGFXTmhkR1ZrSWl3aVpYaHdJam94TnpRM05UUXhPVGc1TENKcFlYUWlPakUzTkRjMU16Z3pPRGtzSW1WdFlXbHNJam9pSWl3aWNHaHZibVVpT2lJaUxDSmhjSEJmYldWMFlXUmhkR0VpT250OUxDSjFjMlZ5WDIxbGRHRmtZWFJoSWpwN0ltbGtJam9pTW1aaU1qVXhPRGN0T0RFMFppMDBNR0l3TFdJNE5UQXRZemswTnpOak1EVTVNakZtSW4wc0luSnZiR1VpT2lKaGRYUm9aVzUwYVdOaGRHVmtJaXdpWVdGc0lqb2lZV0ZzTVNJc0ltRnRjaUk2VzNzaWJXVjBhRzlrSWpvaVlXNXZibmx0YjNWeklpd2lkR2x0WlhOMFlXMXdJam94TnpRME9UWTJOak15ZlYwc0luTmxjM05wYjI1ZmFXUWlPaUl3TjJWaE5UZGtNeTFsT1RNMkxUUXpZVE10WW1Oa05pMW1aREZpTjJOa01ESmpaV0lpTENKcGMxOWhibTl1ZVcxdmRYTWlPblJ5ZFdWOS5jR3VsYlBRRmQ5MzZmcmpmdC1oWjBUQ0k1Rk1sdVU5ZUNac3h3VkNVTkhrIiwidG9rZW5fdHlwZSI6ImJlYXJlciIsImV4cGlyZXNfaW4iOjM2MDAsImV4cGlyZXNfYXQiOjE3NDc1NDE5ODksInJlZnJlc2hfdG9rZW4iOiJ0cG95ZTViN2s2N3kiLCJ1c2VyIjp7ImlkIjoiMTFiNTkzYTgtM2IzZS00YzEwLTgzMDAtNjMwNDNjMjJlMjI2IiwiYXVkIjoiYXV0aGVudGljYXRlZCIsInJvbGUiOiJhdXRoZW50aWNhdGVkIiwiZW1haWwiOiIiLCJwaG9uZSI6IiIsImxhc3Rfc2lnbl9pbl9hdCI6IjIwMjUtMDQtMThUMDg6NTc6MTIuNzMxMzg1WiIsImFwcF9tZXRhZGF0YSI6e30sInVzZXJfbWV0YWRhdGEiOnsiaWQiOiIyZmIyNTE4Ny04MTRmLTQwYjAtYjg1MC1jOTQ3M2MwNTkyMWYifSwiaWRlbnRpdGllcyI6W10sImNyZWF0ZWRfYXQiOiIyMDI1LTA0LTE4VDA4OjU3OjEyLjcyODk4NVoiLCJ1cGRhdGVkX2F0IjoiMjAyNS0wNS0xOFQwMzoxOTo0OS4yNzkxNzJaIiwiaXNfYW5vbnltb3VzIjp0cnVlfX0"
36
- self.agent = LitAgent()
37
- self.headers = {
38
- "accept": "*/*",
39
- "content-type": "application/json",
40
- "origin": "https://beta.lmarena.ai",
41
- "referer": "https://arena-api-stable.vercel.app/",
42
- }
43
- if self.supabase_jwt:
44
- self.headers["supabase-jwt"] = self.supabase_jwt
45
- self.session.headers.update(self.headers)
46
- self.session.proxies = proxies
47
- self.__available_optimizers = (
48
- method
49
- for method in dir(Optimizers)
50
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
51
- )
52
- Conversation.intro = (
53
- AwesomePrompts().get_act(
54
- act, raise_not_found=True, default=None, case_insensitive=True
55
- )
56
- if act
57
- else intro or Conversation.intro
58
- )
59
- self.conversation = Conversation(
60
- is_conversation, self.max_tokens_to_sample, filepath, update_file
61
- )
62
- self.conversation.history_offset = history_offset
63
-
64
- @staticmethod
65
- def _arena_extractor(chunk: Union[str, Dict[str, Any]]) -> Optional[str]:
66
- """Extracts content from the arena stream format 'b0:"..."'."""
67
- if isinstance(chunk, str):
68
- match = re.search(r'b0:"(.*?)"', chunk)
69
- if match:
70
- content = match.group(1).encode().decode('unicode_escape')
71
- return content.replace('\\', '\\').replace('\\"', '"')
72
- return None
73
-
74
- def _build_payload(self, prompt: str) -> Dict[str, Any]:
75
- session_id = str(uuid4())
76
- user_msg_id = str(uuid4())
77
- model_a_msg_id = str(uuid4())
78
- model_b_msg_id = str(uuid4())
79
- return {
80
- "id": session_id,
81
- "mode": "battle",
82
- "userMessageId": user_msg_id,
83
- "modelAMessageId": model_a_msg_id,
84
- "modelBMessageId": model_b_msg_id,
85
- "messages": [
86
- {
87
- "id": user_msg_id,
88
- "role": "user",
89
- "content": prompt,
90
- "experimental_attachments": [],
91
- "parentMessageIds": [],
92
- "participantPosition": "a",
93
- "modelId": None,
94
- "evaluationSessionId": session_id,
95
- "status": "pending",
96
- "failureReason": None
97
- }
98
- ],
99
- "modality": "chat"
100
- }
101
-
102
- def ask(
103
- self,
104
- prompt: str,
105
- stream: bool = False,
106
- raw: bool = False,
107
- optimizer: str = None,
108
- conversationally: bool = False,
109
- ) -> Union[Dict[str, Any], Generator]:
110
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
111
- if optimizer:
112
- if optimizer in self.__available_optimizers:
113
- conversation_prompt = getattr(Optimizers, optimizer)(
114
- conversation_prompt if conversationally else prompt
115
- )
116
- else:
117
- raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
118
- payload = self._build_payload(conversation_prompt)
119
- def for_stream():
120
- try:
121
- response = self.session.post(
122
- self.url,
123
- json=payload,
124
- stream=True,
125
- timeout=self.timeout
126
- )
127
- if not response.ok:
128
- raise exceptions.FailedToGenerateResponseError(
129
- f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
130
- )
131
- streaming_text = ""
132
- processed_stream = sanitize_stream(
133
- data=response.iter_content(chunk_size=None),
134
- intro_value=None,
135
- to_json=False,
136
- content_extractor=self._arena_extractor
137
- )
138
- for content_chunk in processed_stream:
139
- if content_chunk and isinstance(content_chunk, str):
140
- streaming_text += content_chunk
141
- yield content_chunk if raw else dict(text=content_chunk)
142
- self.last_response = {"text": streaming_text}
143
- self.conversation.update_chat_history(prompt, streaming_text)
144
- except CurlError as e:
145
- raise exceptions.FailedToGenerateResponseError(f"Request failed (CurlError): {e}")
146
- except Exception as e:
147
- raise exceptions.FailedToGenerateResponseError(f"An unexpected error occurred ({type(e).__name__}): {e}")
148
- def for_non_stream():
149
- full_text = ""
150
- for chunk_data in for_stream():
151
- if isinstance(chunk_data, dict) and "text" in chunk_data:
152
- full_text += chunk_data["text"]
153
- elif isinstance(chunk_data, str):
154
- full_text += chunk_data
155
- self.last_response = {"text": full_text}
156
- return self.last_response
157
- return for_stream() if stream else for_non_stream()
158
-
159
- def chat(
160
- self,
161
- prompt: str,
162
- stream: bool = False,
163
- optimizer: str = None,
164
- conversationally: bool = False,
165
- ) -> Union[str, Generator]:
166
- def for_stream():
167
- for response in self.ask(prompt, True, optimizer=optimizer, conversationally=conversationally):
168
- yield self.get_message(response)
169
- def for_non_stream():
170
- return self.get_message(
171
- self.ask(prompt, False, optimizer=optimizer, conversationally=conversationally)
172
- )
173
- return for_stream() if stream else for_non_stream()
174
-
175
- def get_message(self, response: dict) -> str:
176
- assert isinstance(response, dict), "Response should be of dict data-type only"
177
- return response["text"]
178
-
179
- if __name__ == "__main__":
180
- print("-" * 80)
181
- print(f"{'Model':<20} {'Status':<10} {'Response'}")
182
- print("-" * 80)
183
- try:
184
- test_ai = lmarena(timeout=60)
185
- response = test_ai.chat("Say 'Hello' in one word", stream=True)
186
- response_text = ""
187
- for chunk in response:
188
- response_text += chunk
189
- print(f"\r{'arena-battle':<20} {'Testing...':<10}", end="", flush=True)
190
- if response_text and len(response_text.strip()) > 0:
191
- status = "✓"
192
- display_text = response_text.strip()[:50] + "..." if len(response_text.strip()) > 50 else response_text.strip()
193
- else:
194
- status = "✗"
195
- display_text = "Empty or invalid response"
196
- print(f"\r{'arena-battle':<20} {status:<10} {display_text}")
197
- except Exception as e:
198
- print(f"\r{'arena-battle':<20} {'✗':<10} {str(e)}")