12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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 PostTongYiAI(content string) map[string]interface{} {
- // API的URL
- apiURL := "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
- // 构造请求数据
- messages := []map[string]interface{}{}
- messages = append(messages, map[string]interface{}{
- "role": "user",
- "content": content,
- })
- //qwen-plus qwen2-72b-instruct
- requestData := map[string]interface{}{
- "model": "qwen2-72b-instruct",
- "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.Set("Content-Type", "application/json")
- // 如果API需要认证,可以在这里设置认证信息
- req.Header.Set("Authorization", "Bearer sk-5db8cfa345754e329c492973b0ecad27")
- // 发起请求
- client := &http.Client{}
- client.Timeout = 180 * time.Second
- resp, err := client.Do(req)
- if err != nil {
- log.Debug("Error: %s", err)
- 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, "")
- result = strings.ReplaceAll(result, "[", "")
- 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{}{}
- }
|