|
@@ -0,0 +1,325 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "eta/eta_task/models"
|
|
|
+ "eta/eta_task/utils"
|
|
|
+ "fmt"
|
|
|
+ "github.com/PuerkitoBio/goquery"
|
|
|
+ "html"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 使用deepseek api 处理报告
|
|
|
+func AutoCreateReportAssess(cont context.Context) (err error) {
|
|
|
+ //获取品种
|
|
|
+ startTime := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ endTime := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 23:59:59"
|
|
|
+ createDate := time.Now().AddDate(0, 0, -1).Format("2006.01.02")
|
|
|
+
|
|
|
+ tagList, err := models.GetWechatArticleTags(startTime, endTime)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetWechatArticleTags Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ req := new(DkCompletionsReq)
|
|
|
+ req.Model = "deepseek-chat"
|
|
|
+ req.FrequencyPenalty = 0
|
|
|
+ req.MaxTokens = 8192
|
|
|
+ req.PresencePenalty = 0
|
|
|
+ req.ResponseFormat.Type = "text"
|
|
|
+ req.Stop = nil
|
|
|
+ req.Stream = false
|
|
|
+ req.StreamOptions = nil
|
|
|
+ req.Temperature = 1
|
|
|
+ req.TopP = 1
|
|
|
+ req.Tools = nil
|
|
|
+ req.ToolChoice = "none"
|
|
|
+ req.Logprobs = false
|
|
|
+ req.TopLogprobs = nil
|
|
|
+
|
|
|
+ msgList := make([]*DkCompletionsMessagesReq, 0)
|
|
|
+ msg := new(DkCompletionsMessagesReq)
|
|
|
+ //msg.Content = "请阅读如下文章。首先根据文章内容,对#品种名称 进行打分,打分规则为10分最看多,0分最看空。其次,对#品种名称 提炼主要逻辑和观点,得出总结,品种和品种之间用横线拆分,输出格式限定为:1. 使用语义化的HTML5标签,2. 采用现代响应式设计(无固定宽度),3. 包含丰富的文本格式元素,4. 通过内联样式确保自适应渲染"
|
|
|
+ msg.Content = "请阅读如下文章。首先根据文章内容,对#品种名称 进行打分,打分规则为10分最看多,0分最看空。其次,对#品种名称 提炼主要逻辑和观点,得出总结,品种和品种之间用横线拆分,输出格式限定为:1. 使用语义化的HTML5标签代码段,不包含html,body,head,2. 采用现代响应式设计(无固定宽度),3. 包含丰富的文本格式元素,4. 通过内联样式确保自适应渲染"
|
|
|
+ msg.Role = "system"
|
|
|
+ msgList = append(msgList, msg)
|
|
|
+
|
|
|
+ for k, v := range tagList {
|
|
|
+ msgItem := new(DkCompletionsMessagesReq)
|
|
|
+ msgItem.Role = "user"
|
|
|
+ fmt.Println(k, v.TagName)
|
|
|
+ //根据标签获取文章
|
|
|
+ articleList, err := models.GetWechatArticle(v.ArticleId, startTime, endTime)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetWechatArticle Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var context string
|
|
|
+ context += `品种 ` + v.TagName + ":"
|
|
|
+ for _, av := range articleList {
|
|
|
+ context += av.TextContent
|
|
|
+ }
|
|
|
+ utils.FileLog.Info(context)
|
|
|
+
|
|
|
+ context = strings.ReplaceAll(context, "\n", "")
|
|
|
+ context = strings.ReplaceAll(context, `"`, "")
|
|
|
+ context = strings.ReplaceAll(context, `\`, "/")
|
|
|
+ context = strings.ReplaceAll(context, ` `, "")
|
|
|
+ context = strings.ReplaceAll(context, ` `, "")
|
|
|
+ context = strings.ReplaceAll(context, `⭐️`, "")
|
|
|
+
|
|
|
+ context = strings.ReplaceAll(context, `"`, "")
|
|
|
+ msgItem.Content = context
|
|
|
+ msgList = append(msgList, msgItem)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(msgList) <= 0 {
|
|
|
+ fmt.Println("没有需要生成的文章")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ req.Messages = msgList
|
|
|
+ reqBody, err := json.Marshal(req)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("json.Marshal err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ isSummary := 0
|
|
|
+ isSuccess := dkCompletions(string(reqBody), startTime, endTime, createDate)
|
|
|
+ if isSuccess {
|
|
|
+ isSummary = 1
|
|
|
+ for _, v := range tagList {
|
|
|
+ models.ModifyWechatArticleSummary(v.ArticleId, isSummary)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 使用deepseek api 处理报告
|
|
|
+func AutoCreateReportAssessV1() {
|
|
|
+ //获取品种
|
|
|
+ startTime := `2025-05-12 00:00:00`
|
|
|
+ endTime := `2025-05-18 00:00:00`
|
|
|
+ tagList, err := models.GetWechatArticleTags(startTime, endTime)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetWechatArticleTags Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var context string
|
|
|
+
|
|
|
+ for _, v := range tagList {
|
|
|
+ context += `品种 ` + v.TagName
|
|
|
+ textContent := strings.ReplaceAll(v.TextContent, "\n", "")
|
|
|
+ //fmt.Println("textContent:" + textContent)
|
|
|
+ context += textContent
|
|
|
+ }
|
|
|
+
|
|
|
+ contextDoc, err := goquery.NewDocumentFromReader(strings.NewReader(context))
|
|
|
+ context = contextDoc.Text()
|
|
|
+ context = strings.ReplaceAll(context, `"`, "")
|
|
|
+ context = strings.ReplaceAll(context, `\`, "/")
|
|
|
+ context = strings.ReplaceAll(context, ` `, "")
|
|
|
+ context = strings.ReplaceAll(context, ` `, "")
|
|
|
+
|
|
|
+ utils.FileLog.Info(context)
|
|
|
+ fmt.Println(len(context))
|
|
|
+ //dkCompletions(context)
|
|
|
+}
|
|
|
+
|
|
|
+func dkCompletions(payloadBody, startTime, endTime, createDate string) (success bool) {
|
|
|
+
|
|
|
+ utils.FileLog.Info(payloadBody)
|
|
|
+
|
|
|
+ url := "https://api.deepseek.com/chat/completions"
|
|
|
+ method := "POST"
|
|
|
+
|
|
|
+ utils.FileLog.Info("dk payload:" + payloadBody)
|
|
|
+
|
|
|
+ payload := strings.NewReader(payloadBody)
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ req, err := http.NewRequest(method, url, payload)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Bearer sk-e21afcf4380249869d06b92a4379ee23")
|
|
|
+
|
|
|
+ res, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+
|
|
|
+ body, err := ioutil.ReadAll(res.Body)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //fmt.Println("chart completions resp body:")
|
|
|
+ //fmt.Println(string(body))
|
|
|
+ utils.FileLog.Info("dk result:" + string(body))
|
|
|
+ fmt.Println(string(body))
|
|
|
+
|
|
|
+ result := new(DkCompletionsResult)
|
|
|
+ err = json.Unmarshal(body, &result)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("DkCompletionsResult json Unmarshal Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(result.Choices) > 0 && result.Choices[0].Message.Content != "" {
|
|
|
+ resultContent := result.Choices[0].Message.Content
|
|
|
+ resultContent = strings.ReplaceAll(resultContent, `\n`, "<br/>")
|
|
|
+ resultContent = strings.ReplaceAll(resultContent, "```html", "")
|
|
|
+ resultContent = strings.ReplaceAll(resultContent, "```", "")
|
|
|
+
|
|
|
+ utils.FileLog.Info(resultContent)
|
|
|
+
|
|
|
+ //判断报告是否存在
|
|
|
+ classifyName := "AI研报"
|
|
|
+ reportClassify, err := models.GetClassifyByName(classifyName)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetClassifyByName Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if reportClassify == nil {
|
|
|
+ fmt.Println("reportClassify is nil")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var isAdd bool
|
|
|
+ reportItem, err := models.GetReport(reportClassify.Id, startTime, endTime)
|
|
|
+ if err != nil {
|
|
|
+ if err.Error() == utils.ErrNoRow() {
|
|
|
+ isAdd = true
|
|
|
+ } else {
|
|
|
+ fmt.Println("GetReport Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if reportItem == nil {
|
|
|
+ isAdd = true
|
|
|
+ }
|
|
|
+ if isAdd {
|
|
|
+ //获取最大期数
|
|
|
+ //GetReportStage
|
|
|
+ maxStage, e := models.GetReportStage(reportClassify.Id, 0, 0)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Println("GetReportStage Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(resultContent)
|
|
|
+
|
|
|
+ context := html.EscapeString(resultContent)
|
|
|
+ item := new(models.Report)
|
|
|
+ item.AddType = 1
|
|
|
+ item.ClassifyIdFirst = reportClassify.Id
|
|
|
+ item.ClassifyNameFirst = reportClassify.ClassifyName
|
|
|
+ item.ClassifyIdSecond = 244
|
|
|
+ item.ClassifyNameSecond = "AI智汇日报"
|
|
|
+ item.Title = "大宗商品AI智汇日报" + createDate
|
|
|
+ item.Frequency = "日度"
|
|
|
+ item.State = 2
|
|
|
+ item.Content = context
|
|
|
+ item.Stage = maxStage + 1
|
|
|
+ item.CreateTime = startTime
|
|
|
+ item.ModifyTime = startTime
|
|
|
+ item.CollaborateType = 1
|
|
|
+ item.IsPublicPublish = 1
|
|
|
+ item.ReportLayout = 1
|
|
|
+ err = models.AddReport(item)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("AddReport Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ reportCode := utils.MD5(strconv.Itoa(item.Id))
|
|
|
+ err = models.ModifyReportCode(item.Id, reportCode)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("ModifyReportCode Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ reportContent := html.UnescapeString(reportItem.Content)
|
|
|
+ resultContent = reportContent + resultContent
|
|
|
+ resultContent = html.EscapeString(resultContent)
|
|
|
+ err = models.ModifyReportContent(reportItem.Id, resultContent)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("ModifyReportContent Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ success = true
|
|
|
+ }
|
|
|
+ return success
|
|
|
+}
|
|
|
+
|
|
|
+type DkCompletionsReq struct {
|
|
|
+ Messages []*DkCompletionsMessagesReq `json:"messages"`
|
|
|
+ Model string `json:"model"`
|
|
|
+ FrequencyPenalty int `json:"frequency_penalty"`
|
|
|
+ MaxTokens int `json:"max_tokens"`
|
|
|
+ PresencePenalty int `json:"presence_penalty"`
|
|
|
+ ResponseFormat struct {
|
|
|
+ Type string `json:"type"`
|
|
|
+ } `json:"response_format"`
|
|
|
+ Stop interface{} `json:"stop"`
|
|
|
+ Stream bool `json:"stream"`
|
|
|
+ StreamOptions interface{} `json:"stream_options"`
|
|
|
+ Temperature int `json:"temperature"`
|
|
|
+ TopP int `json:"top_p"`
|
|
|
+ Tools interface{} `json:"tools"`
|
|
|
+ ToolChoice string `json:"tool_choice"`
|
|
|
+ Logprobs bool `json:"logprobs"`
|
|
|
+ TopLogprobs interface{} `json:"top_logprobs"`
|
|
|
+}
|
|
|
+
|
|
|
+type DkCompletionsMessagesReq struct {
|
|
|
+ Content string `json:"content"`
|
|
|
+ Role string `json:"role"`
|
|
|
+}
|
|
|
+
|
|
|
+type DkCompletionsResult struct {
|
|
|
+ Id string `json:"id"`
|
|
|
+ Object string `json:"object"`
|
|
|
+ Created int `json:"created"`
|
|
|
+ Model string `json:"model"`
|
|
|
+ Choices []struct {
|
|
|
+ Index int `json:"index"`
|
|
|
+ Message struct {
|
|
|
+ Role string `json:"role"`
|
|
|
+ Content string `json:"content"`
|
|
|
+ } `json:"message"`
|
|
|
+ Logprobs interface{} `json:"logprobs"`
|
|
|
+ FinishReason string `json:"finish_reason"`
|
|
|
+ } `json:"choices"`
|
|
|
+ Usage struct {
|
|
|
+ PromptTokens int `json:"prompt_tokens"`
|
|
|
+ CompletionTokens int `json:"completion_tokens"`
|
|
|
+ TotalTokens int `json:"total_tokens"`
|
|
|
+ PromptTokensDetails struct {
|
|
|
+ CachedTokens int `json:"cached_tokens"`
|
|
|
+ } `json:"prompt_tokens_details"`
|
|
|
+ PromptCacheHitTokens int `json:"prompt_cache_hit_tokens"`
|
|
|
+ PromptCacheMissTokens int `json:"prompt_cache_miss_tokens"`
|
|
|
+ } `json:"usage"`
|
|
|
+ SystemFingerprint string `json:"system_fingerprint"`
|
|
|
+ Error struct {
|
|
|
+ Message string `json:"message"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Param interface{} `json:"param"`
|
|
|
+ Code string `json:"code"`
|
|
|
+ } `json:"error"`
|
|
|
+}
|