Skip to content

Commit

Permalink
chore: 更新gpt三方依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Mar 6, 2023
1 parent 739db5d commit 27272b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/glebarez/sqlite v1.7.0
github.com/go-co-op/gocron v1.18.0
github.com/imroc/req/v3 v3.32.0
github.com/sashabaranov/go-gpt3 v1.3.3
github.com/sashabaranov/go-openai v1.4.2
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.15.0
github.com/tidwall/gjson v1.14.4
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,10 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/sashabaranov/go-gpt3 v1.2.1 h1:kfU+vQ1ThI7p+xfwwJC8olEEEWjK3smgKZ3FcYbaLRQ=
github.com/sashabaranov/go-gpt3 v1.2.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/sashabaranov/go-gpt3 v1.3.0 h1:IbvaK2yTnlm7f/oiC2HC9cbzu/4Znt4GkarFiwZ60uI=
github.com/sashabaranov/go-gpt3 v1.3.0/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/sashabaranov/go-gpt3 v1.3.1 h1:ACQOAVX5CAV5rHt0oJOBMKo9BNcqVnmxEdjVxcjVAzw=
github.com/sashabaranov/go-gpt3 v1.3.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/sashabaranov/go-gpt3 v1.3.3 h1:S8Zd4YybnBaNMK+w+XGGWgsjQY1R+6QE2n9SLzVna9k=
github.com/sashabaranov/go-gpt3 v1.3.3/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/sashabaranov/go-openai v1.4.1 h1:EofA9Ipo0JcG0FFTF5zI7i13Fpkn4+frWBH8AqbRJ6Q=
github.com/sashabaranov/go-openai v1.4.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sashabaranov/go-openai v1.4.2 h1:IhacPY7O+ljlBoZRQe9VpsLNm0b4PHa6fOBGA9O4vfc=
github.com/sashabaranov/go-openai v1.4.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
Expand Down
31 changes: 9 additions & 22 deletions plugins/chatgpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"strings"
"time"

gogpt "github.com/sashabaranov/go-gpt3"
"github.com/yqchilde/wxbot/engine/robot"
"github.com/sashabaranov/go-openai"

"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/robot"
)

var (
gptClient *gogpt.Client
gptClient *openai.Client
gptModel *GptModel
)

func AskChatGpt(messages []gogpt.ChatCompletionMessage, delay ...time.Duration) (answer string, err error) {
func AskChatGpt(messages []openai.ChatCompletionMessage, delay ...time.Duration) (answer string, err error) {
// 获取客户端
if gptClient == nil {
gptClient, err = getGptClient()
Expand All @@ -40,28 +40,15 @@ func AskChatGpt(messages []gogpt.ChatCompletionMessage, delay ...time.Duration)
time.Sleep(delay[0])
}

// 请求gpt3
//resp, err := gptClient.CreateCompletion(context.Background(), gogpt.CompletionRequest{
// Model: gptModel.Model,
// Prompt: prompt,
// MaxTokens: gptModel.MaxTokens,
// Temperature: float32(gptModel.Temperature),
// TopP: float32(gptModel.TopP),
// PresencePenalty: float32(gptModel.PresencePenalty),
// FrequencyPenalty: float32(gptModel.FrequencyPenalty),
// Echo: false,
// Stop: []string{"Human:", "AI:"},
//})

chatMessages := []gogpt.ChatCompletionMessage{
chatMessages := []openai.ChatCompletionMessage{
{
Role: "system",
Content: fmt.Sprintf("你是一个强大的助手,你是ChatGPT,我将为你起一个名字叫%s,并且你会用中文回答我的问题", robot.GetBot().GetConfig().BotNickname),
},
}
chatMessages = append(chatMessages, messages...)

resp, err := gptClient.CreateChatCompletion(context.Background(), gogpt.ChatCompletionRequest{
resp, err := gptClient.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
Model: gptModel.Model,
Messages: chatMessages,
})
Expand All @@ -74,7 +61,7 @@ func AskChatGpt(messages []gogpt.ChatCompletionMessage, delay ...time.Duration)
return "", errors.New("OpenAi配额已用完,请联系管理员")
}
apiKeys = apiKeys[1:]
gptClient = gogpt.NewClient(apiKeys[0].Key)
gptClient = openai.NewClient(apiKeys[0].Key)
return AskChatGpt(messages)
}
if strings.Contains(err.Error(), "The server had an error while processing your request") {
Expand Down Expand Up @@ -138,10 +125,10 @@ func AskChatGptWithImage(prompt string, delay ...time.Duration) (b64 string, err
time.Sleep(delay[0])
}

resp, err := gptClient.CreateImage(context.Background(), gogpt.ImageRequest{
resp, err := gptClient.CreateImage(context.Background(), openai.ImageRequest{
Prompt: prompt,
Size: gptModel.ImageSize,
ResponseFormat: gogpt.CreateImageResponseFormatB64JSON,
ResponseFormat: openai.CreateImageResponseFormatB64JSON,
})
if err != nil {
return "", err
Expand Down
24 changes: 12 additions & 12 deletions plugins/chatgpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

gogpt "github.com/sashabaranov/go-gpt3"
"github.com/sashabaranov/go-openai"

"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/pkg/log"
Expand Down Expand Up @@ -102,7 +102,7 @@ func init() {
}

var (
nullMessage []gogpt.ChatCompletionMessage
nullMessage []openai.ChatCompletionMessage
room = ChatRoom{
wxId: wxId,
done: make(chan struct{}),
Expand Down Expand Up @@ -160,14 +160,14 @@ func init() {
continue
}

var messages []gogpt.ChatCompletionMessage
var messages []openai.ChatCompletionMessage
if c, ok := msgContext.Load(wxId); ok {
messages = append(c.([]gogpt.ChatCompletionMessage), gogpt.ChatCompletionMessage{
messages = append(c.([]openai.ChatCompletionMessage), openai.ChatCompletionMessage{
Role: "user",
Content: msg,
})
} else {
messages = []gogpt.ChatCompletionMessage{
messages = []openai.ChatCompletionMessage{
{
Role: "user",
Content: msg,
Expand All @@ -180,7 +180,7 @@ func init() {
ctx.ReplyTextAndAt("ChatGPT出错了,Err:" + err.Error())
continue
}
messages = append(messages, gogpt.ChatCompletionMessage{
messages = append(messages, openai.ChatCompletionMessage{
Role: "assistant",
Content: answer,
})
Expand All @@ -194,7 +194,7 @@ func init() {
engine.OnRegex(`^提问 (.*)$`).SetBlock(true).Handle(func(ctx *robot.Ctx) {
question := ctx.State["regex_matched"].([]string)[1]

messages := []gogpt.ChatCompletionMessage{
messages := []openai.ChatCompletionMessage{
{
Role: "user",
Content: question,
Expand Down Expand Up @@ -385,7 +385,7 @@ func init() {
var apiKeys []ApiKey

// 获取gpt3客户端
func getGptClient() (*gogpt.Client, error) {
func getGptClient() (*openai.Client, error) {
var keys []ApiKey
if err := db.Orm.Table("apikey").Find(&keys).Error; err != nil {
log.Errorf("[ChatGPT] 获取apikey失败, error:%s", err.Error())
Expand All @@ -403,12 +403,12 @@ func getGptClient() (*gogpt.Client, error) {
return nil, errors.New("获取apiProxy失败")
}

config := gogpt.DefaultConfig(keys[0].Key)
config := openai.DefaultConfig(keys[0].Key)
if len(proxy.Url) > 0 {
config.BaseURL = proxy.Url
config.BaseURL = proxy.Url + "/v1"
}

return gogpt.NewClientWithConfig(config), nil
return openai.NewClientWithConfig(config), nil
}

// 获取gpt3模型配置
Expand All @@ -419,7 +419,7 @@ func getGptModel() (*GptModel, error) {
return nil, errors.New("获取模型配置失败")
}
if gptModel.ImageSize == "" {
gptModel.ImageSize = gogpt.CreateImageSize512x512
gptModel.ImageSize = openai.CreateImageSize512x512
}
return &gptModel, nil
}
Expand Down

0 comments on commit 27272b0

Please sign in to comment.