diff --git a/lightrag/llm/ollama.py b/lightrag/llm/ollama.py index c6fb46fc..b013496e 100644 --- a/lightrag/llm/ollama.py +++ b/lightrag/llm/ollama.py @@ -1,11 +1,6 @@ -import sys +from collections.abc import AsyncIterator -if sys.version_info < (3, 9): - from typing import AsyncIterator -else: - from collections.abc import AsyncIterator - -import pipmaster as pm # Pipmaster for dynamic library install +import pipmaster as pm # install specific modules if not pm.is_installed("ollama"): diff --git a/lightrag/llm/openai.py b/lightrag/llm/openai.py index 9f0708ca..1a7d0f5e 100644 --- a/lightrag/llm/openai.py +++ b/lightrag/llm/openai.py @@ -1,14 +1,10 @@ from ..utils import verbose_debug, VERBOSE_DEBUG -import sys import os import logging -if sys.version_info < (3, 9): - from typing import AsyncIterator -else: - from collections.abc import AsyncIterator -import pipmaster as pm # Pipmaster for dynamic library install +from collections.abc import AsyncIterator +import pipmaster as pm # install specific modules if not pm.is_installed("openai"): pm.install("openai") @@ -54,7 +50,7 @@ class InvalidResponseError(Exception): def create_openai_async_client( api_key: str | None = None, base_url: str | None = None, - client_configs: dict[str, Any] = None, + client_configs: dict[str, Any] | None = None, ) -> AsyncOpenAI: """Create an AsyncOpenAI client with the given configuration. @@ -119,7 +115,7 @@ async def openai_complete_if_cache( ) -> str: """Complete a prompt using OpenAI's API with caching support and Chain of Thought (COT) integration. - This function supports automatic integration of reasoning content (思维链) from models that provide + This function supports automatic integration of reasoning content from models that provide Chain of Thought capabilities. The reasoning content is seamlessly integrated into the response using ... tags. @@ -264,19 +260,16 @@ async def openai_complete_if_cache( delta = chunk.choices[0].delta content = getattr(delta, "content", None) - reasoning_content = getattr(delta, "reasoning_content", None) + reasoning_content = getattr(delta, "reasoning_content", "") # Handle COT logic for streaming (only if enabled) if enable_cot: - if content is not None and content != "": + if content: # Regular content is present if not initial_content_seen: initial_content_seen = True # If both content and reasoning_content are present initially, don't start COT - if ( - reasoning_content is not None - and reasoning_content != "" - ): + if reasoning_content: cot_active = False cot_started = False @@ -290,7 +283,7 @@ async def openai_complete_if_cache( content = safe_unicode_decode(content.encode("utf-8")) yield content - elif reasoning_content is not None and reasoning_content != "": + elif reasoning_content: # Only reasoning content is present if not initial_content_seen and not cot_started: # Start COT if we haven't seen initial content yet @@ -308,7 +301,7 @@ async def openai_complete_if_cache( yield reasoning_content else: # COT disabled, only process regular content - if content is not None and content != "": + if content: if r"\u" in content: content = safe_unicode_decode(content.encode("utf-8")) yield content @@ -415,7 +408,7 @@ async def openai_complete_if_cache( message = response.choices[0].message content = getattr(message, "content", None) - reasoning_content = getattr(message, "reasoning_content", None) + reasoning_content = getattr(message, "reasoning_content", "") # Handle COT logic for non-streaming responses (only if enabled) final_content = "" @@ -582,9 +575,9 @@ async def nvidia_openai_complete( async def openai_embed( texts: list[str], model: str = "text-embedding-3-small", - base_url: str = None, - api_key: str = None, - client_configs: dict[str, Any] = None, + base_url: str | None = None, + api_key: str | None = None, + client_configs: dict[str, Any] | None = None, ) -> np.ndarray: """Generate embeddings for a list of texts using OpenAI's API.