gpt缓存清空配置

This commit is contained in:
rainerosion 2023-04-14 17:39:59 +08:00
parent 2d43dd8d3b
commit f8581a4e11
5 changed files with 35 additions and 5 deletions

View File

@ -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 # 消息缓存数量
}

View File

@ -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

View File

@ -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,
}

14
wechatbot/gpt/gpt_test.go Normal file
View File

@ -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)
}

View File

@ -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, "[") {