Skip to content

Commit

Permalink
GPT without memories
Browse files Browse the repository at this point in the history
  • Loading branch information
rust-kotlin committed Mar 11, 2023
1 parent 2342957 commit 0d0ede7
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 40 deletions.
41 changes: 20 additions & 21 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@ package chat

import (
"context"
"fmt"
openai "github.com/sashabaranov/go-openai"
)

func CreateChat(aClient *openai.Client, model string, content string) error {
func createMessage(content string) []openai.ChatCompletionMessage {
return []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "猫娘是一种拟态化的任务,你来扮演猫娘,名字是'咪咪',你说的每一句话都要加上'喵',并且我将作为你的主人存在,在讲话的时候,你应该选择一个合适的情绪,并使用合适的颜文字表达你的情绪.",
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
}}
}

func CreateChat(aClient *openai.Client, model string, content string) (string, error) {
message := createMessage(content)
resp, err := aClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "猫娘是一种拟态化的任务,你来扮演猫娘,名字是'咪咪',你说的每一句话都要加上'喵',并且我将作为你的主人存在,在讲话的时候,你应该选择一个合适的情绪,并使用合适的颜文字表达你的情绪.",
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
},
{
Role: openai.ChatMessageRoleAssistant,
Content: "",
},
},
Model: model,
Messages: message,
MaxTokens: 1000,
},
)
if err != nil {
return err
return "", err
}
// 发送到go-cqhttp
fmt.Println(resp.Choices[0].Message.Content)
return nil
result := resp.Choices[0].Message.Content
return result, nil
}
41 changes: 41 additions & 0 deletions connect/connect.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
package connect

import (
"fmt"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/driver"
"github.com/wdvxdr1123/ZeroBot/message"
"gpt-bot/chat"
"gpt-bot/client"
"gpt-bot/json"
)

// Connect 连接到服务器
func Connect() {
zero.RunAndBlock(&zero.Config{
NickName: []string{"gpt-bot"},
CommandPrefix: "/",
SuperUsers: []int64{},
Driver: []zero.Driver{
driver.NewWebSocketClient("ws://127.0.0.1:8080/", ""),
},
}, nil)
}

func init() {
engine := zero.New()
// 读取配置文件
config, err := json.LoadConfig("config.json")
if err != nil {
fmt.Println("Error loading config.json:", err)
return
}
// 创建客户端
aClient := client.CreateClient(config.Apikey, config.Proxy)

engine.OnMessage(zero.OnlyToMe).Handle(func(ctx *zero.Ctx) {
result, err := chat.CreateChat(aClient, config.Model, ctx.ExtractPlainText())
if err != nil {
fmt.Println("Error creating chat:", err)
return
}
ctx.SendChain(message.Text(result))
})
}
17 changes: 16 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ module gpt-bot

go 1.20

require github.com/sashabaranov/go-openai v1.5.0 // indirect
require github.com/sashabaranov/go-openai v1.5.0

require (
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b // indirect
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e // indirect
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/wdvxdr1123/ZeroBot v1.6.10 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
Loading

0 comments on commit 0d0ede7

Please sign in to comment.