2024-06-30 21:56:34 +08:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
2024-07-14 21:38:39 +08:00
|
|
|
|
"encoding/base64"
|
2024-07-02 18:16:26 +08:00
|
|
|
|
"fmt"
|
2024-07-14 21:38:39 +08:00
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
"log"
|
2024-07-13 18:00:03 +08:00
|
|
|
|
"net/http"
|
2024-07-14 21:38:39 +08:00
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
2024-06-30 23:34:00 +08:00
|
|
|
|
"strings"
|
2024-07-06 22:35:01 +08:00
|
|
|
|
"time"
|
2024-06-30 21:56:34 +08:00
|
|
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
|
"github.com/parnurzeal/gorequest"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
func init() {
|
|
|
|
|
RegisterWorkerFactory("ai", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
|
|
|
|
return &AI{
|
|
|
|
|
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
type AI struct {
|
|
|
|
|
*StdAns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *AI) GetMsg() string {
|
|
|
|
|
if len(a.Parms) < 2 {
|
|
|
|
|
return "使用!ai xxx 向我提问吧"
|
|
|
|
|
}
|
2024-07-01 21:04:22 +08:00
|
|
|
|
|
2024-07-06 19:18:59 +08:00
|
|
|
|
ask := a.Parms[1]
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if ask == "" || strings.HasPrefix(ask, "[CQ:reply,id=") {
|
2024-06-30 23:34:00 +08:00
|
|
|
|
return "不问问题你说个屁!"
|
|
|
|
|
}
|
2024-07-06 19:18:59 +08:00
|
|
|
|
|
|
|
|
|
OPENAI_API_KEY, OPENAI_BaseURL, MODEL := getConfig()
|
|
|
|
|
if OPENAI_API_KEY == "" {
|
|
|
|
|
return "OPENAI_API_KEY 未配置"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.ToLower(a.Parms[1]) == "models" {
|
|
|
|
|
return handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL)
|
|
|
|
|
} else {
|
2024-07-14 21:38:39 +08:00
|
|
|
|
OPENAI_BaseURL = OPENAI_BaseURL + "/chat/completions"
|
|
|
|
|
PROMPT, ok := cfg["PROMPT"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Println("PROMPT 未配置")
|
|
|
|
|
PROMPT = ""
|
|
|
|
|
}
|
|
|
|
|
var requestBody map[string]interface{}
|
|
|
|
|
if !strings.HasPrefix(a.Parms[len(a.Parms)-1], "[CQ:reply,id=") {
|
|
|
|
|
|
|
|
|
|
requestBody = map[string]interface{}{
|
|
|
|
|
"model": MODEL,
|
|
|
|
|
"stream": false,
|
|
|
|
|
"messages": []map[string]string{
|
|
|
|
|
{
|
|
|
|
|
"role": "system",
|
|
|
|
|
"content": PROMPT,
|
|
|
|
|
},
|
|
|
|
|
{"role": "user", "content": a.RawMsg[strings.Index(a.RawMsg, " ")+1:]},
|
|
|
|
|
},
|
|
|
|
|
"temperature": 0.7,
|
|
|
|
|
"presence_penalty": 0,
|
|
|
|
|
"frequency_penalty": 0,
|
|
|
|
|
"top_p": 1,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pattern := `^\[CQ:reply,id=(-?\d+)\]`
|
|
|
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
|
matches := re.FindStringSubmatch(a.Parms[len(a.Parms)-1])
|
|
|
|
|
var msgId string
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
|
|
|
|
|
|
msgId = matches[1]
|
|
|
|
|
} else {
|
|
|
|
|
msgId = ""
|
|
|
|
|
log.Println("未找到回复消息")
|
|
|
|
|
return "未找到回复消息"
|
|
|
|
|
}
|
|
|
|
|
message := a.GetHisMsg(msgId)
|
|
|
|
|
|
|
|
|
|
// 正则表达式匹配 file 和 file_size 的值
|
|
|
|
|
re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`)
|
|
|
|
|
matches = re.FindStringSubmatch(message)
|
|
|
|
|
var file string
|
|
|
|
|
var fileSizeStr string
|
|
|
|
|
if len(matches) > 2 {
|
|
|
|
|
file = matches[1]
|
|
|
|
|
fileSizeStr = matches[2]
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("未找到文件信息")
|
|
|
|
|
return "未找到文件信息"
|
|
|
|
|
}
|
|
|
|
|
// 将 fileSizeStr 转换为整数
|
|
|
|
|
fileSize, err := strconv.ParseFloat(fileSizeStr, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("获取图片大小失败:", err)
|
|
|
|
|
return "获取图片大小失败"
|
|
|
|
|
}
|
|
|
|
|
if fileSize/1024/1024 > 1.0 {
|
|
|
|
|
log.Println("文件大小超过1M")
|
|
|
|
|
return "文件大小超过1M"
|
|
|
|
|
}
|
|
|
|
|
filePath := a.GetImage(file)
|
2024-07-14 21:55:02 +08:00
|
|
|
|
// println("filePath:", filePath)
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if filePath == "" {
|
|
|
|
|
log.Println("获取图片失败")
|
|
|
|
|
return "获取图片失败"
|
|
|
|
|
}
|
|
|
|
|
base64Img := Image2Base64(filePath)
|
|
|
|
|
if base64Img == "" {
|
|
|
|
|
log.Println("图片转换base64失败")
|
|
|
|
|
return "图片转换base64失败"
|
|
|
|
|
}
|
|
|
|
|
// 找到第一个空格的位置
|
|
|
|
|
firstSpaceIndex := strings.Index(a.RawMsg, " ")
|
|
|
|
|
|
|
|
|
|
// 找到最后一个空格的位置
|
|
|
|
|
lastSpaceIndex := strings.LastIndex(a.RawMsg, " ")
|
|
|
|
|
requestBody = map[string]interface{}{
|
|
|
|
|
"model": MODEL,
|
|
|
|
|
"stream": false,
|
|
|
|
|
"messages": []interface{}{
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"role": "system",
|
|
|
|
|
"content": "#角色你是一名AI视觉助手,任务是分析单个图像。",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": []interface{}{
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"text": a.RawMsg[firstSpaceIndex+1 : lastSpaceIndex],
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"type": "image_url",
|
|
|
|
|
"image_url": map[string]string{
|
|
|
|
|
"url": base64Img,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"temperature": 0.7,
|
|
|
|
|
"presence_penalty": 0,
|
|
|
|
|
"frequency_penalty": 0,
|
|
|
|
|
"top_p": 1,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
request := gorequest.New()
|
|
|
|
|
resp, body, errs := request.Post(OPENAI_BaseURL).
|
|
|
|
|
Retry(3, 5*time.Second, http.StatusServiceUnavailable, http.StatusBadGateway).
|
|
|
|
|
Set("Content-Type", "application/json").
|
|
|
|
|
Set("Authorization", "Bearer "+OPENAI_API_KEY).
|
|
|
|
|
Send(requestBody).
|
|
|
|
|
End()
|
|
|
|
|
|
|
|
|
|
if errs != nil {
|
|
|
|
|
log.Println(errs)
|
|
|
|
|
return "请求失败"
|
|
|
|
|
}
|
|
|
|
|
println(resp.StatusCode)
|
|
|
|
|
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)
|
|
|
|
|
return fmt.Sprintf("[CQ:at,qq=%s] %s", a.UID, msg)
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("choices为空")
|
|
|
|
|
return "api解析失败"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return "请求失败!"
|
|
|
|
|
// return handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, a.RawMsg, a.UID, a.Parms)
|
|
|
|
|
|
2024-07-06 19:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getConfig() (string, string, string) {
|
|
|
|
|
var OPENAI_API_KEY, OPENAI_BaseURL, MODEL string
|
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
if cfg["OPENAI_API_KEY"] != nil {
|
|
|
|
|
OPENAI_API_KEY = cfg["OPENAI_API_KEY"].(string)
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("OPENAI_API_KEY 未配置")
|
|
|
|
|
}
|
2024-07-06 19:18:59 +08:00
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
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
|
|
|
|
}
|
2024-07-06 19:18:59 +08:00
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
if cfg["MODEL"] != nil {
|
|
|
|
|
MODEL = cfg["MODEL"].(string)
|
|
|
|
|
} else {
|
2024-07-06 22:35:01 +08:00
|
|
|
|
log.Println("模型 未配置,使用默认 gpt-4o 模型")
|
|
|
|
|
MODEL = "gpt-4o"
|
2024-06-30 21:56:34 +08:00
|
|
|
|
}
|
2024-06-30 23:34:00 +08:00
|
|
|
|
|
2024-07-06 19:18:59 +08:00
|
|
|
|
return OPENAI_API_KEY, OPENAI_BaseURL, MODEL
|
|
|
|
|
}
|
2024-07-01 10:05:42 +08:00
|
|
|
|
|
2024-07-06 19:18:59 +08:00
|
|
|
|
func handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL string) string {
|
|
|
|
|
OPENAI_BaseURL = OPENAI_BaseURL + "/models"
|
|
|
|
|
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 "请求失败"
|
|
|
|
|
}
|
2024-07-01 21:04:22 +08:00
|
|
|
|
|
2024-07-06 19:18:59 +08:00
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
|
var responseBody map[string]interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(body), &responseBody); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return "解析模型列表失败"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
choices := responseBody["data"].([]interface{})
|
|
|
|
|
// var models []interface{}
|
|
|
|
|
if len(choices) > 0 {
|
2024-07-06 21:57:14 +08:00
|
|
|
|
msg := "支持的模型列表:\n"
|
2024-07-06 19:18:59 +08:00
|
|
|
|
for _, choice := range choices {
|
|
|
|
|
model := choice.(map[string]interface{})["id"]
|
2024-07-06 21:57:14 +08:00
|
|
|
|
msg += fmt.Sprintf("%s\n", model)
|
2024-06-30 23:34:00 +08:00
|
|
|
|
}
|
2024-07-06 19:18:59 +08:00
|
|
|
|
return msg
|
|
|
|
|
} else {
|
|
|
|
|
return "模型列表为空"
|
2024-06-30 23:34:00 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-07-06 19:18:59 +08:00
|
|
|
|
log.Println("请求失败")
|
|
|
|
|
return "请求模型列表失败"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 21:38:39 +08:00
|
|
|
|
func Image2Base64(path string) string {
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
2024-07-06 19:18:59 +08:00
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
defer file.Close()
|
2024-07-06 19:18:59 +08:00
|
|
|
|
|
2024-07-14 21:38:39 +08:00
|
|
|
|
if data, err := io.ReadAll(file); err == nil {
|
|
|
|
|
return "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(data)
|
2024-07-06 19:18:59 +08:00
|
|
|
|
}
|
2024-07-14 21:38:39 +08:00
|
|
|
|
return ""
|
2024-07-13 18:00:03 +08:00
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
|
}
|