12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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"
- )
- // 通用提示语···
- func PostDouBaoDSAI(content string) map[string]interface{} {
- // API的URL
- apiURL := "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
- // 构造请求数据
- messages := []map[string]interface{}{}
- messages = append(messages, map[string]interface{}{
- "role": "user",
- "content": content,
- })
- //ep-20250225173301-w557p 线上
- //ep-20250313104433-ptcxr 测试
- requestData := map[string]interface{}{
- "model": "ep-20250225173301-w557p",
- "temperature": 0.1,
- "top_p": 0.7,
- "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("Authorization", "Bearer df50c86b-24f5-4475-b09b-f1f85e5e6564")
- client := &http.Client{}
- client.Timeout = 300 * 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{}{}
- }
- /*****************************
- ******************************
- ******************************
- ******************************
- ******************************
- ******************************/
- // 请求数据外围字段···重试1次···
- func PostDouBaoDSInfo(content string) map[string]interface{} {
- zp, ok := map[string]interface{}{}, 0
- for {
- ok++
- if zp = PostDouBaoDSAI(content); len(zp) > 0 {
- break
- }
- if ok >= req_retry_deepseek {
- break
- }
- }
- return zp
- }
|