123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- 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"
- "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",
- "temperature": 1,
- "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-7e40165d06a04f3c87e2fc15e69df625")
- 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
- //}
|