Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
在云服务器上搭建个人版chatGPT及后端Spring Boot集成chat GPT
sockstack
/
286
/
2023-11-14 11:46:47
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="_2rhmJa"><p>一、国内服务器上搭建chat GPT</p> <p>首先,你需要准备以下东西:</p> <p>1、一台可以访问公网的Linux云服务器,最低配置1核2G即可(当然,有钱可以任性,买最高配置)</p> <p>2、chatGPT的密钥</p> <p>3、开源的仿chatGPT的Docker镜像</p> <p>1.1、准备一台云服务器</p> <p>可以是腾讯云、阿里云或者华为云等,我分别在阿里云和华为云上都能正常搭建。</p> <p>1.2、设置网络代理</p> <p>在部署魔法访问的服务器上,需要在/etc/profile增加代理,确保通过密钥方式的chatGPT接口调用能正常访问:</p> <p>export all_proxy=http://127.0.0.1:8889</p> <p>export http_proxy=http://127.0.0.1:8889</p> <p>export https_proxy=https://127.0.0.1:8889</p> <p>export all_proxy=socks5://127.0.0.1:1080</p> <p>这里的8889和1080需要根据你的魔法访问里的config.json来相应设置。</p> <p>配置完成后,执行source /etc/profile,检验一下curl <a href="https://links.jianshu.com/go?to=https%3A%2F%2Fapi.openai.com%2F" target="_blank">https://api.openai.com/</a></p> <p>可以访问即没问题。可以继续往下走。</p> <p>1.3、安装Docker</p> <p>可以按照我以前记录的一篇关于搭建Docker的方式进行命令行按照<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fwww.cnblogs.com%2Fzhujiqian%2Fp%2F15336179.html" target="_blank">CentOS7安装Docker遇到的问题笔记</a></p> <p>搭建完成后,因为Docker的对外访问若需要走所在宿主的代理话,还需要设置以下操作——</p> <p>创建一个~/.docker/目录,然后在该目录下新建一个config.json文件,在该文件里添加以下命令——</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">{ "default": { "httpProxy": "http://127.0.0.1:8889", "httpsProxy": "http://127.0.0.1:8889", "noProxy": "*.test.example.com,.example2.com,127.0.0.0/8" } } } <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>1.4、Docker镜像</p> <p>目前网上GitHub已经开源了许多优秀的仿写chatGPT 页面的应用,我们无需再额外造轮子,只需要挑选其中一款用来打包部署成Docker容器运行即可。</p> <p>我使用的是chatgpt-mirror这个开源项目。</p> <p>直接克隆项目到对应的Linux服务器——</p> <p>git clone <a href="https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fyuezk%2Fchatgpt-mirror.git" target="_blank">https://github.com/yuezk/chatgpt-mirror.git</a></p> <p>在基于该开源项目以Dockerfile形式打包前,需要执行以下被依赖到的镜像——</p> <p>docker pull node:18-alpine</p> <p>docker pull node:18-slim</p> <p>接下来,就可以执行以下操作来创建一个Docker镜像了——</p> <p>cdchatgpt-mirror#--network host表示与宿主公用网络,即走代理,然后留意下最后有一个 .docker build --networkhost-t chatgpt-mirror.#正常执行成功后,通过该指令能看到一个新镜像docker images</p> <p>具体情况如下:</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 204px;"> <div class="image-container-fill" style="padding-bottom: 21.91%;"></div> <div class="image-view" data-width="931" data-height="204"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/10969527-6f2386880048cf43.png" data-original-width="931" data-original-height="204" data-original-format="image/png" data-original-filesize="87158" data-image-index="0" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image</div> </div> <p>然后需要在cd chatgpt-mirror环境里新增一个文件env,该文件里写入chatGPT密钥与宿主机器的代理:</p> <p>OPENAI_API_KEY=你的chatGPT密钥</p> <p>HTTP_PROXY=http://127.0.0.1:8889</p> <p>完成以上操作后,最后在该目录chatgpt-mirror里执行——</p> <p>docker run -itd --net host -p 3000:3000 -v /app/config.json:/app/config/app.config --env-file env chatgpt-mirror</p> <p>正常执行完成后,即可在浏览上输入http://你的服务器ip:3000,就能出来一个外表仿chatGPT但内在是调用真实chatGPT接口的应用。</p> <p><strong>重点是,如此一来,你的电脑、平台、手机等终端都无需魔法访问,就能直接使用chatGPT了,而且响应速度比直连官网快一倍左右速度,无比丝滑!而且,没有像chatGPT官网直连那样经常出现响应异常以及断开的问题,协助效率大大增加。</strong></p> <p>以下就是访问搭建在我自己服务器上的chatGPT页面,是不是跟真实的很像。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 497px;"> <div class="image-container-fill" style="padding-bottom: 53.44%;"></div> <div class="image-view" data-width="930" data-height="497"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/10969527-848b165df735d032.png" data-original-width="930" data-original-height="497" data-original-format="image/png" data-original-filesize="79399" data-image-index="1" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image</div> </div> <p>二、后端Spring Boot集成chat GPT</p> <p>注意,该方式同样需要魔法访问。</p> <p>首先,在maven依赖引入以下配置——</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-xml"><code class=" language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.theokanning.openai-gpt3-java<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>service<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>0.11.1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>编写以下代码——</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-java"><code class=" language-java"><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/ai"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sendMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开始提问题~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"你是一个工作助手,情帮忙设计一份活动策划书"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//GPT_TOKEN即你的代码密钥</span> <span class="token class-name">OpenAiService</span> service <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">OpenAiService</span><span class="token punctuation">(</span>GPT_TOKEN<span class="token punctuation">,</span><span class="token class-name">Duration</span><span class="token punctuation">.</span><span class="token function">ofSeconds</span><span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">CompletionRequest</span> completionRequest <span class="token operator">=</span> <span class="token class-name">CompletionRequest</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//使用的模型</span> <span class="token punctuation">.</span><span class="token function">model</span><span class="token punctuation">(</span><span class="token string">"text-davinci-003"</span><span class="token punctuation">)</span> <span class="token comment">//输入提示语</span> <span class="token punctuation">.</span><span class="token function">prompt</span><span class="token punctuation">(</span><span class="token string">"设计一份活动策划书"</span><span class="token punctuation">)</span> <span class="token comment">//该值越大每次返回的结果越随机,即相似度越小,可选参数,默认值为 1,取值 0-2</span> <span class="token punctuation">.</span><span class="token function">temperature</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token comment">//返回结果最大分词数</span> <span class="token punctuation">.</span><span class="token function">maxTokens</span><span class="token punctuation">(</span><span class="token number">2048</span><span class="token punctuation">)</span> <span class="token comment">//与temperature类似</span> <span class="token punctuation">.</span><span class="token function">topP</span><span class="token punctuation">(</span><span class="token number">1D</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> service<span class="token punctuation">.</span><span class="token function">createCompletion</span><span class="token punctuation">(</span>completionRequest<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChoices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">:</span><span class="token operator">:</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">6000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>CompletionRequest的属性文档介绍在这里——</p> <p><a href="https://links.jianshu.com/go?to=https%3A%2F%2Fplatform.openai.com%2Fdocs%2Fapi-reference%2Fcompletions%2Fcreate" target="_blank">https://platform.openai.com/docs/api-reference/completions/create</a></p> <p>启动,调用该接口,即可正常使用chat GPT集成到SpringBoot后端代码里——</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 581px;"> <div class="image-container-fill" style="padding-bottom: 82.96%;"></div> <div class="image-view" data-width="927" data-height="769"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/10969527-2fb918e783bce180.png" data-original-width="927" data-original-height="769" data-original-format="image/png" data-original-filesize="128551" data-image-index="2" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image</div> </div> <p>需要注意的是,若是部署在有魔法访问的Linux云服务,代码需要相应做一下调整,否则是无法访问到chatGPT的,会出现以下异常提示:java.net.ConnectException:Failed to connect to api.openai.com/2a03:2880:f10c:283:face:b00c:0:25de:443]</p> <p>故而,需要做以下调整:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-java"><code class=" language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">send1Msg</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开始提问题~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"你是一个工作助手,情帮忙设计一份活动策划书"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//需要额外设置一个能访问chatGPT的魔法访问代理</span> <span class="token class-name">ObjectMapper</span> mapper <span class="token operator">=</span> <span class="token function">defaultObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Proxy</span> proxy <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span><span class="token class-name">Proxy</span><span class="token punctuation">.</span><span class="token class-name">Type</span><span class="token punctuation">.</span>HTTP<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InetSocketAddress</span><span class="token punctuation">(</span><span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token number">8889</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">OkHttpClient</span> client <span class="token operator">=</span> <span class="token function">defaultClient</span><span class="token punctuation">(</span>GPT_TOKEN<span class="token punctuation">,</span><span class="token class-name">Duration</span><span class="token punctuation">.</span><span class="token function">ofSeconds</span><span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">newBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">proxy</span><span class="token punctuation">(</span>proxy<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Retrofit</span> retrofit <span class="token operator">=</span> <span class="token function">defaultRetrofit</span><span class="token punctuation">(</span>client<span class="token punctuation">,</span> mapper<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">OpenAiApi</span> api <span class="token operator">=</span> retrofit<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token class-name">OpenAiApi</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将设置的代理传给OpenAiService即可</span> <span class="token class-name">OpenAiService</span> service <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">OpenAiService</span><span class="token punctuation">(</span>api<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">CompletionRequest</span> completionRequest <span class="token operator">=</span> <span class="token class-name">CompletionRequest</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">model</span><span class="token punctuation">(</span><span class="token string">"text-davinci-003"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">prompt</span><span class="token punctuation">(</span><span class="token string">"设计一份活动策划书"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">temperature</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">maxTokens</span><span class="token punctuation">(</span><span class="token number">2048</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">topP</span><span class="token punctuation">(</span><span class="token number">1D</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> service<span class="token punctuation">.</span><span class="token function">createCompletion</span><span class="token punctuation">(</span>completionRequest<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChoices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">:</span><span class="token operator">:</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">6000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>部署在Linux云服务上的聊天返回打印效果——</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 440px;"> <div class="image-container-fill" style="padding-bottom: 47.31%;"></div> <div class="image-view" data-width="930" data-height="440"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/10969527-0f578dcaec2f6c8a.png" data-original-width="930" data-original-height="440" data-original-format="image/png" data-original-filesize="67592" data-image-index="3" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image</div> </div> </article>
在云服务器上搭建个人版chatGPT及后端Spring Boot集成chat GPT
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-14
修改于
2024-12-22
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:免费1年海外服务器,部署ChatGPT专属网页版(保姆级部署教程)
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
4
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端