mcp_agent.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package llm
  2. import (
  3. "yunion.io/x/pkg/util/sets"
  4. "yunion.io/x/onecloud/pkg/apis"
  5. )
  6. // LLMClientType 定义 LLM 驱动类型
  7. type LLMClientType string
  8. const (
  9. LLM_CLIENT_OLLAMA LLMClientType = "ollama"
  10. LLM_CLIENT_OPENAI LLMClientType = "openai"
  11. MCP_AGENT_SYSTEM_PROMPT = `你是一个 Cloudpods 云平台管理助手。你可以使用提供的工具来帮助用户管理云资源。
  12. ## 你的能力
  13. - 查询云平台资源(虚拟机、镜像、网络、存储、区域等)
  14. - 管理虚拟机(创建、启动、停止、重启、删除、重置密码)
  15. - 获取虚拟机监控信息和实时统计数据
  16. ## 重要规则(必须严格遵守)
  17. **如果用户的问题涉及查询、创建、修改或删除云资源,你必须先调用相应的工具,而不是直接回答。**
  18. - 对于需要查询资源的问题(如"列出虚拟机"、"查询状态"等),必须调用工具获取数据后再回答
  19. - 对于需要操作资源的问题(如"创建"、"启动"、"停止"等),必须调用工具执行操作后再回答
  20. - 只有在以下情况才可以直接回复:
  21. 1. 用户只是询问一般性问题(如"你能做什么"、"如何使用"等)
  22. 2. 没有合适的工具可以解决用户的问题
  23. 3. 工具调用失败后需要向用户说明错误原因
  24. ## 工作流程
  25. 1. 理解用户的需求
  26. 2. **优先检查是否有合适的工具可以完成任务,如果有则必须调用工具**
  27. 3. 分析工具返回的结果
  28. 4. 如果需要更多信息,继续调用其他工具
  29. 5. 最后用自然语言总结结果给用户
  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. // IsLLMClientType 检查给定的字符串是否是有效的 LLM 驱动类型
  44. func IsLLMClientType(t string) bool {
  45. return LLM_CLIENT_TYPES.Has(t)
  46. }
  47. // MCP Agent 配置相关的 API 定义
  48. type MCPAgentListInput struct {
  49. apis.SharableVirtualResourceListInput
  50. LLMDriver string `json:"llm_driver"`
  51. DefaultAgent *bool `json:"default_agent,omitempty" help:"filter by default agent (true to get the default one)"`
  52. }
  53. type MCPAgentCreateInput struct {
  54. apis.SharableVirtualResourceCreateInput
  55. LLMId string `json:"llm_id" help:"LLM 实例 ID,如果提供则自动获取 llm_url"`
  56. LLMUrl string `json:"llm_url" help:"后端大模型的 base 请求地址"`
  57. LLMDriver string `json:"llm_driver" help:"使用的大模型驱动,可以是 ollama 或 openai"`
  58. Model string `json:"model" help:"使用的模型名称"`
  59. ApiKey string `json:"api_key" help:"在 llm_driver 中需要用到的认证"`
  60. McpServer string `json:"mcp_server" help:"mcp 服务器的后端地址"`
  61. DefaultAgent *bool `json:"default_agent,omitempty" help:"set as default MCP agent (only one can be true globally)"`
  62. }
  63. type MCPAgentUpdateInput struct {
  64. apis.SharableVirtualResourceCreateInput
  65. LLMId *string `json:"llm_id,omitempty" help:"LLM 实例 ID,如果提供则自动获取 llm_url"`
  66. LLMUrl *string `json:"llm_url,omitempty" help:"后端大模型的 base 请求地址"`
  67. LLMDriver *string `json:"llm_driver,omitempty" help:"使用的大模型驱动,可以是 ollama 或 openai"`
  68. Model *string `json:"model,omitempty" help:"使用的模型名称"`
  69. ApiKey *string `json:"api_key,omitempty" help:"在 llm_driver 中需要用到的认证"`
  70. McpServer *string `json:"mcp_server,omitempty" help:"mcp 服务器的后端地址"`
  71. DefaultAgent *bool `json:"default_agent,omitempty" help:"set as default MCP agent (only one can be true globally)"`
  72. }
  73. type MCPAgentDetails struct {
  74. apis.SharableVirtualResourceDetails
  75. LLMId string `json:"llm_id"`
  76. LLMName string `json:"llm_name"`
  77. DefaultAgent bool `json:"default_agent"`
  78. }
  79. type LLMToolRequestInput struct {
  80. ToolName string `json:"tool_name"`
  81. Arguments map[string]interface{} `json:"arguments"`
  82. }
  83. type LLMMCPAgentRequestInput struct {
  84. Message string `json:"message" help:"message to send to MCP agent"`
  85. History []MCPAgentChatMessage `json:"history" help:"chat history"`
  86. }
  87. type MCPAgentChatMessage struct {
  88. Role string `json:"role"`
  89. Content string `json:"content"`
  90. // ToolCalls []MCPAgentToolCallRecord `json:"tool_calls,omitempty"`
  91. }
  92. // MCPAgentResponse 表示 Agent 响应
  93. type MCPAgentResponse struct {
  94. // Success 是否成功
  95. Success bool `json:"success"`
  96. // Answer 自然语言回答
  97. Answer string `json:"answer"`
  98. // Error 错误信息
  99. Error string `json:"error,omitempty"`
  100. // ToolCalls 工具调用记录
  101. ToolCalls []MCPAgentToolCallRecord `json:"tool_calls,omitempty"`
  102. }
  103. // MCPAgentToolCallRecord 记录工具调用
  104. type MCPAgentToolCallRecord struct {
  105. Id string `json:"id,omitempty"`
  106. ToolName string `json:"tool_name"`
  107. Arguments map[string]interface{} `json:"arguments"`
  108. Result string `json:"result"`
  109. }
  110. const (
  111. // MCPAgentMaxIterations 最大迭代次数,防止无限循环
  112. MCPAgentMaxIterations = 10
  113. )