62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package aichat
|
||
|
||
import (
|
||
"aiweek/option"
|
||
"bytes"
|
||
"fmt"
|
||
"github.com/tidwall/gjson"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
"strings"
|
||
)
|
||
|
||
type KimiResp string
|
||
|
||
type AiReq struct {
|
||
token string
|
||
body string
|
||
}
|
||
|
||
func NewAiReq(url string) *AiReq {
|
||
body := fmt.Sprintf("{\"model\":\"kimi\",\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"file\",\"file_url\":{\"url\":\"%s\"}},{\"type\":\"text\",\"text\":\"帮忙分析下工单中的错别字,告诉我对应的工单id\"}]}],\"use_search\":false}", url)
|
||
return &AiReq{
|
||
token: option.MODEL_TOKEN,
|
||
body: body,
|
||
}
|
||
}
|
||
|
||
func (a *AiReq) StartChat(url string) KimiResp {
|
||
// 定义 POST 请求的 URL
|
||
// 创建一个带有 JSON 负载的 POST 请求
|
||
payload := []byte(a.body) // 根据需要修改 JSON 负载
|
||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
|
||
// 添加 Token 到请求头
|
||
req.Header.Add("Authorization", "Bearer "+a.token)
|
||
// 设置 Content-Type 为 application/json
|
||
req.Header.Set("Content-Type", "application/json")
|
||
|
||
// 发送请求
|
||
client := &http.Client{}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
body, _ := io.ReadAll(resp.Body)
|
||
log.Println(KimiResp(body))
|
||
return KimiResp(body)
|
||
}
|
||
|
||
func (k KimiResp) PrepareWechatBody() string {
|
||
wechatBody := gjson.Get(string(k), "choices.#.message.content").String()
|
||
r := strings.NewReplacer("\\n", "\n", "\\", "", "[", "", "]", "", "\"", "")
|
||
wechatBody = r.Replace(wechatBody)
|
||
return wechatBody
|
||
}
|