Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
在云服务器上搭建个人版chatGPT及后端Spring Boot集成chat GPT教程
sockstack
/
219
/
2023-11-14 11:44:55
<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>总结/朱季谦</p> <p>本文分成两部分,包括【<strong>国内服务器上搭建chat GPT</strong>】和【<strong>后端Spring Boot集成chat GPT</strong>】。</p> <p>无论是在【国内服务器上搭建chat GPT】和【后端Spring Boot集成chat GPT】,两个方式都需要魔法访问,否则是无法正常使用的,即需要具备正常访问谷歌或者 api.openai.com的能力。</p> <p>至于什么是魔法访问,以及如何搭建魔法访问,请自行研究哈。</p> <p>下面就开始讲解两部分的教程。<br> <br></p> <h2>一、国内服务器上搭建chat GPT</h2> <p>首先,你需要准备以下东西:</p> <p>1、一台可以访问公网的Linux云服务器,最低配置1核2G即可(当然,有钱可以任性,买最高配置)</p> <p>2、chatGPT的密钥</p> <p>3、开源的仿chatGPT的Docker镜像</p> <p><br><br> <br></p> <h3>1.1、准备一台云服务器</h3> <p>可以是腾讯云、阿里云或者华为云等,我分别在阿里云和华为云上都能正常搭建。<br> <br></p> <h3>1.2、设置网络代理</h3> <p>在部署魔法访问的服务器上,需要在/etc/profile增加代理,确保通过密钥方式的chatGPT接口调用能正常访问:</p> <p>export all_proxy=http://127.0.0.1:8889<br> export http_proxy=http://127.0.0.1:8889<br> export https_proxy=https://127.0.0.1:8889<br> 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><br> 可以访问即没问题。可以继续往下走。</p> <p><br></p> <h3>1.3、安装Docker</h3> <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-shell"><code class="shell language-shell">{ "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></code></pre> </div> <p><br></p> <h3>1.4、Docker镜像</h3> <p>目前网上GitHub已经开源了许多优秀的仿写chatGPT 页面的应用,我们无需再额外造轮子,只需要挑选其中一款用来打包部署成Docker容器运行即可。</p> <p>我使用的是chatgpt-mirror这个开源项目。</p> <p>直接克隆项目到对应的Linux服务器——</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-php"><code class=" language-php">git <span class="token keyword">clone</span> https<span class="token punctuation">:</span><span class="token comment">//github.com/yuezk/chatgpt-mirror.git</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>在基于该开源项目以Dockerfile形式打包前,需要执行以下被依赖到的镜像——</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-shell"><code class="shell language-shell">docker pull node:18-alpine docker pull node:18-slim <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>接下来,就可以执行以下操作来创建一个Docker镜像了——</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-shell"><code class="shell language-shell">cd chatgpt-mirror #--network host表示与宿主公用网络,即走代理,然后留意下最后有一个 . docker build --network host -t chatgpt-mirror . #正常执行成功后,通过该指令能看到一个新镜像 docker images <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>具体情况如下:</p> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 372px;"> <div class="image-container-fill" style="padding-bottom: 20.78%;"></div> <div class="image-view" data-width="1790" data-height="372"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/6158331-e9e4a72e0a627580.png" data-original-width="1790" data-original-height="372" data-original-format="image/png" data-original-filesize="141464" data-image-index="0" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image.png</div> </div> <p>然后需要在cd chatgpt-mirror环境里新增一个文件env,该文件里写入chatGPT密钥与宿主机器的代理:</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-cpp"><code class=" language-cpp">OPENAI_API_KEY<span class="token operator">=</span>你的chatGPT密钥 HTTP_PROXY<span class="token operator">=</span>http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token operator">:</span><span class="token number">8889</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <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: 374px;"> <div class="image-container-fill" style="padding-bottom: 53.38%;"></div> <div class="image-view" data-width="1716" data-height="916"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/6158331-09f6f1fbdc1dc570.png" data-original-width="1716" data-original-height="916" data-original-format="image/png" data-original-filesize="222668" data-image-index="1" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image.png</div> </div> <p><br></p> <h2>二、后端Spring Boot集成chat GPT</h2> <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="xml 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="java 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></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> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 542px;"> <div class="image-container-fill" style="padding-bottom: 77.41%;"></div> <div class="image-view" data-width="1762" data-height="1364"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/6158331-48e57d6f39b88afa.png" data-original-width="1762" data-original-height="1364" data-original-format="image/png" data-original-filesize="629858" data-image-index="2" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image.png</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="java 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></code></pre> </div> <p>部署在Linux云服务上的聊天返回打印效果——</p> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 385px;"> <div class="image-container-fill" style="padding-bottom: 54.910000000000004%;"></div> <div class="image-view" data-width="1588" data-height="872"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/6158331-8b0a55dbcede25d7.png" data-original-width="1588" data-original-height="872" data-original-format="image/png" data-original-filesize="177931" data-image-index="3" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">image.png</div> </div> <p>以上就是关于【国内服务器上搭建chat GPT】和【后端Spring Boot集成chat GPT】教程,更多好玩的关于chat GPT相关的内容,可以关注我,因为我对这块很感兴趣,接下来会分享更多相关内容。有不懂的也可以后台问我。</p> </article>
在云服务器上搭建个人版chatGPT及后端Spring Boot集成chat GPT教程
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-14
修改于
2024-12-22
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:《花雕学AI》06:抢先体验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
前端