feat: enhance logging and error handling in backend and editor components

This commit is contained in:
2026-01-25 13:29:11 +08:00
parent bf7dec86ab
commit 5f00e71ceb
6 changed files with 179 additions and 87 deletions
+34 -9
View File
@@ -2,6 +2,7 @@ import os
from typing import AsyncGenerator
from openai import AsyncOpenAI
import json
import time
api_key = os.getenv('OPENAI_API_KEY', 'ollama')
base_url = os.getenv('OLLAMA_BASE_URL', 'http://192.168.0.120:11434/v1/')
@@ -18,9 +19,13 @@ async def stream_openai(prompt: str) -> AsyncGenerator[str, None]:
调用 OpenAI/Ollama API 并流式返回补全内容。
参考 completions-sample-code 的 streaming 逻辑。
"""
print(f"[LLM] Calling API with prompt length: {len(prompt)}")
start_time = time.time()
print(f"[LLM] ========== API Call Start ==========")
print(f"[LLM] Prompt length: {len(prompt)}")
print(f"[LLM] Model: {model}")
try:
print(f"[LLM] Creating streaming chat completion...")
stream = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
@@ -29,16 +34,36 @@ async def stream_openai(prompt: str) -> AsyncGenerator[str, None]:
temperature=0.2,
)
print(f"[LLM] Stream created successfully, iterating...")
chunk_count = 0
async for chunk in stream:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
chunk_count += 1
print(f"[LLM] Chunk {chunk_count}: {content}")
yield json.dumps({"content": content})
first_chunk_time = None
print(f"[LLM] Stream complete, total chunks: {chunk_count}")
async for chunk in stream:
current_time = time.time()
if first_chunk_time is None:
first_chunk_time = current_time - start_time
chunk_count += 1
choice = chunk.choices[0] if chunk.choices else None
if choice and choice.delta.content:
content = choice.delta.content
print(f"[LLM] Chunk {chunk_count}: '{content}' (latency: {current_time - start_time:.3f}s)")
yield json.dumps({"content": content})
elif chunk.choices and hasattr(chunk.choices[0], 'finish_reason'):
finish_reason = chunk.choices[0].finish_reason
print(f"[LLM] Chunk {chunk_count}: finish_reason={finish_reason}")
if finish_reason:
break
else:
print(f"[LLM] Chunk {chunk_count}: empty or no content")
total_time = time.time() - start_time
print(f"[LLM] Stream complete - chunks: {chunk_count}, first chunk latency: {first_chunk_time:.3f}s, total time: {total_time:.3f}s")
print(f"[LLM] ========== API Call End ==========")
except Exception as e:
error_msg = f"Error: {str(e)}"
print(f"[LLM] Error: {error_msg}")
yield json.dumps({"error": str(e)})
import traceback
traceback.print_exc()
yield json.dumps({"error": str(e), "type": type(e).__name__})