From 888a97e4d392f21f064472f8df19e3aa094cca1a Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Wed, 2 Jul 2025 10:25:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=E6=9B=B4=E6=96=B0SearchStockByIndica?= =?UTF-8?q?tors=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新 SearchStockByIndicators 函数描述,使其更准确地反映功能 - 在 Resp 结构中添加 Error 字段,用于处理错误信息 - 修改 openai_api.go 和 openai_api_test.go 中的错误处理逻辑 - 优化消息发送格式,提高错误信息的可读性 --- app.go | 4 ++-- backend/data/openai_api.go | 20 ++++++++++++++------ backend/data/openai_api_test.go | 8 +++++--- backend/models/models.go | 6 ++++++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index 7041a5c..faf10c0 100644 --- a/app.go +++ b/app.go @@ -1134,13 +1134,13 @@ func (a *App) SummaryStockNews(question string, sysPromptId *int) { Type: "function", Function: data.ToolFunction{ Name: "SearchStockByIndicators", - Description: "按行业根据选股指标或策略,返回符合指标或策略的股票列表。多个行业的筛选需按行业顺序调用多次,不支持并行调用", + Description: "根据自然语言筛选股票,返回自然语言选股条件要求的股票所有相关数据。单独输入股票名称可以获取当前股票最新的股价交易数据和基础财务指标信息", Parameters: data.FunctionParameters{ Type: "object", Properties: map[string]any{ "words": map[string]any{ "type": "string", - "description": "行业选股指标或策略,并且条件使用;分隔,或者条件使用,分隔。例如:创新药;PE<30;净利润增长率>50%;", + "description": "选股自然语言,并且条件使用;分隔,或者条件使用,分隔。例1:创新药;PE<30;净利润增长率>50%。 例2:上证指数(指数名称)。 例3:长电科技(股票名称)", }, }, Required: []string{"words"}, diff --git a/backend/data/openai_api.go b/backend/data/openai_api.go index e1e8689..f486d6a 100644 --- a/backend/data/openai_api.go +++ b/backend/data/openai_api.go @@ -766,10 +766,14 @@ func AskAi(o OpenAi, err error, messages []map[string]interface{}, ch chan map[s res := &models.Resp{} if err := json.Unmarshal([]byte(line), res); err == nil { //ch <- line + msg := res.Message + if res.Error.Message != "" { + msg = res.Error.Message + } ch <- map[string]any{ "code": 0, "question": question, - "content": res.Message, + "content": msg, } } } @@ -920,10 +924,10 @@ 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(), + //}) messages = append(messages, map[string]interface{}{ "role": "tool", "content": content, @@ -964,10 +968,14 @@ func AskAiWithTools(o OpenAi, err error, messages []map[string]interface{}, ch c res := &models.Resp{} if err := json.Unmarshal([]byte(line), res); err == nil { //ch <- line + msg := res.Message + if res.Error.Message != "" { + msg = res.Error.Message + } ch <- map[string]any{ "code": 0, "question": question, - "content": res.Message, + "content": msg, } } } diff --git a/backend/data/openai_api_test.go b/backend/data/openai_api_test.go index 3856425..8c97c6d 100644 --- a/backend/data/openai_api_test.go +++ b/backend/data/openai_api_test.go @@ -14,13 +14,13 @@ func TestNewDeepSeekOpenAiConfig(t *testing.T) { Type: "function", Function: ToolFunction{ Name: "SearchStockByIndicators", - Description: "通过解析自然语言,形成选股指标或策略,返回符合指标或策略的股票列表", + Description: "根据自然语言筛选股票,返回自然语言选股条件要求的股票所有相关数据", Parameters: FunctionParameters{ Type: "object", Properties: map[string]any{ "words": map[string]any{ "type": "string", - "description": "选股指标或策略的自然语言", + "description": "选股自然语言,并且条件使用;分隔,或者条件使用,分隔。例如:创新药;PE<30;净利润增长率>50%;", }, }, Required: []string{"words"}, @@ -35,7 +35,9 @@ func TestNewDeepSeekOpenAiConfig(t *testing.T) { for { select { case msg := <-res: - t.Log(msg) + if len(msg) > 0 { + t.Log(msg) + } } } } diff --git a/backend/models/models.go b/backend/models/models.go index 434aa91..cc56ed5 100644 --- a/backend/models/models.go +++ b/backend/models/models.go @@ -195,6 +195,12 @@ func (receiver StockInfoUS) TableName() string { type Resp struct { Code int `json:"code"` Message string `json:"message"` + Error struct { + Code string `json:"code"` + Message string `json:"message"` + Param string `json:"param"` + Type string `json:"type"` + } `json:"error"` } type PromptTemplate struct {