Files
X-Agents/agent/app/agent/llm/anthropic.py
DESKTOP-72TV0V4\caoxiaozhu f9660a3d7b feat: 新增 Agent 应用核心代码
- supervisor, memory, skills 模块
- LLM 工厂

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 16:26:33 +08:00

97 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Anthropic LLM 实现
"""
import os
from typing import Dict, Any, List
from anthropic import AsyncAnthropic
class AnthropicLLM:
"""Anthropic Claude LLM"""
def __init__(self, model_name: str = "claude-3-sonnet-20240229"):
self.model_name = model_name
self.client = AsyncAnthropic(
api_key=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)}"