From bac10a2a045cc9de25b0c5419ba3cbece424694c Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Wed, 2 Apr 2025 13:46:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(app):=E9=87=8D=E6=9E=84=E4=B8=BB?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=92=8C=E4=BC=98=E5=8C=96=E8=82=A1=E7=A5=A8?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构主程序循环,使用 goroutine 启动 systray.Run - 注释掉 onExit 函数中的退出操作 - 优化股票查询功能,增加实时数据获取和处理 - 改进模板替换逻辑,支持多种格式 --- app.go | 6 +++--- backend/data/openai_api.go | 43 +++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/app.go b/app.go index d1886e7..bf4af75 100644 --- a/app.go +++ b/app.go @@ -95,7 +95,7 @@ func (a *App) startup(ctx context.Context) { runtime.WindowReloadApp(ctx) }) - systray.RunWithExternalLoop(func() { + go systray.Run(func() { onReady(a) }, func() { onExit(a) @@ -797,8 +797,8 @@ func getMsgTypeName(msgType int) string { func onExit(a *App) { // 清理操作 logger.SugaredLogger.Infof("onExit") - systray.Quit() - runtime.Quit(a.ctx) + //systray.Quit() + //runtime.Quit(a.ctx) } func onReady(a *App) { diff --git a/backend/data/openai_api.go b/backend/data/openai_api.go index 913a387..42b7fa5 100644 --- a/backend/data/openai_api.go +++ b/backend/data/openai_api.go @@ -143,29 +143,42 @@ func (o OpenAi) NewChatStream(stock, stockCode, userQuestion string, sysPromptId "content": "当前本地时间是:" + time.Now().Format("2006-01-02 15:04:05"), }) + replaceTemplates := map[string]string{ + "{{stockName}}": RemoveAllBlankChar(stock), + "{{stockCode}}": RemoveAllBlankChar(stockCode), + "{stockName}": RemoveAllBlankChar(stock), + "{stockCode}": RemoveAllBlankChar(stockCode), + "stockName": RemoveAllBlankChar(stock), + "stockCode": RemoveAllBlankChar(stockCode), + } + followedStock := NewStockDataApi().GetFollowedStockByStockCode(stockCode) + stockData, err := NewStockDataApi().GetStockCodeRealTimeData(stockCode) + if err == nil && len(*stockData) > 0 { + msg = append(msg, map[string]interface{}{ + "role": "user", + "content": fmt.Sprintf("当前%s[%s]价格是多少?", stock, stockCode), + }) + msg = append(msg, map[string]interface{}{ + "role": "assistant", + "content": fmt.Sprintf("截止到%s,当前%s[%s]价格是%s", (*stockData)[0].Date+" "+(*stockData)[0].Time, stock, stockCode, (*stockData)[0].Price), + }) + } + if followedStock.CostPrice > 0 { + replaceTemplates["{{costPrice}}"] = convertor.ToString(followedStock.CostPrice) + replaceTemplates["{costPrice}"] = convertor.ToString(followedStock.CostPrice) + replaceTemplates["costPrice"] = convertor.ToString(followedStock.CostPrice) + } + question := "" if userQuestion == "" { - replaceTemplates := map[string]string{ - "{{stockName}}": RemoveAllBlankChar(stock), - "{{stockCode}}": RemoveAllBlankChar(stockCode), - } - - followedStock := &FollowedStock{ - StockCode: stockCode, - } - db.Dao.Model(&followedStock).Where("stock_code = ?", stockCode).First(followedStock) - if followedStock.CostPrice > 0 { - replaceTemplates["{{costPrice}}"] = fmt.Sprintf("%.2f", followedStock.CostPrice) - } question = strutil.ReplaceWithMap(o.QuestionTemplate, replaceTemplates) } else { question = userQuestion + question = strutil.ReplaceWithMap(userQuestion, replaceTemplates) } logger.SugaredLogger.Infof("NewChatStream stock:%s stockCode:%s", stock, stockCode) logger.SugaredLogger.Infof("Prompt:%s", sysPrompt) - logger.SugaredLogger.Infof("User Prompt config:%v", o.QuestionTemplate) - logger.SugaredLogger.Infof("User question:%s", userQuestion) logger.SugaredLogger.Infof("final question:%s", question) wg := &sync.WaitGroup{} @@ -413,7 +426,7 @@ func (o OpenAi) NewChatStream(stock, stockCode, userQuestion string, sysPromptId }) //reqJson, _ := json.Marshal(msg) - //logger.SugaredLogger.Errorf("Stream request: \n%s", reqJson) + //logger.SugaredLogger.Errorf("Stream request: \n%s\n", reqJson) client := resty.New() client.SetBaseURL(strutil.Trim(o.BaseUrl))