|
@@ -0,0 +1,247 @@
|
|
|
+package ai
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "data_ai/ul"
|
|
|
+ "encoding/json"
|
|
|
+ log "github.com/donnie4w/go-logger/logger"
|
|
|
+ "io/ioutil"
|
|
|
+ qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+var req_retry_deepseek = 1
|
|
|
+
|
|
|
+// 通用外围
|
|
|
+func PostDeepSeekAI(content string) map[string]interface{} {
|
|
|
+ // API的URL
|
|
|
+ apiURL := "https://api.deepseek.com/chat/completions"
|
|
|
+ // 构造请求数据
|
|
|
+ messages := []map[string]interface{}{}
|
|
|
+ messages = append(messages, map[string]interface{}{
|
|
|
+ "role": "user",
|
|
|
+ "content": content,
|
|
|
+ })
|
|
|
+ //glm-4-air glm-4-0520 glm-4-flash
|
|
|
+ requestData := map[string]interface{}{
|
|
|
+ "model": "deepseek-chat",
|
|
|
+ "messages": messages,
|
|
|
+ }
|
|
|
+ jsonData, _ := json.Marshal(requestData)
|
|
|
+ // 创建HTTP请求
|
|
|
+ req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
|
|
+ if err != nil {
|
|
|
+ log.Debug("Error: %s", err)
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ // 设置请求头
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Bearer sk-832c259e371743a0bb7b280cb86ad806")
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ client.Timeout = 120 * time.Second
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ res := make(map[string]interface{})
|
|
|
+ json.Unmarshal(body, &res)
|
|
|
+ if res != nil {
|
|
|
+ if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
|
|
|
+ if message := qu.ObjToMap(choices[0]["message"]); message != nil {
|
|
|
+ result := qu.ObjToString((*message)["content"])
|
|
|
+ result = ul.Escape.ReplaceAllString(result, "")
|
|
|
+ if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
|
|
|
+ result = new_result
|
|
|
+ }
|
|
|
+ dict := make(map[string]interface{})
|
|
|
+ json.Unmarshal([]byte(result), &dict)
|
|
|
+ return dict
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map[string]interface{}{}
|
|
|
+}
|
|
|
+
|
|
|
+// 分类字段
|
|
|
+func PostClassDeepSeekAI(content string) map[string]interface{} {
|
|
|
+ // API的URL
|
|
|
+ apiURL := "https://api.deepseek.com/chat/completions"
|
|
|
+ // 构造请求数据
|
|
|
+ messages := []map[string]interface{}{}
|
|
|
+ messages = append(messages, map[string]interface{}{
|
|
|
+ "role": "user",
|
|
|
+ "content": content,
|
|
|
+ })
|
|
|
+ //glm-4-air glm-4-0520 glm-4-flash
|
|
|
+ requestData := map[string]interface{}{
|
|
|
+ "model": "deepseek-chat",
|
|
|
+ "messages": messages,
|
|
|
+ }
|
|
|
+ jsonData, _ := json.Marshal(requestData)
|
|
|
+ // 创建HTTP请求
|
|
|
+ req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
|
|
+ if err != nil {
|
|
|
+ log.Debug("Error: %s", err)
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ // 设置请求头
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Bearer sk-832c259e371743a0bb7b280cb86ad806")
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ client.Timeout = 120 * time.Second
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ res := make(map[string]interface{})
|
|
|
+ json.Unmarshal(body, &res)
|
|
|
+ if res != nil {
|
|
|
+ if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
|
|
|
+ if message := qu.ObjToMap(choices[0]["message"]); message != nil {
|
|
|
+ result := qu.ObjToString((*message)["content"])
|
|
|
+ result = ul.Escape.ReplaceAllString(result, "")
|
|
|
+ if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
|
|
|
+ result = new_result
|
|
|
+ }
|
|
|
+ dict := make(map[string]interface{})
|
|
|
+ json.Unmarshal([]byte(result), &dict)
|
|
|
+ return dict
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map[string]interface{}{}
|
|
|
+}
|
|
|
+
|
|
|
+// 分包字段
|
|
|
+func PostPackageDeepSeekAI(content string) map[string]interface{} {
|
|
|
+ // API的URL
|
|
|
+ apiURL := "https://api.deepseek.com/chat/completions"
|
|
|
+ // 构造请求数据
|
|
|
+ messages := []map[string]interface{}{}
|
|
|
+ messages = append(messages, map[string]interface{}{
|
|
|
+ "role": "system",
|
|
|
+ "content": "你是一名’招标工程师’,拥有写标书及阅读理解公告的能力,根据要求抽取所需的内容,抽取内容要实事求是,不会无中生有。",
|
|
|
+ })
|
|
|
+ messages = append(messages, map[string]interface{}{
|
|
|
+ "role": "user",
|
|
|
+ "content": content,
|
|
|
+ })
|
|
|
+ //glm-4-air glm-4-0520 glm-4-flash
|
|
|
+ requestData := map[string]interface{}{
|
|
|
+ "model": "deepseek-chat",
|
|
|
+ "messages": messages,
|
|
|
+ }
|
|
|
+ jsonData, _ := json.Marshal(requestData)
|
|
|
+ // 创建HTTP请求
|
|
|
+ req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
|
|
+ if err != nil {
|
|
|
+ log.Debug("Error: %s", err)
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ // 设置请求头
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Bearer sk-832c259e371743a0bb7b280cb86ad806")
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ client.Timeout = 180 * time.Second
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return map[string]interface{}{}
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ res := make(map[string]interface{})
|
|
|
+ json.Unmarshal(body, &res)
|
|
|
+ if res != nil {
|
|
|
+ if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
|
|
|
+ if message := qu.ObjToMap(choices[0]["message"]); message != nil {
|
|
|
+ result := qu.ObjToString((*message)["content"])
|
|
|
+ //最终正确的结果
|
|
|
+ arr := strings.Split(result, "最终正确的结果")
|
|
|
+ if len(arr) > 1 {
|
|
|
+ result = arr[1]
|
|
|
+ }
|
|
|
+ result = ul.Escape.ReplaceAllString(result, "")
|
|
|
+ if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
|
|
|
+ result = new_result
|
|
|
+ }
|
|
|
+ dict := make(map[string]interface{})
|
|
|
+ json.Unmarshal([]byte(result), &dict)
|
|
|
+ return dict
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map[string]interface{}{}
|
|
|
+}
|
|
|
+
|
|
|
+/*****************************
|
|
|
+******************************
|
|
|
+******************************
|
|
|
+******************************
|
|
|
+******************************
|
|
|
+******************************/
|
|
|
+// 请求数据外围字段···重试1次···
|
|
|
+func PostDeepSeekInfo(content string) map[string]interface{} {
|
|
|
+ zp, ok := map[string]interface{}{}, 0
|
|
|
+ for {
|
|
|
+ ok++
|
|
|
+ if zp = PostDeepSeekAI(content); len(zp) > 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if ok >= req_retry {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return zp
|
|
|
+}
|
|
|
+
|
|
|
+// 请求多包字段...
|
|
|
+func PostDeepSeekPackageInfo(content string) map[string]interface{} {
|
|
|
+ zp, ok := map[string]interface{}{}, 0
|
|
|
+ for {
|
|
|
+ ok++
|
|
|
+ if zp = PostPackageDeepSeekAI(content); len(zp) > 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if ok >= req_retry_deepseek {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return zp
|
|
|
+}
|
|
|
+
|
|
|
+// 请求质谱数据-分类字段
|
|
|
+func PostDeepSeekClassInfo(content string) (map[string]interface{}, bool) {
|
|
|
+ zp := map[string]interface{}{}
|
|
|
+ times := 0
|
|
|
+ ok := false
|
|
|
+ for {
|
|
|
+ times++
|
|
|
+ zp = PostClassDeepSeekAI(content)
|
|
|
+ if len(zp) > 0 {
|
|
|
+ ok = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if times >= req_retry_deepseek {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return zp, ok
|
|
|
+}
|