fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -15,11 +15,13 @@ import httpx
|
||||
KIMI_API_KEY = os.getenv("KIMI_API_KEY", "")
|
||||
KIMI_BASE_URL = os.getenv("KIMI_BASE_URL", "https://api.kimi.com/coding")
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChatMessage:
|
||||
role: str
|
||||
content: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class EntityExtractionResult:
|
||||
name: str
|
||||
@@ -27,6 +29,7 @@ class EntityExtractionResult:
|
||||
definition: str
|
||||
confidence: float
|
||||
|
||||
|
||||
@dataclass
|
||||
class RelationExtractionResult:
|
||||
source: str
|
||||
@@ -34,15 +37,21 @@ class RelationExtractionResult:
|
||||
type: str
|
||||
confidence: float
|
||||
|
||||
|
||||
class LLMClient:
|
||||
"""Kimi API 客户端"""
|
||||
|
||||
def __init__(self, api_key: str = None, base_url: str = None):
|
||||
self.api_key = api_key or KIMI_API_KEY
|
||||
self.base_url = base_url or KIMI_BASE_URL
|
||||
self.headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
async def chat(self, messages: list[ChatMessage], temperature: float = 0.3, stream: bool = False) -> str:
|
||||
async def chat(
|
||||
self, messages: list[ChatMessage], temperature: float = 0.3, stream: bool = False
|
||||
) -> str:
|
||||
"""发送聊天请求"""
|
||||
if not self.api_key:
|
||||
raise ValueError("KIMI_API_KEY not set")
|
||||
@@ -56,13 +65,18 @@ class LLMClient:
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{self.base_url}/v1/chat/completions", headers=self.headers, json=payload, timeout=120.0
|
||||
f"{self.base_url}/v1/chat/completions",
|
||||
headers=self.headers,
|
||||
json=payload,
|
||||
timeout=120.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
return result["choices"][0]["message"]["content"]
|
||||
|
||||
async def chat_stream(self, messages: list[ChatMessage], temperature: float = 0.3) -> AsyncGenerator[str, None]:
|
||||
async def chat_stream(
|
||||
self, messages: list[ChatMessage], temperature: float = 0.3
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""流式聊天请求"""
|
||||
if not self.api_key:
|
||||
raise ValueError("KIMI_API_KEY not set")
|
||||
@@ -76,7 +90,11 @@ class LLMClient:
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with client.stream(
|
||||
"POST", f"{self.base_url}/v1/chat/completions", headers=self.headers, json=payload, timeout=120.0
|
||||
"POST",
|
||||
f"{self.base_url}/v1/chat/completions",
|
||||
headers=self.headers,
|
||||
json=payload,
|
||||
timeout=120.0,
|
||||
) as response:
|
||||
response.raise_for_status()
|
||||
async for line in response.aiter_lines():
|
||||
@@ -164,7 +182,9 @@ class LLMClient:
|
||||
请用中文回答,保持简洁专业。如果信息不足,请明确说明。"""
|
||||
|
||||
messages = [
|
||||
ChatMessage(role="system", content="你是一个专业的项目分析助手,擅长从会议记录中提取洞察。"),
|
||||
ChatMessage(
|
||||
role="system", content="你是一个专业的项目分析助手,擅长从会议记录中提取洞察。"
|
||||
),
|
||||
ChatMessage(role="user", content=prompt),
|
||||
]
|
||||
|
||||
@@ -211,7 +231,10 @@ class LLMClient:
|
||||
async def analyze_entity_evolution(self, entity_name: str, mentions: list[dict]) -> str:
|
||||
"""分析实体在项目中的演变/态度变化"""
|
||||
mentions_text = "\n".join(
|
||||
[f"[{m.get('created_at', '未知时间')}] {m.get('text_snippet', '')}" for m in mentions[:20]] # 限制数量
|
||||
[
|
||||
f"[{m.get('created_at', '未知时间')}] {m.get('text_snippet', '')}"
|
||||
for m in mentions[:20]
|
||||
] # 限制数量
|
||||
)
|
||||
|
||||
prompt = f"""分析实体 "{entity_name}" 在项目中的演变和态度变化:
|
||||
@@ -230,9 +253,11 @@ class LLMClient:
|
||||
messages = [ChatMessage(role="user", content=prompt)]
|
||||
return await self.chat(messages, temperature=0.3)
|
||||
|
||||
|
||||
# Singleton instance
|
||||
_llm_client = None
|
||||
|
||||
|
||||
def get_llm_client() -> LLMClient:
|
||||
global _llm_client
|
||||
if _llm_client is None:
|
||||
|
||||
Reference in New Issue
Block a user