146 lines
3.9 KiB
Go
146 lines
3.9 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 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"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// AgentChatResponse Python Agent 对话响应
|
||
|
|
type AgentChatResponse struct {
|
||
|
|
AgentID int `json:"agent_id"`
|
||
|
|
Response string `json:"response"`
|
||
|
|
ToolCalls []interface{} `json:"tool_calls"`
|
||
|
|
TokensUsed int `json:"tokens_used"`
|
||
|
|
DurationMs int `json:"duration_ms"`
|
||
|
|
SessionID string `json:"session_id"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// TeamChatRequest 多智能体群聊请求
|
||
|
|
type TeamChatRequest struct {
|
||
|
|
SupervisorAgentID int `json:"supervisor_agent_id"`
|
||
|
|
MemberAgentIDs []int `json:"member_agent_ids"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
UserID int `json:"user_id"`
|
||
|
|
SessionID string `json:"session_id,omitempty"`
|
||
|
|
Strategy string `json:"strategy,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// TeamChatResponse 多智能体群聊响应
|
||
|
|
type TeamChatResponse struct {
|
||
|
|
SupervisorAgentID int `json:"supervisor_agent_id"`
|
||
|
|
Response string `json:"response"`
|
||
|
|
SubtaskResults []interface{} `json:"subtask_results"`
|
||
|
|
Strategy string `json:"strategy"`
|
||
|
|
DurationMs int `json:"duration_ms"`
|
||
|
|
SessionID string `json:"session_id"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// AgentService Python Agent 服务
|
||
|
|
type AgentService struct {
|
||
|
|
pythonURL string
|
||
|
|
client *http.Client
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewAgentService 创建 Agent 服务
|
||
|
|
func NewAgentService(pythonURL string) *AgentService {
|
||
|
|
return &AgentService{
|
||
|
|
pythonURL: pythonURL,
|
||
|
|
client: &http.Client{
|
||
|
|
Timeout: 120 * time.Second, // Agent 可能需要较长时间
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Chat 单智能体对话
|
||
|
|
func (s *AgentService) Chat(req AgentChatRequest) (*AgentChatResponse, error) {
|
||
|
|
url := fmt.Sprintf("%s/agent/chat", s.pythonURL)
|
||
|
|
|
||
|
|
jsonData, err := json.Marshal(req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
||
|
|
}
|
||
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
||
|
|
|
||
|
|
resp, err := s.client.Do(httpReq)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to call python agent: %w", err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
body, err := io.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if resp.StatusCode != http.StatusOK {
|
||
|
|
return nil, fmt.Errorf("python agent error: %s", string(body))
|
||
|
|
}
|
||
|
|
|
||
|
|
var result AgentChatResponse
|
||
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// TeamChat 多智能体群聊
|
||
|
|
func (s *AgentService) TeamChat(req TeamChatRequest) (*TeamChatResponse, error) {
|
||
|
|
url := fmt.Sprintf("%s/agent/team/chat", s.pythonURL)
|
||
|
|
|
||
|
|
// 设置默认策略
|
||
|
|
if req.Strategy == "" {
|
||
|
|
req.Strategy = "parallel"
|
||
|
|
}
|
||
|
|
|
||
|
|
jsonData, err := json.Marshal(req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
||
|
|
}
|
||
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
||
|
|
|
||
|
|
resp, err := s.client.Do(httpReq)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to call python agent: %w", err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
body, err := io.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if resp.StatusCode != http.StatusOK {
|
||
|
|
return nil, fmt.Errorf("python agent error: %s", string(body))
|
||
|
|
}
|
||
|
|
|
||
|
|
var result TeamChatResponse
|
||
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &result, nil
|
||
|
|
}
|