Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
Ex-ChatGPT本地部署+Azure OpenAI接口配置+docker部署服务
sockstack
/
262
/
2023-12-02 00:02:38
<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="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-dracula"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><p>Ex-ChatGPT项目分为 Ex-ChatGPT 和 WebChatGPTEnhance 两部分,Ex-ChatGPT启动后是个web服务,通过访问ip+端口体验; WebChatGPTEnhance可编译生成一个浏览器插件,Chrome或者Microsoft edge浏览器可以安装该插件,点击该插件可以直接跳转到chatGPT的聊天页面,相比于官方chatGPT页面,底部多了个web access,即实现网络资料搜索,不仅限于ChatGPT的回答,类似于新必应。<br> 截止2023年5月29日, Ex-ChatGPT实现的第三方api请求比WebChatGPTEnhance 更多,功能更强,侧重研究Ex-ChatGPT即可。</p> <p><strong>本地试验环境:</strong><br> windows 10<br> <strong>服务器环境:</strong><br> Ubuntu<br> <strong>Ex-ChatGPT项目源代码</strong><br> https://github.com/circlestarzero/EX-chatGPT</p> <h1> <a id="_9"></a>一、本地部署</h1> <p>首先将代码clone到本地设备:</p> <pre><code class="prism language-bash"><span class="token function">git</span> clone https://github.com/circlestarzero/EX-chatGPT.git </code></pre> <p>以下步骤参考了博客:<br> https://www.luoxiao123.cn/enhanced-ex-chat-gpt-local-build-tutorial.html<br> 有不明白的可以试着看以上博客查找答案。</p> <h2> <a id="11_ExchatGPT__18"></a>1.1 Ex-chatGPT 安装</h2> <p>本人使用python3.11.3, 安装该项目的python模块依赖:</p> <pre><code class="prism language-bash">pip <span class="token function">install</span> -r requirements.txt -i -i https://pypi.tuna.tsinghua.edu.cn/simple </code></pre> <h3> <a id="111_IDKEY_24"></a>1.1.1 ID和KEY申请</h3> <h4> <a id="api_key_25"></a>谷歌api key申请</h4> <p>要翻墙<br> Google api key and search engine id 申请<br> 得到如下SEARCH_ENGINE_ID:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9d90e7800b0e4ccbb6969a2dda52ca6b.png" alt="在这里插入图片描述"><br> 以及GOOGLE_API_KEY:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/81c31f6aa15f4eafb142cacda58d6396.png" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/fd5f3efd016444f8ade93ec2c052a524.png" alt="在这里插入图片描述"></p> <h4> <a id="wolframAlpha_key_33"></a>wolframAlpha key申请</h4> <p>wolframAlpha app id key 申请</p> <h4> <a id="openAI_key__35"></a>openAI key 申请</h4> <p>要翻墙<br> openAI api key( 新功能 ) 或 chatGPT access_token ( 旧版本 ) <code>[申请](https://platform.openai.com/)</code></p> <h3> <a id="112_API_key_39"></a>1.1.2 API key配置</h3> <p>将.\EX-chatGPT\chatGPTEx\apikey.ini.example复制一份,重命名成apikey.ini,修改其中的配置:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7fcf6448c9c444098433cbe32133713e.png" alt="在这里插入图片描述"></p> <h3> <a id="113__42"></a>1.1.3 运行访问</h3> <p>首先谷歌和openAI的接口都需要翻墙才能访问,所以要确保你的梯子在本地开了全局模式,保证执行下面的代码时能通过代理访问到外网。</p> <p>执行 .\EX-chatGPT\chatGPTEx\main.py :</p> <pre><code class="prism language-bash">python main.py </code></pre> <p>在本地浏览器打开 http://127.0.0.1:1234/,即打开Ex-ChatGPT的窗口,:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/71b5096d499d41faab5f228319fb0ef8.png" alt="在这里插入图片描述"><br> 上图下方文字输入框左侧有个模式选择上拉框,鼠标移到选项中文字就会显示出来(显示有bug),这里讲一下其中主要的两个模式:<br> <strong>web模式:</strong><br> 当你输入一个问题,比如数学问题:请计算1+5等于多少?,该工具首先会调用chatGPT进行问题理解,然后生成谷歌接口、wolframAlpha接口、wiki接口、chatGPT分别进行查询,4个接口得到的结果会返回到窗口进行一一展示。<br> <strong>chat模式:</strong><br> 和上面同样的问题,只会查询chatGPT接口,返回结果的速度较快,但是由于chatGPT的信息较为落后,比如基于gpt3.5的chatGPT,其信息是2021年9月之前的,结果不如web模式全面和时效。</p> <h2> <a id="12_WebChatGPTEnhance_57"></a>1.2 WebChatGPTEnhance安装</h2> <p><strong>注意:</strong><br> https://github.com/circlestarzero/EX-chatGPT.git下.\EX-chatGPT\chatGPTChromeEnhance的代码是fork别人的代码,而且不完整(少了build目录),无法使用,本人找到了其fork的源代码库:</p> <pre><code class="prism language-bash"><span class="token function">git</span> clone https://github.com/qunash/chatgpt-advanced.git </code></pre> <h3> <a id="121_VC_64"></a>1.2.1 VC++编译环境配置</h3> <p>在进行下一步之前,首先得安装VC++,否则会报错,visualstudio下载链接,选择<br> https://visualstudio.microsoft.com/zh-hans/downloads/<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/639a186a640d4ce595bf881c303e3925.png#pic_center" alt="在这里插入图片描述"><br> 选择如上版本即可,在安装界面,选择.NET桌面开发、Node.js开发、C++桌面开发即可,其他勾都取消掉:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/549b194660a44685a2ba72662c35433d.png" alt="在这里插入图片描述"><br> 安装重启后应该就可以用npm和node命令了,如果不行,则可能没有自动添加命令路径到系统环境变量,那么你得找到node.exe所在路径,添加到系统环境变量,npm同理。</p> <h3> <a id="122__71"></a>1.2.2 插件生成和使用</h3> <p>接下来编译生成插件包:</p> <pre><code class="prism language-bash"><span class="token builtin class-name">cd</span> .<span class="token punctuation">\</span>chatgpt-advanced<span class="token punctuation">\</span> <span class="token function">npm</span> <span class="token function">install</span> <span class="token function">npm</span> run build-prod </code></pre> <p>之后在目录\chatgpt-advanced\bulid内会有个插件包,将包安装到chrome或edge浏览器即可:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/fd15338772da41dca97631d151f747b3.png" alt="在这里插入图片描述"></p> <h1> <a id="Azure_OpenAI_80"></a>二、Azure OpenAI服务配置</h1> <p>本公司购买了微软的Azure OpenAI接口服务,可以不用翻墙,通过Azure OpenAI接口访问到chatGPT;Ex-chatGPT默认是使用原生的openAI接口调用。下面从用openai模块访问chatgpt的方式来说明一下原openAI接口和Azure OpenAI接口的不同,如下图所示:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/64e374fde721499b89671e7f4468ce4c.png" alt="在这里插入图片描述"><br> 在Ex-chatGPT中并没有用到openai模块访问chatgpt,而是用requests模块访问chatgpt,当访问chatGPT的请求到了后,会执行到.\EX-chatGPT\chatGPTEx\optimizeOpenAI.py代码中的ask_stream函数,发起chatgpt接口请求,其中请求部分的代码为:</p> <pre><code class="prism language-python"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>response <span class="token operator">=</span> self<span class="token punctuation">.</span>session<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">"https://api.openai.com/v1/chat/completions"</span><span class="token punctuation">,</span>headers<span class="token operator">=</span><span class="token punctuation">{<!-- --></span><span class="token string">"Authorization"</span><span class="token punctuation">:</span> <span class="token string-interpolation"><span class="token string">f"Bearer </span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>kwargs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'api_key'</span><span class="token punctuation">,</span> apiKey<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span><span class="token punctuation">,</span>json<span class="token operator">=</span><span class="token punctuation">{<!-- --></span><span class="token string">"model"</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>engine<span class="token punctuation">,</span><span class="token string">"messages"</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>conversation<span class="token punctuation">[</span>convo_id<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"stream"</span><span class="token punctuation">:</span> <span class="token boolean">True</span><span class="token punctuation">,</span><span class="token comment"># kwargs</span><span class="token string">"temperature"</span><span class="token punctuation">:</span> kwargs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"temperature"</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>temperature<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"top_p"</span><span class="token punctuation">:</span> kwargs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"top_p"</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>top_p<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"n"</span><span class="token punctuation">:</span> kwargs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"n"</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>reply_count<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"user"</span><span class="token punctuation">:</span> role<span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">,</span>stream<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> </code></pre> <p>由以上代码可见,默认用了"https://api.openai.com/v1/chat/completions"这个原生的openai接口,这里本人修改这部分代码,使其默认访问Azure openai的接口去请求chatgpt,修改代码如下:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/e679df6394c94c93a41ccdbdc5f4afac.png" alt="在这里插入图片描述"><br> 修改后的代码中url和api-key都是写死的,没有用到apikey.ini文件内有关opneAI的key,这样所有chatgpt的请求都会走Azure openai的接口了,不再需要翻墙。上图修改后代码的url规则参考官网进行更改:<br> https://learn.microsoft.com/zh-cn/azure/cognitive-services/openai/reference<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/d2ef004fade347b8b23c027ccb537b2b.png" alt="在这里插入图片描述"></p> <h1> <a id="Docker_112"></a>三、Docker部署服务</h1> <p>任务是部署一个公司内网的员工都可以访问的Ex-chatGPT,由于很多员工都没有翻墙梯子,所以要如第二节一样,配置Ex-chatGPT通过Azure Openai的服务接口访问chatgpt。</p> <h2> <a id="31__114"></a>3.1 启动一个原生的容器</h2> <p>登录目标服务器进行如下操作:<br> 创建配置文件目录并拉取配置文件</p> <pre><code class="prism language-bash"><span class="token builtin class-name">cd</span> /data/ml/crl/ <span class="token function">mkdir</span> config <span class="token operator">&&</span> <span class="token function">wget</span> https://raw.githubusercontent.com/circlestarzero/EX-chatGPT/main/chatGPTEx/apikey.ini.example -O ./config/apikey.ini </code></pre> <p>编辑配置文件或者把编辑好的配置文件传到config文件夹下。</p> <pre><code class="prism language-bash"><span class="token function">vim</span> ./config/apikey.ini </code></pre> <p>拉取docker镜像</p> <pre><code class="prism language-bash"><span class="token function">docker</span> pull 0nlylty/exchatgpt:latest </code></pre> <p>创建容器</p> <pre><code class="prism language-bash"><span class="token function">docker</span> run -dit <span class="token punctuation">\</span>-v /data/ml/crl/config:/config <span class="token punctuation">\</span>-p <span class="token number">5100</span>:5000 <span class="token punctuation">\</span>--name exchatgpt <span class="token punctuation">\</span>--restart unless-stopped <span class="token punctuation">\</span>0nlylty/exchatgpt:latest </code></pre> <p>注意以上5100接口是宿主机服务器的接口,要自行找个未被占用的接口。容器启动后,找到这个容器的id, 然后进入容器中:</p> <pre><code class="prism language-bash"><span class="token function">docker</span> <span class="token builtin class-name">exec</span> -it 57de2cbe08bb /bin/bash </code></pre> <p>切换到目录中/app/chatGPTEx,删除代码文件optimizeOpenAI.py:</p> <pre><code class="prism language-bash">root@57de2cbe08bb:/app/chatGPTEx<span class="token comment"># pwd</span> /app/chatGPTEx root@57de2cbe08bb:/app/chatGPTEx<span class="token comment"># rm optimizeOpenAI.py</span> </code></pre> <h2> <a id="32_optimizeOpenAIpy_151"></a>3.2 本地修改optimizeOpenAI.py</h2> <p>在本地电脑,按照第二节方法修改optimizeOpenAI.py,然后将本地修改后的optimizeOpenAI.py传到服务器上面,如传到服务器目录:/data/ml/crl/chatGPTEx/optimizeOpenAI.py。<br> 最后在服务器上,将已修改的/data/ml/crl/chatGPTEx/optimizeOpenAI.py传到上小节已启动的容器中:</p> <pre><code class="prism language-bash"><span class="token function">docker</span> <span class="token function">cp</span> /data/ml/crl/chatGPTEx/optimizeOpenAI.py 57de2cbe08bb:/app/chatGPTEx </code></pre> <h2> <a id="33__158"></a>3.3 用修改后的容器生成新版镜像</h2> <p>经过上小节,容器中/app/chatGPTEx/optimizeOpenAI.py已经是修改过后的,访问chatGPT时,走的是Azure OpenAI接口,不再需要翻墙。将此修改后的容器生成一版新的镜像:</p> <pre><code class="prism language-bash"><span class="token function">docker</span> commit 57de2cbe08bb 0nlylty/exchatgpt:v1.0.0 </code></pre> <p>之后就不再需要57de2cbe08bb这个容器了,将其杀死和删除:</p> <pre><code class="prism language-bash"><span class="token function">docker</span> <span class="token function">kill</span> 57de2cbe08bb <span class="token function">docker</span> <span class="token function">rm</span> 57de2cbe08bb </code></pre> <h2> <a id="34__170"></a>3.4 用新镜像启动新容器</h2> <p>用上小节生成的新版镜像0nlylty/exchatgpt:v1.0.0,启动服务:</p> <pre><code class="prism language-bash"><span class="token function">docker</span> run -dit -v /data/ml/crl/config:/config -p <span class="token number">5100</span>:5000 --name exchatgpt --restart unless-stopped 0nlylty/exchatgpt:v1.0.0 </code></pre> <p>启动服务后,就可在本地访问Ex-ChatGPT了,能够访问服务器的其他员工也可以访问到该Ex-ChatGPT服务:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/44348aea23024124919e1fbe36f705e6.png" alt="在这里插入图片描述"></p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
Ex-ChatGPT本地部署+Azure OpenAI接口配置+docker部署服务
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-12-02
修改于
2025-01-02
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇: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
前端