AI聊天机器人项目(Chatgpt接口)
2024-09-11 16:03:49

AI聊天机器人项目(Chatgpt接口)

本篇文章目的

记录使用Docker-compose的过程,以便后续项目能方便部署。

源码下载地址:[点击下载]

项目介绍

随着ChatGPT的大热,许多小伙伴国内使用不了ChatGPT,于是在2023年3月份的时候,我就创建了这个项目。主要是放在香港服务器做中转ChatGpt的接口用的。后来因为API收费太高,没多久余额用光了,项目就没有继续优化。

界面如下图:

聊天软件

项目文件结构

如下树状图所示,我将前端后端放在同一个文件夹位置,使用Docker compose对容器进行项目管理。

前端部分:使用了reactJs进行编写。

后端部分:使用了express.js进行编写。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|-- client
| |-- Docker.sh
| |-- Dockerfile
| |-- index.html
| |-- package-lock.json
| |-- package.json
| |-- public
| |-- src
| | |-- App.tsx
| | |-- assets
| | | |-- chatgpt_avatar.png
| | | `-- me.jpg
| | |-- components
| | | |-- LJNButton
| | | | |-- index.module.scss
| | | | `-- index.tsx
| | | |-- LJNEmptyLine
| | | | |-- index.module.scss
| | | | `-- index.tsx
| | | `-- LJNPopup
| | | |-- index.scss
| | | `-- index.tsx
| | |-- index.css
| | |-- main.tsx
| | |-- pages
| | | `-- Index
| | | |-- index.module.scss
| | | `-- index.tsx
| | |-- router.tsx
| | |-- utils
| | | |-- api.ts
| | | `-- fetch.ts
| | `-- vite-env.d.ts
| |-- tsconfig.json
| |-- tsconfig.node.json
| |-- vite.config.ts
| `-- yarn.lock
|-- deploy_script
| |-- centos9.sh
| `-- rebuild.sh
|-- docker-compose.yml
`-- server
|-- Dockerfile
|-- dist
| |-- Modules
| | |-- ChatGpt
| | | |-- BaseController.js
| | | |-- IndexController.js
| | | `-- module.js
| | `-- Home
| | |-- BaseController.js
| | |-- IndexController.js
| | `-- module.js
| |-- config
| | `-- config.js
| |-- db.js
| |-- main.js
| |-- pages
| | |-- BaseController.js
| | |-- IndexController.js
| | `-- index.js
| |-- router.js
| `-- src
| |-- db.js
| |-- main.js
| |-- pages
| | `-- index.js
| `-- router.js
|-- nodemon.json
|-- package-lock.json
|-- package.json
|-- src
| |-- Modules
| | |-- ChatGpt
| | | |-- BaseController.ts
| | | |-- IndexController.ts
| | | `-- module.ts
| | `-- Home
| | |-- BaseController.ts
| | |-- IndexController.ts
| | `-- module.ts
| |-- config
| | |-- config.ts
| | `-- config_bak.ts
| |-- db.ts
| |-- main.ts
| `-- router.ts
`-- tsconfig.json

26 directories, 62 files

Docker-Compose配置

/docker-compose.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: "3"
services:
# 服务器端
apiservice:
container_name: apiservice
build:
context: ./server
dockerfile: ./Dockerfile

# 客户端
clientservice:
container_name: clientservice
build:
context: ./client
dockerfile: ./Dockerfile
ports:
- "1234:80"
tty: true
stdin_open: true

客户端Dockerfile配置

/client/Docker.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

echo 'server {
listen 80;
server_name 0.0.0.0;
root /usr/share/nginx/html/;
index index.html index.htm;

location /chatapi/ {
proxy_pass http://apiservice:3000/;
}
}' > /etc/nginx/conf.d/default.conf
cat /etc/nginx/conf.d/default.conf

nginx -g 'daemon off;'

/client/Dockerfile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 构建打包
FROM node AS builder

WORKDIR /app
COPY ./ ./
RUN rm -rf ./node_modules &&\
npm set registry https://registry.npmjs.org/ &&\
npm install &&\
npm run build

# 构建nginx服务
FROM nginx AS webserver
COPY --from=builder /app/dist/ /usr/share/nginx/html
COPY ./Docker.sh /
RUN chmod +x ./Docker.sh

ENTRYPOINT [ "/Docker.sh" ]

服务端Docker配置

/server/Dockerfile文件

1
2
3
4
5
6
7
8
9
10
# 构建打包
FROM node AS builder

WORKDIR /app
COPY ./ ./
RUN rm -rf ./node_modules &&\
npm set registry https://registry.npmjs.org/ &&\
npm install

CMD npm start

部署过程

Docker环境安装

卸载老Docker,安装新Docker社区版,并设置开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo systemctl start docker
sudo systemctl enable docker

容器启动

执行如下命令,按照/docker-compose.yml的配置将会有一个1234的端口暴露出来提供给用户使用。

1
docker compose up -d