llm_client_driver.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package models
  2. import (
  3. "context"
  4. "github.com/mark3labs/mcp-go/mcp"
  5. llm "yunion.io/x/onecloud/pkg/apis/llm"
  6. )
  7. type ILLMChatMessage interface {
  8. GetRole() string
  9. GetContent() string
  10. GetToolCalls() []ILLMToolCall
  11. }
  12. // ILLMToolCall 表示工具调用接口
  13. type ILLMToolCall interface {
  14. GetId() string
  15. GetFunction() ILLMFunctionCall
  16. GetIndex() int
  17. }
  18. // ILLMFunctionCall 表示函数调用详情接口
  19. type ILLMFunctionCall interface {
  20. GetName() string
  21. GetArguments() map[string]interface{}
  22. GetRawArguments() string
  23. }
  24. // ILLMTool 表示工具定义接口
  25. type ILLMTool interface {
  26. GetType() string
  27. GetFunction() ILLMToolFunction
  28. }
  29. // ILLMToolFunction 表示工具函数定义接口
  30. type ILLMToolFunction interface {
  31. GetName() string
  32. GetDescription() string
  33. GetParameters() map[string]interface{}
  34. }
  35. // ILLMChatResponse 表示 LLM 聊天响应接口
  36. // 参考 mcp_agent.go 中的 LLMChatResponse 接口设计
  37. type ILLMChatResponse interface {
  38. // HasToolCalls 检查响应是否包含工具调用
  39. HasToolCalls() bool
  40. // GetToolCalls 获取工具调用列表
  41. GetToolCalls() []ILLMToolCall
  42. // GetContent 获取响应内容
  43. GetContent() string
  44. // GetReasoningContent 获取推理/思考内容(如 DeepSeek reasoning_content)
  45. GetReasoningContent() string
  46. }
  47. type ILLMClient interface {
  48. GetType() llm.LLMClientType
  49. Chat(ctx context.Context, mcpAgent *SMCPAgent, messages interface{}, tools interface{}) (ILLMChatResponse, error)
  50. ChatStream(ctx context.Context, mcpAgent *SMCPAgent, messages interface{}, tools interface{}, onChunk func(ILLMChatResponse) error) error
  51. NewUserMessage(content string) ILLMChatMessage
  52. NewAssistantMessage(content string) ILLMChatMessage
  53. NewAssistantMessageWithToolCalls(toolCalls []ILLMToolCall) ILLMChatMessage
  54. NewAssistantMessageWithToolCallsAndReasoning(reasoningContent, content string, toolCalls []ILLMToolCall) ILLMChatMessage
  55. NewToolMessage(toolId string, toolName string, content string) ILLMChatMessage
  56. NewSystemMessage(content string) ILLMChatMessage
  57. ConvertMCPTools(mcpTools []mcp.Tool) []ILLMTool
  58. }
  59. type ILLMClientModelLister interface {
  60. ListModels(ctx context.Context, endpoint string) ([]string, error)
  61. }
  62. type SLLMToolCall struct {
  63. Id string
  64. Function SLLMFunctionCall
  65. }
  66. func (tc *SLLMToolCall) GetId() string {
  67. return tc.Id
  68. }
  69. func (tc *SLLMToolCall) GetIndex() int {
  70. return 0
  71. }
  72. func (tc *SLLMToolCall) GetFunction() ILLMFunctionCall {
  73. return &tc.Function
  74. }
  75. type SLLMFunctionCall struct {
  76. Name string
  77. Arguments map[string]interface{}
  78. }
  79. func (fc *SLLMFunctionCall) GetName() string {
  80. return fc.Name
  81. }
  82. func (fc *SLLMFunctionCall) GetRawArguments() string {
  83. return ""
  84. }
  85. func (fc *SLLMFunctionCall) GetArguments() map[string]interface{} {
  86. return fc.Arguments
  87. }
  88. var (
  89. llmClientDrivers = newDrivers()
  90. )
  91. func RegisterLLMClientDriver(drv ILLMClient) {
  92. registerDriver(llmClientDrivers, drv.GetType(), drv)
  93. }
  94. func GetLLMClientDriver(typ llm.LLMClientType) ILLMClient {
  95. return getDriver[llm.LLMClientType, ILLMClient](llmClientDrivers, typ)
  96. }
  97. func GetLLMClientDriverWithError(typ llm.LLMClientType) (ILLMClient, error) {
  98. return getDriverWithError[llm.LLMClientType, ILLMClient](llmClientDrivers, typ)
  99. }