Fix SSE route dependency and align architecture docs
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
"""LLM客户端基类 - 统一接口定义"""
|
||||
"""Provide service-layer logic for base client."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict, Optional, Any
|
||||
from enum import Enum
|
||||
# Keep provider-specific behavior explicit so debugging stays straightforward.
|
||||
|
||||
|
||||
|
||||
class LLMProvider(Enum):
|
||||
"""LLM提供商"""
|
||||
"""Define the L L M Provider enumeration."""
|
||||
DEEPSEEK = "deepseek"
|
||||
QWEN = "qwen"
|
||||
QWEN_VL = "qwen_vl"
|
||||
@@ -15,7 +17,7 @@ class LLMProvider(Enum):
|
||||
|
||||
@dataclass
|
||||
class LLMResponse:
|
||||
"""LLM响应结果"""
|
||||
"""Represent the L L M Response type."""
|
||||
content: str
|
||||
model: str
|
||||
usage: Dict[str, int] = field(default_factory=dict)
|
||||
@@ -25,12 +27,13 @@ class LLMResponse:
|
||||
|
||||
@property
|
||||
def is_success(self) -> bool:
|
||||
"""Return whether success for the L L M Response instance."""
|
||||
return self.error is None
|
||||
|
||||
|
||||
@dataclass
|
||||
class LLMConfig:
|
||||
"""LLM配置"""
|
||||
"""Define configuration for l l m config."""
|
||||
provider: LLMProvider
|
||||
model: str
|
||||
api_key: str
|
||||
@@ -38,19 +41,20 @@ class LLMConfig:
|
||||
max_tokens: int = 4096
|
||||
temperature: float = 0.7
|
||||
top_p: float = 0.9
|
||||
timeout: int = 300 # 默认超时300秒(摘要/Skills生成可能需要较长时间)
|
||||
timeout: int = 300 # Keep provider-specific behavior explicit so debugging stays straightforward.
|
||||
|
||||
|
||||
class BaseLLMClient(ABC):
|
||||
"""LLM客户端基类"""
|
||||
"""Represent the Base L L M Client type."""
|
||||
|
||||
def __init__(self, config: LLMConfig):
|
||||
"""Initialize the Base L L M Client instance."""
|
||||
self.config = config
|
||||
self._client = None
|
||||
|
||||
@abstractmethod
|
||||
def _init_client(self):
|
||||
"""初始化客户端"""
|
||||
"""Handle init client for this module for the Base L L M Client instance."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
@@ -61,18 +65,7 @@ class BaseLLMClient(ABC):
|
||||
temperature: Optional[float] = None,
|
||||
**kwargs
|
||||
) -> LLMResponse:
|
||||
"""
|
||||
对话补全
|
||||
|
||||
Args:
|
||||
messages: 对话消息列表 [{"role": "user/assistant/system", "content": "..."}]
|
||||
max_tokens: 最大输出token数
|
||||
temperature: 温度参数
|
||||
**kwargs: 其他参数
|
||||
|
||||
Returns:
|
||||
LLMResponse: 响应结果
|
||||
"""
|
||||
"""Handle chat for the Base L L M Client instance."""
|
||||
pass
|
||||
|
||||
def complete(
|
||||
@@ -83,18 +76,7 @@ class BaseLLMClient(ABC):
|
||||
temperature: Optional[float] = None,
|
||||
**kwargs
|
||||
) -> LLMResponse:
|
||||
"""
|
||||
单轮补全(便捷方法)
|
||||
|
||||
Args:
|
||||
prompt: 用户输入
|
||||
system_prompt: 系统提示词
|
||||
max_tokens: 最大输出token数
|
||||
temperature: 温度参数
|
||||
|
||||
Returns:
|
||||
LLMResponse: 响应结果
|
||||
"""
|
||||
"""Handle complete for the Base L L M Client instance."""
|
||||
messages = []
|
||||
if system_prompt:
|
||||
messages.append({"role": "system", "content": system_prompt})
|
||||
@@ -104,12 +86,12 @@ class BaseLLMClient(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def get_available_models(self) -> List[str]:
|
||||
"""获取可用模型列表"""
|
||||
"""Return available models for the Base L L M Client instance."""
|
||||
pass
|
||||
|
||||
def estimate_tokens(self, text: str) -> int:
|
||||
"""估算文本token数(粗略估计)"""
|
||||
# 中文字符约1.5 token,英文约0.25 token
|
||||
"""Handle estimate tokens for the Base L L M Client instance."""
|
||||
# Keep provider-specific behavior explicit so debugging stays straightforward.
|
||||
chinese_chars = sum(1 for c in text if '一' <= c <= '鿿')
|
||||
other_chars = len(text) - chinese_chars
|
||||
return int(chinese_chars * 1.5 + other_chars * 0.25)
|
||||
|
||||
Reference in New Issue
Block a user