Skip to content

Commit

Permalink
feat: support system message
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotiaWang committed Mar 13, 2023
1 parent 324a8b4 commit 72ebd42
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
# WeeChatGPT

将 ChatGPT 集成到微信个人号。基于 [openwechat](https://github.com/eatmoreapple/openwechat)
将 ChatGPT 集成到微信个人号。基于 [openwechat](https://github.com/eatmoreapple/openwechat),支持网页版和桌面版微信

## 运行方法

1. 前往 [Releases](https://github.com/AnotiaWang/WeeChatGPT/releases/latest) ,根据你使用的平台,下载最新版本的 WeeChatGPT。
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 。没有暗箱操作,请放心使用。

<details>
<summary>Nginx 反代配置</summary>

```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;
}
}
```
</details>
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
2 changes: 1 addition & 1 deletion handler/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions model/chat_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 72ebd42

Please sign in to comment.