From 72ebd428a5342955f38927c129232858aa56a595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=89=BE=E8=BF=AA?= <62269186+AnotiaWang@users.noreply.github.com> Date: Mon, 13 Mar 2023 13:55:00 +0800 Subject: [PATCH] feat: support system message --- README.md | 34 ++++++++++++++++++++++++++++++++-- build.sh | 2 +- handler/message.go | 2 +- model/chat_completion.go | 10 ++++++++-- model/config.go | 5 ++++- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4ae8c46..38c05c5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WeeChatGPT -将 ChatGPT 集成到微信个人号。基于 [openwechat](https://github.com/eatmoreapple/openwechat)。 +将 ChatGPT 集成到微信个人号。基于 [openwechat](https://github.com/eatmoreapple/openwechat),支持网页版和桌面版微信。 ## 运行方法 @@ -8,8 +8,38 @@ 2. 先运行一次 WeeChatGPT,程序会生成配置文件 `config.yml`,然后根据文件中的提示,填写配置。 3. 再次运行 WeeChatGPT,会显示微信的登录二维码链接,在浏览器中打开它,扫码登录微信即可。 -### 提示 +![](https://i.328888.xyz/2023/03/13/v602P.png) + +### 说明 - 程序支持热登录,如果两次登录之间的间隔较短,可以自动登录。 - 建议将群组添加到通讯录,否则机器人可能无法获取到群组信息,导致回复失败。 - 目前仅测试过 gpt-3.5-turbo 模型的支持。 +- 目前不支持多轮对话。 + +如果无法访问 OpenAI 的 API,可以考虑我自建的 Nginx 反代 https://proxy.api.ataw.top/openai/v1/chat/completions 。没有暗箱操作,请放心使用。 + +
+Nginx 反代配置 + +```text +server { + listen 80; + listen 443 ssl http2; + server_name proxy.api.ataw.top; + + ssl_certificate /etc/nginx/conf.d/proxy.api.ataw.top_bundle.crt; + ssl_certificate_key /etc/nginx/conf.d/proxy.api.ataw.top.key; + ssl_session_timeout 5m; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; + ssl_prefer_server_ciphers on; + + location /openai/ { + proxy_pass https://api.openai.com/; + proxy_set_header Host api.openai.com; + proxy_set_header X-Real-IP $remote_addr; + } +} +``` +
diff --git a/build.sh b/build.sh index 93a8716..3603965 100644 --- a/build.sh +++ b/build.sh @@ -3,4 +3,4 @@ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./dist/weechatbot_linux_amd64 echo Building for darwin_amd64... CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ./dist/weechatbot_darwin_amd64 . echo Building for windows_amd64... -CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ./dist/weechatbot_windows_amd64 . +CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ./dist/weechatbot_windows_amd64.exe . diff --git a/handler/message.go b/handler/message.go index c858af2..c63d8db 100644 --- a/handler/message.go +++ b/handler/message.go @@ -23,7 +23,7 @@ func Default(ctx context.Context) openwechat.MessageHandler { if strings.Index(msg.Content, config.OpenAI.Prefix) == 0 { log.Println("found message match: " + msg.Content) query := msg.Content[4:] - response, err := model.ChatCompletion(ctx, model.MakeMessage(query)) + response, err := model.ChatCompletion(ctx, model.MakeMessage(query, config.OpenAI.SystemMsg)) if err != nil { log.Println("ChatCompletion error: " + err.Error()) return diff --git a/model/chat_completion.go b/model/chat_completion.go index e421a7d..d4e6aed 100644 --- a/model/chat_completion.go +++ b/model/chat_completion.go @@ -48,9 +48,15 @@ type ChatCompletionResponse struct { } } -func MakeMessage(content string) []Message { +func MakeMessage(content string, sysMsg string) []Message { messages := make([]Message, 0) - // TODO: config.OpenAI.SystemMessage + + if sysMsg != "" { + messages = append(messages, Message{ + Role: "system", + Content: sysMsg, + }) + } messages = append(messages, Message{ Role: "user", Content: content, diff --git a/model/config.go b/model/config.go index 0503690..6888ed0 100644 --- a/model/config.go +++ b/model/config.go @@ -29,6 +29,7 @@ type OpenAIConfig struct { Model string `yaml:"model"` SecretKey string `yaml:"secretKey"` Prefix string `yaml:"prefix"` + SystemMsg string `yaml:"systemMsg"` } func InitConfigFile() error { @@ -40,10 +41,12 @@ openai: endpoint: https://api.openai.com/v1/chat/completions # 模型 model: gpt-3.5-turbo - # OpenAI SecretKey (SK) + # OpenAI SecretKey ( https://platform.openai.com/account/api-keys ) secretKey: # 前缀 prefix: ChatGPT, + # 设定身份,如:你是 ChatGPT,OpenAI 发布的语言模型 + systemMsg: ` err := os.WriteFile("./config.yml", []byte(str), 0644) if err != nil {