From 9a607367394233c9fe49106c0004c16c57ca44af Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Wed, 2 Jul 2025 18:41:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=E4=BC=98=E5=8C=96AI=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E8=B0=83=E7=94=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当模型不支持函数调用时,重新使用 AI 模型询问 - 添加函数调用相关的消息结构 - 优化错误处理逻辑 --- backend/data/openai_api.go | 51 ++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/backend/data/openai_api.go b/backend/data/openai_api.go index ea34412..9a77f33 100644 --- a/backend/data/openai_api.go +++ b/backend/data/openai_api.go @@ -941,10 +941,22 @@ func AskAiWithTools(o OpenAi, err error, messages []map[string]interface{}, ch c //logger.SugaredLogger.Infof("SearchStockByIndicators:words:%s --> %s", words, content) - //messages = append(messages, map[string]interface{}{ - // "role": "assistant", - // "content": currentAIContent.String(), - //}) + messages = append(messages, map[string]interface{}{ + "role": "assistant", + "content": currentAIContent.String(), + "tool_calls": []map[string]any{ + { + "id": currentCallId, + "tool_call_id": currentCallId, + "type": "function", + "function": map[string]string{ + "name": funcName, + "arguments": funcArguments, + "parameters": funcArguments, + }, + }, + }, + }) messages = append(messages, map[string]interface{}{ "role": "tool", "content": content, @@ -970,6 +982,23 @@ func AskAiWithTools(o OpenAi, err error, messages []map[string]interface{}, ch c } res := NewStockDataApi().GetHK_KLineData(stockCode, "day", toIntDay) searchRes, _ := json.Marshal(res) + + messages = append(messages, map[string]interface{}{ + "role": "assistant", + "content": currentAIContent.String(), + "tool_calls": []map[string]any{ + { + "id": currentCallId, + "tool_call_id": currentCallId, + "type": "function", + "function": map[string]string{ + "name": funcName, + "arguments": funcArguments, + "parameters": funcArguments, + }, + }, + }, + }) messages = append(messages, map[string]interface{}{ "role": "tool", "content": stockCode + convertor.ToString(toIntDay) + "日K线数据:\n" + string(searchRes) + "\n", @@ -1014,11 +1043,17 @@ func AskAiWithTools(o OpenAi, err error, messages []map[string]interface{}, ch c if res.Error.Message != "" { msg = res.Error.Message } - ch <- map[string]any{ - "code": 0, - "question": question, - "content": msg, + + if msg == "Function call is not supported for this model." { + AskAi(o, err, messages, ch, question) + } else { + ch <- map[string]any{ + "code": 0, + "question": question, + "content": msg, + } } + } }