2026-05-18 16:32:42 +08:00
|
|
|
|
"""Provide service-layer logic for deepseek client."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
from typing import List, Dict, Optional
|
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
from .base_client import BaseLLMClient, LLMResponse, LLMConfig, LLMProvider
|
2026-05-18 16:32:42 +08:00
|
|
|
|
# Keep provider-specific behavior explicit so debugging stays straightforward.
|
|
|
|
|
|
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeepSeekClient(BaseLLMClient):
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Represent the Deep Seek Client type."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
|
|
SUPPORTED_MODELS = [
|
|
|
|
|
|
"deepseek-chat",
|
|
|
|
|
|
"deepseek-coder",
|
|
|
|
|
|
"deepseek-reasoner",
|
|
|
|
|
|
"deepseek-v3",
|
|
|
|
|
|
"deepseek-v3.2",
|
|
|
|
|
|
"deepseek-v4-flash"
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, config: LLMConfig):
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Initialize the Deep Seek Client instance."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
if config.provider != LLMProvider.DEEPSEEK:
|
|
|
|
|
|
raise ValueError(f"配置provider应为DEEPSEEK,实际为{config.provider}")
|
|
|
|
|
|
super().__init__(config)
|
|
|
|
|
|
self._init_client()
|
|
|
|
|
|
|
|
|
|
|
|
def _init_client(self):
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Handle init client for this module for the Deep Seek Client instance."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
self._client = httpx.Client(
|
|
|
|
|
|
base_url=self.config.base_url,
|
|
|
|
|
|
headers={
|
|
|
|
|
|
"Authorization": f"Bearer {self.config.api_key}",
|
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
|
},
|
|
|
|
|
|
timeout=self.config.timeout
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.info(f"DeepSeek客户端初始化完成: {self.config.base_url} - {self.config.model}")
|
|
|
|
|
|
|
|
|
|
|
|
def chat(
|
|
|
|
|
|
self,
|
|
|
|
|
|
messages: List[Dict[str, str]],
|
|
|
|
|
|
max_tokens: Optional[int] = None,
|
|
|
|
|
|
temperature: Optional[float] = None,
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
) -> LLMResponse:
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Handle chat for the Deep Seek Client instance."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
"model": self.config.model,
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"max_tokens": max_tokens or self.config.max_tokens,
|
|
|
|
|
|
"temperature": temperature or self.config.temperature,
|
|
|
|
|
|
"top_p": kwargs.get("top_p", self.config.top_p),
|
|
|
|
|
|
"stream": False
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response = self._client.post("/chat/completions", json=payload)
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
latency_ms = int((time.time() - start_time) * 1000)
|
|
|
|
|
|
|
|
|
|
|
|
choices = data.get("choices", [{}])
|
|
|
|
|
|
message = choices[0].get("message", {})
|
|
|
|
|
|
|
|
|
|
|
|
return LLMResponse(
|
|
|
|
|
|
content=message.get("content", ""),
|
|
|
|
|
|
model=data.get("model", self.config.model),
|
|
|
|
|
|
usage=data.get("usage", {}),
|
|
|
|
|
|
finish_reason=choices[0].get("finish_reason", "stop"),
|
|
|
|
|
|
latency_ms=latency_ms
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
|
|
logger.error(f"DeepSeek API错误: {e.response.status_code} - {e.response.text}")
|
|
|
|
|
|
return LLMResponse(
|
|
|
|
|
|
content="",
|
|
|
|
|
|
model=self.config.model,
|
|
|
|
|
|
error=f"API错误: {e.response.status_code} - {e.response.text[:200]}"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"DeepSeek调用失败: {e}")
|
|
|
|
|
|
return LLMResponse(
|
|
|
|
|
|
content="",
|
|
|
|
|
|
model=self.config.model,
|
|
|
|
|
|
error=str(e)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-20 23:34:08 +08:00
|
|
|
|
def stream_chat(
|
|
|
|
|
|
self,
|
|
|
|
|
|
messages: List[Dict[str, str]],
|
|
|
|
|
|
max_tokens: Optional[int] = None,
|
|
|
|
|
|
temperature: Optional[float] = None,
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
):
|
|
|
|
|
|
"""Stream chat for the Deep Seek Client instance."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
"model": self.config.model,
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"max_tokens": max_tokens or self.config.max_tokens,
|
|
|
|
|
|
"temperature": temperature or self.config.temperature,
|
|
|
|
|
|
"top_p": kwargs.get("top_p", self.config.top_p),
|
|
|
|
|
|
"stream": True
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with self._client.stream("POST", "/chat/completions", json=payload) as response:
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
for line in response.iter_lines():
|
2026-05-20 23:39:15 +08:00
|
|
|
|
if not line:
|
2026-05-20 23:34:08 +08:00
|
|
|
|
continue
|
|
|
|
|
|
|
2026-05-20 23:39:15 +08:00
|
|
|
|
line = line.strip()
|
|
|
|
|
|
if line.startswith(":"):
|
2026-05-20 23:34:08 +08:00
|
|
|
|
continue
|
|
|
|
|
|
|
2026-05-20 23:39:15 +08:00
|
|
|
|
if not line.startswith("data: "):
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
data_str = line[6:]
|
2026-05-20 23:34:08 +08:00
|
|
|
|
if data_str == "[DONE]":
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
import json
|
|
|
|
|
|
data = json.loads(data_str)
|
|
|
|
|
|
choices = data.get("choices", [])
|
|
|
|
|
|
if choices:
|
|
|
|
|
|
delta = choices[0].get("delta", {})
|
|
|
|
|
|
content = delta.get("content", "")
|
|
|
|
|
|
if content:
|
|
|
|
|
|
yield content
|
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
|
|
logger.error(f"DeepSeek Stream API错误: {e.response.status_code}")
|
|
|
|
|
|
yield ""
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"DeepSeek Stream调用失败: {e}")
|
|
|
|
|
|
yield ""
|
|
|
|
|
|
|
2026-05-14 15:07:34 +08:00
|
|
|
|
def get_available_models(self) -> List[str]:
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Return available models for the Deep Seek Client instance."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
return self.SUPPORTED_MODELS
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Release the resources held by this component."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
if self._client:
|
|
|
|
|
|
self._client.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_deepseek_client(
|
|
|
|
|
|
api_key: str,
|
|
|
|
|
|
model: str = "deepseek-v4-flash",
|
|
|
|
|
|
base_url: str = "http://6.86.80.4:30080/v1",
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
) -> DeepSeekClient:
|
2026-05-18 16:32:42 +08:00
|
|
|
|
"""Create deepseek client."""
|
2026-05-14 15:07:34 +08:00
|
|
|
|
config = LLMConfig(
|
|
|
|
|
|
provider=LLMProvider.DEEPSEEK,
|
|
|
|
|
|
model=model,
|
|
|
|
|
|
api_key=api_key,
|
|
|
|
|
|
base_url=base_url,
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
)
|
|
|
|
|
|
return DeepSeekClient(config)
|