Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加makefile,docker-compose自动build MySQL等 #14

Merged
merged 9 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
VERSION=0.1.1
BINARY_NAME=chatgpt-wecom

all: amd64 arm64 win64 mac

dockerenv:
docker build -t ${BINARY_NAME}:${VERSION} -f $(shell pwd)/docker/callback.Dockerfile .

mac:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mac 上应该有 amd64 和 arm64 两个版本

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done by 51336ba

GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags "-s -w " -o $(BINARY_NAME).$(VERSION).amd64-darwin ./cmd/app

amd64:
GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags "-s -w " -o $(BINARY_NAME).$(VERSION).amd64-linux ./cmd/app

arm64:
GOOS=linux GOARCH=arm64 $(GOBUILD) -ldflags "-s -w " -o $(BINARY_NAME).$(VERSION).arm64-linux ./cmd/app

win64:
GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags "-s -w -H windowsgui" -o $(BINARY_NAME).$(VERSION).exe ./cmd/app

clean:
$(GOCLEAN)
rm -f $(BINARY_NAME).$(VERSION).amd64-linux $(BINARY_NAME).$(VERSION).amd64-darwin $(BINARY_NAME).$(VERSION).arm64-linux $(BINARY_NAME).$(VERSION).exe
50 changes: 28 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,59 @@

相较于官方提供的 `CreateCompletion` 接口,该项目增加了会话管理功能,能够较好地提供多轮对话能力。

## 快速开始
## 快速安装

1. 修改配置
### 0. 前置步骤
* 登录企业微信的[管理后台](https://work.weixin.qq.com/wework_admin/loginpage_wx)创建机器人,名字随意取
* 点击【应用管理】-【创建应用】,填写完毕保存。

修改 `conf/online.conf` 文件,主要涉及企业微信应用配置、GPT3 API Key、会话管理数据库配置等。
### 1. 配置

修改 `conf/chatgpt.conf` 文件,主要涉及企业微信应用配置、GPT3 API Key、会话管理数据库配置等。

- 企业微信应用配置
- corp_id:在企业微信后台【我的企业】-【企业信息】处获取【企业ID】
- corp_secret:在企业微信后台【应用管理】处获取【Secret】
- agent_id:在企业微信后台【应用管理】处获取【AgentId】
- encoding_aes_key:企业微信后台 【接收消息】- 【API 接收消息】获取【EncodingAESKey】,可以随机生成
- token:企业微信后台 【接收消息】- 【API 接收消息】获取【Token】,可以随机生成
- Open AI Key
- OpenAI Key
- 需要自行申请
- 数据库
- ~数据库需要自行创建,数据表的创建可以通过命令行方式执行。~
- 数据库支持 sqlite3,可以通过修改配置使用。如果使用 MySQL,需要自行创建数据库。
- **数据表在程序启动时自动创建。**

2. `Docker` 运行
### 2. 运行
* **选择1:Docker运行(推荐)**

```shell
docker-compose up -d
git clone https://github.com/yijia2413/chatgpt-wecom.git
cd chatgpt-wecom
# 构建镜像
make dockerenv
# 运行带sqlite的镜像,运行前确认chatgpt.conf修改完毕
docker run -it -d --name chatgpt --restart=always \
-v $(pwd)/conf/chatgpt.conf:/home/works/program/chatgpt.conf chatgpt-wecom:0.1.1
```

3. ~初始化数据表~

数据表在程序启动时自动创建。

* **选择2:本地运行**
* 下载对应的二进制,[chatgpt-wecom](https://github.com/yijia2413/chatgpt-wecom/releases)
* 执行命令 `./chatgpt-wecom -conf=conf/chatgpt.conf` 即可,同理需要确认`chatgpt.conf`配置完毕

4. 配置企业微信应用。在企业微信后台 【接收消息】- 【API 接收消息】配置接收消息服务器配置。
### 3. 配置企业微信

- URL 配置格式:`http[s]://ip:port/wecom/receive`
* URL 配置格式:`http://ip:port/wecom/receive`
* 在企业微信后台,添加可信IP地址

## FAQ

**怎么创建数据库**

- v0.1.1 版本中支持 sqlite3 数据库,只需要修改配置文件的配置,程序启动后便会初始化数据库和数据表,不需要额外的操作。

- 如果使用的是 MySQL,则需要自行创建数据库,建库 SQL 可以参考下面的命令:

```sql
CREATE DATABASE chatgpt DEFAULT CHARACTER SET utf8mb4
```
- 如果使用的是 MySQL,则需要自行创建数据库,建库 SQL 可以参考下面的命令:[init.sql](/init.sql)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQL 只需要建库 SQL 即可,程序启动后便可以自动生成数据库表。保留 init.sql 可以的,也可以不使用初始化脚本。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除了建表语句,docker compose 版本会自动建库,手动创建可参考init.sql即可


之后程序启动后,便可以自动创建数据表。

**数据库连接失败**

Expand Down Expand Up @@ -79,9 +85,6 @@ driver="sqlite3"
dataSource="file:chatgpt?_fk=1&parseTime=True"
```




## Changelog

### v0.1.1
Expand All @@ -94,4 +97,7 @@ dataSource="file:chatgpt?_fk=1&parseTime=True"

### v0.1.0

- 项目初始化
- 项目初始化

### Happy Chatting
![img](/png/example.jpg)
4 changes: 2 additions & 2 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

func main() {
conf := flag.String("conf", "conf/online.conf", "配置文件")
initEnt := flag.Bool("init-ent", false, "是否初始化数据库")
conf := flag.String("conf", "chatgpt.conf", "配置文件")
initEnt := flag.Bool("initdb", false, "是否初始化数据库")
flag.Parse()
cfg, err := config.New(*conf)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions conf/online.conf → conf/chatgpt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ version = "0.1.1"
port = 8000

[logger]
level = "debug"
level = "info"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得可以默认 debug,用户试用阶段有一些日志也好排查问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修正

console_enabled = true
file_enabled = true
filename = "logs/chatgpt-wecom.log"

[gpt]
api_key = ""
api_key = "sk-"

[wecom]
corp_id=""
corp_secret=""
agent_id=0
agent_id=
encoding_aes_key=""
token=""
url="https://qyapi.weixin.qq.com"
Expand All @@ -34,4 +34,4 @@ dataSource="file:chatgpt?_fk=1&parseTime=True"
closeSessionFlag="/restart"
closeSessionReply="会话已重启。"
enableEnterEvent=true
enterEventReply="欢迎来到 ChatGPT,在这里您可以和我对话,我将尽我所能回答您的问题。如果想关闭会话,请回复“/restart”。"
enterEventReply="欢迎来到 ChatGPT,在这里您可以和我对话,我将尽我所能回答您的问题。如果想关闭会话,请回复“/restart”。"
24 changes: 22 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: "3.7"

# only works for linux amd64
services:
chatgpt-wecom:
build:
Expand All @@ -8,7 +9,26 @@ services:
ports:
- 8000:8000
container_name: chatgpt-wecom
depends_on:
[chatgpt-db]
volumes:
- ./logs:/home/works/program/logs
- ./conf/online.conf:/home/works/program/conf/online.conf
restart: always
- ./conf/chatgpt.conf:/home/works/program/chatgpt.conf
restart: always

chatgpt-db:
image: mysql:5.7
container_name: chatgpt-mysql
restart: always
environment:
MYSQL_DATABASE: 'chatgpt'
MYSQL_ROOT_PASSWORD: 'VOSE1@qblt6'
MYSQL_ROOT_HOST: '%'
expose:
- '3306'
volumes:
- db:/var/lib/mysql'
- './init.sql:/docker-entrypoint-initdb.d/init.sql'

volumes:
db: {}
18 changes: 8 additions & 10 deletions docker/callback.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Step 1: Modules caching
FROM golang:1.18 AS base

FROM golang:1.19 AS base
ENV GOPROXY=https://goproxy.cn

# Move to working directory /build
Expand All @@ -9,22 +8,21 @@ COPY go.mod go.sum ./
RUN go mod download

# Step 2: Builder
FROM golang:1.18 as builder
FROM golang:1.19 as builder

ENV GO111MODULE=on \
CGO_ENABLED=1 \
GOOS=linux \
GOARCH=amd64
CGO_ENABLED=1

COPY --from=base /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN go build -ldflags '-linkmode external -extldflags "-static"' -o /bin/app ./cmd/app
RUN go build -ldflags '-linkmode external -extldflags "-static"' -o /bin/chatgpt-wecom ./cmd/app

# Step 3: Final
FROM alpine:latest
WORKDIR /home/works/program
COPY ./conf/online.conf ./conf/online.conf
COPY --from=builder /bin/app .
COPY ./conf/chatgpt.conf ./chatgpt.conf
COPY --from=builder /bin/chatgpt-wecom .

EXPOSE 8000
ENTRYPOINT ["./app"]
CMD ["./chatgpt-wecom", "-conf=chatgpt.conf"]
38 changes: 18 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/fanchunke/chatgpt-wecom

go 1.18
go 1.19

require (
github.com/fanchunke/xgpt3 v0.1.2
Expand All @@ -10,52 +10,50 @@ require (
github.com/go-sql-driver/mysql v1.7.0
github.com/mattn/go-sqlite3 v1.14.16
github.com/rs/xid v1.4.0
github.com/rs/zerolog v1.28.0
github.com/sashabaranov/go-gpt3 v1.0.0
github.com/spf13/viper v1.14.0
go.uber.org/automaxprocs v1.5.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
github.com/rs/zerolog v1.29.0
github.com/sashabaranov/go-gpt3 v1.1.0
github.com/spf13/viper v1.15.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
ariga.io/atlas v0.9.1-0.20230119145809-92243f7c55cb // indirect
entgo.io/ent v0.11.7 // indirect
entgo.io/ent v0.11.8 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/ugorji/go/codec v1.2.8 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading