- CLI + Web 双平台,基于 LLM 的 TDD+DDD 自动化测试生成 - 集成 unified-test-platform 903 个 D-Cloud 用例 + 13 个 Robogo 场景 - Agent 5 步流水线: 扫描 -> DDD -> LLM 生成测试 - 精确回归 + UI 变更检查 - 可视化 HTML 测试报告 - 暗色/亮色主题切换 - 19 个 API 端点 + 6 个 SPA 页面 - 15/15 项 TDD+DDD 合规
112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
import os
|
|
import json
|
|
import httpx
|
|
from dataclasses import dataclass, field
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass
|
|
class LLMConfig:
|
|
provider: str = "deepseek"
|
|
model: str = "deepseek-chat"
|
|
base_url: str = "https://api.deepseek.com"
|
|
api_key: str = ""
|
|
temperature: float = 0.3
|
|
max_tokens: int = 8192
|
|
|
|
|
|
@dataclass
|
|
class LLMMessage:
|
|
role: str
|
|
content: str
|
|
|
|
|
|
@dataclass
|
|
class LLMResponse:
|
|
content: str
|
|
usage: dict = field(default_factory=dict)
|
|
|
|
|
|
class LLMClient:
|
|
|
|
def __init__(self, config: LLMConfig):
|
|
self.config = config
|
|
self._client = httpx.Client(timeout=120.0)
|
|
if config.provider == "deepseek":
|
|
self.config.base_url = self.config.base_url.rstrip("/")
|
|
|
|
def chat(self, messages: list[LLMMessage], system_prompt: str = "") -> LLMResponse:
|
|
full_messages = []
|
|
if system_prompt:
|
|
full_messages.append({"role": "system", "content": system_prompt})
|
|
for msg in messages:
|
|
full_messages.append({"role": msg.role, "content": msg.content})
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {self.config.api_key}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
if self.config.provider == "deepseek":
|
|
url = f"{self.config.base_url}/v1/chat/completions"
|
|
else:
|
|
url = f"{self.config.base_url}/chat/completions"
|
|
|
|
payload = {
|
|
"model": self.config.model,
|
|
"messages": full_messages,
|
|
"temperature": self.config.temperature,
|
|
"max_tokens": self.config.max_tokens,
|
|
}
|
|
|
|
response = self._client.post(url, headers=headers, json=payload)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
|
|
content = data["choices"][0]["message"]["content"]
|
|
usage = data.get("usage", {})
|
|
return LLMResponse(content=content, usage=usage)
|
|
|
|
def chat_stream(self, messages: list[LLMMessage], system_prompt: str = ""):
|
|
full_messages = []
|
|
if system_prompt:
|
|
full_messages.append({"role": "system", "content": system_prompt})
|
|
for msg in messages:
|
|
full_messages.append({"role": msg.role, "content": msg.content})
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {self.config.api_key}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
if self.config.provider == "deepseek":
|
|
url = f"{self.config.base_url}/v1/chat/completions"
|
|
else:
|
|
url = f"{self.config.base_url}/chat/completions"
|
|
|
|
payload = {
|
|
"model": self.config.model,
|
|
"messages": full_messages,
|
|
"temperature": self.config.temperature,
|
|
"max_tokens": self.config.max_tokens,
|
|
"stream": True,
|
|
}
|
|
|
|
with self._client.stream("POST", url, headers=headers, json=payload) as response:
|
|
response.raise_for_status()
|
|
for line in response.iter_lines():
|
|
if line.startswith("data: "):
|
|
data_str = line[6:]
|
|
if data_str == "[DONE]":
|
|
break
|
|
try:
|
|
data = json.loads(data_str)
|
|
delta = data["choices"][0].get("delta", {})
|
|
content = delta.get("content", "")
|
|
if content:
|
|
yield content
|
|
except json.JSONDecodeError:
|
|
continue
|
|
|
|
def close(self):
|
|
self._client.close() |