123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- 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"
- )
- // 智谱清言-通用外围
- func PostZhiPuAI(content string) map[string]interface{} {
- // API的URL
- apiURL := "https://open.bigmodel.cn/api/paas/v4/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": ul.FlashModel,
- "messages": messages,
- "temperature": 0.1,
- "max_tokens": 4096,
- }
- 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.Set("Content-Type", "application/json")
- // 如果API需要认证,可以在这里设置认证信息
- req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
- // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
- 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"])
- result = strings.ReplaceAll(result, "\n", "")
- result = strings.ReplaceAll(result, "json", "")
- result = strings.ReplaceAll(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 PostClassZhiPuAI(content string) map[string]interface{} {
- // API的URL
- apiURL := "https://open.bigmodel.cn/api/paas/v4/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": ul.FlashModel,
- "messages": messages,
- "temperature": 0.2,
- "top_p": 0.7,
- //"max_tokens": 4096,
- }
- 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.Set("Content-Type", "application/json")
- // 如果API需要认证,可以在这里设置认证信息
- req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
- // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
- 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"])
- result = strings.ReplaceAll(result, "\n", "")
- result = strings.ReplaceAll(result, "json", "")
- result = strings.ReplaceAll(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 PostPackageZhiPuAI(content string) map[string]interface{} {
- // API的URL
- apiURL := "https://open.bigmodel.cn/api/paas/v4/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": ul.FlashModel,
- "messages": messages,
- "temperature": 0.1,
- "max_tokens": 4096,
- }
- 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.Set("Content-Type", "application/json")
- // 如果API需要认证,可以在这里设置认证信息
- req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
- // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
- 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 = strings.ReplaceAll(result, "\n", "")
- result = strings.ReplaceAll(result, "json", "")
- result = strings.ReplaceAll(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 PostZhiPuInfo(content string) map[string]interface{} {
- zp, ok := map[string]interface{}{}, 0
- for {
- ok++
- if zp = PostZhiPuAI(content); len(zp) > 0 {
- break
- }
- if ok >= 2 {
- break
- }
- }
- return zp
- }
- // 请求质谱多包字段...
- func PostZhiPuPackageInfo(content string) map[string]interface{} {
- zp, ok := map[string]interface{}{}, 0
- for {
- ok++
- if zp = PostPackageZhiPuAI(content); len(zp) > 0 {
- break
- }
- if ok >= 2 {
- break
- }
- }
- return zp
- }
- // 请求质谱数据-分类字段
- func PostZhiPuClassInfo(content string) (map[string]interface{}, bool) {
- zp := map[string]interface{}{}
- times := 0
- ok := false
- for {
- times++
- zp = PostClassZhiPuAI(content)
- if len(zp) > 0 {
- ok = true
- break
- }
- if times >= 2 {
- break
- }
- }
- return zp, ok
- }
|