From 1e46711ab0696759bbacdf37a9f9ee259245a30f Mon Sep 17 00:00:00 2001 From: vahidrezvani Date: Sat, 28 Feb 2026 09:38:07 +0330 Subject: [PATCH] add kia ai chat tools19 --- src/routes/chatbot.py | 6 +++++- src/services/kiaai/chat.py | 3 ++- src/services/kiaai/workflow.py | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/routes/chatbot.py b/src/routes/chatbot.py index 1ee4633..634fb8d 100644 --- a/src/routes/chatbot.py +++ b/src/routes/chatbot.py @@ -104,7 +104,11 @@ async def send_message( ], doc=[(settings.base_url + doc_url) if doc_url else None, doc_path], ), - media_type="application/json", + media_type="application/x-ndjson", + headers={ + "X-Accel-Buffering": "no", + "Cache-Control": "no-cache", + }, ) diff --git a/src/services/kiaai/chat.py b/src/services/kiaai/chat.py index 2e17d23..8442803 100644 --- a/src/services/kiaai/chat.py +++ b/src/services/kiaai/chat.py @@ -146,7 +146,7 @@ class KiaAIService: buf = "" done = False - async for raw in resp.aiter_bytes(chunk_size=32): + async for raw in resp.aiter_bytes(chunk_size=1): buf += raw.decode("utf-8", errors="replace") while "\n" in buf: @@ -198,6 +198,7 @@ class KiaAIService: token = delta.get("content") or "" if token: yield AIMessageChunk(content=token) + await asyncio.sleep(0) # flush immediately # Capture usage from final summary chunk if data.get("usage"): diff --git a/src/services/kiaai/workflow.py b/src/services/kiaai/workflow.py index e6d180f..bd3b3d2 100644 --- a/src/services/kiaai/workflow.py +++ b/src/services/kiaai/workflow.py @@ -1,4 +1,5 @@ import json +import asyncio from datetime import datetime, UTC from typing import AsyncGenerator @@ -71,6 +72,7 @@ async def kia_token_stream( if token: full_ai_content += token yield json.dumps({"content": token}, ensure_ascii=False) + "\n" + await asyncio.sleep(0) # flush token to client immediately if chunk.usage_metadata: input_tokens = chunk.usage_metadata.get("input_tokens", 0) output_tokens = chunk.usage_metadata.get("output_tokens", 0)