123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package main
- import (
- "bytes"
- "encoding/json"
- log "github.com/donnie4w/go-logger/logger"
- "io/ioutil"
- "net/http"
- "time"
- )
- // 通用提示语···
- func PostDouBaoDSAI(content string) string {
- // 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,
- })
- //glm-4-air glm-4-0520 glm-4-flash
- requestData := map[string]interface{}{
- "model": "ep-20250313104433-ptcxr",
- //"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 ""
- }
- // 设置请求头
- 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 ""
- }
- defer resp.Body.Close()
- // 解析响应
- body, _ := ioutil.ReadAll(resp.Body)
- res := make(map[string]interface{})
- json.Unmarshal(body, &res)
- if res != nil {
- if choices, ok := res["choices"].([]interface{}); ok {
- if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
- if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
- if rescontent, ok4 := message["content"].(string); ok4 {
- return rescontent
- }
- }
- }
- }
- }
- //log.Debug(res)
- return ""
- //return map[string]interface{}{}
- }
|