@@ -125,13 +125,6 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
125125 ch := make (chan string )
126126 go func () {
127127 defer close (ch )
128- client := resty .New ()
129- client .SetBaseURL (o .BaseUrl )
130- client .SetHeader ("Authorization" , "Bearer " + o .ApiKey )
131- client .SetHeader ("Content-Type" , "application/json" )
132- client .SetRetryCount (3 )
133- client .SetTimeout (time .Second * 60 )
134-
135128 msg := []map [string ]interface {}{
136129 {
137130 "role" : "system" ,
@@ -142,7 +135,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
142135 }
143136
144137 wg := & sync.WaitGroup {}
145- wg .Add (2 )
138+ wg .Add (4 )
146139
147140 go func () {
148141 defer wg .Done ()
@@ -168,33 +161,38 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
168161 }
169162 }()
170163
171- // go func() {
172- // defer wg.Done()
173- // messages := SearchStockInfo(stock, "depth")
174- // for _, message := range *messages {
175- // msg = append(msg, map[string]interface{}{
176- // "role": "assistant",
177- // "content": message,
178- // })
179- // }
180- // }()
181- // go func() {
182- // defer wg.Done()
183- // messages := SearchStockInfo(stock, "telegram")
184- // for _, message := range *messages {
185- // msg = append(msg, map[string]interface{}{
186- // "role": "assistant",
187- // "content": message,
188- // })
189- // }
190- // }()
164+ go func () {
165+ defer wg .Done ()
166+ messages := SearchStockInfo (stock , "depth" )
167+ for _ , message := range * messages {
168+ msg = append (msg , map [string ]interface {}{
169+ "role" : "assistant" ,
170+ "content" : message ,
171+ })
172+ }
173+ }()
174+ go func () {
175+ defer wg .Done ()
176+ messages := SearchStockInfo (stock , "telegram" )
177+ for _ , message := range * messages {
178+ msg = append (msg , map [string ]interface {}{
179+ "role" : "assistant" ,
180+ "content" : message ,
181+ })
182+ }
183+ }()
191184 wg .Wait ()
192185
193186 msg = append (msg , map [string ]interface {}{
194187 "role" : "user" ,
195188 "content" : stock + "分析和总结" ,
196189 })
197-
190+ client := resty .New ()
191+ client .SetBaseURL (o .BaseUrl )
192+ client .SetHeader ("Authorization" , "Bearer " + o .ApiKey )
193+ client .SetHeader ("Content-Type" , "application/json" )
194+ client .SetRetryCount (3 )
195+ client .SetTimeout (1 * time .Minute )
198196 resp , err := client .R ().
199197 SetDoNotParseResponse (true ).
200198 SetBody (map [string ]interface {}{
@@ -207,6 +205,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
207205 Post ("/chat/completions" )
208206
209207 if err != nil {
208+ logger .SugaredLogger .Infof ("Stream error : %s" , err .Error ())
210209 ch <- err .Error ()
211210 return
212211 }
@@ -228,26 +227,32 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
228227 Content string `json:"content"`
229228 ReasoningContent string `json:"reasoning_content"`
230229 } `json:"delta"`
230+ FinishReason string `json:"finish_reason"`
231231 } `json:"choices"`
232232 }
233233
234234 if err := json .Unmarshal ([]byte (data ), & streamResponse ); err == nil {
235235 for _ , choice := range streamResponse .Choices {
236- txt := ""
237236 if content := choice .Delta .Content ; content != "" {
238- txt = content
239- logger .SugaredLogger .Infof ("Content data: %s" , txt )
237+ ch <- content
238+ logger .SugaredLogger .Infof ("Content data: %s" , content )
240239 }
241240 if reasoningContent := choice .Delta .ReasoningContent ; reasoningContent != "" {
242- txt = reasoningContent
243- logger .SugaredLogger .Infof ("ReasoningContent data: %s" , txt )
241+ ch <- reasoningContent
242+ logger .SugaredLogger .Infof ("ReasoningContent data: %s" , reasoningContent )
243+ }
244+ if choice .FinishReason == "stop" {
245+ return
244246 }
245- ch <- txt
246247 }
247248 } else {
248249 logger .SugaredLogger .Infof ("Stream data error : %s" , err .Error ())
250+ ch <- err .Error ()
249251 }
252+ } else {
253+ ch <- line
250254 }
255+
251256 }
252257 }()
253258 return ch
@@ -379,7 +384,7 @@ func GetTelegraphList() *[]string {
379384 }
380385 var telegraph []string
381386 document .Find ("div.telegraph-content-box" ).Each (func (i int , selection * goquery.Selection ) {
382- // logger.SugaredLogger.Info(selection.Text())
387+ logger .SugaredLogger .Info (selection .Text ())
383388 telegraph = append (telegraph , selection .Text ())
384389 })
385390 return & telegraph
0 commit comments