|
| 1 | +package llm |
| 2 | + |
| 3 | +import ( |
| 4 | + "yunion.io/x/pkg/util/sets" |
| 5 | + |
| 6 | + "yunion.io/x/onecloud/pkg/apis" |
| 7 | +) |
| 8 | + |
| 9 | +// LLMClientType 定义 LLM 驱动类型 |
| 10 | +type LLMClientType string |
| 11 | + |
| 12 | +const ( |
| 13 | + LLM_CLIENT_OLLAMA LLMClientType = "ollama" |
| 14 | + LLM_CLIENT_OPENAI LLMClientType = "openai" |
| 15 | + |
| 16 | + MCP_AGENT_SYSTEM_PROMPT = `你是一个 Cloudpods 云平台管理助手。你可以使用提供的工具来帮助用户管理云资源。 |
| 17 | +
|
| 18 | +## 你的能力 |
| 19 | +- 查询云平台资源(虚拟机、镜像、网络、存储、区域等) |
| 20 | +- 管理虚拟机(创建、启动、停止、重启、删除、重置密码) |
| 21 | +- 获取虚拟机监控信息和实时统计数据 |
| 22 | +
|
| 23 | +## 工作流程 |
| 24 | +1. 理解用户的需求 |
| 25 | +2. 选择合适的工具来完成任务 |
| 26 | +3. 分析工具返回的结果 |
| 27 | +4. 如果需要更多信息,继续调用其他工具 |
| 28 | +5. 最后用自然语言总结结果给用户 |
| 29 | +
|
| 30 | +## 注意事项 |
| 31 | +- 认证信息已由系统自动处理,调用工具时无需提供认证参数 |
| 32 | +- 如果工具调用失败,尝试分析错误原因并告知用户 |
| 33 | +- 回复时使用中文,语言简洁明了 |
| 34 | +` |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + LLM_CLIENT_TYPES = sets.NewString( |
| 39 | + string(LLM_CLIENT_OLLAMA), |
| 40 | + string(LLM_CLIENT_OPENAI), |
| 41 | + ) |
| 42 | +) |
| 43 | + |
| 44 | +// IsLLMClientType 检查给定的字符串是否是有效的 LLM 驱动类型 |
| 45 | +func IsLLMClientType(t string) bool { |
| 46 | + return LLM_CLIENT_TYPES.Has(t) |
| 47 | +} |
| 48 | + |
| 49 | +// MCP Agent 配置相关的 API 定义 |
| 50 | +type MCPAgentListInput struct { |
| 51 | + apis.SharableVirtualResourceListInput |
| 52 | + |
| 53 | + LLMDriver string `json:"llm_driver"` |
| 54 | +} |
| 55 | + |
| 56 | +type MCPAgentCreateInput struct { |
| 57 | + apis.SharableVirtualResourceCreateInput |
| 58 | + |
| 59 | + LLMId string `json:"llm_id" help:"LLM 实例 ID,如果提供则自动获取 llm_url"` |
| 60 | + LLMUrl string `json:"llm_url" help:"后端大模型的 base 请求地址"` |
| 61 | + LLMDriver string `json:"llm_driver" help:"使用的大模型驱动,可以是 ollama 或 openai"` |
| 62 | + Model string `json:"model" help:"使用的模型名称"` |
| 63 | + ApiKey string `json:"api_key" help:"在 llm_driver 中需要用到的认证"` |
| 64 | + McpServer string `json:"mcp_server" help:"mcp 服务器的后端地址"` |
| 65 | +} |
| 66 | + |
| 67 | +type MCPAgentUpdateInput struct { |
| 68 | + apis.SharableVirtualResourceCreateInput |
| 69 | + |
| 70 | + LLMId *string `json:"llm_id,omitempty" help:"LLM 实例 ID,如果提供则自动获取 llm_url"` |
| 71 | + LLMUrl *string `json:"llm_url,omitempty" help:"后端大模型的 base 请求地址"` |
| 72 | + LLMDriver *string `json:"llm_driver,omitempty" help:"使用的大模型驱动,可以是 ollama 或 openai"` |
| 73 | + Model *string `json:"model,omitempty" help:"使用的模型名称"` |
| 74 | + ApiKey *string `json:"api_key,omitempty" help:"在 llm_driver 中需要用到的认证"` |
| 75 | + McpServer *string `json:"mcp_server,omitempty" help:"mcp 服务器的后端地址"` |
| 76 | +} |
| 77 | + |
| 78 | +type MCPAgentDetails struct { |
| 79 | + apis.SharableVirtualResourceDetails |
| 80 | + |
| 81 | + LLMUrl string `json:"llm_url"` |
| 82 | + LLMDriver string `json:"llm_driver"` |
| 83 | + Model string `json:"model"` |
| 84 | + ApiKey string `json:"api_key"` |
| 85 | + McpServer string `json:"mcp_server"` |
| 86 | +} |
| 87 | + |
| 88 | +type LLMToolRequestInput struct { |
| 89 | + ToolName string `json:"tool_name"` |
| 90 | + Arguments map[string]interface{} `json:"arguments"` |
| 91 | +} |
| 92 | + |
| 93 | +type LLMChatTestInput struct { |
| 94 | + Message string `json:"message" help:"test message to send to LLM"` |
| 95 | +} |
| 96 | + |
| 97 | +type LLMMCPAgentRequestInput struct { |
| 98 | + Query string `json:"query" help:"query to send to MCP agent"` |
| 99 | +} |
| 100 | + |
| 101 | +// MCPAgentResponse 表示 Agent 响应 |
| 102 | +type MCPAgentResponse struct { |
| 103 | + // Success 是否成功 |
| 104 | + Success bool `json:"success"` |
| 105 | + // Answer 自然语言回答 |
| 106 | + Answer string `json:"answer"` |
| 107 | + // Error 错误信息 |
| 108 | + Error string `json:"error,omitempty"` |
| 109 | + // ToolCalls 工具调用记录 |
| 110 | + ToolCalls []MCPAgentToolCallRecord `json:"tool_calls,omitempty"` |
| 111 | +} |
| 112 | + |
| 113 | +// MCPAgentToolCallRecord 记录工具调用 |
| 114 | +type MCPAgentToolCallRecord struct { |
| 115 | + ToolName string `json:"tool_name"` |
| 116 | + Arguments map[string]interface{} `json:"arguments"` |
| 117 | + Result string `json:"result"` |
| 118 | +} |
| 119 | + |
| 120 | +const ( |
| 121 | + // MCPAgentMaxIterations 最大迭代次数,防止无限循环 |
| 122 | + MCPAgentMaxIterations = 10 |
| 123 | +) |
0 commit comments