add kia ai chat tools16
CI/CD Pipeline / build-and-deploy (push) Successful in 19s Details

This commit is contained in:
vahidrezvani 2026-02-26 09:59:10 +03:30
parent b94402df6b
commit 1ece942eba
1 changed files with 60 additions and 44 deletions

View File

@ -122,6 +122,10 @@ class KiaAIService:
Core SSE parser yields one AIMessageChunk per content delta token. Core SSE parser yields one AIMessageChunk per content delta token.
Final chunk: AIMessageChunk(content="", usage_metadata={...}). Final chunk: AIMessageChunk(content="", usage_metadata={...}).
Raises RuntimeError on KIA server errors (triggers retry in stream()). Raises RuntimeError on KIA server errors (triggers retry in stream()).
Uses aiter_bytes(chunk_size=32) instead of aiter_lines() so every
byte received from the network is processed immediately without
waiting for httpx's internal 64 KB read buffer to fill up.
""" """
usage: dict = {} usage: dict = {}
@ -139,7 +143,15 @@ class KiaAIService:
resp.raise_for_status() resp.raise_for_status()
line_count = 0 line_count = 0
async for line in resp.aiter_lines(): buf = ""
done = False
async for raw in resp.aiter_bytes(chunk_size=32):
buf += raw.decode("utf-8", errors="replace")
while "\n" in buf:
line, buf = buf.split("\n", 1)
line = line.rstrip("\r")
line_count += 1 line_count += 1
if line_count <= 3: if line_count <= 3:
print(f"[KiaAI] raw[{line_count}]: {line!r}") print(f"[KiaAI] raw[{line_count}]: {line!r}")
@ -165,6 +177,7 @@ class KiaAIService:
if raw_data.strip() == "[DONE]": if raw_data.strip() == "[DONE]":
print("[KiaAI] [DONE]") print("[KiaAI] [DONE]")
done = True
break break
try: try:
@ -190,6 +203,9 @@ class KiaAIService:
if data.get("usage"): if data.get("usage"):
usage = data["usage"] usage = data["usage"]
if done:
break
# Final chunk carries usage metadata # Final chunk carries usage metadata
print(f"[KiaAI] Stream done | total_lines={line_count} | usage={usage}") print(f"[KiaAI] Stream done | total_lines={line_count} | usage={usage}")
yield AIMessageChunk( yield AIMessageChunk(