2024-06-30 21:56:34 +08:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
2024-07-02 18:16:26 +08:00
|
|
|
"fmt"
|
2024-07-01 10:05:42 +08:00
|
|
|
"go-bot/config"
|
2024-06-30 21:56:34 +08:00
|
|
|
"log"
|
2024-06-30 23:34:00 +08:00
|
|
|
"strings"
|
2024-06-30 21:56:34 +08:00
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/parnurzeal/gorequest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AI struct {
|
|
|
|
*StdAns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AI) GetMsg() string {
|
|
|
|
if len(a.Parms) < 2 {
|
|
|
|
return "使用!ai xxx 向我提问吧"
|
|
|
|
|
|
|
|
}
|
2024-06-30 23:34:00 +08:00
|
|
|
ask := a.Parms[1]
|
2024-07-01 21:04:22 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
if ask == "" {
|
|
|
|
return "不问问题你说个屁!"
|
|
|
|
}
|
2024-06-30 21:56:34 +08:00
|
|
|
var msg string
|
|
|
|
var OPENAI_API_KEY string
|
|
|
|
if cfg["OPENAI_API_KEY"] != nil {
|
|
|
|
OPENAI_API_KEY = cfg["OPENAI_API_KEY"].(string)
|
|
|
|
} else {
|
|
|
|
log.Println("OPENAI_API_KEY 未配置")
|
|
|
|
return "OPENAI_API_KEY 未配置"
|
|
|
|
}
|
|
|
|
var OPENAI_BaseURL string
|
|
|
|
if cfg["OPENAI_BaseURL"] != nil {
|
|
|
|
OPENAI_BaseURL = cfg["OPENAI_BaseURL"].(string)
|
|
|
|
} else {
|
|
|
|
log.Println("OPENAI_BaseURL 未配置,使用openai默认配置")
|
2024-06-30 23:34:00 +08:00
|
|
|
OPENAI_BaseURL = "https://api.openai.com/v1"
|
2024-06-30 21:56:34 +08:00
|
|
|
}
|
|
|
|
var MODEL string
|
|
|
|
if cfg["MODEL"] != nil {
|
|
|
|
MODEL = cfg["MODEL"].(string)
|
|
|
|
} else {
|
|
|
|
log.Println("模型 未配置,使用默认chatglm_pro模型")
|
|
|
|
MODEL = "chatglm_pro"
|
|
|
|
}
|
2024-06-30 23:34:00 +08:00
|
|
|
|
|
|
|
if strings.ToLower(a.Parms[1]) == "models" {
|
2024-07-01 10:05:42 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
OPENAI_BaseURL = OPENAI_BaseURL + "/models"
|
2024-07-01 21:04:22 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
request := gorequest.New()
|
|
|
|
resp, body, errs := request.Get(OPENAI_BaseURL).
|
|
|
|
Set("Content-Type", "application/json").
|
|
|
|
Set("Authorization", "Bearer "+OPENAI_API_KEY).
|
|
|
|
End()
|
|
|
|
if errs != nil {
|
|
|
|
log.Println(errs)
|
|
|
|
return "请求失败"
|
|
|
|
} else {
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
var responseBody map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return "解析模型列表失败"
|
|
|
|
}
|
2024-07-01 21:04:22 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
choices := responseBody["data"].([]interface{})
|
2024-07-01 10:05:42 +08:00
|
|
|
var models []interface{}
|
2024-06-30 23:34:00 +08:00
|
|
|
if len(choices) > 0 {
|
|
|
|
msg = "支持的模型列表:\n"
|
|
|
|
for _, choice := range choices {
|
2024-07-01 10:05:42 +08:00
|
|
|
model := choice.(map[string]interface{})["id"].(string)
|
|
|
|
if model == MODEL {
|
|
|
|
msg = msg + model + "\t ✔\n"
|
2024-07-01 00:26:26 +08:00
|
|
|
} else {
|
2024-07-01 10:05:42 +08:00
|
|
|
msg = msg + model + "\n"
|
2024-07-01 00:26:26 +08:00
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
models = append(models, model)
|
2024-07-01 00:26:26 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
} else {
|
|
|
|
msg = "没查到支持模型列表"
|
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
if len(a.Parms) > 3 && strings.ToLower(a.Parms[2]) == "set" {
|
|
|
|
// 判断允许设置权限,需要AllowUser和发消息用户账号相同
|
2024-07-02 13:21:29 +08:00
|
|
|
if a.Master != nil && contains(a.Master, a.UID) {
|
2024-07-01 10:05:42 +08:00
|
|
|
if contains(models, a.Parms[3]) {
|
|
|
|
cfg["MODEL"] = a.Parms[3]
|
|
|
|
msg = "已设置模型为 " + a.Parms[3]
|
|
|
|
config.ModifyConfig("MODEL", a.Parms[3])
|
|
|
|
config.ReloadConfig()
|
|
|
|
config.PrintConfig(cfg, "")
|
|
|
|
} else {
|
|
|
|
msg = "不支持的模型"
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
msg = "无权限设置模型"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-06-30 23:34:00 +08:00
|
|
|
return msg
|
2024-06-30 21:56:34 +08:00
|
|
|
} else {
|
2024-06-30 23:34:00 +08:00
|
|
|
log.Println("请求失败")
|
|
|
|
return "请求模型列表失败"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OPENAI_BaseURL = OPENAI_BaseURL + "/chat/completions"
|
|
|
|
PROMPT, ok := cfg["PROMPT"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Println("PROMRT 未配置")
|
|
|
|
PROMPT = ""
|
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
// PROMPT = ""
|
2024-07-02 13:21:29 +08:00
|
|
|
// println("PROMPT:", PROMPT)
|
|
|
|
// println("ask:", ask)
|
2024-06-30 23:34:00 +08:00
|
|
|
requestBody := map[string]interface{}{
|
2024-07-01 00:26:26 +08:00
|
|
|
"model": MODEL,
|
|
|
|
"stream": false,
|
2024-07-01 10:05:42 +08:00
|
|
|
"messages": []map[string]string{
|
|
|
|
{
|
|
|
|
"role": "system",
|
|
|
|
"content": PROMPT,
|
|
|
|
},
|
2024-07-03 17:01:00 +08:00
|
|
|
{"role": "user", "content": a.RawMsg[strings.Index(a.RawMsg, " ")+1:]}},
|
2024-07-01 21:04:22 +08:00
|
|
|
// "max_tokens": 200,
|
2024-06-30 23:34:00 +08:00
|
|
|
"temperature": 0.7,
|
|
|
|
}
|
|
|
|
request := gorequest.New()
|
|
|
|
resp, body, errs := request.Post(OPENAI_BaseURL).
|
|
|
|
Set("Content-Type", "application/json").
|
|
|
|
Set("Authorization", "Bearer "+OPENAI_API_KEY).
|
|
|
|
Send(requestBody).
|
|
|
|
End()
|
|
|
|
if errs != nil {
|
|
|
|
log.Println(errs)
|
|
|
|
return "请求失败"
|
|
|
|
} else {
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
var responseBody map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return "解析失败"
|
|
|
|
}
|
|
|
|
choices := responseBody["choices"].([]interface{})
|
|
|
|
if len(choices) > 0 {
|
|
|
|
choice := choices[0].(map[string]interface{})
|
|
|
|
msg = choice["message"].(map[string]interface{})["content"].(string)
|
2024-07-01 21:04:22 +08:00
|
|
|
// println("msg:", msg)
|
|
|
|
|
2024-06-30 23:34:00 +08:00
|
|
|
} else {
|
|
|
|
log.Println("choices为空")
|
2024-07-02 18:16:26 +08:00
|
|
|
msg = "api解析失败"
|
2024-06-30 23:34:00 +08:00
|
|
|
}
|
2024-06-30 21:56:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:16:26 +08:00
|
|
|
return fmt.Sprintf("[CQ:at,qq=%s] %s", a.UID, msg)
|
2024-06-30 21:56:34 +08:00
|
|
|
|
|
|
|
}
|