Skip to content

Commit 3c52057

Browse files
committed
feat(data):添加投资者互动问答数据
- 在 app.go 中添加了 InteractiveAnswer 工具函数 - 在 market_news_api.go 中实现了 InteractiveAnswer 方法 - 在 market_news_api_test.go 中添加了 InteractiveAnswer 测试用例 - 在 models.go 中定义了 InteractiveAnswer 相关的结构体 - 在 openai_api.go 中集成了 InteractiveAnswer 功能
1 parent b1b34d9 commit 3c52057

File tree

5 files changed

+195
-2
lines changed

5 files changed

+195
-2
lines changed

app.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ func AddTools(tools []data.Tool) []data.Tool {
104104
},
105105
})
106106

107+
tools = append(tools, data.Tool{
108+
Type: "function",
109+
Function: data.ToolFunction{
110+
Name: "InteractiveAnswer",
111+
Description: "获取投资者与上市公司互动问答的数据,反映当前投资者关注的热点问题",
112+
Parameters: data.FunctionParameters{
113+
Type: "object",
114+
Properties: map[string]any{
115+
"page": map[string]any{
116+
"type": "string",
117+
"description": "分页号",
118+
},
119+
"pageSize": map[string]any{
120+
"type": "string",
121+
"description": "分页大小",
122+
},
123+
"keyWord": map[string]any{
124+
"type": "string",
125+
"description": "搜索关键词,多个关键词空格隔开(可输入股票名称或者当前热门板块/行业/概念/标的/事件等)",
126+
},
127+
},
128+
Required: []string{"page", "pageSize"},
129+
},
130+
},
131+
})
132+
107133
return tools
108134
}
109135

backend/data/market_news_api.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,3 +886,35 @@ func (m MarketNewsApi) ReutersNew() *models.ReutersNews {
886886
logger.SugaredLogger.Infof("Articles:%+v", news.Result.Articles)
887887
return news
888888
}
889+
890+
func (m MarketNewsApi) InteractiveAnswer(page int, pageSize int, keyWord string) *models.InteractiveAnswer {
891+
client := resty.New()
892+
config := GetSettingConfig()
893+
if config.HttpProxyEnabled && config.HttpProxy != "" {
894+
client.SetProxy(config.HttpProxy)
895+
}
896+
url := fmt.Sprintf("https://irm.cninfo.com.cn/newircs/index/search?_t=%d", time.Now().Unix())
897+
answers := &models.InteractiveAnswer{}
898+
logger.SugaredLogger.Infof("请求url:%s", url)
899+
resp, err := client.SetTimeout(time.Duration(5)*time.Second).R().
900+
SetHeader("Host", "irm.cninfo.com.cn").
901+
SetHeader("Origin", "https://irm.cninfo.com.cn").
902+
SetHeader("Referer", "https://irm.cninfo.com.cn/views/interactiveAnswer").
903+
SetHeader("handleError", "true").
904+
SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0").
905+
SetFormData(map[string]string{
906+
"pageNo": convertor.ToString(page),
907+
"pageSize": convertor.ToString(pageSize),
908+
"searchTypes": "1,11",
909+
"highLight": "true",
910+
"keyWord": keyWord,
911+
}).
912+
SetResult(answers).
913+
Post(url)
914+
if err != nil {
915+
logger.SugaredLogger.Errorf("InteractiveAnswer-err:%+v", err)
916+
}
917+
logger.SugaredLogger.Debugf("InteractiveAnswer-resp:%s", resp.Body())
918+
return answers
919+
920+
}

backend/data/market_news_api_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package data
33
import (
44
"encoding/json"
55
"github.com/coocood/freecache"
6+
"github.com/duke-git/lancet/v2/convertor"
67
"github.com/tidwall/gjson"
78
"go-stock/backend/db"
89
"go-stock/backend/logger"
910
"go-stock/backend/util"
1011
"strings"
1112
"testing"
13+
"time"
1214
)
1315

1416
// @Author spark
@@ -216,3 +218,20 @@ func TestReutersNew(t *testing.T) {
216218
db.Init("../../data/stock.db")
217219
NewMarketNewsApi().ReutersNew()
218220
}
221+
222+
func TestInteractiveAnswer(t *testing.T) {
223+
db.Init("../../data/stock.db")
224+
datas := NewMarketNewsApi().InteractiveAnswer(1, 25, "")
225+
logger.SugaredLogger.Debugf("PageSize:%d", datas.PageSize)
226+
for _, res := range datas.Results {
227+
toInt, err := convertor.ToInt(res.AttachedPubDate)
228+
if err != nil {
229+
continue
230+
}
231+
logger.SugaredLogger.Debugf("%s companyShortName:%s[%s] \n MainContent:%s \n AttachedContent:%s", time.UnixMilli(toInt).Format("2006-01-02 15:04:05"), res.CompanyShortName, res.StockCode, res.MainContent, res.AttachedContent)
232+
}
233+
234+
md := util.MarkdownTableWithTitle("投资互动", datas.Results)
235+
logger.SugaredLogger.Debugf(md)
236+
237+
}

backend/data/openai_api.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,21 @@ func (o *OpenAi) NewSummaryStockNewsStreamWithTools(userQuestion string, sysProm
188188
"content": "当前本地时间是:" + time.Now().Format("2006-01-02 15:04:05"),
189189
})
190190
wg := &sync.WaitGroup{}
191-
wg.Add(5)
191+
wg.Add(6)
192+
193+
go func() {
194+
defer wg.Done()
195+
datas := NewMarketNewsApi().InteractiveAnswer(1, 100, "")
196+
content := util.MarkdownTableWithTitle("当前最新投资者互动数据", datas.Results)
197+
msg = append(msg, map[string]interface{}{
198+
"role": "user",
199+
"content": "投资者互动数据",
200+
})
201+
msg = append(msg, map[string]interface{}{
202+
"role": "assistant",
203+
"content": content,
204+
})
205+
}()
192206

193207
go func() {
194208
defer wg.Done()
@@ -381,7 +395,7 @@ func (o *OpenAi) NewSummaryStockNewsStream(userQuestion string, sysPromptId *int
381395
"content": "当前本地时间是:" + time.Now().Format("2006-01-02 15:04:05"),
382396
})
383397
wg := &sync.WaitGroup{}
384-
wg.Add(3)
398+
wg.Add(4)
385399
go func() {
386400
defer wg.Done()
387401
var market strings.Builder
@@ -442,6 +456,20 @@ func (o *OpenAi) NewSummaryStockNewsStream(userQuestion string, sysPromptId *int
442456
})
443457
}()
444458

459+
go func() {
460+
defer wg.Done()
461+
datas := NewMarketNewsApi().InteractiveAnswer(1, 100, "")
462+
content := util.MarkdownTableWithTitle("当前最新投资者互动数据", datas.Results)
463+
msg = append(msg, map[string]interface{}{
464+
"role": "user",
465+
"content": "投资者互动数据",
466+
})
467+
msg = append(msg, map[string]interface{}{
468+
"role": "assistant",
469+
"content": content,
470+
})
471+
}()
472+
445473
wg.Wait()
446474

447475
news := NewMarketNewsApi().GetNewsList("", 100)
@@ -1292,6 +1320,52 @@ func AskAiWithTools(o *OpenAi, err error, messages []map[string]interface{}, ch
12921320
}
12931321
}
12941322

1323+
if funcName == "InteractiveAnswer" {
1324+
page := gjson.Get(funcArguments, "page").String()
1325+
pageSize := gjson.Get(funcArguments, "pageSize").String()
1326+
keyWord := gjson.Get(funcArguments, "keyWord").String()
1327+
ch <- map[string]any{
1328+
"code": 1,
1329+
"question": question,
1330+
"chatId": streamResponse.Id,
1331+
"model": streamResponse.Model,
1332+
"content": "\r\n```\r\n开始调用工具:InteractiveAnswer,\n参数:" + page + "," + pageSize + "," + keyWord + "\r\n```\r\n",
1333+
"time": time.Now().Format(time.DateTime),
1334+
}
1335+
pageNo, err := convertor.ToInt(page)
1336+
if err != nil {
1337+
pageNo = 1
1338+
}
1339+
pageSizeNum, err := convertor.ToInt(pageSize)
1340+
if err != nil {
1341+
pageSizeNum = 50
1342+
}
1343+
datas := NewMarketNewsApi().InteractiveAnswer(int(pageNo), int(pageSizeNum), keyWord)
1344+
content := util.MarkdownTableWithTitle("投资互动数据", datas.Results)
1345+
logger.SugaredLogger.Infof("InteractiveAnswer=\n%s", content)
1346+
messages = append(messages, map[string]interface{}{
1347+
"role": "assistant",
1348+
"content": currentAIContent.String(),
1349+
"tool_calls": []map[string]any{
1350+
{
1351+
"id": currentCallId,
1352+
"tool_call_id": currentCallId,
1353+
"type": "function",
1354+
"function": map[string]string{
1355+
"name": funcName,
1356+
"arguments": funcArguments,
1357+
"parameters": funcArguments,
1358+
},
1359+
},
1360+
},
1361+
})
1362+
messages = append(messages, map[string]interface{}{
1363+
"role": "tool",
1364+
"content": content,
1365+
"tool_call_id": currentCallId,
1366+
})
1367+
}
1368+
12951369
}
12961370
AskAiWithTools(o, err, messages, ch, question, tools)
12971371
}

backend/models/models.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,45 @@ type ReutersNews struct {
635635
} `json:"result"`
636636
Id string `json:"_id"`
637637
}
638+
639+
type InteractiveAnswer struct {
640+
PageNo int `json:"pageNo"`
641+
PageSize int `json:"pageSize"`
642+
TotalRecord int `json:"totalRecord"`
643+
TotalPage int `json:"totalPage"`
644+
Results []InteractiveAnswerResults `json:"results"`
645+
Count bool `json:"count"`
646+
}
647+
648+
type InteractiveAnswerResults struct {
649+
EsId string `json:"esId" md:"-"`
650+
IndexId string `json:"indexId" md:"-"`
651+
ContentType int `json:"contentType" md:"-"`
652+
Trade []string `json:"trade" md:"行业名称"`
653+
MainContent string `json:"mainContent" md:"投资者提问"`
654+
StockCode string `json:"stockCode" md:"股票代码"`
655+
Secid string `json:"secid" md:"-"`
656+
CompanyShortName string `json:"companyShortName" md:"股票名称"`
657+
CompanyLogo string `json:"companyLogo,omitempty" md:"-"`
658+
BoardType []string `json:"boardType" md:"-"`
659+
PubDate string `json:"pubDate" md:"发布时间"`
660+
UpdateDate string `json:"updateDate" md:"-"`
661+
Author string `json:"author" md:"-"`
662+
AuthorName string `json:"authorName" md:"-"`
663+
PubClient string `json:"pubClient" md:"-"`
664+
AttachedId string `json:"attachedId" md:"-"`
665+
AttachedContent string `json:"attachedContent" md:"上市公司回复"`
666+
AttachedAuthor string `json:"attachedAuthor" md:"-"`
667+
AttachedPubDate string `json:"attachedPubDate" md:"回复时间"`
668+
Score float64 `json:"score" md:"-"`
669+
TopStatus int `json:"topStatus" md:"-"`
670+
PraiseCount int `json:"praiseCount" md:"-"`
671+
PraiseStatus bool `json:"praiseStatus" md:"-"`
672+
FavoriteStatus bool `json:"favoriteStatus" md:"-"`
673+
AttentionCompany bool `json:"attentionCompany" md:"-"`
674+
IsCheck string `json:"isCheck" md:"-"`
675+
QaStatus int `json:"qaStatus" md:"-"`
676+
PackageDate string `json:"packageDate" md:"-"`
677+
RemindStatus bool `json:"remindStatus" md:"-"`
678+
InterviewLive bool `json:"interviewLive" md:"-"`
679+
}

0 commit comments

Comments
 (0)