Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix

Bingxi Zhaofe9c169939cf
diff --git a/src/services/llm/client.py b/src/services/llm/client.py
index b182b0373..d2d6adb68 100644
--- a/src/services/llm/client.py
+++ b/src/services/llm/client.py
@@ -142,15 +142,21 @@ class LLMClient:
history_messages: Optional[List[Dict]] = None,
**kwargs: Any,
):
+ # Only pass api_version if set (for Azure OpenAI)
+ lightrag_kwargs = {
+ "system_prompt": system_prompt,
+ "history_messages": history_messages or [],
+ "api_key": self.config.api_key,
+ "base_url": self.config.base_url,
+ **kwargs,
+ }
+ api_version = getattr(self.config, "api_version", None)
+ if api_version:
+ lightrag_kwargs["api_version"] = api_version
return openai_complete_if_cache(
self.config.model,
prompt,
- system_prompt=system_prompt,
- history_messages=history_messages or [],
- api_key=self.config.api_key,
- base_url=self.config.base_url,
- api_version=getattr(self.config, "api_version", None),
- **kwargs,
+ **lightrag_kwargs,
)
return llm_model_func
@@ -196,6 +202,9 @@ class LLMClient:
# OpenAI-compatible bindings
from lightrag.llm.openai import openai_complete_if_cache
+ # Get api_version once for reuse
+ api_version = getattr(self.config, "api_version", None)
+
def vision_model_func(
prompt: str,
system_prompt: Optional[str] = None,
@@ -211,14 +220,18 @@ class LLMClient:
for k, v in kwargs.items()
if k not in ["messages", "prompt", "system_prompt", "history_messages"]
}
+ lightrag_kwargs = {
+ "messages": messages,
+ "api_key": self.config.api_key,
+ "base_url": self.config.base_url,
+ **clean_kwargs,
+ }
+ if api_version:
+ lightrag_kwargs["api_version"] = api_version
return openai_complete_if_cache(
self.config.model,
prompt="",
- messages=messages,
- api_key=self.config.api_key,
- base_url=self.config.base_url,
- api_version=getattr(self.config, "api_version", None),
- **clean_kwargs,
+ **lightrag_kwargs,
)
# Handle image data
@@ -234,26 +247,34 @@ class LLMClient:
},
],
}
+ lightrag_kwargs = {
+ "messages": [image_message],
+ "api_key": self.config.api_key,
+ "base_url": self.config.base_url,
+ **kwargs,
+ }
+ if api_version:
+ lightrag_kwargs["api_version"] = api_version
return openai_complete_if_cache(
self.config.model,
prompt="",
- messages=[image_message],
- api_key=self.config.api_key,
- base_url=self.config.base_url,
- api_version=getattr(self.config, "api_version", None),
- **kwargs,
+ **lightrag_kwargs,
)
# Fallback to regular completion
+ lightrag_kwargs = {
+ "system_prompt": system_prompt,
+ "history_messages": history_messages or [],
+ "api_key": self.config.api_key,
+ "base_url": self.config.base_url,
+ **kwargs,
+ }
+ if api_version:
+ lightrag_kwargs["api_version"] = api_version
return openai_complete_if_cache(
self.config.model,
prompt,
- system_prompt=system_prompt,
- history_messages=history_messages or [],
- api_key=self.config.api_key,
- base_url=self.config.base_url,
- api_version=getattr(self.config, "api_version", None),
- **kwargs,
+ **lightrag_kwargs,
)
return vision_model_func
diff --git a/src/services/llm/cloud_provider.py b/src/services/llm/cloud_provider.py
index bae516705..8f1257a33 100644
--- a/src/services/llm/cloud_provider.py
+++ b/src/services/llm/cloud_provider.py
@@ -6,12 +6,18 @@ Handles all cloud API LLM calls (OpenAI, DeepSeek, Anthropic, etc.)
Provides both complete() and stream() methods.
"""
+import logging
import os
from typing import AsyncGenerator, Dict, List, Optional
import aiohttp
from lightrag.llm.openai import openai_complete_if_cache
+# Get loggers for suppression during fallback scenarios
+# (lightrag logs errors internally before raising exceptions)
+_lightrag_logger = logging.getLogger("lightrag")
+_openai_logger = logging.getLogger("openai")
+
from .capabilities import supports_response_format
from .config import get_token_limit_kwargs
from .exceptions import LLMAPIError, LLMAuthenticationError, LLMConfigError
@@ -156,15 +162,30 @@ async def _openai_complete(
content = None
try:
# Try using lightrag's openai_complete_if_cache first (has caching)
- content = await openai_complete_if_cache(
- model=model,
- prompt=prompt,
- system_prompt=system_prompt,
- api_key=api_key,
- base_url=base_url,
- api_version=api_version,
+ # Only pass api_version if it's set (for Azure OpenAI)
+ # Standard OpenAI SDK doesn't accept api_version parameter
+ lightrag_kwargs = {
+ "system_prompt": system_prompt,
+ "history_messages": [], # Required by lightrag to build messages array
+ "api_key": api_key,
+ "base_url": base_url,
**kwargs,
- )
+ }
+ if api_version:
+ lightrag_kwargs["api_version"] = api_version
+
+ # Suppress lightrag's and openai's internal error logging during the call
+ # (errors are handled by our fallback mechanism)
+ original_lightrag_level = _lightrag_logger.level
+ original_openai_level = _openai_logger.level
+ _lightrag_logger.setLevel(logging.CRITICAL)
+ _openai_logger.setLevel(logging.CRITICAL)
+ try:
+ # model and prompt must be positional arguments
+ content = await openai_complete_if_cache(model, prompt, **lightrag_kwargs)
+ finally:
+ _lightrag_logger.setLevel(original_lightrag_level)
+ _openai_logger.setLevel(original_openai_level)
except Exception:
pass # Fall through to direct call