Zhi Wei 的个人博客
如何搭建一个chatGPT3.5的镜像站
如何搭建一个chatGPT3.5的镜像站

如何搭建一个chatGPT3.5的镜像站

本文方法已失效,因为ChatGPT接口API更新,@yuezk不再更新项目。新项目地址https://github.com/mckaywrigley/chatbot-ui

同时ChatGPT官网似乎已经不再屏蔽特定地区的IP地址,所以本文也不再更新。

近来智能AI-chatGPT的热度可谓是引爆互联网,因为前段时间一直在写一个小程序,所有没能在第一时间体验chatGPT,就在上周,算是历经千难万险,终于是在体验上了,效果十分炸裂!码代码算是我的业余爱好,相比我花了三天设计实现的一个简单的共享数据容器,chatGPT只花了1秒,几十行代码就已经浮现在眼前。不得不佩服,当即我就喜欢上了这个东西。简直就是神器,我觉得它是一个划时代的AI,IronMan的贾维斯可能真的会在未来十几年出现。

但是在国内因为种种不肯抗力的因素,直接访问chatGPT官网似乎是一件不太现实的事情,事实也确实如此,首先chatGPT就已经屏蔽了来自中国的IP地址,即使设置代理也会出现Access denied Error reference number: 1020之类的报错。

我尝试了多个代理节点,基本上都是Access denied,后来查询google才知道,原来这个错误是因为CDN节点的问题,只有使用cloudflare公司的CDN节点才能正常访问chatGPT,一番操作下来,又是用美国手机卡接收短信,又是使用google账号登录,最后都是报unsupported_country这个错。好事多磨没办法。一筹莫展之际还是想起了刚开始体验站点fastgpt.app,干脆自己搭建一个镜像站,调用OpenAI的API接口来使用chatGPT吧。Github项目地址chatgpt-mirror。感谢@yuezk

自己搭建一个镜像站需要准备的东西有:

一台国外vpsOS:Centos8.2
需要 Node.js 18.x 环境
一个OpenAI的Key用来调用API
一条可靠的访问链路八仙过海 各显神通

首先在CentOS8上配置Node.js 18.x的环境,这里需要安装Node.js环境和一个pnpm模块。

[root@localhost ~]#yum install -y epel-release 
//安装epel源
[root@localhost ~]# curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
//安装Node.js18源
## Installing the NodeSource Node.js 18.x repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release || rpm -q --whatprovides fedora-release
+ uname -m

## Confirming "el8-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_18.x/el/8/x86_64/nodesource-release-el8-1.noarch.rpm'

## As yum will try to install Node.js from the AppStream repository
instead of the NodeSource repository, the AppStream's version of Node.js has to be disabled.
## Run `sudo yum module enable -y nodejs` to reactivate the AppStream's Node.js repository.

+ yum module disable -y nodejs
Last metadata expiration check: 0:03:35 ago on Thu 16 Mar 2023 08:17:39 PM CST.
Dependencies resolved.
=======================================================================================================================
 Package                     Architecture               Version                      Repository                   Size
=======================================================================================================================
Disabling modules:
 nodejs                                                                                                               

Transaction Summary
=======================================================================================================================

Complete!

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.OYEoCrdzvJ' 'https://rpm.nodesource.com/pub_18.x/el/8/x86_64/nodesource-release-el8-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.OYEoCrdzvJ'

## Cleaning up...

+ rm -f '/tmp/tmp.OYEoCrdzvJ'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Run `sudo yum install -y nodejs` to install Node.js 18.x and npm.
## You may run dnf if yum is not available:
     sudo dnf install -y nodejs
## You may also need development tools to build native addons:
     sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
     sudo yum install yarn
[root@localhost ~]# yum install -y nodejs
//安装Node.js18
Node.js Packages for Enterprise Linux 8 - x86_64                                       132 kB/s | 538 kB     00:04    
Last metadata expiration check: 0:00:01 ago on Thu 16 Mar 2023 08:23:56 PM CST.
Dependencies resolved.
=======================================================================================================================
 Package                Architecture           Version                                Repository                  Size
=======================================================================================================================
Installing:
 nodejs                 x86_64                 2:18.15.0-1nodesource                  nodesource                  34 M

Transaction Summary
=======================================================================================================================
Install  1 Package

Total download size: 34 M
Installed size: 99 M
Downloading Packages:
nodejs-18.15.0-1nodesource.x86_64.rpm                                                  2.2 MB/s |  34 MB     00:15    
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  2.2 MB/s |  34 MB     00:15     
warning: /var/cache/dnf/nodesource-8f869c4324c3809d/packages/nodejs-18.15.0-1nodesource.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 34fa74dd: NOKEY
Node.js Packages for Enterprise Linux 8 - x86_64                                       1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x34FA74DD:
 Userid     : "NodeSource <gpg-rpm@nodesource.com>"
 Fingerprint: 2E55 207A 95D9 944B 0CC9 3261 5DDB E8D4 34FA 74DD
 From       : /etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                               1/1 
  Running scriptlet: nodejs-2:18.15.0-1nodesource.x86_64                                                           1/1 
  Installing       : nodejs-2:18.15.0-1nodesource.x86_64                                                           1/1 
  Running scriptlet: nodejs-2:18.15.0-1nodesource.x86_64                                                           1/1 
  Verifying        : nodejs-2:18.15.0-1nodesource.x86_64                                                           1/1 
Installed products updated.

Installed:
  nodejs-2:18.15.0-1nodesource.x86_64                                                                                  

Complete!
[root@localhost ~]# npm install -g pnpm

//安装pnpm
added 1 package in 4s

1 package is looking for funding
  run `npm fund` for details
[root@localhost ~]# node -v
v18.15.0
[root@localhost ~]# pnpm -v
7.29.3
//测试是否安装完成

然后安装Cloudflare WARP设置为代理模式,否则会导致VPS本地网络无法访问!切记不能直接connect哦!

[root@localhost ~]# rpm -ivh https://pkg.cloudflareclient.com/cloudflare-release-el8.rpm
//安装Cloudflare软件源
Retrieving https://pkg.cloudflareclient.com/cloudflare-release-el8.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:cloudflare-release-3.0.0-el8     ################################# [100%]
[root@localhost ~]# yum search Cloudflare
//验证是否安装成功
Cloudflare CentOS Packages                                                             665  B/s | 833  B     00:01    
Cloudflare CentOS Packages                                                             1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x4FA1C3BA:
 Userid     : "Cloudflare Package Repository <support@cloudflare.com>"
 Fingerprint: C068 A2B5 7717 7519 3CBE 1F2F 6E2D D217 4FA1 C3BA
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CLOUDFLARE-3
Is this ok [y/N]: y
Cloudflare CentOS Packages                                                             3.6 kB/s | 6.1 kB     00:01    
========================================= Name & Summary Matched: Cloudflare ==========================================
cloudflare-warp.x86_64 : Cloudflare Warp Client
cloudflare-release.noarch : Packages for Cloudflare products
python3-cloudflare.noarch : Python wrapper for the Cloudflare Client API v4
python-certbot-dns-cloudflare-doc.noarch : certbot-dns-cloudflare documentation
python3-certbot-dns-cloudflare.noarch : Cloudflare DNS Authenticator plugin for Certbot
[root@localhost ~]# yum install -y cloudflare-warp
//安装软件
Last metadata expiration check: 0:00:15 ago on Thu 16 Mar 2023 08:34:35 PM CST.
Dependencies resolved.
=======================================================================================================================
 Package                         Architecture           Version                       Repository                  Size
=======================================================================================================================
Installing:
 cloudflare-warp                 x86_64                 2023.1.133-1                  cloudflare                  38 M

Transaction Summary
=======================================================================================================================
Install  1 Package

Total download size: 38 M
Installed size: 165 M
Downloading Packages:
cloudflare_warp_2023_1_133_1_x86_64_e16441d71c.rpm                                     4.5 MB/s |  38 MB     00:08    
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  4.5 MB/s |  38 MB     00:08     
warning: /var/cache/dnf/cloudflare-a64266ee1a0b9fa0/packages/cloudflare_warp_2023_1_133_1_x86_64_e16441d71c.rpm: Header V4 RSA/SHA512 Signature, key ID 4fa1c3ba: NOKEY
Cloudflare CentOS Packages                                                             1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x4FA1C3BA:
 Userid     : "Cloudflare Package Repository <support@cloudflare.com>"
 Fingerprint: C068 A2B5 7717 7519 3CBE 1F2F 6E2D D217 4FA1 C3BA
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CLOUDFLARE-3
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                               1/1 
  Installing       : cloudflare-warp-2023.1.133-1.x86_64                                                           1/1 
  Running scriptlet: cloudflare-warp-2023.1.133-1.x86_64                                                           1/1 
Created symlink /etc/systemd/system/multi-user.target.wants/warp-svc.service → /etc/systemd/system/warp-svc.service.

  Verifying        : cloudflare-warp-2023.1.133-1.x86_64                                                           1/1 
Installed products updated.

Installed:
  cloudflare-warp-2023.1.133-1.x86_64                                                                                  

Complete!
[root@localhost ~]# warp-cli register
NOTICE:

Cloudflare only collects limited DNS query and traffic data (excluding payload)
that is sent to our network when you have the app enabled on your device. We
will not sell, rent, share, or otherwise disclose your personal information to
anyone, except as otherwise described in this Policy, without first providing
you with notice and the opportunity to consent. All information is handled in
accordance with our Privacy Policy.

More information is available at:
- https://www.cloudflare.com/application/terms/
- https://www.cloudflare.com/application/privacypolicy/

Accept Terms of Service and Privacy Policy? [y/N] y

Success
[root@localhost ~]# warp-cli register
Success
[root@localhost ~]# warp-cli set-mode proxy
//设置代理模式 默认端口40000
Success
[root@localhost ~]# warp-cli connect
Success
[root@localhost ~]# curl https://www.cloudflare.com/cdn-cgi/trace/ -x socks5://127.0.0.1:40000

//出现下面字段即为成功
...
warp=on
...

接下来需要申请一个OpenAI的key,每个账号会送一个5刀的体验key,大概可以调用1000次接口。这里需要提一下,注册账号确实是需要用非国内的手机接收一个验证码,我这里使用的是这个平台sms-activate.org,反正方法有很多,只要能注册上并且拿到key就行,这里贴一个网易的教程。网上有很多类似的教程。

接下来就是安装和配置项目了,还是很简单的,这里再次感谢@yuezk

[root@localhost ~]# yum install -y git
//先装一个git
Last metadata expiration check: 0:35:27 ago on Thu 16 Mar 2023 08:34:35 PM CST.
Dependencies resolved.
=======================================================================================================================
 Package                         Architecture          Version                          Repository                Size
=======================================================================================================================
Installing:
 git                             x86_64                2.18.4-2.el8_2                   AppStream                186 k
Installing dependencies:
 git-core                        x86_64                2.18.4-2.el8_2                   AppStream                4.0 M
 git-core-doc                    noarch                2.18.4-2.el8_2                   AppStream                2.3 M
 perl-Error                      noarch                1:0.17025-2.el8                  AppStream                 46 k
 perl-Git                        noarch                2.18.4-2.el8_2                   AppStream                 77 k
 perl-TermReadKey                x86_64                2.37-7.el8                       AppStream                 40 k

Transaction Summary
=======================================================================================================================
Install  6 Packages

Total download size: 6.6 M
Installed size: 36 M
Downloading Packages:
(1/6): git-2.18.4-2.el8_2.x86_64.rpm                                                   601 kB/s | 186 kB     00:00    
(2/6): perl-Error-0.17025-2.el8.noarch.rpm                                             248 kB/s |  46 kB     00:00    
(3/6): perl-Git-2.18.4-2.el8_2.noarch.rpm                                              150 kB/s |  77 kB     00:00    
(4/6): perl-TermReadKey-2.37-7.el8.x86_64.rpm                                          453 kB/s |  40 kB     00:00    
(5/6): git-core-doc-2.18.4-2.el8_2.noarch.rpm                                          940 kB/s | 2.3 MB     00:02    
(6/6): git-core-2.18.4-2.el8_2.x86_64.rpm                                              1.1 MB/s | 4.0 MB     00:03    
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  1.8 MB/s | 6.6 MB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                               1/1 
  Installing       : git-core-2.18.4-2.el8_2.x86_64                                                                1/6 
  Installing       : git-core-doc-2.18.4-2.el8_2.noarch                                                            2/6 
  Installing       : perl-TermReadKey-2.37-7.el8.x86_64                                                            3/6 
  Installing       : perl-Error-1:0.17025-2.el8.noarch                                                             4/6 
  Installing       : perl-Git-2.18.4-2.el8_2.noarch                                                                5/6 
  Installing       : git-2.18.4-2.el8_2.x86_64                                                                     6/6 
  Running scriptlet: git-2.18.4-2.el8_2.x86_64                                                                     6/6 
  Verifying        : git-2.18.4-2.el8_2.x86_64                                                                     1/6 
  Verifying        : git-core-2.18.4-2.el8_2.x86_64                                                                2/6 
  Verifying        : git-core-doc-2.18.4-2.el8_2.noarch                                                            3/6 
  Verifying        : perl-Error-1:0.17025-2.el8.noarch                                                             4/6 
  Verifying        : perl-Git-2.18.4-2.el8_2.noarch                                                                5/6 
  Verifying        : perl-TermReadKey-2.37-7.el8.x86_64                                                            6/6 
Installed products updated.

Installed:
  git-2.18.4-2.el8_2.x86_64               git-core-2.18.4-2.el8_2.x86_64       git-core-doc-2.18.4-2.el8_2.noarch      
  perl-Error-1:0.17025-2.el8.noarch       perl-Git-2.18.4-2.el8_2.noarch       perl-TermReadKey-2.37-7.el8.x86_64      

Complete!
[root@localhost ~]# git clone https://github.com/yuezk/chatgpt-mirror.git
//将项目安装到当前路径
Cloning into 'chatgpt-mirror'...
remote: Enumerating objects: 221, done.
remote: Counting objects: 100% (53/53), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 221 (delta 36), reused 35 (delta 28), pack-reused 168
Receiving objects: 100% (221/221), 1.49 MiB | 34.00 KiB/s, done.
Resolving deltas: 100% (81/81), done.
[root@localhost chatgpt-mirror]# pnpm install
//安装项目生成node_modules文件夹
Lockfile is up to date, resolution step is skipped
Packages: +724
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Packages are cloned from the content-addressable store to the virtual store.
  Content-addressable store is at: /root/.local/share/pnpm/store/v3
  Virtual store is at:             node_modules/.pnpm
Downloading registry.npmjs.org/@dqbd/tiktoken/0.4.0: 7.15 MB/7.15 MB, done
Downloading registry.npmjs.org/typescript/4.9.5: 11.6 MB/11.6 MB, done
Progress: resolved 724, reused 0, downloaded 721, added 724, done
node_modules/.pnpm/@nestjs+core@9.3.9_q6agyr4hwia55oswpsa7zjxcpm/node_modules/@nestjs/core: Running postinstall script.node_modules/.pnpm/@nestjs+core@9.3.9_q6agyr4hwia55oswpsa7zjxcpm/node_modules/@nestjs/core: Running postinstall script, done in 1.4s

dependencies:
+ @nestjs/common 9.3.9
+ @nestjs/config 2.3.1
+ @nestjs/core 9.3.9
+ @nestjs/platform-express 9.3.9
+ @nestjs/serve-static 3.0.1
+ chatgpt 5.0.8
+ compression 1.7.4
+ dotenv 16.0.3
+ node-fetch 3.3.0
+ proxy-agent-v2 1.0.3
+ reflect-metadata 0.1.13
+ rxjs 7.8.0
+ strip-json-comments 5.0.0
+ uuid 9.0.0

devDependencies:
+ @nestjs/cli 9.2.0
+ @nestjs/schematics 9.0.4
+ @nestjs/testing 9.3.9
+ @types/compression 1.7.2
+ @types/express 4.17.17
+ @types/jest 29.2.4
+ @types/node 18.11.18
+ @types/supertest 2.0.12
+ @types/uuid 9.0.1
+ @typescript-eslint/eslint-plugin 5.54.1
+ @typescript-eslint/parser 5.54.1
+ eslint 8.35.0
+ eslint-config-prettier 8.7.0
+ eslint-plugin-prettier 4.2.1
+ jest 29.3.1
+ prettier 2.8.4
+ source-map-support 0.5.21
+ supertest 6.3.3
+ ts-jest 29.0.3
+ ts-loader 9.4.2
+ ts-node 10.9.1
+ tsconfig-paths 4.1.1
+ typescript 4.9.5

Done in 45s

在项目根目录下创建一个.env的环境配置文件,并且填入下面的文本

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxx
# optional, support http or socks proxy
#HTTP_PROXY=http://proxy-server:port
HTTP_PROXY=socks://127.0.0.1:40000

接下来启动项目,因为启动以后需要放入后台,我这里推荐使用screen,如果没有的话yum装一个就行,不出意外你就通过访问这个vps来使用chatGPT了。

[root@localhost chatgpt-mirror]# pnpm run start
启动项目
> chatgpt-mirror@0.0.1 start /root/chatgpt-mirror
> nest start

[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [NestFactory] Starting Nest application...
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +19ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [InstanceLoader] ServeStaticModule dependencies initialized +0ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [InstanceLoader] AppModule dependencies initialized +2ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [RoutesResolver] AppController {/backend-api}: +8ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [RouterExplorer] Mapped {/backend-api/moderations, POST} route +3ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [RouterExplorer] Mapped {/backend-api/conversations, GET} route +0ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [RouterExplorer] Mapped {/backend-api/conversation, POST} route +1ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [RouterExplorer] Mapped {/backend-api/conversation/gen_title, POST} route +1ms
[Nest] 35703  - 03/16/2023, 9:28:49 PM     LOG [NestApplication] Nest application successfully started +7ms

这次启动以后,可以ctrl+c结束进程,然后使用pnpm run start:prod来启动一个稳定版的项目(不确定)。默认监听的端口是3000,只要项目能起来正常工作,后面就能用nginx反代或者其他什么骚操作了。

再此之前,我从来没有接触过Node.js,之所以能部署成功,也有chatGPT的帮助,我尝试让它指导我安装这个环境,经过几次尝试以后,我发现如果提的问题描述的够清晰,它还是能给到你很大帮助的。就在我写完这篇文章,我发现chatGPT已经迭代到4.0了,嗨,接着折腾呗~

参考资料:

https://github.com/yuezk/chatgpt-mirror

https://pkg.cloudflareclient.com/install

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注