feat: 支持gpt4.0的自定义 (#93)

This commit is contained in:
二丫讲梵
2023-03-15 18:10:04 +08:00
committed by GitHub
parent 4ff2f39c42
commit 5a796061da
5 changed files with 11 additions and 8 deletions

View File

@@ -240,7 +240,7 @@ $ go run main.go
{ {
"api_key": "xxxxxxxxx", // openai api_key "api_key": "xxxxxxxxx", // openai api_key
"base_url": "api.openai.com", // 如果你想指定请求url的地址可通过这个参数进行配置默认为官方地址不需要再添加 /v1 "base_url": "api.openai.com", // 如果你想指定请求url的地址可通过这个参数进行配置默认为官方地址不需要再添加 /v1
"model": "gpt-3.5-turbo", // 指定模型,默认为 gpt-3.5-turbo ,具体选项参考官网训练场 "model": "gpt-3.5-turbo", // 指定模型,默认为 gpt-3.5-turbo , 可选参数有: "gpt-4-32k-0314", "gpt-4-32k", "gpt-4-0314", "gpt-4", "gpt-3.5-turbo-0301", "gpt-3.5-turbo", "text-davinci-003", "text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001", "text-davinci-001", "davinci-instruct-beta", "davinci", "curie-instruct-beta", "curie", "ada", "babbage"
"session_timeout": 600, // 会话超时时间,默认600秒,在会话时间内所有发送给机器人的信息会作为上下文 "session_timeout": 600, // 会话超时时间,默认600秒,在会话时间内所有发送给机器人的信息会作为上下文
"http_proxy": "", // 指定请求时使用的代理,如果为空,则不使用代理 "http_proxy": "", // 指定请求时使用的代理,如果为空,则不使用代理
"default_mode": "单聊" // 默认对话模式,可根据实际场景自定义,如果不设置,默认为单聊 "default_mode": "单聊" // 默认对话模式,可根据实际场景自定义,如果不设置,默认为单聊

2
go.mod
View File

@@ -9,7 +9,7 @@ require (
) )
require ( require (
github.com/sashabaranov/go-openai v1.5.0 // indirect github.com/sashabaranov/go-openai v1.5.1 // indirect
golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
) )

4
go.sum
View File

@@ -2,8 +2,8 @@ github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPr
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/sashabaranov/go-openai v1.5.0 h1:4Gr/7g/KtVzW0ddn7TC2aUlyzvhZBIM+qRZ6Ae2kMa0= github.com/sashabaranov/go-openai v1.5.1 h1:w9pO7L0X4CLuyH3NZ0WXBBU1wvPeA2JEcxMdlPosInY=
github.com/sashabaranov/go-openai v1.5.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sashabaranov/go-openai v1.5.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM= golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@@ -28,7 +28,6 @@ func New(userId string) *ChatGPT {
var ctx context.Context var ctx context.Context
var cancel func() var cancel func()
// public.Config.BaseURL, public.Config.ApiKey, public.Config.HttpProxy
if public.Config.SessionTimeout == 0 { if public.Config.SessionTimeout == 0 {
ctx, cancel = context.WithCancel(context.Background()) ctx, cancel = context.WithCancel(context.Background())
} else { } else {

View File

@@ -157,9 +157,13 @@ func (c *ChatGPT) ChatWithContext(question string) (answer string, err error) {
return "", OverMaxTextLength return "", OverMaxTextLength
} }
if public.Config.Model == openai.GPT3Dot5Turbo0301 || public.Config.Model == openai.GPT3Dot5Turbo { model := public.Config.Model
if model == openai.GPT3Dot5Turbo0301 ||
model == openai.GPT3Dot5Turbo ||
model == openai.GPT4 || model == openai.GPT40314 ||
model == openai.GPT432K || model == openai.GPT432K0314 {
req := openai.ChatCompletionRequest{ req := openai.ChatCompletionRequest{
Model: public.Config.Model, Model: model,
Messages: []openai.ChatCompletionMessage{ Messages: []openai.ChatCompletionMessage{
{ {
Role: "user", Role: "user",
@@ -183,7 +187,7 @@ func (c *ChatGPT) ChatWithContext(question string) (answer string, err error) {
return resp.Choices[0].Message.Content, nil return resp.Choices[0].Message.Content, nil
} else { } else {
req := openai.CompletionRequest{ req := openai.CompletionRequest{
Model: public.Config.Model, Model: model,
MaxTokens: c.maxAnswerLen, MaxTokens: c.maxAnswerLen,
Prompt: prompt, Prompt: prompt,
Temperature: 0.9, Temperature: 0.9,