File size: 26,930 Bytes
033253e b0316b9 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e 3ef5e4f 033253e | 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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | # server_sse.py
import asyncio
import inspect
import logging
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, AsyncGenerator,Tuple
import time
import json
import uuid
import aiohttp
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse, JSONResponse
from curl_cffi.requests import Session
# try fast json library
try:
import orjson as _jsonlib # type: ignore
def _loads(b: bytes):
return _jsonlib.loads(b)
def _dumps(obj) -> str:
# orjson.dumps returns bytes
return _jsonlib.dumps(obj).decode("utf-8")
except Exception:
import json as _jsonlib # type: ignore
def _loads(b: bytes):
return _jsonlib.loads(b)
def _dumps(obj) -> str:
return _jsonlib.dumps(obj)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("chat-server-sse")
# preserve this global Requests (unchanged)
Requests = Session(impersonate="chrome110")
app = FastAPI()
M2 = [
{
"tag": "@cf",
"model": "meta/llama-3.1-70b-instruct",
"max_tokens" : 8192
},
{
"tag": "@cf",
"model": "qwen/qwen2.5-coder-32b-instruct",
"max_tokens" : 8192
},
{
"tag": "@cf",
"model": "deepseek-ai/deepseek-r1-distill-qwen-32b",
"max_tokens" : 40960
# ok
},
{
"tag": "@cf",
"model": "meta/llama-4-scout-17b-16e-instruct",
"max_tokens" : 40960
# ok
},
{
"tag": "@cf",
"model": "google/gemma-3-12b-it",
"max_tokens" : 40960
# ok
},
{
"tag": "@cf",
"model": "mistralai/mistral-small-3.1-24b-instruct",
"max_tokens" : 40960
# ok
},
{
"tag": "@cf",
"model": "meta/llama-3.3-70b-instruct-fp8-fast",
"max_tokens" : 8192
},
{
"tag": "@cf",
"model": "meta/llama-3.2-3b-instruct",
"max_tokens" : 40960
# ok
},
{
"tag": "@cf",
"model": "meta/llama-3.2-1b-instruct",
"max_tokens" : 40960
# ok
},
{
"tag": "@hf",
"model": "meta-llama/meta-llama-3-8b-instruct",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-3-8b-instruct",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-2-7b-chat-int8",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-2-7b-chat-fp16",
"max_tokens" : None
},
{
"tag": "@cf",
"model": "meta/llama-3-8b-instruct-awq",
"max_tokens" : 4391
},
{
"tag": "@hf",
"model": "meta-llama/meta-llama-3-8b-instruct",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-3-8b-instruct",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-2-7b-chat-int8",
"max_tokens" : 4391
},
{
"tag": "@cf",
"model": "meta/llama-3-8b-instruct-awq",
"max_tokens" : 4391
},
{
"tag": "@hf",
"model": "google/gemma-7b-it",
"max_tokens" : None
},
{
"tag": "@cf",
"model": "google/gemma-2b-it-lora",
"max_tokens" : 4391
},
{
"tag": "@hf",
"model": "mistral/mistral-7b-instruct-v0.2",
"max_tokens" : 8192
},
{
"tag": "@cf",
"model": "mistral/mistral-7b-instruct-v0.2-lora",
"max_tokens" : 8192
}
]
def FREEGPT(
RQ : Any,
messages : List[Dict],
model : str = "deepseek-ai/deepseek-r1-distill-qwen-32b",
max_token : int = 40960,
stream : bool = True,
timeout: Optional[float] = None
):
md = next((item["tag"] + "/" + item["model"] for item in M2 if item["model"] == model), "@cf/meta/llama-3.2-1b-instruct")
URL = f"https://llmchat.in/inference/stream?model={md}"
headers = {
"Accept": "text/event-stream,*/*",
"Content-Type": "application/json",
"Origin": "https://llmchat.in",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
"Cache-Control": "no-cache",
"Accept-Encoding": "identity",
"cf-ray" : "9cba9edd9f909aaf-SIN",
}
payload = {
"messages": messages,
"stream": stream,
**({"max_tokens": max_token} if max_token is not None else {}),
**({"max_tokens": next((item["max_tokens"] for item in M2 if item["model"] == model and item["max_tokens"] is not None), None)} if next((True for item in M2 if item["model"] == model and item["max_tokens"] is not None), None) else {})
}
# print(payload)
try:
RESP = RQ.post(url=URL,json=payload , headers=headers , timeout=timeout,stream=stream)
print(RESP.status_code)
except:
return
if RESP.status_code == 200:
for raw in RESP.iter_lines():
if not raw:
continue
try:
line = raw.decode("utf-8", errors="replace").strip()
except Exception:
line = raw.decode("latin-1", errors="replace").strip()
if line.startswith("data:"):
data_json = line.split('data: ')[1]
try:
data = json.loads(data_json)
except:
continue
try:
yield data["response"]
except: pass
else:
print(RESP.status_code)
class CONV:
def __init__(self, default_system: str = ""):
self.default_system = default_system
@staticmethod
def _make_id() -> str:
return uuid.uuid4().hex[:20]
def alpaca_to_msg(
self,
alpaca_obj: Dict[str, Any],
insert_system: bool = True,
system_override: Optional[str] = None,
skip_empty: bool = True,
) -> Tuple[List[Dict[str, str]], float]:
t0 = time.perf_counter()
out: List[Dict[str, str]] = []
sys_text = system_override if system_override is not None else self.default_system
if insert_system and sys_text is not None:
out.append({"role": "system", "content": sys_text})
msgs = alpaca_obj
append = out.append # micro-optimization
for m in msgs:
role = (m.get("role") or "").strip().lower()
if role not in ("user", "assistant", "system"):
role = "user"
parts = m.get("parts") or []
# gather textual parts quickly
texts: List[str] = []
for p in parts: # iterate in order
# only include parts with type == "text" and non-empty text
if isinstance(p, dict) and p.get("type") == "text":
txt = p.get("text", "")
if isinstance(txt, str) and txt:
# keep as-is except trim trailing spaces/newlines
texts.append(txt.rstrip())
if not texts and skip_empty:
continue
if texts:
content = "\n\n".join(texts)
append({"role": role, "content": content})
else:
# if not skipping empty, include empty content to preserve role
append({"role": role, "content": ""})
elapsed = time.perf_counter() - t0
return out, elapsed
def msg_to_alpaca(
self,
msg_list: List[Dict[str, Any]],
include_step_start: bool = True,
assistant_state_done: bool = True,
preserve_ids: bool = False,
skip_empty_text_parts: bool = False,
) -> Tuple[Dict[str, List[Dict[str, Any]]], float]:
t0 = time.perf_counter()
out_messages: List[Dict[str, Any]] = []
append = out_messages.append
for entry in msg_list:
# allow both dicts and fallback strings
if not isinstance(entry, dict):
role = "user"
content = str(entry)
entry_id = None
else:
role = (entry.get("role") or "user").strip().lower()
content = entry.get("content", "")
entry_id = entry.get("id") if preserve_ids else None
if role not in ("user", "assistant"):
role = "user"
parts: List[Dict[str, Any]] = []
if role == "assistant" and include_step_start:
parts.append({"type": "step-start"})
# Only add the text part if it's non-empty (or skip_empty_text_parts False)
if isinstance(content, str):
if not skip_empty_text_parts or content.strip() != "":
text_part: Dict[str, Any] = {"type": "text", "text": content}
if role == "assistant" and assistant_state_done:
text_part["state"] = "done"
parts.append(text_part)
# Build message object
msg_obj: Dict[str, Any] = {
"id": entry_id if (entry_id is not None and isinstance(entry_id, str) and entry_id != "") else self._make_id(),
"role": role,
"parts": parts,
"metadata": {"custom": {}},
}
append(msg_obj)
elapsed = time.perf_counter() - t0
return out_messages, elapsed
M1=[
"zai-org/glm-4.6",
"openai/gpt-5-nano-2025-08-07",
"deepseek-ai/deepseek-v3.2-thinking",
"nvidia/nvidia-nemotron-3-nano-30b-a3b",
"nvidia/nvidia-nemotron-3-nano-30b-a3b-thinking",
"openai/gpt-5-mini-2025-08-07",
"qwen/qwen3-vl-235b-a22b-thinking",
"qwen/qwen3-vl-235b-a22b-instruct",
"perplexity/sonar",
"moonshotai/kimi-k2.5",
"anthropic/claude-haiku-4-5-20251001", #-----depcriating model
"google/gemini-2.5-flash-lite",
"moonshotai/kimi-k2-thinking"
"mistralai/devstral-2-123b-instruct-2512" #good mordal
"mistralai/mistral-large-3-675b-instruct-2512",
"openai/gpt-oss-safeguard-20b",
"openai/gpt-oss-120b"
]
def Adarsh_Personal(
RQ : Any,
messages : List[Dict],
model : str = "deepseek-ai/deepseek-r1-distill-qwen-32b",
max_token : int = 40960,
stream : bool = True,
timeout: Optional[float] = None
):
RES=False
URL = "https://hadadxyz-ai.hf.space/api/mz1a85y5n80zy5127hgsba5f3a9c2d1Np0x300vcgduqxb7ep084fygd016c9a2d16fa8b3c41gut432pvjctr75hhspjae25d6f7a8b9c0d1e2pjf43v16f3a4b5c6dd7e8fba2bdx9a0b6dv1c2d7e2b4c9f83d6a4f1bb6c152f9pe3c7a88qv5d91f3c2b765g134bp9a41ne4yx4b3vda8w074"
NEW_MSGS , S = CONV().msg_to_alpaca(messages, include_step_start=True, assistant_state_done=True)
# print(NEW_MSGS)
payload = {
"tools": {},
"modelId": model,
"sessionId": "sess_7ef524b9_mlfe4ped",
"clientId": "7ef524b98a963b507ec9f4000fdea38c-mlfe4pea",
"requestId": "req_7ef524b9_mlfg1cpq_jjxb7p",
"clientIp": "122.161.52.54",
"realIp": "122.161.52.54",
"forwardedFor": "122.161.52.54",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
"id": "DEFAULT_THREAD_ID",
"messages": NEW_MSGS,
"trigger": "submit-message",
"metadata": {}
}
headers = {
"Accept": "text/event-stream, */*",
"Content-Type": "application/json",
"Origin": "https://hadadxyz-ai.hf.space",
"User-Agent": payload["userAgent"],
"Cache-Control": "no-cache",
"Accept-Encoding": "identity",
"x-turnstile-token": "mlfe5357-zq9depfzhpb-e18cbvzrpid",
"x-turnstile-verified": "true",
}
RESP = RQ.post(URL, json=payload, headers=headers, stream=stream, timeout=timeout)
if RESP.status_code == 200:
for raw in RESP.iter_lines():
if not raw:
continue
try:
line = raw.decode("utf-8", errors="replace").strip()
except Exception:
line = raw.decode("latin-1", errors="replace").strip()
if line.startswith("data:"):
data_json = line.split('data: ')[1]
try:
data = json.loads(data_json)
except:
continue
try:
if data['type']=="reasoning-delta":
if not RES:
RES = True
yield "<think>\n"
try:
yield data["delta"]
except:
pass
except :
pass
try:
if data["type"]=="text-delta":
if RES:
RES = False
yield "\n</think>\n"
try:
yield data["delta"]
except:
pass
except:
pass
M3 = ["qwen3-4b-thinking-2507"]
def QWEN(
RQ : Any,
messages : List[Dict],
model : str = "NONE",
max_token : int = 40960,
stream : bool = True,
timeout: Optional[float] = None
):
def GEN(RQ:any,messages:list,timeout:int=None):
API_URL = "https://teichai-qwen3-4b-thinking-2507-claude-4-5-opus.hf.space/api/chat"
payload = {
"messages":messages,
"searchEnabled":False
}
headers = {"Accept": "*/*","Content-Type": "application/json","Origin": "https://teichai-qwen3-4b-thinking-2507-claude-4-5-opus.hf.space","Referer": "https://teichai-qwen3-4b-thinking-2507-claude-4-5-opus.hf.space/","User-Agent": "python-requests/2.x"}
# c = t()
RESPO = RQ.post(API_URL, headers=headers, json=payload, stream=stream, timeout=timeout)
# print(c-t())
# print(RESPO)
buffer_lines = []
for raw in RESPO.iter_lines():
if raw is None:
continue
try:
line = raw.decode("utf-8", errors="replace").strip()
except Exception:
line = raw.decode("latin-1", errors="replace").strip()
if line == "":
if not buffer_lines:
continue
data_text = "".join(buffer_lines)
buffer_lines = []
if data_text == "[DONE]":
break
try:
obj = json.loads(data_text)
try:
yield obj
except:
pass
except json.JSONDecodeError:
pass
continue
if line.startswith("data:"):
buffer_lines.append(line[len("data:"):].lstrip())
RES = False
for i in GEN(RQ=RQ,messages=messages,timeout=timeout):
if i["type"]=="reasoning":
if not RES:
RES = True
yield "<think>\n"
yield i["content"]
else:
if RES:
RES = False
yield "\n</think>\n\n"
try:
yield i["content"]
except:
pass
PROVIDERS: Dict[str, Dict[str, Any]] = {
"1": {"__func__": Adarsh_Personal, "models": M1},
"2": {"__func__": QWEN, "models": M2},
"3": {"__func__": FREEGPT, "models": M3},
}
# precomputed provider metadata for speed
PROVIDER_META: Dict[str, Dict[str, Any]] = {}
class Config:
DEFAULT_PROVIDER = "1"
DEFAULT_MODEL = "llama-3.3-70b-versatile"
DEFAULT_MAX_TOKENS = 512
DEFAULT_TEMPERATURE = 0.7
TIMEOUT = 30.0
STREAM = True
@dataclass
class ChatRequest:
api_key: str
messages: List[Dict[str, Any]]
model: Optional[str] = None
provider: str = Config.DEFAULT_PROVIDER
max_tokens: int = Config.DEFAULT_MAX_TOKENS
temperature: float = Config.DEFAULT_TEMPERATURE
stream: bool = Config.STREAM
@staticmethod
def from_dict(payload: Dict[str, Any]) -> "ChatRequest":
api_key = payload.get("api_key") or payload.get("key") or payload.get("apikey")
messages = payload.get("messages") or payload.get("message") or payload.get("msgs")
model = payload.get("model_name") or payload.get("model")
provider = payload.get("provider") or Config.DEFAULT_PROVIDER
provider = str(provider)
max_tokens = payload.get("max_tokens", Config.DEFAULT_MAX_TOKENS)
temperature = payload.get("temperature", Config.DEFAULT_TEMPERATURE)
stream = payload.get("stream", Config.STREAM)
if messages is None:
messages = []
if isinstance(messages, dict):
messages = [messages]
return ChatRequest(
api_key=api_key,
messages=messages,
model=model,
provider=provider,
max_tokens=max_tokens,
temperature=temperature,
stream=stream,
)
GLOBAL_AIOHTTP: Optional[aiohttp.ClientSession] = None
@app.on_event("startup")
async def on_startup():
global GLOBAL_AIOHTTP, PROVIDER_META
logger.info("startup: create global aiohttp session and analyze providers")
GLOBAL_AIOHTTP = aiohttp.ClientSession()
for key, payload in PROVIDERS.items():
func = payload["__func__"]
PROVIDER_META[key] = {
"func": func,
"is_async_gen_fn": inspect.isasyncgenfunction(func),
"is_coroutine_fn": inspect.iscoroutinefunction(func),
"is_generator_fn": inspect.isgeneratorfunction(func),
"is_sync_fn": not (inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func) or inspect.isgeneratorfunction(func)),
}
logger.info("provider meta ready: %s", {k: {kk: vv for kk, vv in v.items() if kk != "func"} for k, v in PROVIDER_META.items()})
@app.on_event("shutdown")
async def on_shutdown():
global GLOBAL_AIOHTTP
logger.info("shutdown: close global aiohttp session")
if GLOBAL_AIOHTTP and not GLOBAL_AIOHTTP.closed:
await GLOBAL_AIOHTTP.close()
async def _stream_sync_generator_in_thread(func, *args, **kwargs) -> AsyncGenerator[bytes, None]:
"""
Run a sync generator in a thread and stream items back via an asyncio.Queue.
This allows streaming without blocking the event loop.
"""
loop = asyncio.get_running_loop()
q: asyncio.Queue = asyncio.Queue(maxsize=32)
sentinel = object()
def worker():
try:
gen = func(*args, **kwargs)
# if the function is not actually a generator but returns a value, handle that
if gen is None:
loop.call_soon_threadsafe(q.put_nowait, sentinel)
return
# If it's iterable, iterate and put items into queue
for item in gen:
loop.call_soon_threadsafe(q.put_nowait, item)
except Exception as e:
# pass the exception object forward to the async side
loop.call_soon_threadsafe(q.put_nowait, e)
finally:
loop.call_soon_threadsafe(q.put_nowait, sentinel)
# start worker in thread
thread_task = loop.run_in_executor(None, worker)
# consume from queue
while True:
item = await q.get()
if item is sentinel:
break
if isinstance(item, Exception):
# re-raise in async context so upstream can handle
raise item
if item is None:
continue
if isinstance(item, bytes):
yield item
elif isinstance(item, str):
yield item.encode("utf-8")
else:
yield str(item).encode("utf-8")
# ensure worker finished/propagated exceptions
await asyncio.shield(thread_task)
async def _call_provider_and_stream(
provider_key: str,
messages: List[Dict],
model: str,
max_token: int,
stream_flag: bool,
timeout: float,
) -> AsyncGenerator[bytes, None]:
"""
Core streaming logic. Yields raw bytes as soon as provider yields items.
"""
if provider_key not in PROVIDER_META:
raise ValueError(f"Unknown provider '{provider_key}'")
meta = PROVIDER_META[provider_key]
func = meta["func"]
# pass arguments using your original parameter names so providers stay unchanged
kwargs = dict(messages=messages, model=model, max_token=max_token, stream=stream_flag, timeout=timeout)
try:
# 1) Async generator functions -> call returns an async generator (do NOT await)
if meta["is_async_gen_fn"]:
agen = func(Requests, **kwargs)
# iterate immediately (no waiting for full result)
async for item in agen:
if item is None:
continue
if isinstance(item, bytes):
yield item
elif isinstance(item, str):
yield item.encode("utf-8")
else:
yield str(item).encode("utf-8")
return
# 2) Sync generator functions -> call returns generator; iterate it in background thread
if meta["is_generator_fn"]:
# Note: call func in thread via helper which will iterate and push items to queue
async for item in _stream_sync_generator_in_thread(lambda *a, **k: func(Requests, **kwargs)):
yield item
return
# 3) Coroutine functions (async def) that return final result -> await it (can't stream before it completes)
if meta["is_coroutine_fn"]:
# await the coroutine under timeout (can't stream until it returns)
res = await asyncio.wait_for(func(Requests, **kwargs), timeout=timeout)
if res is None:
return
# if it returned an async generator (rare), iterate it
if inspect.isasyncgen(res):
async for item in res:
if item is None:
continue
if isinstance(item, bytes):
yield item
elif isinstance(item, str):
yield item.encode("utf-8")
else:
yield str(item).encode("utf-8")
return
# if it returned a sync iterable -> iterate and yield
if inspect.isgenerator(res) or (hasattr(res, "__iter__") and not isinstance(res, (str, bytes, dict))):
for item in res:
if item is None:
continue
if isinstance(item, bytes):
yield item
elif isinstance(item, str):
yield item.encode("utf-8")
else:
yield str(item).encode("utf-8")
return
# single value
if isinstance(res, bytes):
yield res
elif isinstance(res, str):
yield res.encode("utf-8")
else:
yield str(res).encode("utf-8")
return
# 4) Sync plain function (not generator) -> run in thread (returns value or iterable)
# We call func in a thread and stream results as they appear if it's iterable.
def sync_call_wrapper():
return func(Requests, **kwargs)
sync_res = await asyncio.wait_for(asyncio.to_thread(sync_call_wrapper), timeout=timeout)
if sync_res is None:
return
if inspect.isgenerator(sync_res) or (hasattr(sync_res, "__iter__") and not isinstance(sync_res, (str, bytes, dict))):
for item in sync_res:
if item is None:
continue
if isinstance(item, bytes):
yield item
elif isinstance(item, str):
yield item.encode("utf-8")
else:
yield str(item).encode("utf-8")
return
if isinstance(sync_res, bytes):
yield sync_res
elif isinstance(sync_res, str):
yield sync_res.encode("utf-8")
else:
yield str(sync_res).encode("utf-8")
except asyncio.TimeoutError:
err = f"[server_timeout] provider {provider_key} exceeded {timeout}s\n"
logger.warning(err.strip())
yield err.encode("utf-8")
except Exception as e:
logger.exception("provider error")
err = f"[server_error] {type(e).__name__}: {e}\n"
yield err.encode("utf-8")
@app.post("/chat")
async def chat_endpoint(request: Request):
# fast load
try:
body_bytes = await request.body()
payload = _loads(body_bytes)
except Exception as e:
raise HTTPException(status_code=400, detail=f"invalid json: {e}")
req = ChatRequest.from_dict(payload)
if not req.api_key or not req.messages:
raise HTTPException(status_code=400, detail="api_key and messages required")
provider_key = req.provider
if req.stream:
async def sse_stream():
# iterate provider stream and immediately send SSE-formatted chunks
async for raw_chunk in _call_provider_and_stream(
provider_key=provider_key,
messages=req.messages,
model=req.model or Config.DEFAULT_MODEL,
max_token=req.max_tokens,
stream_flag=req.stream,
timeout=Config.TIMEOUT,
):
# decode raw chunk to text
text = raw_chunk.decode("utf-8", errors="ignore") if isinstance(raw_chunk, (bytes, bytearray)) else str(raw_chunk)
# prepare JSON payload object
payload_obj = {"response": text}
try:
json_str = _dumps(payload_obj)
except Exception:
# fallback
import json as _fallback_json
json_str = _fallback_json.dumps(payload_obj)
# send SSE data line + blank line
sse_event = f"data: {json_str}\n\n"
yield sse_event.encode("utf-8")
# final termination marker exactly as requested
yield ("[DONE]\n").encode("utf-8")
return StreamingResponse(sse_stream(), media_type="text/event-stream")
else:
# non-stream: collect (only for non-stream requests)
collected = []
async for chunk in _call_provider_and_stream(
provider_key=provider_key,
messages=req.messages,
model=req.model or Config.DEFAULT_MODEL,
max_token=req.max_tokens,
stream_flag=req.stream,
timeout=Config.TIMEOUT,
):
collected.append(chunk.decode("utf-8", errors="ignore") if isinstance(chunk, (bytes, bytearray)) else str(chunk))
return JSONResponse({"text": "".join(collected)})
@app.get("/model")
async def model():
return {"models": [M1, M2, M3]}
@app.get("/health")
async def health_check():
return {"status": "ok"}
|