Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
详细解读ChatGPT:如何调用ChatGPT的API接口到官方例子的说明以及GitHub上的源码应用
sockstack
/
360
/
2023-11-23 12:02:39
<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-6e43165c0a.css"> <div id="content_views" class="markdown_views prism-atom-one-light"> <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></p> <div class="toc"> <h3>文章目录</h3> <ul> <li>1. 解读ChatGPT</li> <li><ul> <li>1.1 词语解释</li> <li>1.2 功能解读</li> </ul></li> <li>2. GitHub上ChatGPT的应用源码</li> <li>3. 调用ChatGPT的API</li> <li>4. 官方例子说明</li> <li>5. 集成`ChatGPT`</li> </ul> </div> <p></p> <p>自<code>ChatGPT</code>出来到如今,始终走在火热的道路上,如今日活用户破亿,他为何有如此大的魅力,深受广大用户或资本的追捧,下面我便详细揭开他奇妙的面纱。</p> <h1> <a id="1_ChatGPT_4"></a>1. 解读ChatGPT</h1> <h2> <a id="11__5"></a>1.1 词语解释</h2> <p><code>ChatGPT</code>分开解</p> <ol> <li> <p><code>chat</code>是<code>闲聊,聊天</code>的意思</p> </li> <li> <p><code>GPT</code>是<code>Generative Pretrained Transformer</code>(生成预训练变换器)的缩写</p> </li> </ol> <p><code>chatGPT</code>它就是智能的对话聊天工具,由美国“开放人工智能研究中心”研发 - <code>OpenAI</code></p> <p><code>ChatGPT</code>自己回答如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ba2ed09545df44b5a85c1f1a8378afc3.png" alt="在这里插入图片描述"></p> <h2> <a id="12__18"></a>1.2 功能解读</h2> <p>他自己的回复有点抽象,简而言之,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,真正像人类一样来聊天交流,甚至能完成撰写邮件、视频脚本、文案、翻译、代码等任务。</p> <p>它的核心功能和特点如下:</p> <ol> <li> <p>它由人工智能技术驱动</p> </li> <li> <p>一款自然语言处理工具</p> </li> <li> <p>学习人类语言、理解人类语言</p> </li> <li> <p>通过大量训练能够与人类对话</p> </li> <li> <p>还能根据聊天上下文进行互动</p> </li> <li> <p>像人类一样完成如下任务:</p> <ul> <li>邮件撰写</li> <li>视频脚本编写</li> <li>写文案、写文章</li> <li>语言翻译</li> <li>甚至是代码编写编程</li> <li>…</li> </ul> </li> </ol> <h1> <a id="2_GitHubChatGPT_42"></a>2. GitHub上ChatGPT的应用源码</h1> <p>如今很多应用都集成了<code>ChatGPT</code>的插件,很多这样的应用已经在<code>GitHub</code>开源了。</p> <p>因而,我之前整理了<code>GitHub</code>上<code>ChatGPT</code>的应用源码,可以点击我的这篇博文访问:全网推荐8款github上有趣的ChatGPT的应用源码</p> <h1> <a id="3_ChatGPTAPI_49"></a>3. 调用ChatGPT的API</h1> <p>官方提供了很多种开发语言的调用,本篇主要以<code>java</code>开发语言进行调用,首先要引入<code>jar</code>包:</p> <pre><code class="prism 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>api<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>version<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> </code></pre> <p>或者</p> <pre><code class="prism 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>client<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>version<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> </code></pre> <p>调用代码非常简单,只需要填写相关<code>api-key</code>即可,如下代码所示:</p> <pre><code class="prism language-java"><span class="token keyword">package</span> <span class="token namespace">example</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>theokanning<span class="token punctuation">.</span>openai<span class="token punctuation">.</span></span><span class="token class-name">OpenAiService</span></span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>theokanning<span class="token punctuation">.</span>openai<span class="token punctuation">.</span>completion<span class="token punctuation">.</span></span><span class="token class-name">CompletionRequest</span></span><span class="token punctuation">;</span><span class="token keyword">class</span> <span class="token class-name">OpenAiApiExample</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token class-name">String</span> token <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">getenv</span><span class="token punctuation">(</span><span class="token string">"OPENAI_TOKEN"</span><span class="token punctuation">)</span><span class="token punctuation">;</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>token<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">"\nCreating completion..."</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 punctuation">.</span><span class="token function">model</span><span class="token punctuation">(</span><span class="token string">"ada"</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">"Somebody once told me the world is gonna roll me"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">echo</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token string">"testing"</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 function">println</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://platform.openai.com/docs/libraries,找到你所需要的语言。</p> <p>其提供了各种语言的示例,点击每种语言的示例链接,如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1c24c1325dd4486aa19b9e90acdd6fd4.png" alt="在这里插入图片描述"></p> <p>即可跳转到其对应的<code>GitHub</code>上,比如我点击的是<code>java</code>,如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8d878dc9fbd9409bbf804253b1ab45f3.png" alt="在这里插入图片描述"></p> <h1> <a id="4__107"></a>4. 官方例子说明</h1> <p>官方示例说明的链接地址:https://platform.openai.com/examples。点击此链接,你会看到一个搜索框,一个下拉框,如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/6f84376b39da47c2bada5a819efe6496.png" alt="在这里插入图片描述"></p> <p>下拉框是类别,其分为如下类别:</p> <ol> <li> <p>问答(<code>Answers</code>)</p> </li> <li> <p>分类(<code>Classification</code>)</p> </li> <li> <p>代码(<code>Code</code>)</p> </li> <li> <p>会话(<code>Conversation</code>)</p> </li> <li> <p>生成(<code>Generation</code>)</p> </li> <li> <p>翻译(<code>Translation</code>)</p> </li> <li> <p>转换(<code>Transformation</code>)</p> </li> </ol> <p>据我猜测,将来<code>ChatGPT</code>绝不止这些类别,后期肯定会有更多的类别出现,我们静静地等待即可。</p> <p>那么,我以<code>问答类(Answers)</code>为例子,来解读示例中类别,其他类别大家可自行学习了解。<strong>实在不行,可以使用谷歌翻译成中文看。</strong></p> <p>当下拉框选择为<code>问答类(Answers)</code>时,其出现的界面如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ab79b062c696495d8c9ae66bee25d82f.png" alt="在这里插入图片描述"></p> <ol> <li> <p><code>Q&A</code>:基于现有知识回答问题</p> </li> <li> <p><code>Factual answering</code>:通过向模型展示如何回答超出其知识库的问题,引导模型走向事实答案。使用<code>?</code>表示对它不知道的单词和短语的反应提供了一种自然的反应,似乎比更抽象的回答更有效</p> </li> <li> <p><code>JavaScript helper chatbot</code>:这是一个消息风格的聊天机器人,可以回答有关使用<code>JavaScript</code>的问题。</p> </li> <li> <p><code>ML/AI language model tutor</code>:这是一个<code>QA</code>风格的聊天机器人,回答有关语言模型的问题。</p> </li> </ol> <p>在此(<code>问答类(Answers)</code>)类别下出现多个列表,你单击每个列表都会出现一个示例弹框,比如单击<code>JavaScript helper chatbot</code>,如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7028962e95d4411d9520fb6ec449b717.png" alt="在这里插入图片描述"></p> <p><strong>当然,每个类别下都会有多个列表,你单击每个列表都会出现一个示例弹框。</strong></p> <h1> <a id="5_ChatGPT_150"></a>5. 集成<code>ChatGPT</code> </h1> <p>目前,很多<code>APP</code>或者公众号都集成了<code>ChatGPT</code>,那么,我以<code>csdn</code>为例子来演示。</p> <p>csdn网站<code>ChatGPT</code>的<code>PC</code>端地址:https://so.csdn.net/so/chat,点击链接如下图所示:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/cd69f7eb0699465e91b66fd81ff3c3f7.png" alt="在这里插入图片描述"></p> <p>csdn客户端在<code>我的 - 更多服务 </code>中找到:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/51f09e96a35e4da1b45c995b304e0109.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/5188426baced4ca7a2480815c4b25b16.png" alt="在这里插入图片描述"></p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-0407448025.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-bb308a51ed.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
详细解读ChatGPT:如何调用ChatGPT的API接口到官方例子的说明以及GitHub上的源码应用
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-23
修改于
2024-12-23
上一篇:软件:常用 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
前端