From f8581a4e112f43bd9d754c233baf586901f4c405 Mon Sep 17 00:00:00 2001 From: rainerosion Date: Fri, 14 Apr 2023 17:39:59 +0800 Subject: [PATCH] =?UTF-8?q?gpt=E7=BC=93=E5=AD=98=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wechatbot/config.json.example | 1 + wechatbot/config/config.go | 5 +++++ wechatbot/gpt/gpt.go | 15 +++++++++++---- wechatbot/gpt/gpt_test.go | 14 ++++++++++++++ wechatbot/handlers/group_msg_handler.go | 5 ++++- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 wechatbot/gpt/gpt_test.go diff --git a/wechatbot/config.json.example b/wechatbot/config.json.example index 9e56478..f525071 100644 --- a/wechatbot/config.json.example +++ b/wechatbot/config.json.example @@ -4,4 +4,5 @@ "bing_chat_url": "" # New Bing 聊天接口 "bing_chat_wake_word": "#bing", # new Bing唤醒词 "gpt_chat_wake_word": "#gpt" # ChatGPT唤醒词 + "gpt_message_cache": 1 # 消息缓存数量 } diff --git a/wechatbot/config/config.go b/wechatbot/config/config.go index ec6e713..d24c9ba 100644 --- a/wechatbot/config/config.go +++ b/wechatbot/config/config.go @@ -15,9 +15,14 @@ type Configuration struct { AutoPass bool `json:"auto_pass"` // bing 聊天接口 BingChatUrl string `json:"bing_chat_url"` + GptChatUrl string `json:"gpt_chat_url"` // 机器人唤醒词 BingChatWakeWord string `json:"bing_chat_wake_word"` GptChatWakeWord string `json:"gpt_chat_wake_word"` + // 消息缓存数量 + GptMessageCache int `json:"gpt_message_cache"` + // gpt上下文清空指令 + GptCleanContext string `json:"gpt_clean_context"` } var config *Configuration diff --git a/wechatbot/gpt/gpt.go b/wechatbot/gpt/gpt.go index 3cfefb5..9755cae 100644 --- a/wechatbot/gpt/gpt.go +++ b/wechatbot/gpt/gpt.go @@ -10,7 +10,7 @@ import ( "github.com/bujnlc8/wechatbot/config" ) -const BASEURL = "https://api.openai.com/v1/chat/" +var BASEURL = config.LoadConfig().GptChatUrl type Message struct { Role string `json:"role"` @@ -42,7 +42,14 @@ type ChatGPTRequestBody struct { var MessageCacheRegistry = make(map[string][]Message) +func CleanContext(nickName string) (string, error) { + messageCache := []Message{} + MessageCacheRegistry[nickName] = messageCache + return "用户[" + nickName + "]上下文已清空", nil +} + func Completions(msg string, nickName string) (string, error) { + cache := config.LoadConfig().GptMessageCache messageCache := MessageCacheRegistry[nickName] message := Message{Role: "user", Content: msg} if messageCache == nil || len(messageCache) == 0 { @@ -50,14 +57,14 @@ func Completions(msg string, nickName string) (string, error) { } else { messageCache = append(messageCache, message) // 只保留20条 - if len(messageCache) > 20 { - messageCache = messageCache[(len(messageCache) - 20):] + if len(messageCache) > cache { + messageCache = messageCache[(len(messageCache) - cache):] } } MessageCacheRegistry[nickName] = messageCache requestBody := ChatGPTRequestBody{ Model: "gpt-3.5-turbo", - MaxTokens: 2048, + MaxTokens: 4096, Temperature: 1.2, Messages: messageCache, } diff --git a/wechatbot/gpt/gpt_test.go b/wechatbot/gpt/gpt_test.go new file mode 100644 index 0000000..e8ec475 --- /dev/null +++ b/wechatbot/gpt/gpt_test.go @@ -0,0 +1,14 @@ +package gpt + +import ( + "fmt" + "testing" +) + +func TestGpt(t *testing.T) { + reply, err := Completions("今天北京的天气怎么样", "nickname") + if err != nil { + t.Error(err) + } + fmt.Printf("%+v\n", reply) +} diff --git a/wechatbot/handlers/group_msg_handler.go b/wechatbot/handlers/group_msg_handler.go index a433ad3..200de80 100644 --- a/wechatbot/handlers/group_msg_handler.go +++ b/wechatbot/handlers/group_msg_handler.go @@ -53,7 +53,10 @@ func (g *GroupMessageHandler) ReplyText(msg *openwechat.Message) error { requestText := strings.TrimSpace(strings.ReplaceAll(msg.Content, gptWakeWord, "")) var reply = "" - if strings.Contains(msg.Content, bingWakeWord) { + if strings.EqualFold(msg.Content, config.LoadConfig().GptCleanContext) { + cleanReply, _ := gpt.CleanContext(group.UserName) + reply = "\n" + cleanReply + } else if strings.Contains(msg.Content, bingWakeWord) { requestText = strings.TrimSpace(strings.ReplaceAll(msg.Content, bingWakeWord, "")) reply, err = gpt.BingSearch(requestText, group.UserName) if reply != "" && strings.HasPrefix(reply, "[") {