From 3b43d2e7b2446ab297765c794fca27e9fafeba5a Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Tue, 30 Sep 2025 18:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.yaml | 3 +- internal/biz/llm_service/langchain.go | 1 + internal/biz/router.go | 6 ++- internal/entitys/response.go | 3 +- internal/entitys/types.go | 1 + internal/pkg/utils_ollama/client.go | 6 +-- internal/tools/manager.go | 5 ++ internal/tools/normal_chat.go | 78 +++++++++++++++++++++++++++ 8 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 internal/tools/normal_chat.go diff --git a/config/config.yaml b/config/config.yaml index 95cd9fa..c5ad4ce 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -9,7 +9,8 @@ ollama: timeout: "120s" level: "info" format: "json" - + + sys: session_len: 6 diff --git a/internal/biz/llm_service/langchain.go b/internal/biz/llm_service/langchain.go index c8cc3ee..63f8815 100644 --- a/internal/biz/llm_service/langchain.go +++ b/internal/biz/llm_service/langchain.go @@ -18,6 +18,7 @@ type LangChainService struct { func NewLangChainGenerate( client *utils_langchain.UtilLangChain, ) *LangChainService { + return &LangChainService{ client: client, } diff --git a/internal/biz/router.go b/internal/biz/router.go index 3a4e8d3..df7e82c 100644 --- a/internal/biz/router.go +++ b/internal/biz/router.go @@ -194,10 +194,12 @@ func (r *AiRouterBiz) recognize(ctx context.Context, requireData *entitys.Requir Content: "意图识别结束", Type: entitys.ResponseLog, } - if err = json.Unmarshal([]byte(recognizeMsg), requireData.Match); err != nil { + var match entitys.Match + if err = json.Unmarshal([]byte(recognizeMsg), &match); err != nil { err = errors.SysErr("数据结构错误:%v", err.Error()) return } + requireData.Match = &match return } @@ -265,7 +267,7 @@ func (r *AiRouterBiz) handleMatch(ctx context.Context, requireData *entitys.Requ if !requireData.Match.IsMatch { requireData.Ch <- entitys.Response{ Index: "", - Content: requireData.Match.Reasoning, + Content: requireData.Match.Chat, Type: entitys.ResponseText, } return diff --git a/internal/entitys/response.go b/internal/entitys/response.go index d5ba6dc..5dd2496 100644 --- a/internal/entitys/response.go +++ b/internal/entitys/response.go @@ -48,9 +48,10 @@ func MsgSet(msgType ResponseType, msg string, done bool) []byte { func MsgSend(c *websocket.Conn, msg Response) error { // 检查上下文是否已取消 + if msg.Type == ResponseText { + } jsonByte, _ := json.Marshal(msg) - return c.WriteMessage(websocket.TextMessage, jsonByte) } diff --git a/internal/entitys/types.go b/internal/entitys/types.go index fe77e4e..ca4eb96 100644 --- a/internal/entitys/types.go +++ b/internal/entitys/types.go @@ -121,6 +121,7 @@ type Match struct { History []byte `json:"history"` UserInput string `json:"user_input"` Auth string `json:"auth"` + Chat string `json:"chat"` } type ChatHis struct { SessionId string `json:"session_id"` diff --git a/internal/pkg/utils_ollama/client.go b/internal/pkg/utils_ollama/client.go index 93b1985..38b9c18 100644 --- a/internal/pkg/utils_ollama/client.go +++ b/internal/pkg/utils_ollama/client.go @@ -45,10 +45,10 @@ func (c *Client) ToolSelect(ctx context.Context, messages []api.Message, tools [ Model: c.config.Model, Messages: messages, Stream: new(bool), // 设置为false,不使用流式响应 - Think: &api.ThinkValue{Value: true}, - //Tools: tools, + Think: &api.ThinkValue{Value: false}, + Tools: tools, } - + c.client.ListRunning() err = c.client.Chat(ctx, req, func(resp api.ChatResponse) error { res = resp return nil diff --git a/internal/tools/manager.go b/internal/tools/manager.go index b31277b..4ef85a6 100644 --- a/internal/tools/manager.go +++ b/internal/tools/manager.go @@ -69,6 +69,11 @@ func NewManager(config *config.Config, llm *utils_ollama.Client) *Manager { knowledgeTool := NewKnowledgeBaseTool(config.Tools.Knowledge) m.tools[knowledgeTool.Name()] = knowledgeTool } + + // 普通对话 + chat := NewNormalChatTool(m.llm) + m.tools[chat.Name()] = chat + return m } diff --git a/internal/tools/normal_chat.go b/internal/tools/normal_chat.go new file mode 100644 index 0000000..489eb2e --- /dev/null +++ b/internal/tools/normal_chat.go @@ -0,0 +1,78 @@ +package tools + +import ( + "ai_scheduler/internal/entitys" + "ai_scheduler/internal/pkg" + "ai_scheduler/internal/pkg/utils_ollama" + "context" + "encoding/json" + "fmt" + + "github.com/ollama/ollama/api" +) + +// NormalChatTool 普通对话 +type NormalChatTool struct { + llm *utils_ollama.Client +} + +// NewNormalChatTool 实例普通对话 +func NewNormalChatTool(llm *utils_ollama.Client) *NormalChatTool { + return &NormalChatTool{llm: llm} +} + +// Name 返回工具名称 +func (w *NormalChatTool) Name() string { + return "normalChat" +} + +// Description 返回工具描述 +func (w *NormalChatTool) Description() string { + return "用户想进行一般性问答" +} + +type NormalChat struct { + ChatContent string `json:"chat_content"` +} + +// Definition 返回工具定义 +func (w *NormalChatTool) Definition() entitys.ToolDefinition { + return entitys.ToolDefinition{} +} + +// Execute 执行直连天下订单详情查询 +func (w *NormalChatTool) Execute(ctx context.Context, requireData *entitys.RequireData) error { + var req NormalChat + if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil { + return fmt.Errorf("invalid zltxOrderDetail request: %w", err) + } + if req.ChatContent == "" { + req.ChatContent = "介绍一下你能做什么" + } + + // 这里可以集成真实的直连天下订单详情API + return w.chat(requireData, &req) +} + +// getMockZltxOrderDetail 获取模拟直连天下订单详情数据 +func (w *NormalChatTool) chat(requireData *entitys.RequireData, chat *NormalChat) (err error) { + err = w.llm.ChatStream(context.TODO(), requireData.Ch, []api.Message{ + { + Role: "system", + Content: "你是一个聊天助手", + }, + { + Role: "assistant", + Content: fmt.Sprintf("聊天记录:%s", pkg.JsonStringIgonErr(requireData.Histories)), + }, + { + Role: "user", + Content: requireData.UserInput, + }, + }, w.Name()) + if err != nil { + return fmt.Errorf(":%s", err) + } + + return +}