diff --git a/internal/biz/router.go b/internal/biz/router.go index 8a40efe..1ad473e 100644 --- a/internal/biz/router.go +++ b/internal/biz/router.go @@ -198,7 +198,8 @@ func (r *AiRouterBiz) RouteWithSocket(c *websocket.Conn, req *entitys.ChatSockRe log.Info(resMsg) return errors.SysErr("数据结构错误:%v", err.Error()) } - + matchJson.History = pkg.JsonByteIgonErr(history) + matchJson.UserInput = req.Text if err := r.handleMatch(ctx, c, ch, &matchJson, task, sysInfo); err != nil { return err } @@ -268,13 +269,13 @@ func (r *AiRouterBiz) handleMatch(ctx context.Context, c *websocket.Conn, ch cha } func (r *AiRouterBiz) handleTask(channel chan entitys.Response, c *websocket.Conn, matchJson *entitys.Match, task *model.AiTask) (err error) { - var configData entitys.ConfigDataTool err = json.Unmarshal([]byte(task.Config), &configData) if err != nil { return } - err = r.toolManager.ExecuteTool(channel, c, configData.Tool, []byte(matchJson.Parameters)) + + err = r.toolManager.ExecuteTool(channel, c, configData.Tool, []byte(matchJson.Parameters), matchJson) if err != nil { return } @@ -358,7 +359,7 @@ func (r *AiRouterBiz) handleKnowle(channel chan entitys.Response, c *websocket.C } // 执行工具 - err = r.toolManager.ExecuteTool(channel, c, configData.Tool, b) + err = r.toolManager.ExecuteTool(channel, c, configData.Tool, b, nil) if err != nil { return } diff --git a/internal/entitys/types.go b/internal/entitys/types.go index 40d6c89..d437a86 100644 --- a/internal/entitys/types.go +++ b/internal/entitys/types.go @@ -73,7 +73,7 @@ type Tool interface { Name() string Description() string Definition() ToolDefinition - Execute(channel chan Response, c *websocket.Conn, args json.RawMessage) error + Execute(channel chan Response, c *websocket.Conn, args json.RawMessage, matchJson *Match) error } type ConfigDataHttp struct { @@ -116,6 +116,8 @@ type Match struct { IsMatch bool `json:"is_match"` Parameters string `json:"parameters"` Reasoning string `json:"reasoning"` + History []byte `json:"history"` + UserInput string `json:"user_input"` } type ChatHis struct { SessionId string `json:"session_id"` diff --git a/internal/pkg/func.go b/internal/pkg/func.go index 20d9d7c..0583a46 100644 --- a/internal/pkg/func.go +++ b/internal/pkg/func.go @@ -6,8 +6,12 @@ import ( ) func JsonStringIgonErr(data interface{}) string { + return string(JsonByteIgonErr(data)) +} + +func JsonByteIgonErr(data interface{}) []byte { dataByte, _ := json.Marshal(data) - return string(dataByte) + return dataByte } // IsChannelClosed 检查给定的 channel 是否已经关闭 diff --git a/internal/tools/konwledge_base.go b/internal/tools/konwledge_base.go index f7ad5a9..9aca733 100644 --- a/internal/tools/konwledge_base.go +++ b/internal/tools/konwledge_base.go @@ -60,7 +60,7 @@ func (k *KnowledgeBaseTool) Definition() entitys.ToolDefinition { } // Execute 执行知识库查询 -func (k *KnowledgeBaseTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error { +func (k *KnowledgeBaseTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage, matchJson *entitys.Match) error { var params KnowledgeBaseRequest if err := json.Unmarshal(args, ¶ms); err != nil { diff --git a/internal/tools/manager.go b/internal/tools/manager.go index 0851f86..c061301 100644 --- a/internal/tools/manager.go +++ b/internal/tools/manager.go @@ -100,13 +100,13 @@ func (m *Manager) GetToolDefinitions(caller constants.Caller) []entitys.ToolDefi } // ExecuteTool 执行工具 -func (m *Manager) ExecuteTool(channel chan entitys.Response, c *websocket.Conn, name string, args json.RawMessage) error { +func (m *Manager) ExecuteTool(channel chan entitys.Response, c *websocket.Conn, name string, args json.RawMessage, matchJson *entitys.Match) error { tool, exists := m.GetTool(name) if !exists { return fmt.Errorf("tool not found: %s", name) } - return tool.Execute(channel, c, args) + return tool.Execute(channel, c, args, matchJson) } // ExecuteToolCalls 执行多个工具调用 diff --git a/internal/tools/zltx_order_detail.go b/internal/tools/zltx_order_detail.go index e7238a8..c6782c5 100644 --- a/internal/tools/zltx_order_detail.go +++ b/internal/tools/zltx_order_detail.go @@ -81,7 +81,7 @@ type ZltxOrderDetailData struct { } // Execute 执行直连天下订单详情查询 -func (w *ZltxOrderDetailTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error { +func (w *ZltxOrderDetailTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage, matchJson *entitys.Match) error { var req ZltxOrderDetailRequest if err := json.Unmarshal(args, &req); err != nil { return fmt.Errorf("invalid zltxOrderDetail request: %w", err) @@ -92,11 +92,11 @@ func (w *ZltxOrderDetailTool) Execute(channel chan entitys.Response, c *websocke } // 这里可以集成真实的直连天下订单详情API - return w.getZltxOrderDetail(channel, c, req.OrderNumber) + return w.getZltxOrderDetail(channel, c, req.OrderNumber, matchJson) } // getMockZltxOrderDetail 获取模拟直连天下订单详情数据 -func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.Response, c *websocket.Conn, number string) (err error) { +func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.Response, c *websocket.Conn, number string, matchJson *entitys.Match) (err error) { //查询订单详情 var auth string if c != nil { @@ -169,9 +169,17 @@ func (w *ZltxOrderDetailTool) getZltxOrderDetail(ch chan entitys.Response, c *we Role: "system", Content: "你是一个订单日志助手。用户可能会提供订单日志,你需要分析订单日志,提取出订单失败的原因。", }, + { + Role: "assistant", + Content: fmt.Sprintf("聊天记录:%s", string(matchJson.History)), + }, + { + Role: "assistant", + Content: fmt.Sprintf("需要分析的订单日志:%s", string(dataJson)), + }, { Role: "user", - Content: fmt.Sprintf("订单日志:%s", string(dataJson)), + Content: matchJson.UserInput, }, }, w.Name()) if err != nil { diff --git a/internal/tools/zltx_order_direct_log.go b/internal/tools/zltx_order_direct_log.go index ce193f0..2ef61ba 100644 --- a/internal/tools/zltx_order_direct_log.go +++ b/internal/tools/zltx_order_direct_log.go @@ -67,7 +67,7 @@ type ZltxOrderDirectLogData struct { Data map[string]interface{} `json:"data"` } -func (t *ZltxOrderLogTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error { +func (t *ZltxOrderLogTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage, matchJson *entitys.Match) error { var req ZltxOrderLogRequest if err := json.Unmarshal(args, &req); err != nil { return fmt.Errorf("invalid zltxOrderLog request: %w", err) diff --git a/internal/tools/zltx_product.go b/internal/tools/zltx_product.go index 78ad14b..d6801c0 100644 --- a/internal/tools/zltx_product.go +++ b/internal/tools/zltx_product.go @@ -53,7 +53,7 @@ type ZltxProductRequest struct { Name string `json:"name"` } -func (z ZltxProductTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error { +func (z ZltxProductTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage, matchJson *entitys.Match) error { var req ZltxProductRequest if err := json.Unmarshal(args, &req); err != nil { return fmt.Errorf("invalid zltxProduct request: %w", err) diff --git a/internal/tools/zltx_statistics.go b/internal/tools/zltx_statistics.go index 83c2a40..52440f6 100644 --- a/internal/tools/zltx_statistics.go +++ b/internal/tools/zltx_statistics.go @@ -46,7 +46,7 @@ type ZltxOrderStatisticsRequest struct { Number string `json:"number"` } -func (z ZltxOrderStatisticsTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage) error { +func (z ZltxOrderStatisticsTool) Execute(channel chan entitys.Response, c *websocket.Conn, args json.RawMessage, matchJson *entitys.Match) error { var req ZltxOrderStatisticsRequest if err := json.Unmarshal(args, &req); err != nil { return err