Skip to content

Commit

Permalink
More options to set. Sensible context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rust-kotlin committed Mar 17, 2023
1 parent 97f02ac commit c46fe75
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
13 changes: 7 additions & 6 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func createMessage(content string, contents []string) []openai.ChatCompletionMes
Content: contents[0],
},
{
Role: openai.ChatMessageRoleUser,
Role: openai.ChatMessageRoleAssistant,
Content: contents[1],
},
{
Expand All @@ -30,21 +30,22 @@ func createMessage(content string, contents []string) []openai.ChatCompletionMes
}
}

func CreateChat(aClient *openai.Client, model string, maxTokens int, content string, contents []string) (string, error) {
func CreateChat(aClient *openai.Client, model string, maxTokens int, temperature float32, content string, contents []string) (string, error) {
message := createMessage(content, contents)
resp, err := aClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: model,
Messages: message,
MaxTokens: maxTokens,
Model: model,
Messages: message,
MaxTokens: maxTokens,
Temperature: temperature,
},
)
if err != nil {
return "", err
}
result := resp.Choices[0].Message.Content
contents[0] = contents[1]
contents[0] = content
contents[1] = result
return result, nil
}
23 changes: 21 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ import (
"net/url"
)

func CreateClient(api string, proxy string) *openai.Client {
func CreateClient(api string, proxy string, baseUrl string) *openai.Client {
// 设置代理
aConfig := openai.DefaultConfig(api)
if proxy == "" {
if proxy == "" && baseUrl == "" {
return openai.NewClientWithConfig(aConfig)
} else if baseUrl == "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
fmt.Println("Error parsing proxy url:", err)
panic(err)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
aConfig.HTTPClient = &http.Client{
Transport: transport,
}
// 创建客户端
return openai.NewClientWithConfig(aConfig)
} else if proxy == "" {
aConfig.BaseURL = baseUrl + "/v1"
// 创建客户端
return openai.NewClientWithConfig(aConfig)
} else {
proxyUrl, err := url.Parse(proxy)
Expand All @@ -24,6 +42,7 @@ func CreateClient(api string, proxy string) *openai.Client {
aConfig.HTTPClient = &http.Client{
Transport: transport,
}
aConfig.BaseURL = baseUrl + "/v1"
// 创建客户端
return openai.NewClientWithConfig(aConfig)
}
Expand Down
21 changes: 17 additions & 4 deletions connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

var superUsers []int64
var flag bool

// Connect 连接到服务器
func Connect() {
Expand All @@ -38,13 +39,13 @@ func init() {
}
superUsers = config.SuperUsers
// 创建客户端
aClient := client.CreateClient(config.Apikey, config.Proxy)
aClient := client.CreateClient(config.Apikey, config.Proxy, config.BaseUrl)
chat.SystemContent = chat.Dog
// 创建一个哈希表
dict := make(map[int64][]string)
// 帮助
engine.OnCommand("help").SetBlock(true).Handle(func(ctx *zero.Ctx) {
ctx.Send(message.Text(`gpt-bot v1.6.0,一个由TomZz开发的人工智能机器人,已开源在https://github.com/rust-kotlin/gpt-bot
ctx.Send(message.Text(`gpt-bot v2.0.0,一个由TomZz开发的人工智能机器人,已开源在https://github.com/rust-kotlin/gpt-bot
#help 获取帮助信息
#time 获取当前时间
#weather 获取当前天气,命令后跟着城市名称,例如:#weather北京
Expand Down Expand Up @@ -101,6 +102,11 @@ func init() {
ctx.Send(message.Text("未配置天气API,无法获取天气信息"))
})
}
// 重置私聊记录
engine.OnCommand("reset").Handle(func(ctx *zero.Ctx) {
flag = true
ctx.Send(message.Text("已重置私聊记录"))
})
// 私发消息
engine.OnMessage(zero.OnlyToMe).Handle(func(ctx *zero.Ctx) {
qq := ctx.Event.UserID
Expand All @@ -111,11 +117,18 @@ func init() {
if _, ok := dict[qq]; !ok {
dict[qq] = make([]string, 2)
}
if flag {
dict[qq] = make([]string, 2)
flag = false
return
}
//a := time.Now()
//name := ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").Str
result, err := chat.CreateChat(aClient, config.Model, config.MaxTokens, ctx.ExtractPlainText(), dict[qq])
result, err := chat.CreateChat(aClient, config.Model, config.MaxTokens, config.Temperature, ctx.ExtractPlainText(), dict[qq])
//ctx.Send(message.Text(time.Since(a).Milliseconds()))
if err != nil {
//fmt.Println("Error creating chat:", err)
ctx.Send(message.Text("请求失败了,可能是由于网络问题或者短时间内网络请求太多,过一会再试试吧!"))
ctx.Send(message.Text(fmt.Sprintln("请求失败了,可能是由于网络问题或者短时间内网络请求太多,过一会再试试吧!", err)))
return
}
if ctx.Event.GroupID != 0 {
Expand Down
4 changes: 4 additions & 0 deletions jsonconfig/jsonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Config struct {
MaxTokens int `json:"max_tokens"`
SuperUsers []int64 `json:"super_users"`
WeatherApikey string `json:"weather_apikey"`
Temperature float32 `json:"temperature"`
BaseUrl string `json:"base_url"`
}

// LoadConfig 从文件中读取配置,如果文件不存在则返回默认配置
Expand All @@ -24,6 +26,8 @@ func LoadConfig(filename string) (*Config, error) {
MaxTokens: 800,
SuperUsers: []int64{},
WeatherApikey: "",
Temperature: 0.5,
BaseUrl: "",
}

// 如果文件不存在,则创建文件并写入默认配置
Expand Down

0 comments on commit c46fe75

Please sign in to comment.