fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -15,6 +15,7 @@ import httpx
|
||||
KIMI_API_KEY = os.getenv("KIMI_API_KEY", "")
|
||||
KIMI_BASE_URL = os.getenv("KIMI_BASE_URL", "https://api.kimi.com/coding")
|
||||
|
||||
|
||||
class ReasoningType(Enum):
|
||||
"""推理类型"""
|
||||
|
||||
@@ -24,6 +25,7 @@ class ReasoningType(Enum):
|
||||
COMPARATIVE = "comparative" # 对比推理
|
||||
SUMMARY = "summary" # 总结推理
|
||||
|
||||
|
||||
@dataclass
|
||||
class ReasoningResult:
|
||||
"""推理结果"""
|
||||
@@ -35,6 +37,7 @@ class ReasoningResult:
|
||||
related_entities: list[str] # 相关实体
|
||||
gaps: list[str] # 知识缺口
|
||||
|
||||
|
||||
@dataclass
|
||||
class InferencePath:
|
||||
"""推理路径"""
|
||||
@@ -44,24 +47,35 @@ class InferencePath:
|
||||
path: list[dict] # 路径上的节点和关系
|
||||
strength: float # 路径强度
|
||||
|
||||
|
||||
class KnowledgeReasoner:
|
||||
"""知识推理引擎"""
|
||||
|
||||
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 _call_llm(self, prompt: str, temperature: float = 0.3) -> str:
|
||||
"""调用 LLM"""
|
||||
if not self.api_key:
|
||||
raise ValueError("KIMI_API_KEY not set")
|
||||
|
||||
payload = {"model": "k2p5", "messages": [{"role": "user", "content": prompt}], "temperature": temperature}
|
||||
payload = {
|
||||
"model": "k2p5",
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"temperature": temperature,
|
||||
}
|
||||
|
||||
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()
|
||||
@@ -124,7 +138,9 @@ class KnowledgeReasoner:
|
||||
|
||||
return {"type": "factual", "entities": [], "intent": "general", "complexity": "simple"}
|
||||
|
||||
async def _causal_reasoning(self, query: str, project_context: dict, graph_data: dict) -> ReasoningResult:
|
||||
async def _causal_reasoning(
|
||||
self, query: str, project_context: dict, graph_data: dict
|
||||
) -> ReasoningResult:
|
||||
"""因果推理 - 分析原因和影响"""
|
||||
|
||||
# 构建因果分析提示
|
||||
@@ -183,7 +199,9 @@ class KnowledgeReasoner:
|
||||
gaps=["无法完成因果推理"],
|
||||
)
|
||||
|
||||
async def _comparative_reasoning(self, query: str, project_context: dict, graph_data: dict) -> ReasoningResult:
|
||||
async def _comparative_reasoning(
|
||||
self, query: str, project_context: dict, graph_data: dict
|
||||
) -> ReasoningResult:
|
||||
"""对比推理 - 比较实体间的异同"""
|
||||
|
||||
prompt = f"""基于以下知识图谱进行对比分析:
|
||||
@@ -235,7 +253,9 @@ class KnowledgeReasoner:
|
||||
gaps=[],
|
||||
)
|
||||
|
||||
async def _temporal_reasoning(self, query: str, project_context: dict, graph_data: dict) -> ReasoningResult:
|
||||
async def _temporal_reasoning(
|
||||
self, query: str, project_context: dict, graph_data: dict
|
||||
) -> ReasoningResult:
|
||||
"""时序推理 - 分析时间线和演变"""
|
||||
|
||||
prompt = f"""基于以下知识图谱进行时序分析:
|
||||
@@ -287,7 +307,9 @@ class KnowledgeReasoner:
|
||||
gaps=[],
|
||||
)
|
||||
|
||||
async def _associative_reasoning(self, query: str, project_context: dict, graph_data: dict) -> ReasoningResult:
|
||||
async def _associative_reasoning(
|
||||
self, query: str, project_context: dict, graph_data: dict
|
||||
) -> ReasoningResult:
|
||||
"""关联推理 - 发现实体间的隐含关联"""
|
||||
|
||||
prompt = f"""基于以下知识图谱进行关联分析:
|
||||
@@ -360,7 +382,9 @@ class KnowledgeReasoner:
|
||||
adj[tgt] = []
|
||||
adj[src].append({"target": tgt, "relation": r.get("type", "related"), "data": r})
|
||||
# 无向图也添加反向
|
||||
adj[tgt].append({"target": src, "relation": r.get("type", "related"), "data": r, "reverse": True})
|
||||
adj[tgt].append(
|
||||
{"target": src, "relation": r.get("type", "related"), "data": r, "reverse": True}
|
||||
)
|
||||
|
||||
# BFS 搜索路径
|
||||
from collections import deque
|
||||
@@ -478,9 +502,11 @@ class KnowledgeReasoner:
|
||||
"confidence": 0.5,
|
||||
}
|
||||
|
||||
|
||||
# Singleton instance
|
||||
_reasoner = None
|
||||
|
||||
|
||||
def get_knowledge_reasoner() -> KnowledgeReasoner:
|
||||
global _reasoner
|
||||
if _reasoner is None:
|
||||
|
||||
Reference in New Issue
Block a user