feat(ai): 使用第三方OpenAI库支持并更新依赖

This commit is contained in:
liyp 2024-08-31 16:31:06 +08:00
parent 28fe74161d
commit 9b2c187566
5 changed files with 74 additions and 114 deletions

View file

@ -1,7 +1,7 @@
# 使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目 # 使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目
本项目是一个使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目。原使用go-cqhttp的机器人项目由于go-cqhttp不再维护有众多bug无法使用所以更换使用 [napcat](https://github.com/NapNeko/NapCatQQ) 实现。 本项目是一个使用Go语言重新实现 [sihuan/XZZ](https://github.com/sihuan/XZZ) 机器人项目。原使用go-cqhttp的机器人项目由于go-cqhttp不再维护有众多bug无法使用所以更换使用 [napcat](https://github.com/NapNeko/NapCatQQ) 实现。
当前项目的功能都在`workers`目录下。同时所有接收到的消息都保存在一个sqllite数据库中文件名为`data.db`保存在项目根目录。 当前项目的功能都在`workers`目录下。同时所有接收到的消息都保存在一个sqlite数据库中文件名为`data.db`保存在项目根目录。
## 部署服务: ## 部署服务:
1. 先使用docker部署[napcat](https://github.com/NapNeko/NapCatQQ)然后修改配置文件将机器人的token替换为napcat的token然后运行项目即可。 1. 先使用docker部署[napcat](https://github.com/NapNeko/NapCatQQ)然后修改配置文件将机器人的token替换为napcat的token然后运行项目即可。
部署[napcat](https://github.com/NapNeko/NapCatQQ)可参考下面的docker-compose.yml文件 部署[napcat](https://github.com/NapNeko/NapCatQQ)可参考下面的docker-compose.yml文件

1
go.mod
View file

@ -45,6 +45,7 @@ require (
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/moul/http2curl v1.0.0 // indirect github.com/moul/http2curl v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/sashabaranov/go-openai v1.29.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/smartystreets/goconvey v1.8.1 // indirect github.com/smartystreets/goconvey v1.8.1 // indirect
golang.org/x/net v0.26.0 // indirect golang.org/x/net v0.26.0 // indirect

2
go.sum
View file

@ -71,6 +71,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/sashabaranov/go-openai v1.29.0 h1:eBH6LSjtX4md5ImDCX8hNhHQvaRf22zujiERoQpsvLo=
github.com/sashabaranov/go-openai v1.29.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=

1
utils/openai.go Normal file
View file

@ -0,0 +1 @@
package utils

View file

@ -1,20 +1,18 @@
package workers package workers
import ( import (
"context"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io" "io"
"os" "os"
"log" "log"
"net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/goccy/go-json" openai "github.com/sashabaranov/go-openai"
"github.com/parnurzeal/gorequest"
) )
func init() { func init() {
@ -43,35 +41,46 @@ func (a *AI) GetMsg() string {
if OPENAI_API_KEY == "" { if OPENAI_API_KEY == "" {
return "OPENAI_API_KEY 未配置" return "OPENAI_API_KEY 未配置"
} }
oaiconfig := openai.DefaultConfig(OPENAI_API_KEY)
oaiconfig.BaseURL = OPENAI_BaseURL
if strings.ToLower(a.Parms[1]) == "models" { client := openai.NewClientWithConfig(oaiconfig)
return handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL) msg := fmt.Sprintf("[CQ:at,qq=%s] ", a.UID)
} else { if strings.ToLower(a.Parms[1]) != "models" {
OPENAI_BaseURL = OPENAI_BaseURL + "/chat/completions" // OPENAI_BaseURL = OPENAI_BaseURL + "/chat/completions"
PROMPT, ok := cfg["PROMPT"].(string) PROMPT, ok := cfg["PROMPT"].(string)
if !ok { if !ok {
log.Println("PROMPT 未配置") log.Println("PROMPT 未配置")
PROMPT = "" PROMPT = ""
} }
var requestBody map[string]interface{} // var requestBody map[string]interface{}
// 如果非回复消息
if !strings.HasPrefix(a.Parms[len(a.Parms)-1], "[CQ:reply,id=") { if !strings.HasPrefix(a.Parms[len(a.Parms)-1], "[CQ:reply,id=") {
resp, err := client.CreateChatCompletion(
requestBody = map[string]interface{}{ context.Background(),
"model": MODEL, openai.ChatCompletionRequest{
"stream": false, Model: MODEL,
"messages": []map[string]string{ Stream: false,
{ Messages: []openai.ChatCompletionMessage{
"role": "system", {
"content": PROMPT, Role: "system",
Content: PROMPT,
},
{
Role: "user",
Content: a.RawMsg[strings.Index(a.RawMsg, " ")+1:],
},
}, },
{"role": "user", "content": a.RawMsg[strings.Index(a.RawMsg, " ")+1:]},
}, },
"temperature": 0.7, )
"presence_penalty": 0, if err != nil {
"frequency_penalty": 0, log.Println("ChatCompletion error: ", err)
"top_p": 1, return "请求失败"
} }
// println(resp.Choices[0].Message.Content)
return msg + resp.Choices[0].Message.Content
} else { } else {
// 匹配回复消息
pattern := `^\[CQ:reply,id=(-?\d+)\]` pattern := `^\[CQ:reply,id=(-?\d+)\]`
re := regexp.MustCompile(pattern) re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(a.Parms[len(a.Parms)-1]) matches := re.FindStringSubmatch(a.Parms[len(a.Parms)-1])
@ -124,70 +133,55 @@ func (a *AI) GetMsg() string {
// 找到最后一个空格的位置 // 找到最后一个空格的位置
lastSpaceIndex := strings.LastIndex(a.RawMsg, " ") lastSpaceIndex := strings.LastIndex(a.RawMsg, " ")
requestBody = map[string]interface{}{ // 调用图片分析
"model": MODEL, resp, err := client.CreateChatCompletion(
"stream": false, context.Background(),
"messages": []interface{}{ openai.ChatCompletionRequest{
map[string]interface{}{ Model: MODEL,
"role": "system", Messages: []openai.ChatCompletionMessage{
"content": "#角色你是一名AI视觉助手任务是分析单个图像。", {
}, Role: "user",
map[string]interface{}{ MultiContent: []openai.ChatMessagePart{
"role": "user", {
"content": []interface{}{ Type: openai.ChatMessagePartTypeText,
map[string]interface{}{ Text: a.RawMsg[firstSpaceIndex+1 : lastSpaceIndex],
"type": "text", }, {
"text": a.RawMsg[firstSpaceIndex+1 : lastSpaceIndex], Type: openai.ChatMessagePartTypeImageURL,
}, ImageURL: &openai.ChatMessageImageURL{
map[string]interface{}{ URL: base64Img,
"type": "image_url", Detail: openai.ImageURLDetailAuto,
"image_url": map[string]string{ },
"url": base64Img,
}, },
}, },
}, },
}, },
}, },
"temperature": 0.7, )
"presence_penalty": 0, if err != nil {
"frequency_penalty": 0, log.Println("ChatCompletion error: ", err)
"top_p": 1, return "请求失败"
}
}
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解析失败"
} }
msg += resp.Choices[0].Message.Content
} }
return "请求失败!" return msg
// return handleChatRequest(OPENAI_API_KEY, OPENAI_BaseURL, MODEL, a.RawMsg, a.UID, a.Parms) }
models, err := client.ListModels(context.Background())
if err != nil {
log.Println("ListModels error: ", err)
return "查询支持模型失败!"
} }
var modelList string
for _, model := range models.Models {
if MODEL == model.ID {
model.ID = model.ID + "(当前使用)"
}
modelList += model.ID + "\n"
}
return modelList
// return handleModelRequest(OPENAI_API_KEY, OPENAI_BaseURL)
} }
func getConfig() (string, string, string) { func getConfig() (string, string, string) {
@ -216,44 +210,6 @@ func getConfig() (string, string, string) {
return OPENAI_API_KEY, OPENAI_BaseURL, MODEL return OPENAI_API_KEY, OPENAI_BaseURL, MODEL
} }
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 "请求失败"
}
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 {
msg := "支持的模型列表:\n"
for _, choice := range choices {
model := choice.(map[string]interface{})["id"]
msg += fmt.Sprintf("%s\n", model)
}
return msg
} else {
return "模型列表为空"
}
} else {
log.Println("请求失败")
return "请求模型列表失败"
}
}
func Image2Base64(path string) string { func Image2Base64(path string) string {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {