forked from rainerosion/wechatbot
modify context
This commit is contained in:
parent
dcac1149f9
commit
48bf5368b5
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
storage.json
|
||||
/.idea/wechatbot.iml
|
||||
/config.json
|
||||
|
||||
@ -4,5 +4,5 @@
|
||||
"bing_chat_url": "" # New Bing 聊天接口
|
||||
"bing_chat_wake_word": "#bing", # new Bing唤醒词
|
||||
"gpt_chat_wake_word": "#gpt" # ChatGPT唤醒词
|
||||
"gpt_message_cache": 1 # 消息缓存数量
|
||||
"gpt_message_cache": 20 # 消息缓存数量
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
func TestBing(t *testing.T) {
|
||||
reply, err := BingSearch("今天北京的天气怎么样", "nickname")
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", reply)
|
||||
|
||||
11
gpt/gpt.go
11
gpt/gpt.go
@ -25,12 +25,16 @@ type ChatGPTResponseBody struct {
|
||||
Model string `json:"model"`
|
||||
Choices []ChoiceItem `json:"choices"`
|
||||
Usage map[string]interface{} `json:"usage"`
|
||||
Error ErrorGPTResponseBody `json:"error"`
|
||||
}
|
||||
|
||||
type ChoiceItem struct {
|
||||
Message Message `json:"message"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
}
|
||||
type ErrorGPTResponseBody struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// ChatGPTRequestBody 响应体
|
||||
type ChatGPTRequestBody struct {
|
||||
@ -45,7 +49,8 @@ var MessageCacheRegistry = make(map[string][]Message)
|
||||
func CleanContext(nickName string) (string, error) {
|
||||
messageCache := []Message{}
|
||||
MessageCacheRegistry[nickName] = messageCache
|
||||
return "用户[" + nickName + "]上下文已清空", nil
|
||||
log.Println("用户[" + nickName + "]上下文已清空")
|
||||
return "上下文已清空", nil
|
||||
}
|
||||
|
||||
func Completions(msg string, nickName string) (string, error) {
|
||||
@ -64,7 +69,7 @@ func Completions(msg string, nickName string) (string, error) {
|
||||
MessageCacheRegistry[nickName] = messageCache
|
||||
requestBody := ChatGPTRequestBody{
|
||||
Model: "gpt-3.5-turbo",
|
||||
MaxTokens: 4096,
|
||||
MaxTokens: 2048,
|
||||
Temperature: 1.2,
|
||||
Messages: messageCache,
|
||||
}
|
||||
@ -107,6 +112,8 @@ func Completions(msg string, nickName string) (string, error) {
|
||||
reply = v.Message.Content
|
||||
break
|
||||
}
|
||||
} else {
|
||||
reply = gptResponseBody.Error.Message
|
||||
}
|
||||
log.Printf("gpt response text: %s \n", reply)
|
||||
return reply, nil
|
||||
|
||||
@ -42,10 +42,19 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
|
||||
sender, err := msg.Sender()
|
||||
group := openwechat.Group{User: sender}
|
||||
log.Printf("Received Group %v Text Msg : %v", group.NickName, msg.Content)
|
||||
inGroup, _ := msg.SenderInGroup()
|
||||
// 组合群名+用户名
|
||||
userName := inGroup.UserName + group.UserName
|
||||
|
||||
bingWakeWord := config.LoadConfig().BingChatWakeWord
|
||||
gptWakeWord := config.LoadConfig().GptChatWakeWord
|
||||
|
||||
// gpt上下文清理
|
||||
if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) {
|
||||
cleanReply, _ := gpt.CleanContext(userName)
|
||||
msg.ReplyText(cleanReply)
|
||||
}
|
||||
|
||||
// @GPTBot 或者 @bing的消息才处理
|
||||
if !(strings.Contains(msg.Content, gptWakeWord) || strings.Contains(msg.Content, bingWakeWord)) {
|
||||
return nil
|
||||
@ -53,17 +62,14 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error {
|
||||
|
||||
requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, ""))
|
||||
var reply = ""
|
||||
if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) {
|
||||
cleanReply, _ := gpt.CleanContext(group.UserName)
|
||||
reply = "\n" + cleanReply
|
||||
} else if strings.Contains(msg.Content, bingWakeWord) {
|
||||
if strings.Contains(msg.Content, bingWakeWord) {
|
||||
requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, ""))
|
||||
reply, err = gpt.BingSearch(requestText, group.UserName)
|
||||
reply, err = gpt.BingSearch(requestText, userName)
|
||||
if reply != "" && strings.HasPrefix(reply, "[") {
|
||||
reply = "\n" + reply
|
||||
}
|
||||
} else {
|
||||
reply, err = gpt.Completions(requestText, group.UserName)
|
||||
reply, err = gpt.Completions(requestText, userName)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("gpt request error: %v \n", err)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user