File size: 20,489 Bytes
a4b70d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
from __future__ import annotations
import uuid
import json
from aiohttp import ClientSession, BaseConnector
from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import get_connector
from ..requests import raise_for_status
from ..errors import RateLimitError
models = {
"claude-3-5-sonnet-20241022": {
"id": "claude-3-5-sonnet-20241022",
"name": "claude-3-5-sonnet-20241022",
"model": "claude-3-5-sonnet-20241022",
"provider": "Anthropic",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 25.366666666666667,
},
"claude-3-5-sonnet-20241022-t": {
"id": "claude-3-5-sonnet-20241022-t",
"name": "claude-3-5-sonnet-20241022-t",
"model": "claude-3-5-sonnet-20241022-t",
"provider": "Anthropic",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 39.820754716981135,
},
"claude-3-7-sonnet-20250219": {
"id": "claude-3-7-sonnet-20250219",
"name": "claude-3-7-sonnet-20250219",
"model": "claude-3-7-sonnet-20250219",
"provider": "Anthropic",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 47.02970297029703,
},
"claude-3-7-sonnet-20250219-t": {
"id": "claude-3-7-sonnet-20250219-t",
"name": "claude-3-7-sonnet-20250219-t",
"model": "claude-3-7-sonnet-20250219-t",
"provider": "Anthropic",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 39.04289693593315,
},
"deepseek-v3": {
"id": "deepseek-v3",
"name": "deepseek-v3",
"model": "deepseek-v3",
"provider": "DeepSeek",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 40.484657419083646,
},
"gemini-1.0-pro-latest-123": {
"id": "gemini-1.0-pro-latest-123",
"name": "gemini-1.0-pro-latest-123",
"model": "gemini-1.0-pro-latest-123",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 10,
},
"gemini-2.0-flash": {
"id": "gemini-2.0-flash",
"name": "gemini-2.0-flash",
"model": "gemini-2.0-flash",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 216.44162436548223,
},
"gemini-2.0-flash-exp": {
"id": "gemini-2.0-flash-exp",
"name": "gemini-2.0-flash-exp",
"model": "gemini-2.0-flash-exp",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 0,
"tps": 0,
},
"gemini-2.0-flash-thinking-exp": {
"id": "gemini-2.0-flash-thinking-exp",
"name": "gemini-2.0-flash-thinking-exp",
"model": "gemini-2.0-flash-thinking-exp",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 0,
"tps": 0,
},
"gemini-2.5-flash-preview-04-17": {
"id": "gemini-2.5-flash-preview-04-17",
"name": "gemini-2.5-flash-preview-04-17",
"model": "gemini-2.5-flash-preview-04-17",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 189.84010840108402,
},
"gemini-2.5-pro-official": {
"id": "gemini-2.5-pro-official",
"name": "gemini-2.5-pro-official",
"model": "gemini-2.5-pro-official",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 91.00613496932516,
},
"gemini-2.5-pro-preview-03-25": {
"id": "gemini-2.5-pro-preview-03-25",
"name": "gemini-2.5-pro-preview-03-25",
"model": "gemini-2.5-pro-preview-03-25",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 99.05660377358491,
"tps": 45.050511247443765,
},
"gemini-2.5-pro-preview-05-06": {
"id": "gemini-2.5-pro-preview-05-06",
"name": "gemini-2.5-pro-preview-05-06",
"model": "gemini-2.5-pro-preview-05-06",
"provider": "Google",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 99.29617834394904,
},
"gpt-4-turbo-2024-04-09": {
"id": "gpt-4-turbo-2024-04-09",
"name": "gpt-4-turbo-2024-04-09",
"model": "gpt-4-turbo-2024-04-09",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 1,
},
"gpt-4.1": {
"id": "gpt-4.1",
"name": "gpt-4.1",
"model": "gpt-4.1",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 42.857142857142854,
"tps": 19.58032786885246,
},
"gpt-4.1-mini": {
"id": "gpt-4.1-mini",
"name": "gpt-4.1-mini",
"model": "gpt-4.1-mini",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 68.75,
"tps": 12.677576601671309,
},
"gpt-4.1-mini-2025-04-14": {
"id": "gpt-4.1-mini-2025-04-14",
"name": "gpt-4.1-mini-2025-04-14",
"model": "gpt-4.1-mini-2025-04-14",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 94.23076923076923,
"tps": 8.297687861271676,
},
"gpt-4o-2024-11-20": {
"id": "gpt-4o-2024-11-20",
"name": "gpt-4o-2024-11-20",
"model": "gpt-4o-2024-11-20",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 73.3955223880597,
},
"gpt-4o-mini-2024-07-18": {
"id": "gpt-4o-mini-2024-07-18",
"name": "gpt-4o-mini-2024-07-18",
"model": "gpt-4o-mini-2024-07-18",
"provider": "OpenAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 26.874455100261553,
},
"grok-3": {
"id": "grok-3",
"name": "grok-3",
"model": "grok-3",
"provider": "xAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 51.110652663165794,
},
"grok-3-reason": {
"id": "grok-3-reason",
"name": "grok-3-reason",
"model": "grok-3-reason",
"provider": "xAI",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 62.81976744186046,
},
"o3-mini-2025-01-31": {
"id": "o3-mini-2025-01-31",
"name": "o3-mini-2025-01-31",
"model": "o3-mini-2025-01-31",
"provider": "Unknown",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 125.31410256410257,
},
"qwen3-235b-a22b": {
"id": "qwen3-235b-a22b",
"name": "qwen3-235b-a22b",
"model": "qwen3-235b-a22b",
"provider": "Alibaba",
"maxLength": 0,
"tokenLimit": 0,
"context": 0,
"success_rate": 100,
"tps": 25.846153846153847,
},
}
class Liaobots(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://liaobots.work"
working = True
supports_message_history = True
supports_system_message = True
default_model = "grok-3"
models = list(models.keys())
model_aliases = {
# Anthropic
"claude-3.5-sonnet": "claude-3-5-sonnet-20241022",
"claude-3.5-sonnet": "claude-3-5-sonnet-20241022-t",
"claude-3.7-sonnet": "claude-3-7-sonnet-20250219",
"claude-3.7-sonnet": "claude-3-7-sonnet-20250219-t",
# DeepSeek
#"deepseek-v3": "deepseek-v3",
# Google
"gemini-1.0-pro": "gemini-1.0-pro-latest-123",
"gemini-2.0-flash": "gemini-2.0-flash-exp",
"gemini-2.0-flash-thinking": "gemini-2.0-flash-thinking-exp",
"gemini-2.5-flash": "gemini-2.5-flash-preview-04-17",
"gemini-2.5-pro": "gemini-2.5-pro-official",
"gemini-2.5-pro": "gemini-2.5-pro-preview-03-25",
"gemini-2.5-pro": "gemini-2.5-pro-preview-05-06",
# OpenAI
"gpt-4-turbo": "gpt-4-turbo-2024-04-09",
"gpt-4.1-mini": "gpt-4.1-mini-2025-04-14",
"gpt-4": "gpt-4o-2024-11-20",
"gpt-4o": "gpt-4o-2024-11-20",
"gpt-4o-mini": "gpt-4o-mini-2024-07-18",
# xAI
"grok-3-reason": "grok-3-reason",
"o3-mini": "o3-mini-2025-01-31",
"qwen-3-235b": "qwen3-235b-a22b",
}
_auth_code = None
_cookie_jar = None
@classmethod
def is_supported(cls, model: str) -> bool:
"""
Check if the given model is supported.
"""
return model in models or model in cls.model_aliases
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
connector: BaseConnector = None,
**kwargs
) -> AsyncResult:
model = cls.get_model(model)
headers = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json",
"dnt": "1",
"origin": "https://liaobots.work",
"priority": "u=1, i",
"referer": "https://liaobots.work/en",
"sec-ch-ua": "\"Chromium\";v=\"135\", \"Not-A.Brand\";v=\"8\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Linux\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
}
async with ClientSession(
headers=headers,
cookie_jar=cls._cookie_jar,
connector=get_connector(connector, proxy, True)
) as session:
# First, get a valid auth code
await cls.get_auth_code(session)
# Create conversation ID
conversation_id = str(uuid.uuid4())
# Prepare request data
data = {
"conversationId": conversation_id,
"models": [{
"modelId": model,
"provider": models[model]["provider"]
}],
"search": "false",
"messages": messages,
"key": "",
"prompt": kwargs.get("system_message", "你是 {{model}},一个由 {{provider}} 训练的大型语言模型,请仔细遵循用户的指示。")
}
# Try to make the chat request
try:
# Make the chat request with the current auth code
async with session.post(
f"{cls.url}/api/chat",
json=data,
headers={"x-auth-code": cls._auth_code},
ssl=False
) as response:
# Check if we got a streaming response
content_type = response.headers.get("Content-Type", "")
if "text/event-stream" in content_type:
async for line in response.content:
if line.startswith(b"data: "):
try:
response_data = json.loads(line[6:])
# Check for error response
if response_data.get("error") is True:
# Raise RateLimitError for payment required or other errors
if "402" in str(response_data.get("res_status", "")):
raise RateLimitError("This model requires payment or credits")
else:
error_msg = response_data.get('message', 'Unknown error')
raise RateLimitError(f"Error: {error_msg}")
# Process normal response
if response_data.get("role") == "assistant" and "content" in response_data:
content = response_data.get("content")
yield content
except json.JSONDecodeError:
continue
else:
# Not a streaming response, might be an error or HTML
response_text = await response.text()
# If we got HTML, we need to bypass CAPTCHA
if response_text.startswith("<!DOCTYPE html>"):
await cls.bypass_captcha(session)
# Get a fresh auth code
await cls.get_auth_code(session)
# Try the request again
async with session.post(
f"{cls.url}/api/chat",
json=data,
headers={"x-auth-code": cls._auth_code},
ssl=False
) as response2:
# Check if we got a streaming response
content_type = response2.headers.get("Content-Type", "")
if "text/event-stream" in content_type:
async for line in response2.content:
if line.startswith(b"data: "):
try:
response_data = json.loads(line[6:])
# Check for error response
if response_data.get("error") is True:
# Raise RateLimitError for payment required or other errors
if "402" in str(response_data.get("res_status", "")):
raise RateLimitError("This model requires payment or credits")
else:
error_msg = response_data.get('message', 'Unknown error')
raise RateLimitError(f"Error: {error_msg}")
# Process normal response
if response_data.get("role") == "assistant" and "content" in response_data:
content = response_data.get("content")
yield content
except json.JSONDecodeError:
continue
else:
raise RateLimitError("Failed to get streaming response")
else:
raise RateLimitError("Failed to connect to the service")
except Exception as e:
# If it's already a RateLimitError, re-raise it
if isinstance(e, RateLimitError):
raise
# Otherwise, wrap it in a RateLimitError
raise RateLimitError(f"Error processing request: {str(e)}")
@classmethod
async def bypass_captcha(cls, session: ClientSession) -> None:
"""
Bypass the CAPTCHA verification by directly making the recaptcha API request.
"""
try:
# First, try the direct recaptcha API request
async with session.post(
f"{cls.url}/recaptcha/api/login",
json={"token": "abcdefghijklmnopqrst"},
ssl=False
) as response:
if response.status == 200:
try:
response_text = await response.text()
# Try to parse as JSON
try:
response_data = json.loads(response_text)
# Check if we got a successful response
if response_data.get("code") == 200:
cls._cookie_jar = session.cookie_jar
except json.JSONDecodeError:
pass
except Exception:
pass
except Exception:
pass
@classmethod
async def get_auth_code(cls, session: ClientSession) -> None:
"""
Get a valid auth code by sending a request with an empty authcode.
"""
try:
# Send request with empty authcode to get a new one
auth_request_data = {
"authcode": "",
"recommendUrl": "https://liaobots.work/zh"
}
async with session.post(
f"{cls.url}/api/user",
json=auth_request_data,
ssl=False
) as response:
if response.status == 200:
response_text = await response.text()
try:
response_data = json.loads(response_text)
if "authCode" in response_data:
cls._auth_code = response_data["authCode"]
cls._cookie_jar = session.cookie_jar
return
except json.JSONDecodeError:
# If we got HTML, it might be the CAPTCHA page
if response_text.startswith("<!DOCTYPE html>"):
await cls.bypass_captcha(session)
# Try again after bypassing CAPTCHA
async with session.post(
f"{cls.url}/api/user",
json=auth_request_data,
ssl=False
) as response2:
if response2.status == 200:
response_text2 = await response2.text()
try:
response_data2 = json.loads(response_text2)
if "authCode" in response_data2:
cls._auth_code = response_data2["authCode"]
cls._cookie_jar = session.cookie_jar
return
except json.JSONDecodeError:
pass
except Exception:
pass
# If we're here, we couldn't get a valid auth code
# Set a default one as a fallback
cls._auth_code = "DvS3A5GTE9f0D" # Fallback to one of the provided auth codes
|