feat(app):更新SearchStockByIndicators工具函数描述并优化错误处理

- 更新 SearchStockByIndicators 函数描述,使其更准确地反映功能
- 在 Resp 结构中添加 Error 字段,用于处理错误信息
- 修改 openai_api.go 和 openai_api_test.go 中的错误处理逻辑
- 优化消息发送格式,提高错误信息的可读性
This commit is contained in:
ArvinLovegood 2025-07-02 10:25:03 +08:00
parent ebeaf104bb
commit 888a97e4d3
4 changed files with 27 additions and 11 deletions

4
app.go
View File

@ -1134,13 +1134,13 @@ func (a *App) SummaryStockNews(question string, sysPromptId *int) {
Type: "function", Type: "function",
Function: data.ToolFunction{ Function: data.ToolFunction{
Name: "SearchStockByIndicators", Name: "SearchStockByIndicators",
Description: "按行业根据选股指标或策略,返回符合指标或策略的股票列表。多个行业的筛选需按行业顺序调用多次,不支持并行调用", Description: "根据自然语言筛选股票,返回自然语言选股条件要求的股票所有相关数据。单独输入股票名称可以获取当前股票最新的股价交易数据和基础财务指标信息",
Parameters: data.FunctionParameters{ Parameters: data.FunctionParameters{
Type: "object", Type: "object",
Properties: map[string]any{ Properties: map[string]any{
"words": map[string]any{ "words": map[string]any{
"type": "string", "type": "string",
"description": "行业选股指标或策略,并且条件使用;分隔,或者条件使用,分隔。例如:创新药;PE<30;净利润增长率>50%;", "description": "选股自然语言,并且条件使用;分隔,或者条件使用,分隔。例1创新药;PE<30;净利润增长率>50%。 例2上证指数(指数名称)。 例3长电科技(股票名称)",
}, },
}, },
Required: []string{"words"}, Required: []string{"words"},

View File

@ -766,10 +766,14 @@ func AskAi(o OpenAi, err error, messages []map[string]interface{}, ch chan map[s
res := &models.Resp{} res := &models.Resp{}
if err := json.Unmarshal([]byte(line), res); err == nil { if err := json.Unmarshal([]byte(line), res); err == nil {
//ch <- line //ch <- line
msg := res.Message
if res.Error.Message != "" {
msg = res.Error.Message
}
ch <- map[string]any{ ch <- map[string]any{
"code": 0, "code": 0,
"question": question, "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) logger.SugaredLogger.Infof("SearchStockByIndicators:words:%s --> %s", words, content)
messages = append(messages, map[string]interface{}{ //messages = append(messages, map[string]interface{}{
"role": "assistant", // "role": "assistant",
"content": currentAIContent.String(), // "content": currentAIContent.String(),
}) //})
messages = append(messages, map[string]interface{}{ messages = append(messages, map[string]interface{}{
"role": "tool", "role": "tool",
"content": content, "content": content,
@ -964,10 +968,14 @@ func AskAiWithTools(o OpenAi, err error, messages []map[string]interface{}, ch c
res := &models.Resp{} res := &models.Resp{}
if err := json.Unmarshal([]byte(line), res); err == nil { if err := json.Unmarshal([]byte(line), res); err == nil {
//ch <- line //ch <- line
msg := res.Message
if res.Error.Message != "" {
msg = res.Error.Message
}
ch <- map[string]any{ ch <- map[string]any{
"code": 0, "code": 0,
"question": question, "question": question,
"content": res.Message, "content": msg,
} }
} }
} }

View File

@ -14,13 +14,13 @@ func TestNewDeepSeekOpenAiConfig(t *testing.T) {
Type: "function", Type: "function",
Function: ToolFunction{ Function: ToolFunction{
Name: "SearchStockByIndicators", Name: "SearchStockByIndicators",
Description: "通过解析自然语言,形成选股指标或策略,返回符合指标或策略的股票列表", Description: "根据自然语言筛选股票,返回自然语言选股条件要求的股票所有相关数据",
Parameters: FunctionParameters{ Parameters: FunctionParameters{
Type: "object", Type: "object",
Properties: map[string]any{ Properties: map[string]any{
"words": map[string]any{ "words": map[string]any{
"type": "string", "type": "string",
"description": "选股指标或策略的自然语言", "description": "选股自然语言,并且条件使用;分隔,或者条件使用,分隔。例如:创新药;PE<30;净利润增长率>50%;",
}, },
}, },
Required: []string{"words"}, Required: []string{"words"},
@ -35,10 +35,12 @@ func TestNewDeepSeekOpenAiConfig(t *testing.T) {
for { for {
select { select {
case msg := <-res: case msg := <-res:
if len(msg) > 0 {
t.Log(msg) t.Log(msg)
} }
} }
} }
}
func TestGetTopNewsList(t *testing.T) { func TestGetTopNewsList(t *testing.T) {
news := GetTopNewsList(30) news := GetTopNewsList(30)

View File

@ -195,6 +195,12 @@ func (receiver StockInfoUS) TableName() string {
type Resp struct { type Resp struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` 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 { type PromptTemplate struct {