mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
refactor(backend): 重构 OpenAI 和股票数据 API
-优化了 OpenAI API 的调用逻辑,提高了错误处理和数据处理的能力 - 改进了股票数据 API 的数据抓取和处理方式 - 移除了测试代码中冗余的部分,提高了代码可读性和维护性
This commit is contained in:
parent
b00bddcdec
commit
3de2ad3cdc
@ -125,13 +125,6 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
|
|||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
client := resty.New()
|
|
||||||
client.SetBaseURL(o.BaseUrl)
|
|
||||||
client.SetHeader("Authorization", "Bearer "+o.ApiKey)
|
|
||||||
client.SetHeader("Content-Type", "application/json")
|
|
||||||
client.SetRetryCount(3)
|
|
||||||
client.SetTimeout(time.Second * 60)
|
|
||||||
|
|
||||||
msg := []map[string]interface{}{
|
msg := []map[string]interface{}{
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "system",
|
||||||
@ -142,7 +135,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(2)
|
wg.Add(4)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@ -168,33 +161,38 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//go func() {
|
go func() {
|
||||||
// defer wg.Done()
|
defer wg.Done()
|
||||||
// messages := SearchStockInfo(stock, "depth")
|
messages := SearchStockInfo(stock, "depth")
|
||||||
// for _, message := range *messages {
|
for _, message := range *messages {
|
||||||
// msg = append(msg, map[string]interface{}{
|
msg = append(msg, map[string]interface{}{
|
||||||
// "role": "assistant",
|
"role": "assistant",
|
||||||
// "content": message,
|
"content": message,
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
//}()
|
}()
|
||||||
//go func() {
|
go func() {
|
||||||
// defer wg.Done()
|
defer wg.Done()
|
||||||
// messages := SearchStockInfo(stock, "telegram")
|
messages := SearchStockInfo(stock, "telegram")
|
||||||
// for _, message := range *messages {
|
for _, message := range *messages {
|
||||||
// msg = append(msg, map[string]interface{}{
|
msg = append(msg, map[string]interface{}{
|
||||||
// "role": "assistant",
|
"role": "assistant",
|
||||||
// "content": message,
|
"content": message,
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
//}()
|
}()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
msg = append(msg, map[string]interface{}{
|
msg = append(msg, map[string]interface{}{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
"content": stock + "分析和总结",
|
"content": stock + "分析和总结",
|
||||||
})
|
})
|
||||||
|
client := resty.New()
|
||||||
|
client.SetBaseURL(o.BaseUrl)
|
||||||
|
client.SetHeader("Authorization", "Bearer "+o.ApiKey)
|
||||||
|
client.SetHeader("Content-Type", "application/json")
|
||||||
|
client.SetRetryCount(3)
|
||||||
|
client.SetTimeout(1 * time.Minute)
|
||||||
resp, err := client.R().
|
resp, err := client.R().
|
||||||
SetDoNotParseResponse(true).
|
SetDoNotParseResponse(true).
|
||||||
SetBody(map[string]interface{}{
|
SetBody(map[string]interface{}{
|
||||||
@ -207,6 +205,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
|
|||||||
Post("/chat/completions")
|
Post("/chat/completions")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.SugaredLogger.Infof("Stream error : %s", err.Error())
|
||||||
ch <- err.Error()
|
ch <- err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -228,26 +227,32 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
ReasoningContent string `json:"reasoning_content"`
|
ReasoningContent string `json:"reasoning_content"`
|
||||||
} `json:"delta"`
|
} `json:"delta"`
|
||||||
|
FinishReason string `json:"finish_reason"`
|
||||||
} `json:"choices"`
|
} `json:"choices"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(data), &streamResponse); err == nil {
|
if err := json.Unmarshal([]byte(data), &streamResponse); err == nil {
|
||||||
for _, choice := range streamResponse.Choices {
|
for _, choice := range streamResponse.Choices {
|
||||||
txt := ""
|
|
||||||
if content := choice.Delta.Content; content != "" {
|
if content := choice.Delta.Content; content != "" {
|
||||||
txt = content
|
ch <- content
|
||||||
logger.SugaredLogger.Infof("Content data: %s", txt)
|
logger.SugaredLogger.Infof("Content data: %s", content)
|
||||||
}
|
}
|
||||||
if reasoningContent := choice.Delta.ReasoningContent; reasoningContent != "" {
|
if reasoningContent := choice.Delta.ReasoningContent; reasoningContent != "" {
|
||||||
txt = reasoningContent
|
ch <- reasoningContent
|
||||||
logger.SugaredLogger.Infof("ReasoningContent data: %s", txt)
|
logger.SugaredLogger.Infof("ReasoningContent data: %s", reasoningContent)
|
||||||
|
}
|
||||||
|
if choice.FinishReason == "stop" {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
ch <- txt
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.SugaredLogger.Infof("Stream data error : %s", err.Error())
|
logger.SugaredLogger.Infof("Stream data error : %s", err.Error())
|
||||||
|
ch <- err.Error()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ch <- line
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return ch
|
return ch
|
||||||
@ -379,7 +384,7 @@ func GetTelegraphList() *[]string {
|
|||||||
}
|
}
|
||||||
var telegraph []string
|
var telegraph []string
|
||||||
document.Find("div.telegraph-content-box").Each(func(i int, selection *goquery.Selection) {
|
document.Find("div.telegraph-content-box").Each(func(i int, selection *goquery.Selection) {
|
||||||
//logger.SugaredLogger.Info(selection.Text())
|
logger.SugaredLogger.Info(selection.Text())
|
||||||
telegraph = append(telegraph, selection.Text())
|
telegraph = append(telegraph, selection.Text())
|
||||||
})
|
})
|
||||||
return &telegraph
|
return &telegraph
|
||||||
|
@ -585,8 +585,8 @@ func SearchStockInfo(stock, msgType string) *[]string {
|
|||||||
err := chromedp.Run(ctx,
|
err := chromedp.Run(ctx,
|
||||||
chromedp.Navigate(url),
|
chromedp.Navigate(url),
|
||||||
// 等待页面加载完成,可以根据需要调整等待时间
|
// 等待页面加载完成,可以根据需要调整等待时间
|
||||||
chromedp.Sleep(3*time.Second),
|
//chromedp.Sleep(3*time.Second),
|
||||||
//chromedp.WaitVisible("a.search-content", chromedp.ByQuery),
|
chromedp.WaitVisible(".search-content", chromedp.ByQuery),
|
||||||
chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
|
chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -599,11 +599,11 @@ func SearchStockInfo(stock, msgType string) *[]string {
|
|||||||
return &[]string{}
|
return &[]string{}
|
||||||
}
|
}
|
||||||
var messages []string
|
var messages []string
|
||||||
document.Find("a.search-content").Each(func(i int, selection *goquery.Selection) {
|
document.Find(".search-content").Each(func(i int, selection *goquery.Selection) {
|
||||||
text := strutil.RemoveNonPrintable(selection.Text())
|
text := strutil.RemoveNonPrintable(selection.Text())
|
||||||
if strings.Contains(text, stock) {
|
if strings.Contains(text, stock) {
|
||||||
messages = append(messages, text)
|
messages = append(messages, text)
|
||||||
logger.SugaredLogger.Infof("搜索到消息: %s", text)
|
logger.SugaredLogger.Infof("搜索到消息-%s: %s", msgType, text)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return &messages
|
return &messages
|
||||||
|
@ -3,7 +3,6 @@ package data
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/PuerkitoBio/goquery"
|
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
"github.com/duke-git/lancet/v2/strutil"
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
@ -21,29 +20,12 @@ import (
|
|||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
func TestGetTelegraph(t *testing.T) {
|
func TestGetTelegraph(t *testing.T) {
|
||||||
url := "https://www.cls.cn/telegraph"
|
GetTelegraphList()
|
||||||
response, err := resty.New().R().
|
|
||||||
SetHeader("Referer", "https://www.cls.cn/").
|
|
||||||
SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60").
|
|
||||||
Get(fmt.Sprintf(url))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logger.SugaredLogger.Info(string(response.Body()))
|
|
||||||
document, err := goquery.NewDocumentFromReader(strings.NewReader(string(response.Body())))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
document.Find("div.telegraph-content-box").Each(func(i int, selection *goquery.Selection) {
|
|
||||||
text := selection.Text()
|
|
||||||
logger.SugaredLogger.Info(text)
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetTelegraphSearch(t *testing.T) {
|
func TestGetTelegraphSearch(t *testing.T) {
|
||||||
//url := "https://www.cls.cn/searchPage?keyword=%E9%97%BB%E6%B3%B0%E7%A7%91%E6%8A%80&type=telegram"
|
//url := "https://www.cls.cn/searchPage?keyword=%E9%97%BB%E6%B3%B0%E7%A7%91%E6%8A%80&type=telegram"
|
||||||
messages := SearchStockInfo("闻泰科技", "depth")
|
messages := SearchStockInfo("闻泰科技", "telegram")
|
||||||
for _, message := range *messages {
|
for _, message := range *messages {
|
||||||
logger.SugaredLogger.Info(message)
|
logger.SugaredLogger.Info(message)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user