- agent/main.py: 添加日志记录功能 - agent/llm: 更新 anthropic, openai, factory - agent/core/agent.py: 更新 - server: agent_handler, agent_service, chat_service 更新 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
97 lines
2.9 KiB
Python
97 lines
2.9 KiB
Python
"""
|
||
Anthropic LLM 实现
|
||
"""
|
||
import os
|
||
from typing import Dict, Any, List, Optional
|
||
from anthropic import AsyncAnthropic
|
||
|
||
|
||
class AnthropicLLM:
|
||
"""Anthropic Claude LLM"""
|
||
|
||
def __init__(self, model_name: str = "claude-3-sonnet-20240229", api_key: Optional[str] = None):
|
||
self.model_name = model_name
|
||
self.client = AsyncAnthropic(
|
||
api_key=api_key or os.getenv("ANTHROPIC_API_KEY", "")
|
||
)
|
||
|
||
async def decide(self, prompt: str) -> Dict[str, Any]:
|
||
"""
|
||
LLM 决策 - 判断是否需要调用技能
|
||
|
||
Args:
|
||
prompt: 完整的 Prompt
|
||
|
||
Returns:
|
||
Dict: 包含 needs_skill, tool_calls, response 等
|
||
"""
|
||
system_prompt = """你是一个智能助手。请分析用户请求,判断是否需要调用工具来回答问题。
|
||
|
||
如果需要调用工具,请按以下格式返回 JSON:
|
||
{"needs_skill": true, "tool_calls": [{"skill_id": "工具名称", "parameters": {"参数": "值"}, "reason": "调用原因"}]}
|
||
|
||
如果不需要调用工具,请返回:
|
||
{"needs_skill": false, "response": "直接回答用户的内容"}
|
||
|
||
请只返回 JSON,不要其他内容。"""
|
||
|
||
try:
|
||
response = await self.client.messages.create(
|
||
model=self.model_name,
|
||
max_tokens=2000,
|
||
system=system_prompt,
|
||
messages=[{"role": "user", "content": prompt}]
|
||
)
|
||
|
||
content = response.content[0].text
|
||
|
||
# 尝试解析 JSON
|
||
import json
|
||
try:
|
||
result = json.loads(content)
|
||
return result
|
||
except json.JSONDecodeError:
|
||
return {
|
||
"needs_skill": False,
|
||
"response": content
|
||
}
|
||
|
||
except Exception as e:
|
||
return {
|
||
"needs_skill": False,
|
||
"response": f"LLM 调用失败: {str(e)}"
|
||
}
|
||
|
||
async def generate(self, prompt: str, tool_results: List[Dict]) -> str:
|
||
"""
|
||
生成回复
|
||
|
||
Args:
|
||
prompt: 完整的 Prompt
|
||
tool_results: 工具调用结果
|
||
|
||
Returns:
|
||
str: 生成的回复
|
||
"""
|
||
user_message = prompt
|
||
|
||
# 添加工具结果作为上下文
|
||
if tool_results:
|
||
tool_context = "\n\n工具返回结果:\n"
|
||
for result in tool_results:
|
||
if result.get("success"):
|
||
tool_context += f"- {result.get('skill_id')}: {result.get('result')}\n"
|
||
user_message += tool_context
|
||
|
||
try:
|
||
response = await self.client.messages.create(
|
||
model=self.model_name,
|
||
max_tokens=4000,
|
||
messages=[{"role": "user", "content": user_message}]
|
||
)
|
||
|
||
return response.content[0].text
|
||
|
||
except Exception as e:
|
||
return f"生成回复失败: {str(e)}"
|