feat: Agent 服务日志功能和后端更新
- 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>
This commit is contained in:
@@ -296,7 +296,7 @@ func main() {
|
||||
toolService := service.NewToolService(toolRepo)
|
||||
mcpService := service.NewMCPService(mcpRepo)
|
||||
skillService := service.NewSkillService(skillRepo)
|
||||
agentService := service.NewAgentService(cfg.PythonServiceURL)
|
||||
agentService := service.NewAgentService(cfg.PythonServiceURL, modelRepo)
|
||||
memoryService := service.NewMemoryService(agentRepo)
|
||||
|
||||
// 4.2 初始化默认工具
|
||||
|
||||
@@ -25,6 +25,7 @@ type ChatRequest struct {
|
||||
AgentID int `json:"agent_id" binding:"required"`
|
||||
Message string `json:"message" binding:"required"`
|
||||
SessionID string `json:"session_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
}
|
||||
|
||||
// ChatResponse 对话响应
|
||||
@@ -54,6 +55,7 @@ func (h *AgentHandler) Chat(c *gin.Context) {
|
||||
Message: req.Message,
|
||||
UserID: userID,
|
||||
SessionID: req.SessionID,
|
||||
ModelID: req.ModelID,
|
||||
}
|
||||
|
||||
result, err := h.agentService.Chat(pythonReq)
|
||||
|
||||
@@ -105,6 +105,7 @@ type AgentRequest struct {
|
||||
AgentID string `json:"agent_id" binding:"required"`
|
||||
Message string `json:"message" binding:"required"`
|
||||
SessionID string `json:"session_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
Context map[string]interface{} `json:"context"`
|
||||
}
|
||||
|
||||
|
||||
@@ -5,16 +5,24 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"x-agents/server/internal/repository"
|
||||
)
|
||||
|
||||
// AgentChatRequest Python Agent 对话请求
|
||||
type AgentChatRequest struct {
|
||||
AgentID int `json:"agent_id"`
|
||||
Message string `json:"message"`
|
||||
UserID int `json:"user_id"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
AgentID int `json:"agent_id"`
|
||||
Message string `json:"message"`
|
||||
UserID int `json:"user_id"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
ModelID string `json:"model_id,omitempty"`
|
||||
ModelName string `json:"model_name,omitempty"`
|
||||
ModelProvider string `json:"model_provider,omitempty"`
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
BaseURL string `json:"base_url,omitempty"`
|
||||
}
|
||||
|
||||
// AgentChatResponse Python Agent 对话响应
|
||||
@@ -51,20 +59,55 @@ type TeamChatResponse struct {
|
||||
type AgentService struct {
|
||||
pythonURL string
|
||||
client *http.Client
|
||||
modelRepo *repository.ModelRepository
|
||||
}
|
||||
|
||||
// NewAgentService 创建 Agent 服务
|
||||
func NewAgentService(pythonURL string) *AgentService {
|
||||
func NewAgentService(pythonURL string, modelRepo *repository.ModelRepository) *AgentService {
|
||||
return &AgentService{
|
||||
pythonURL: pythonURL,
|
||||
client: &http.Client{
|
||||
Timeout: 120 * time.Second, // Agent 可能需要较长时间
|
||||
},
|
||||
modelRepo: modelRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// Chat 单智能体对话
|
||||
func (s *AgentService) Chat(req AgentChatRequest) (*AgentChatResponse, error) {
|
||||
// 如果传入了 model_id,查询模型配置获取 api_key 和 base_url
|
||||
log.Printf("[AgentService] Chat called, model_id: %s, modelRepo: %v", req.ModelID, s.modelRepo != nil)
|
||||
|
||||
if req.ModelID != "" && s.modelRepo != nil {
|
||||
model, err := s.modelRepo.FindByID(req.ModelID)
|
||||
if err != nil {
|
||||
log.Printf("[AgentService] Error finding model: %v", err)
|
||||
} else if model != nil {
|
||||
log.Printf("[AgentService] Found model: id=%s, provider=%s, model=%s, base_url=%s, api_key_len=%d",
|
||||
model.ID, model.Provider, model.Model, model.BaseURL, len(model.APIKey))
|
||||
req.APIKey = model.APIKey
|
||||
req.BaseURL = model.BaseURL
|
||||
req.ModelProvider = model.Provider
|
||||
req.ModelName = model.Model
|
||||
log.Printf("[AgentService] Set req.APIKey=%s, req.BaseURL=%s", req.APIKey[:10]+"...", req.BaseURL)
|
||||
} else {
|
||||
log.Printf("[AgentService] Model not found for id: %s", req.ModelID)
|
||||
}
|
||||
} else if s.modelRepo == nil {
|
||||
log.Printf("[AgentService] WARNING: modelRepo is nil!")
|
||||
}
|
||||
|
||||
// 打印传给 Python 的请求内容
|
||||
apiKeyPreview := ""
|
||||
if req.APIKey != "" {
|
||||
apiKeyPreview = req.APIKey
|
||||
if len(apiKeyPreview) > 10 {
|
||||
apiKeyPreview = apiKeyPreview[:10] + "..."
|
||||
}
|
||||
}
|
||||
log.Printf("[AgentService] Sending to Python: model_id=%s, api_key=%s, base_url=%s, provider=%s, model=%s",
|
||||
req.ModelID, apiKeyPreview, req.BaseURL, req.ModelProvider, req.ModelName)
|
||||
|
||||
url := fmt.Sprintf("%s/agent/chat", s.pythonURL)
|
||||
|
||||
jsonData, err := json.Marshal(req)
|
||||
|
||||
@@ -16,13 +16,15 @@ import (
|
||||
|
||||
type ChatService struct {
|
||||
pythonURL string
|
||||
agentRepo *repository.AgentRepository
|
||||
agentRepo *repository.AgentRepository
|
||||
modelRepo *repository.ModelRepository
|
||||
}
|
||||
|
||||
func NewChatService(pythonURL string, agentRepo *repository.AgentRepository) *ChatService {
|
||||
func NewChatService(pythonURL string, agentRepo *repository.AgentRepository, modelRepo *repository.ModelRepository) *ChatService {
|
||||
return &ChatService{
|
||||
pythonURL: pythonURL,
|
||||
agentRepo: agentRepo,
|
||||
agentRepo: agentRepo,
|
||||
modelRepo: modelRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +32,19 @@ type ChatRequest struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
Message string `json:"message"`
|
||||
SessionID string `json:"session_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
Context map[string]interface{} `json:"context"`
|
||||
}
|
||||
|
||||
// ModelConfig 模型配置,用于传递给 Python 服务
|
||||
type ModelConfig struct {
|
||||
Provider string `json:"provider"`
|
||||
Model string `json:"model"`
|
||||
APIKey string `json:"api_key"`
|
||||
BaseURL string `json:"base_url"`
|
||||
APIEndpoint string `json:"api_endpoint"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
Reply string `json:"reply"`
|
||||
SessionID string `json:"session_id"`
|
||||
@@ -59,14 +71,40 @@ func (s *ChatService) Chat(ctx context.Context, userID string, req model.AgentRe
|
||||
sessionID = uuid.New().String()
|
||||
}
|
||||
|
||||
// 4. 调用 Python 服务
|
||||
// 4. 如果提供了 ModelID,获取模型配置
|
||||
var modelConfig *ModelConfig
|
||||
if req.ModelID != "" {
|
||||
modelInfo, err := s.modelRepo.FindByID(req.ModelID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("model not found: %w", err)
|
||||
}
|
||||
modelConfig = &ModelConfig{
|
||||
Provider: modelInfo.Provider,
|
||||
Model: modelInfo.Model,
|
||||
APIKey: modelInfo.APIKey,
|
||||
BaseURL: modelInfo.BaseURL,
|
||||
APIEndpoint: modelInfo.APIEndpoint,
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 调用 Python 服务
|
||||
pythonReq := ChatRequest{
|
||||
AgentID: req.AgentID,
|
||||
Message: req.Message,
|
||||
SessionID: sessionID,
|
||||
ModelID: req.ModelID,
|
||||
Context: req.Context,
|
||||
}
|
||||
|
||||
// 将模型配置放入 Context 中传递给 Python 服务
|
||||
if modelConfig != nil {
|
||||
pythonReq.Context = make(map[string]interface{})
|
||||
for k, v := range req.Context {
|
||||
pythonReq.Context[k] = v
|
||||
}
|
||||
pythonReq.Context["model_config"] = modelConfig
|
||||
}
|
||||
|
||||
pythonResp, err := s.callPythonChat(ctx, pythonReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to call python service: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user