webscout 8.3.6__py3-none-any.whl → 2025.10.11__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/AIauto.py +250 -250
- webscout/AIbase.py +379 -379
- webscout/AIutel.py +60 -58
- webscout/Bard.py +1012 -1012
- webscout/Bing_search.py +417 -417
- webscout/DWEBS.py +529 -529
- webscout/Extra/Act.md +309 -309
- webscout/Extra/GitToolkit/__init__.py +10 -10
- webscout/Extra/GitToolkit/gitapi/README.md +110 -110
- webscout/Extra/GitToolkit/gitapi/__init__.py +11 -11
- webscout/Extra/GitToolkit/gitapi/repository.py +195 -195
- webscout/Extra/GitToolkit/gitapi/user.py +96 -96
- webscout/Extra/GitToolkit/gitapi/utils.py +61 -61
- webscout/Extra/YTToolkit/README.md +375 -375
- webscout/Extra/YTToolkit/YTdownloader.py +956 -956
- webscout/Extra/YTToolkit/__init__.py +2 -2
- webscout/Extra/YTToolkit/transcriber.py +475 -475
- webscout/Extra/YTToolkit/ytapi/README.md +44 -44
- webscout/Extra/YTToolkit/ytapi/__init__.py +6 -6
- webscout/Extra/YTToolkit/ytapi/channel.py +307 -307
- webscout/Extra/YTToolkit/ytapi/errors.py +13 -13
- webscout/Extra/YTToolkit/ytapi/extras.py +118 -118
- webscout/Extra/YTToolkit/ytapi/https.py +88 -88
- webscout/Extra/YTToolkit/ytapi/patterns.py +61 -61
- webscout/Extra/YTToolkit/ytapi/playlist.py +58 -58
- webscout/Extra/YTToolkit/ytapi/pool.py +7 -7
- webscout/Extra/YTToolkit/ytapi/query.py +39 -39
- webscout/Extra/YTToolkit/ytapi/stream.py +62 -62
- webscout/Extra/YTToolkit/ytapi/utils.py +62 -62
- webscout/Extra/YTToolkit/ytapi/video.py +232 -232
- webscout/Extra/autocoder/__init__.py +9 -9
- webscout/Extra/autocoder/autocoder.py +1105 -1105
- webscout/Extra/autocoder/autocoder_utiles.py +332 -332
- webscout/Extra/gguf.md +429 -429
- webscout/Extra/gguf.py +1213 -1213
- webscout/Extra/tempmail/README.md +487 -487
- webscout/Extra/tempmail/__init__.py +27 -27
- webscout/Extra/tempmail/async_utils.py +140 -140
- webscout/Extra/tempmail/base.py +160 -160
- webscout/Extra/tempmail/cli.py +186 -186
- webscout/Extra/tempmail/emailnator.py +84 -84
- webscout/Extra/tempmail/mail_tm.py +360 -360
- webscout/Extra/tempmail/temp_mail_io.py +291 -291
- webscout/Extra/weather.md +281 -281
- webscout/Extra/weather.py +193 -193
- webscout/Litlogger/README.md +10 -10
- webscout/Litlogger/__init__.py +15 -15
- webscout/Litlogger/formats.py +13 -13
- webscout/Litlogger/handlers.py +121 -121
- webscout/Litlogger/levels.py +13 -13
- webscout/Litlogger/logger.py +134 -134
- webscout/Provider/AISEARCH/Perplexity.py +332 -332
- webscout/Provider/AISEARCH/README.md +279 -279
- webscout/Provider/AISEARCH/__init__.py +33 -11
- webscout/Provider/AISEARCH/felo_search.py +206 -206
- webscout/Provider/AISEARCH/genspark_search.py +323 -323
- webscout/Provider/AISEARCH/hika_search.py +185 -185
- webscout/Provider/AISEARCH/iask_search.py +410 -410
- webscout/Provider/AISEARCH/monica_search.py +219 -219
- webscout/Provider/AISEARCH/scira_search.py +316 -314
- webscout/Provider/AISEARCH/stellar_search.py +177 -177
- webscout/Provider/AISEARCH/webpilotai_search.py +255 -255
- webscout/Provider/Aitopia.py +314 -315
- webscout/Provider/Andi.py +3 -3
- webscout/Provider/Apriel.py +306 -0
- webscout/Provider/ChatGPTClone.py +236 -236
- webscout/Provider/ChatSandbox.py +343 -342
- webscout/Provider/Cloudflare.py +324 -324
- webscout/Provider/Cohere.py +208 -207
- webscout/Provider/Deepinfra.py +370 -369
- webscout/Provider/ExaAI.py +260 -260
- webscout/Provider/ExaChat.py +308 -387
- webscout/Provider/Flowith.py +221 -221
- webscout/Provider/GMI.py +293 -0
- webscout/Provider/Gemini.py +164 -162
- webscout/Provider/GeminiProxy.py +167 -166
- webscout/Provider/GithubChat.py +371 -370
- webscout/Provider/Groq.py +800 -800
- webscout/Provider/HeckAI.py +383 -379
- webscout/Provider/Jadve.py +282 -297
- webscout/Provider/K2Think.py +308 -0
- webscout/Provider/Koboldai.py +206 -384
- webscout/Provider/LambdaChat.py +423 -425
- webscout/Provider/Nemotron.py +244 -245
- webscout/Provider/Netwrck.py +248 -247
- webscout/Provider/OLLAMA.py +395 -394
- webscout/Provider/OPENAI/Cloudflare.py +394 -395
- webscout/Provider/OPENAI/FalconH1.py +452 -457
- webscout/Provider/OPENAI/FreeGemini.py +297 -299
- webscout/Provider/OPENAI/{monochat.py → K2Think.py} +432 -329
- webscout/Provider/OPENAI/NEMOTRON.py +241 -244
- webscout/Provider/OPENAI/PI.py +428 -427
- webscout/Provider/OPENAI/README.md +959 -959
- webscout/Provider/OPENAI/TogetherAI.py +345 -345
- webscout/Provider/OPENAI/TwoAI.py +466 -467
- webscout/Provider/OPENAI/__init__.py +33 -59
- webscout/Provider/OPENAI/ai4chat.py +313 -303
- webscout/Provider/OPENAI/base.py +249 -269
- webscout/Provider/OPENAI/chatglm.py +528 -0
- webscout/Provider/OPENAI/chatgpt.py +593 -588
- webscout/Provider/OPENAI/chatgptclone.py +521 -524
- webscout/Provider/OPENAI/chatsandbox.py +202 -177
- webscout/Provider/OPENAI/deepinfra.py +319 -315
- webscout/Provider/OPENAI/e2b.py +1665 -1665
- webscout/Provider/OPENAI/exaai.py +420 -420
- webscout/Provider/OPENAI/exachat.py +452 -452
- webscout/Provider/OPENAI/friendli.py +232 -232
- webscout/Provider/OPENAI/{refact.py → gmi.py} +324 -274
- webscout/Provider/OPENAI/groq.py +364 -364
- webscout/Provider/OPENAI/heckai.py +314 -311
- webscout/Provider/OPENAI/llmchatco.py +337 -337
- webscout/Provider/OPENAI/netwrck.py +355 -354
- webscout/Provider/OPENAI/oivscode.py +290 -290
- webscout/Provider/OPENAI/opkfc.py +518 -518
- webscout/Provider/OPENAI/pydantic_imports.py +1 -1
- webscout/Provider/OPENAI/scirachat.py +535 -529
- webscout/Provider/OPENAI/sonus.py +308 -308
- webscout/Provider/OPENAI/standardinput.py +442 -442
- webscout/Provider/OPENAI/textpollinations.py +340 -348
- webscout/Provider/OPENAI/toolbaz.py +419 -413
- webscout/Provider/OPENAI/typefully.py +362 -362
- webscout/Provider/OPENAI/utils.py +295 -295
- webscout/Provider/OPENAI/venice.py +436 -436
- webscout/Provider/OPENAI/wisecat.py +387 -387
- webscout/Provider/OPENAI/writecream.py +166 -166
- webscout/Provider/OPENAI/x0gpt.py +378 -378
- webscout/Provider/OPENAI/yep.py +389 -389
- webscout/Provider/OpenGPT.py +230 -230
- webscout/Provider/Openai.py +244 -496
- webscout/Provider/PI.py +405 -404
- webscout/Provider/Perplexitylabs.py +430 -431
- webscout/Provider/QwenLM.py +272 -254
- webscout/Provider/STT/__init__.py +32 -2
- webscout/Provider/{Llama3.py → Sambanova.py} +257 -258
- webscout/Provider/StandardInput.py +309 -309
- webscout/Provider/TTI/README.md +82 -82
- webscout/Provider/TTI/__init__.py +33 -12
- webscout/Provider/TTI/aiarta.py +413 -413
- webscout/Provider/TTI/base.py +136 -136
- webscout/Provider/TTI/bing.py +243 -243
- webscout/Provider/TTI/gpt1image.py +149 -149
- webscout/Provider/TTI/imagen.py +196 -196
- webscout/Provider/TTI/infip.py +211 -211
- webscout/Provider/TTI/magicstudio.py +232 -232
- webscout/Provider/TTI/monochat.py +219 -219
- webscout/Provider/TTI/piclumen.py +214 -214
- webscout/Provider/TTI/pixelmuse.py +232 -232
- webscout/Provider/TTI/pollinations.py +232 -232
- webscout/Provider/TTI/together.py +288 -288
- webscout/Provider/TTI/utils.py +12 -12
- webscout/Provider/TTI/venice.py +367 -367
- webscout/Provider/TTS/README.md +192 -192
- webscout/Provider/TTS/__init__.py +33 -10
- webscout/Provider/TTS/parler.py +110 -110
- webscout/Provider/TTS/streamElements.py +333 -333
- webscout/Provider/TTS/utils.py +280 -280
- webscout/Provider/TeachAnything.py +237 -236
- webscout/Provider/TextPollinationsAI.py +311 -318
- webscout/Provider/TogetherAI.py +356 -357
- webscout/Provider/TwoAI.py +313 -569
- webscout/Provider/TypliAI.py +312 -311
- webscout/Provider/UNFINISHED/ChatHub.py +208 -208
- webscout/Provider/UNFINISHED/ChutesAI.py +313 -313
- webscout/Provider/{GizAI.py → UNFINISHED/GizAI.py} +294 -294
- webscout/Provider/{Marcus.py → UNFINISHED/Marcus.py} +198 -198
- webscout/Provider/{Qodo.py → UNFINISHED/Qodo.py} +477 -477
- webscout/Provider/UNFINISHED/VercelAIGateway.py +338 -338
- webscout/Provider/{XenAI.py → UNFINISHED/XenAI.py} +324 -324
- webscout/Provider/UNFINISHED/Youchat.py +330 -330
- webscout/Provider/UNFINISHED/liner.py +334 -0
- webscout/Provider/UNFINISHED/liner_api_request.py +262 -262
- webscout/Provider/UNFINISHED/puterjs.py +634 -634
- webscout/Provider/UNFINISHED/samurai.py +223 -223
- webscout/Provider/UNFINISHED/test_lmarena.py +119 -119
- webscout/Provider/Venice.py +251 -250
- webscout/Provider/VercelAI.py +256 -255
- webscout/Provider/WiseCat.py +232 -231
- webscout/Provider/WrDoChat.py +367 -366
- webscout/Provider/__init__.py +33 -86
- webscout/Provider/ai4chat.py +174 -174
- webscout/Provider/akashgpt.py +331 -334
- webscout/Provider/cerebras.py +446 -340
- webscout/Provider/chatglm.py +394 -214
- webscout/Provider/cleeai.py +211 -212
- webscout/Provider/deepseek_assistant.py +1 -1
- webscout/Provider/elmo.py +282 -282
- webscout/Provider/geminiapi.py +208 -208
- webscout/Provider/granite.py +261 -261
- webscout/Provider/hermes.py +263 -265
- webscout/Provider/julius.py +223 -222
- webscout/Provider/learnfastai.py +309 -309
- webscout/Provider/llama3mitril.py +214 -214
- webscout/Provider/llmchat.py +243 -243
- webscout/Provider/llmchatco.py +290 -290
- webscout/Provider/meta.py +801 -801
- webscout/Provider/oivscode.py +309 -309
- webscout/Provider/scira_chat.py +384 -457
- webscout/Provider/searchchat.py +292 -291
- webscout/Provider/sonus.py +258 -258
- webscout/Provider/toolbaz.py +370 -364
- webscout/Provider/turboseek.py +274 -265
- webscout/Provider/typefully.py +208 -207
- webscout/Provider/x0gpt.py +1 -0
- webscout/Provider/yep.py +372 -371
- webscout/__init__.py +30 -31
- webscout/__main__.py +5 -5
- webscout/auth/api_key_manager.py +189 -189
- webscout/auth/config.py +175 -175
- webscout/auth/models.py +185 -185
- webscout/auth/routes.py +664 -664
- webscout/auth/simple_logger.py +236 -236
- webscout/cli.py +523 -523
- webscout/conversation.py +438 -438
- webscout/exceptions.py +361 -361
- webscout/litagent/Readme.md +298 -298
- webscout/litagent/__init__.py +28 -28
- webscout/litagent/agent.py +581 -581
- webscout/litagent/constants.py +59 -59
- webscout/litprinter/__init__.py +58 -58
- webscout/models.py +181 -181
- webscout/optimizers.py +419 -419
- webscout/prompt_manager.py +288 -288
- webscout/sanitize.py +1078 -1078
- webscout/scout/README.md +401 -401
- webscout/scout/__init__.py +8 -8
- webscout/scout/core/__init__.py +6 -6
- webscout/scout/core/crawler.py +297 -297
- webscout/scout/core/scout.py +706 -706
- webscout/scout/core/search_result.py +95 -95
- webscout/scout/core/text_analyzer.py +62 -62
- webscout/scout/core/text_utils.py +277 -277
- webscout/scout/core/web_analyzer.py +51 -51
- webscout/scout/element.py +599 -599
- webscout/scout/parsers/__init__.py +69 -69
- webscout/scout/parsers/html5lib_parser.py +172 -172
- webscout/scout/parsers/html_parser.py +236 -236
- webscout/scout/parsers/lxml_parser.py +178 -178
- webscout/scout/utils.py +37 -37
- webscout/swiftcli/Readme.md +323 -323
- webscout/swiftcli/__init__.py +95 -95
- webscout/swiftcli/core/__init__.py +7 -7
- webscout/swiftcli/core/cli.py +308 -308
- webscout/swiftcli/core/context.py +104 -104
- webscout/swiftcli/core/group.py +241 -241
- webscout/swiftcli/decorators/__init__.py +28 -28
- webscout/swiftcli/decorators/command.py +221 -221
- webscout/swiftcli/decorators/options.py +220 -220
- webscout/swiftcli/decorators/output.py +302 -302
- webscout/swiftcli/exceptions.py +21 -21
- webscout/swiftcli/plugins/__init__.py +9 -9
- webscout/swiftcli/plugins/base.py +135 -135
- webscout/swiftcli/plugins/manager.py +269 -269
- webscout/swiftcli/utils/__init__.py +59 -59
- webscout/swiftcli/utils/formatting.py +252 -252
- webscout/swiftcli/utils/parsing.py +267 -267
- webscout/update_checker.py +117 -117
- webscout/version.py +1 -1
- webscout/webscout_search.py +1183 -1183
- webscout/webscout_search_async.py +649 -649
- webscout/yep_search.py +346 -346
- webscout/zeroart/README.md +89 -89
- webscout/zeroart/__init__.py +134 -134
- webscout/zeroart/base.py +66 -66
- webscout/zeroart/effects.py +100 -100
- webscout/zeroart/fonts.py +1238 -1238
- {webscout-8.3.6.dist-info → webscout-2025.10.11.dist-info}/METADATA +937 -936
- webscout-2025.10.11.dist-info/RECORD +300 -0
- webscout/Provider/AISEARCH/DeepFind.py +0 -254
- webscout/Provider/AllenAI.py +0 -440
- webscout/Provider/Blackboxai.py +0 -793
- webscout/Provider/FreeGemini.py +0 -250
- webscout/Provider/GptOss.py +0 -207
- webscout/Provider/Hunyuan.py +0 -283
- webscout/Provider/Kimi.py +0 -445
- webscout/Provider/MCPCore.py +0 -322
- webscout/Provider/MiniMax.py +0 -207
- webscout/Provider/OPENAI/BLACKBOXAI.py +0 -1045
- webscout/Provider/OPENAI/MiniMax.py +0 -298
- webscout/Provider/OPENAI/Qwen3.py +0 -304
- webscout/Provider/OPENAI/autoproxy.py +0 -1067
- webscout/Provider/OPENAI/copilot.py +0 -321
- webscout/Provider/OPENAI/gptoss.py +0 -288
- webscout/Provider/OPENAI/kimi.py +0 -469
- webscout/Provider/OPENAI/mcpcore.py +0 -431
- webscout/Provider/OPENAI/multichat.py +0 -378
- webscout/Provider/OPENAI/qodo.py +0 -630
- webscout/Provider/OPENAI/xenai.py +0 -514
- webscout/Provider/Reka.py +0 -214
- webscout/Provider/UNFINISHED/fetch_together_models.py +0 -90
- webscout/Provider/asksteve.py +0 -220
- webscout/Provider/copilot.py +0 -441
- webscout/Provider/freeaichat.py +0 -294
- webscout/Provider/koala.py +0 -182
- webscout/Provider/lmarena.py +0 -198
- webscout/Provider/monochat.py +0 -275
- webscout/Provider/multichat.py +0 -375
- webscout/Provider/scnet.py +0 -244
- webscout/Provider/talkai.py +0 -194
- webscout/tempid.py +0 -128
- webscout-8.3.6.dist-info/RECORD +0 -327
- {webscout-8.3.6.dist-info → webscout-2025.10.11.dist-info}/WHEEL +0 -0
- {webscout-8.3.6.dist-info → webscout-2025.10.11.dist-info}/entry_points.txt +0 -0
- {webscout-8.3.6.dist-info → webscout-2025.10.11.dist-info}/licenses/LICENSE.md +0 -0
- {webscout-8.3.6.dist-info → webscout-2025.10.11.dist-info}/top_level.txt +0 -0
webscout/Provider/freeaichat.py
DELETED
|
@@ -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)}")
|
webscout/Provider/koala.py
DELETED
|
@@ -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)
|
webscout/Provider/lmarena.py
DELETED
|
@@ -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)}")
|