Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
关于ChatGPT的一些随笔
sockstack
/
371
/
2024-02-21 12:02:43
<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-atom-one-dark"> <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>大家好,我是老三,最近几个月关于ChatGPT的信息可以说是铺天盖地。</p> <p>“王炸,ChatGPT……”</p> <p>“xxx震撼发布……”</p> <p>“真的要失业了,xxx来袭……”</p> <p>“普通如何利用ChatGPT……”</p> <p>……</p> <p>不过老三前一阵比较忙,对ChatGPT的使用基本只限于一些简单的问答,比如:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/af0b4188a0034f6584b675ddd43812d4.png" alt="三分恶是谁?"></p> <p>可以说是落伍到家了,刚好公司组织了对ChatGPT的一些讨论和实践,趁这个机会赶紧补补课。</p> <h1> <a id="_18"></a>快速上手</h1> <p>作为一般的后端开发人员,要利用ChatGPT,其实主要还是是调用openai的接口,先跟着官方文档(https://platform.openai.com/docs/quickstart),快速开始。</p> <h2> <a id="_22"></a>简介</h2> <ul><li>completions接口:https://platform.openai.com/docs/api-reference/completions/create</li></ul> <p>这个接口是openai核心接口,输入里有个参数<strong>prompt</strong>,可以简单理解为问,响应里有个参数<strong>completion</strong>,可以简单理解为答。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/16125978f227426792fe6aee2f5e1f2d.png" alt="prompt和completion"></p> <h2> <a id="_30"></a>创建应用</h2> <p>官方给了Node.js和Python的Demo,没有关系,反正无非是接口调用,用什么都可以。</p> <p>因为我本地配置了Python的环境,所以就跟着官方的Python Demo走一走。</p> <h3> <a id="_36"></a>拉取项目</h3> <p>Python环境的创建这里就不多说了,直接把官方的Demo拉下来。</p> <pre><code class="prism language-shell"><span class="token function">git</span> clone https://github.com/openai/openai-quickstart-python.git </code></pre> <h3> <a id="API_Key_44"></a>添加API Key</h3> <p>首先需要在这个地址:https://platform.openai.com/account/api-keys 添加一个api-key.</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/5478a86ccccd4eb68fa30e86b12c273e.png" alt="添加api key"></p> <p>在.env文件里添加配置<code>OPENAI_API_KEY</code>:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/16e1c13400b1492e96f7c354f5921a20.png" alt="添加OPENAI_API_KEY"></p> <h3> <a id="_54"></a>运行</h3> <ul><li>安装依赖</li></ul> <pre><code class="prism language-shell">python -m venv venv <span class="token builtin class-name">.</span> venv/bin/activate pip <span class="token function">install</span> -r requirements.txt </code></pre> <ul><li>运行项目</li></ul> <pre><code class="prism language-shell">flask run </code></pre> <ul><li>访问http://localhost:5000</li></ul> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/b091780741a14e5a8e55f4da4dcaf424.png" alt="页面"></p> <p>简单看下官方Demo,官方提供了Python包:</p> <pre><code class="prism language-python"><span class="token keyword">import</span> openai </code></pre> <p>官方还提供了node.js的包,社区也贡献了一些其他语言的包(https://platform.openai.com/docs/libraries/community-libraries):</p> <ul> <li>Java:https://github.com/TheoKanning/openai-java</li> <li>Go:https://github.com/sashabaranov/go-openai</li> </ul> <p>……</p> <p>Python Demo里就是直接用封装的方法调用:</p> <pre><code class="prism language-python">response <span class="token operator">=</span> openai<span class="token punctuation">.</span>Completion<span class="token punctuation">.</span>create<span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"text-davinci-003"</span><span class="token punctuation">,</span>prompt<span class="token operator">=</span>generate_prompt<span class="token punctuation">(</span>animal<span class="token punctuation">)</span><span class="token punctuation">,</span>temperature<span class="token operator">=</span><span class="token number">0.6</span><span class="token punctuation">,</span><span class="token punctuation">)</span> </code></pre> <p>不用官方和社区封装的包,直接http调用也可以。</p> <p>当然openai也不是免费的,以gpt-3.5-turbo为例,费用是$0.002 / 1K tokens,也就是大概$0.002 / 750个词,具体费用可以看看:https://openai.com/pricing,注册送了18💰,大概能答不到1000个问题吧。</p> <h1> <a id="API_103"></a>常见API</h1> <p>在快速开始里用了Completion API,接下来再看看常见的API。</p> <h2> <a id="_107"></a>模型</h2> <p>Model(模型)是openai的核心,有一系列不同功能不同版本的模型。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3fb229404854410f96512db1e830dd61.png" alt="目前大概有这些模型"></p> <h2> <a id="Playground_113"></a>Playground</h2> <p>在前面,我们跑了官方的Demo,其实还有一个更简单快捷的方式,官方提供了一个好用的<code>Playground</code>,有点像开发版的chatGPT,可以直接指定不同的模型不同的参数,调用不同的api:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/a0e07bda9a9f49519b13bd1f3844d200.png" alt="Playground"></p> <p>还可以直接查看代码,支持python、node.js、curl。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/fe82a84d35904a0ab76d0f991df379eb.png" alt="生成代码"></p> <p>接下来看看openai的内容处理,主要包括三种:<code>Text completion</code>、<code>Code completion</code>和<code>Image generation</code>,分别针对文本、代码和图片。</p> <h2> <a id="API_125"></a>内容处理API</h2> <p>调用API还是用Python,因为官方给了Demo,Python跑起来也比较轻便。</p> <p>接着用官方的实例,open-ai需要升级到最新版本:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/056e23c3bf934aa5b52edf059d8ad008.png" alt="升级"></p> <h3> <a id="Completion_133"></a>Completion</h3> <ul><li>Completions:https://platform.openai.com/docs/api-reference/completions</li></ul> <p>这个是比较早,用的比较多的接口,不支持gpt-3.5和gpt-4模型的模型。</p> <table> <thead><tr> <th>参数</th> <th>类型</th> <th>说明</th> </tr></thead> <tbody> <tr> <td>model</td> <td>string</td> <td>必填,指定功能模型</td> </tr> <tr> <td>prompt</td> <td>string 或 array</td> <td>提示语</td> </tr> <tr> <td>max_tokens</td> <td>integer</td> <td>默认值 16,The maximum number of tokens to generate in the completion.</td> </tr> <tr> <td>temperature</td> <td>number</td> <td>默认值 1,采样精度,取值范围为 0~2,较高的值(如 0.8)将使输出更加随机,而较低的值(例如 0.2)将使其更加相关</td> </tr> <tr> <td>top_p</td> <td>number</td> <td>默认值 1,采样精度的另一种表示方式,如 0.1 表示更相关的 10% 的内容,请不要和 temperature 同时使用(2 选 1)</td> </tr> <tr> <td>n</td> <td>integer</td> <td>默认值 1,返回内容的条数</td> </tr> <tr> <td>stream</td> <td>boolean</td> <td>默认值 false,是否流式输出内容</td> </tr> <tr> <td>echo</td> <td>boolean</td> <td>默认值 false,是否回显 prompt 内容</td> </tr> <tr> <td>stop</td> <td>string 或 array</td> <td>默认值 null,Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.</td> </tr> </tbody> </table> <ul><li>Python请求</li></ul> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span>response<span class="token operator">=</span>openai<span class="token punctuation">.</span>Completion<span class="token punctuation">.</span>create<span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"text-davinci-003"</span><span class="token punctuation">,</span>prompt<span class="token operator">=</span><span class="token string">"Hello World"</span><span class="token punctuation">,</span>max_tokens<span class="token operator">=</span><span class="token number">512</span><span class="token punctuation">,</span>temperature<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span> </code></pre> <ul><li>响应</li></ul> <pre><code class="prism language-python"><span class="token punctuation">{<!-- --></span><span class="token string">"choices"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string">"finish_reason"</span><span class="token punctuation">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span><span class="token string">"index"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token string">"logprobs"</span><span class="token punctuation">:</span> null<span class="token punctuation">,</span><span class="token string">"text"</span><span class="token punctuation">:</span> <span class="token string">"\n\nMy name is John and I am new to this website. I am interested in learning more about programming and computer science."</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"created"</span><span class="token punctuation">:</span> <span class="token number">1679751379</span><span class="token punctuation">,</span><span class="token string">"id"</span><span class="token punctuation">:</span> <span class="token string">"cmpl-6xyM7jPbvSSO7umjVk8HkrRfJVoHZ"</span><span class="token punctuation">,</span><span class="token string">"model"</span><span class="token punctuation">:</span> <span class="token string">"text-davinci-003"</span><span class="token punctuation">,</span><span class="token string">"object"</span><span class="token punctuation">:</span> <span class="token string">"text_completion"</span><span class="token punctuation">,</span><span class="token string">"usage"</span><span class="token punctuation">:</span> <span class="token punctuation">{<!-- --></span><span class="token string">"completion_tokens"</span><span class="token punctuation">:</span> <span class="token number">26</span><span class="token punctuation">,</span><span class="token string">"prompt_tokens"</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token string">"total_tokens"</span><span class="token punctuation">:</span> <span class="token number">28</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> </code></pre> <p>可以看到已经回复了,我们要是想继续怎么办呢?也就是类似chatGPT里接着会话——那就需要把整个上下文传过去。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/274e1728eedf473296ea5d884fa1ed58.png" alt="维护会话"></p> <p>比如上面一个,我希望它能继续回答:</p> <pre><code class="prism language-java"><span class="token keyword">import</span> <span class="token namespace">os</span> <span class="token keyword">import</span> <span class="token namespace">openai</span> openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span><span class="token function">getenv</span><span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span>prompt_context<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"Hello World"</span><span class="token punctuation">,</span><span class="token string">"\n\nMy name is John and I am new to this website. I am interested in learning more about programming and computer science."</span><span class="token punctuation">,</span><span class="token string">"Any more?"</span><span class="token punctuation">]</span>response<span class="token operator">=</span><span class="token class-name"><span class="token namespace">openai<span class="token punctuation">.</span></span>Completion</span><span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"text-davinci-003"</span><span class="token punctuation">,</span>prompt<span class="token operator">=</span>prompt_context<span class="token punctuation">,</span>max_tokens<span class="token operator">=</span><span class="token number">1024</span><span class="token punctuation">,</span>temperature<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span> </code></pre> <ul><li>响应:可以看到接着继续回答了</li></ul> <pre><code class="prism language-json"><span class="token punctuation">{<!-- --></span><span class="token string-property property">"choices"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"finish_reason"</span><span class="token operator">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span><span class="token string-property property">"index"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token string-property property">"logprobs"</span><span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span><span class="token string-property property">"text"</span><span class="token operator">:</span> <span class="token string">"\n\nMy name is John and I am new to this website. I am interested in learning more about programming and computer science."</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"finish_reason"</span><span class="token operator">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span><span class="token string-property property">"index"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string-property property">"logprobs"</span><span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span><span class="token string-property property">"text"</span><span class="token operator">:</span> <span class="token string">" I have some basic knowledge of HTML and CSS, but I am looking to expand my knowledge and learn more about other programming languages. I am also interested in learning more about web development and design."</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"finish_reason"</span><span class="token operator">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span><span class="token string-property property">"index"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token string-property property">"logprobs"</span><span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span><span class="token string-property property">"text"</span><span class="token operator">:</span> <span class="token string">"\n\n-Making sure to get enough sleep\n-Eating a balanced diet\n-Drinking plenty of water\n-Exercising regularly\n-Managing stress\n-Limiting alcohol and caffeine intake\n-Practicing relaxation techniques\n-Spending time outdoors\n-Connecting with friends and family\n-Engaging in creative activities\n-Volunteering in the community\n-Practicing mindfulness and meditation"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string-property property">"created"</span><span class="token operator">:</span> <span class="token number">1679751585</span><span class="token punctuation">,</span><span class="token string-property property">"id"</span><span class="token operator">:</span> <span class="token string">"cmpl-6xyPRzgmrTskbfQ85l5MdmIdUU6iZ"</span><span class="token punctuation">,</span><span class="token string-property property">"model"</span><span class="token operator">:</span> <span class="token string">"text-davinci-003"</span><span class="token punctuation">,</span><span class="token string-property property">"object"</span><span class="token operator">:</span> <span class="token string">"text_completion"</span><span class="token punctuation">,</span><span class="token string-property property">"usage"</span><span class="token operator">:</span> <span class="token punctuation">{<!-- --></span><span class="token string-property property">"completion_tokens"</span><span class="token operator">:</span> <span class="token number">149</span><span class="token punctuation">,</span><span class="token string-property property">"prompt_tokens"</span><span class="token operator">:</span> <span class="token number">31</span><span class="token punctuation">,</span><span class="token string-property property">"total_tokens"</span><span class="token operator">:</span> <span class="token number">180</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> </code></pre> <p>当然prompt传的越多,网络消耗,以及💰消耗都越多,那怎么减少上下文传递呢?</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/2e8a553ce0aa4b8b8de96f24f9bec5e1.png" alt="有什么办法减少上下文传递"></p> <p>具体实现,感兴趣的也可以再问问ChatGPT。</p> <h3> <a id="Chat_255"></a>Chat</h3> <ul><li>Chat:https://platform.openai.com/docs/api-reference/chat</li></ul> <p>2023 年 3 月 1 日新增接口,使用的是最新的 GPT-3.5 模型和 GPT-4 模型。与 <code>Completion</code> API 不同,<code>ChatCompletion</code> API 更适合用于实时的、多轮的对话。</p> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span>chat <span class="token operator">=</span> openai<span class="token punctuation">.</span>ChatCompletion<span class="token punctuation">.</span>create<span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"gpt-3.5-turbo"</span><span class="token punctuation">,</span>messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"system"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"You are a assistant of shein,you can help user to resolve problems."</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"How can I refund my order?"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> </code></pre> <ul><li>响应:</li></ul> <pre><code class="prism language-json"><span class="token punctuation">{<!-- --></span><span class="token string-property property">"choices"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"finish_reason"</span><span class="token operator">:</span> <span class="token string">"stop"</span><span class="token punctuation">,</span><span class="token string-property property">"index"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token string-property property">"message"</span><span class="token operator">:</span> <span class="token punctuation">{<!-- --></span><span class="token string-property property">"content"</span><span class="token operator">:</span> <span class="token string">"To initiate a refund for your Shein order, you can follow these steps:\n\n1. Log in to your Shein account.\n2. Go to your order history and find the order you want to refund.\n3. Click on the \"Return\" button and select the item(s) you want to return.\n4. Choose the reason for the return and add any pictures or comments if necessary.\n5. Submit the return request and wait for Shein to send you a confirmation email.\n6. Pack and send your items back to Shein according to the instructions provided in the email.\n7. Once Shein receives and processes your return, your refund will be issued to your original payment method within 7-14 business days.\n\nNote: Make sure to check Shein's return policy before initiating a refund to ensure that your item is eligible for return."</span><span class="token punctuation">,</span><span class="token string-property property">"role"</span><span class="token operator">:</span> <span class="token string">"assistant"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string-property property">"created"</span><span class="token operator">:</span> <span class="token number">1679876980</span><span class="token punctuation">,</span><span class="token string-property property">"id"</span><span class="token operator">:</span> <span class="token string">"chatcmpl-6yV1wSwqM09Aqb6QvFENUvuvC4ehW"</span><span class="token punctuation">,</span><span class="token string-property property">"model"</span><span class="token operator">:</span> <span class="token string">"gpt-3.5-turbo-0301"</span><span class="token punctuation">,</span><span class="token string-property property">"object"</span><span class="token operator">:</span> <span class="token string">"chat.completion"</span><span class="token punctuation">,</span><span class="token string-property property">"usage"</span><span class="token operator">:</span> <span class="token punctuation">{<!-- --></span><span class="token string-property property">"completion_tokens"</span><span class="token operator">:</span> <span class="token number">171</span><span class="token punctuation">,</span><span class="token string-property property">"prompt_tokens"</span><span class="token operator">:</span> <span class="token number">36</span><span class="token punctuation">,</span><span class="token string-property property">"total_tokens"</span><span class="token operator">:</span> <span class="token number">207</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> </code></pre> <p><code>ChatCompletion</code>支持三种角色:</p> <ul> <li>user:用户,提出问题或者输入内容的人</li> <li>assint:聊天机器人,基于GTP3等模型的的机器人,可以回答用户的问题或者会话。</li> <li>system:应用程序,负责处理用户输入,然后传递给聊天机器人,或者接收聊天机器人的回答,展现给用户。应用程序也可以负责处理其他任务,例如将用户输入保存到数据库中。</li> </ul> <p>实际应用中,可以通过system指定指定 chatGPT 的功能定位,在 assistant 中传 chatGPT 回答的消息就实现了上下文。</p> <h3> <a id="Images_309"></a>Images</h3> <p>OpenAI的图片接口,支持通过描述生图,和以图生图:</p> <ul><li>Images(通过描述生成图):https://platform.openai.com/docs/api-reference/images</li></ul> <table> <thead><tr> <th>参数</th> <th>说明</th> </tr></thead> <tbody> <tr> <td>prompt</td> <td>图片描述,必填</td> </tr> <tr> <td>n</td> <td>需要生成的图片数量,取值 1-10,默认值 1</td> </tr> <tr> <td>size</td> <td>图片大小,取值只能为 256x256、512x512、1024x1024,默认值 1024x1024</td> </tr> <tr> <td>response_format</td> <td>返回图片数据类型,取值 url 或 b64_json,默认值 url</td> </tr> </tbody> </table> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span>response<span class="token operator">=</span>openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create<span class="token punctuation">(</span>prompt<span class="token operator">=</span><span class="token string">"A beautiful young and generous Chinese girl"</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span></code></pre> <ul><li>响应</li></ul> <pre><code class="prism language-python"><span class="token punctuation">{<!-- --></span><span class="token string">"created"</span><span class="token punctuation">:</span> <span class="token number">1679877722</span><span class="token punctuation">,</span><span class="token string">"data"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string">"url"</span><span class="token punctuation">:</span> <span class="token string">"https://oaidalleapiprodscus.blob.core.windows.net/private/org-ZPdLSXE667sQLqeMtNJSlY4u/user-CdRYPYeBIHN8eGFIq87eroQN/img-sSV6B5nOzOpRre41J73guwu6.png?st=2023-03-26T23%3A42%3A02Z&se=2023-03-27T01%3A42%3A02Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-03-26T20%3A06%3A23Z&ske=2023-03-27T20%3A06%3A23Z&sks=b&skv=2021-08-06&sig=EphBdwHcs4p5x3F5su8r9NvG8mGRKx4cOkCjbBWekGA%3D"</span><span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token punctuation">}</span> </code></pre> <p>我们看看生成的图怎么样?</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/397a4a0f7a894c58bbd85f557c97be57.png" alt="生成的图片"></p> <p>我滴妈,哕……</p> <ul><li>Create image variationBeta(以图生图):https://platform.openai.com/docs/api-reference/images/create-variation</li></ul> <p>从某竞品网站下了一张好看的模特图:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/abcc011327994accb4346a2655f9c175.png" alt="模特"></p> <p>图片需要裁剪和压缩一下,图片长宽需要1:1,是png文件,而且小于4M。</p> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span> response<span class="token operator">=</span>openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_variation<span class="token punctuation">(</span>image<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"apis/model.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span> </code></pre> <ul><li>响应</li></ul> <pre><code class="prism language-json"><span class="token punctuation">{<!-- --></span><span class="token string-property property">"created"</span><span class="token operator">:</span> <span class="token number">1679878760</span><span class="token punctuation">,</span><span class="token string-property property">"data"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"url"</span><span class="token operator">:</span> <span class="token string">"https://oaidalleapiprodscus.blob.core.windows.net/private/org-ZPdLSXE667sQLqeMtNJSlY4u/user-CdRYPYeBIHN8eGFIq87eroQN/img-uZUO48KfwJpEuCf6Na7fIFG1.png?st=2023-03-26T23%3A59%3A20Z&se=2023-03-27T01%3A59%3A20Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-03-26T14%3A21%3A52Z&ske=2023-03-27T14%3A21%3A52Z&sks=b&skv=2021-08-06&sig=YR39Y3sxD7kqnoJcVqEqKxc9bBi//nsn3X0SOtX/wI4%3D"</span><span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token punctuation">}</span> </code></pre> <p>看看生成的图片,</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/a0d2c508e587479386801f309c12cec6.png" alt="在这里插入图片描述"></p> <p>啊这。。。</p> <p>当然chatGPT也支持对现有图的修改。</p> <ul><li>Create image edit(以图修图可修改):https://platform.openai.com/docs/api-reference/images/create-edit)</li></ul> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span> response<span class="token operator">=</span>openai<span class="token punctuation">.</span>Image<span class="token punctuation">.</span>create_edit<span class="token punctuation">(</span>image<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"apis/model.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>mask<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"apis/model.png"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>prompt<span class="token operator">=</span><span class="token string">"Generate a female model who is equally beautiful, hotter, but with different looks and clothes"</span><span class="token punctuation">,</span>n<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token string">"1024x1024"</span> <span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span> </code></pre> <ul><li>响应:</li></ul> <pre><code class="prism language-json"><span class="token punctuation">{<!-- --></span><span class="token string-property property">"created"</span><span class="token operator">:</span> <span class="token number">1680363981</span><span class="token punctuation">,</span><span class="token string-property property">"data"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string-property property">"url"</span><span class="token operator">:</span> <span class="token string">"https://oaidalleapiprodscus.blob.core.windows.net/private/org-ZPdLSXE667sQLqeMtNJSlY4u/user-CdRYPYeBIHN8eGFIq87eroQN/img-vYOBGDlf5su7sMfQSITKna8w.png?st=2023-04-01T14%3A46%3A21Z&se=2023-04-01T16%3A46%3A21Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-04-01T08%3A48%3A23Z&ske=2023-04-02T08%3A48%3A23Z&sks=b&skv=2021-08-06&sig=ajPilUO%2BzBNQDtFDHrEyK2/njZQrZsaTyV7wjQRFRJM%3D"</span><span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token punctuation">}</span> </code></pre> <p>虽然,但是,这个api我尝试了好几个prompt,给我吐回的都是原图。</p> <p>整体上来讲,chatGPT的Images API还是没有文本问答那么好用,而且比较贵,尺寸 1024x1024 像素的图一张收费 0.02 美元,大约人民币 0.13 元左右。</p> <h3> <a id="Audio_432"></a>Audio</h3> <p>chatGPT还支持语音识别</p> <ul><li>Audio:https://platform.openai.com/docs/api-reference/audio</li></ul> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> os<span class="token punctuation">.</span>getenv<span class="token punctuation">(</span><span class="token string">"OPENAI_API_KEY"</span><span class="token punctuation">)</span> audio_file <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"apis/audio.mp3"</span><span class="token punctuation">,</span> <span class="token string">"rb"</span><span class="token punctuation">)</span> transcript <span class="token operator">=</span> openai<span class="token punctuation">.</span>Audio<span class="token punctuation">.</span>transcribe<span class="token punctuation">(</span><span class="token string">"whisper-1"</span><span class="token punctuation">,</span> audio_file<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>transcript<span class="token punctuation">)</span> </code></pre> <p>我一开始用了一段中英混杂的语言,结果响应:</p> <pre><code class="prism language-python"><span class="token punctuation">{<!-- --></span><span class="token string">"text"</span><span class="token punctuation">:</span> <span class="token string">"Java\u662f\u8fd9\u4e2a\u4e16\u754c\u4e0a\u6700\u597d\u7684\u8bed\u8a00 \u4f46\u662f\u6211\u7528Python"</span> <span class="token punctuation">}</span> </code></pre> <p>乱码了,赶紧问下chatGPT:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/4c8414b4bdf94441ad768f09159a5943.png" alt="在这里插入图片描述"></p> <p>嗯,这次翻车了,连续尝试了五六个回答,都没能解决乱码的问题,看样子OpenAI确对中文语言输入不太友好。</p> <p>改成英文语音输入的结果:</p> <pre><code class="prism language-json"><span class="token punctuation">{<!-- --></span><span class="token string-property property">"text"</span><span class="token operator">:</span> <span class="token string">"Jawa is the best language in the world, but I use Python."</span> <span class="token punctuation">}</span> </code></pre> <p>英文的语音识别还是比较准确的,当然,语音识别早就不是什么问题了。</p> <p>总体上来看,OpenAI的文本处理能力还是非常强大的,图像处理还有待提升,不过以OpenAI的迭代速度,以及全世界用户的不断”投喂“,相信新模型”远超Midjourney“绝对不是一句空话。</p> <h1> <a id="_476"></a>一些思考</h1> <h2> <a id="OpenAI_478"></a>可以用OpenAI干什么?</h2> <p>怎么应用OpenAI,或者说怎么应用AI呢?</p> <p>先从工作看起吧,我是做跨境电商业务的,我问了一下ChatGPT,看看它的回答:</p> <blockquote> <p>跨境电商可以使用 OpenAI 来帮助改善客户体验、提高销售量和降低成本。以下是一些可能的应用:</p> <ol> <li>在线客服:使用 OpenAI 的聊天机器人来提供 24/7 的在线客服服务,帮助解决客户的问题和疑虑,提高客户满意度和忠诚度。</li> <li>智能推荐:使用 OpenAI 的推荐系统来根据客户的历史购买记录和行为数据,推荐最符合客户需求的商品,提高销售量和客户满意度。</li> <li>自然语言处理:使用 OpenAI 的自然语言处理技术来分析客户的评论和评价,了解客户对商品和服务的看法,帮助改进产品和服务。</li> <li>欺诈检测:使用 OpenAI 的机器学习和深度学习技术来检测可能的欺诈交易和订单,提高安全性和信誉度。</li> <li>语言翻译:使用 OpenAI 的自然语言处理和机器翻译技术来进行跨语言交流,帮助拓展跨境电商业务。</li> </ol> <p>总之,OpenAI 可以为跨境电商提供智能化的解决方案,从而提高客户体验、销售量和利润。</p> </blockquote> <p>可以看一下,这里面包含客服、推荐、内容、翻译、风控等等……</p> <p>有一些正在被我的同事实现,有些甚至已经投产。</p> <p>那么我还是一个程序员,具体点Java程序员,我可以怎么利用OpenAI呢?</p> <blockquote> <p>作为一个Java程序员,你可以利用OpenAI来提高工作效率,以下是一些建议:</p> <ol> <li>自然语言处理:OpenAI提供了自然语言处理API,你可以使用它来处理文本数据。例如,你可以使用OpenAI的API来创建自动化回答常见问题的程序。</li> <li>代码自动完成:OpenAI的代码自动完成功能可以帮助你快速编写代码。你可以使用Java SDK来调用OpenAI的代码自动完成API,从而提高编程效率。</li> <li>数据分析:OpenAI的语言模型可以分析大量的数据,你可以使用它来进行数据分析。例如,你可以使用OpenAI的API来分析用户评论数据,从而了解用户的反馈。</li> <li>智能助手:你可以使用OpenAI的语言模型来创建智能助手,帮助你完成一些重复性的任务。例如,你可以使用OpenAI的API来创建一个智能助手,自动完成一些常见的数据处理任务。</li> </ol> <p>总的来说,OpenAI提供了很多的API和工具,可以帮助Java程序员提高工作效率。你可以根据自己的需求选择合适的API,并利用Java SDK来实现对OpenAI的调用。</p> </blockquote> <p>这里面有一些能力,相信很多朋友更加熟悉,现在日常开发,可以不用搜索引擎,而是直接向ChatGPT提问。</p> <p>问答助手、代码生成插件、SQL助手……目前,这些也都有同事进行了实现。</p> <h3> <a id="_513"></a>大概都是怎么实现的呢?</h3> <p>其实这些应用,这些应用的关键,就是在于正确地向ChatGPT提问,有这么一条“公式”:<strong>定义角色+明确任务+限制输出 = better</strong> <strong>prompt</strong></p> <p>例如,我现在希望OpenAI作为一个商品评论审核员:</p> <ul><li>你是一个跨境电商的商品评论审核员,你需要对用户的评论进行审核,你需要识别不同语言的评论,用户评论中不能出现暴力、色情、政治等负面因素,你需要对评论进行打分,60分以上是合格评论,60分以下是不合格评论,用户评论越积极分数越高,你还要给出给用户打分的原因,你的回答只能用json格式{“score”:30,“reason”:“Violence”},score表示评分,reason表示原因,除此之外,你不应该回答任何内容</li></ul> <p>然后再输入一个评论:</p> <ul><li>这个商品是在太垃圾了</li></ul> <p>它的响应:</p> <pre><code class="prism language-java"><span class="token punctuation">{<!-- --></span> <span class="token string">"score"</span><span class="token operator">:</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token string">"reason"</span><span class="token operator">:</span> <span class="token string">"Negative sentiment"</span> <span class="token punctuation">}</span> </code></pre> <p>开发的时候,就是要先把上下文给传过去:</p> <pre><code class="prism language-python">chat <span class="token operator">=</span> openai<span class="token punctuation">.</span>ChatCompletion<span class="token punctuation">.</span>create<span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"gpt-3.5-turbo-0301"</span><span class="token punctuation">,</span>messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"system"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"你是一个跨境电商的商品评论审核员,你需要对用户的评论进行审核,你需要识别不同语言的评论,用户评论中不能出现暴力、色情、政治等负面因素,你需要对评论进行打分,60分以上是合格评论,60分以下是不合格评论,用户评论越积极分数越高,你还要给出给用户打分的原因,你的回答只能用json格式{\"score\":30,\"reason\":\"Violence\"},score表示评分,reason表示原因,除此之外,你不应该回答任何内容"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"system"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"OK"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{<!-- --></span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"什么烂东西,浪费钱"</span><span class="token punctuation">}</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> </code></pre> <p>是不是还挺简单的。</p> <p>除此之外,有条件的还可以对OpenAI的模型进行微调,提升模型的性能。</p> <h2> <a id="_551"></a>我们会被淘汰吗?</h2> <p>坦白讲,大家都拥抱OpenAI很久,我实在落伍了。</p> <p>现在对于OpenAI的应用,其实主要还是集中在AIGC方面,我主要搞的是支付开发,是交易类的,想了很久也没想出什么应用。</p> <p>随后问了ChatGPT一个问题:“接入Alipay支付”:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/052522253aba44ccba1172fa18222dc9.png" alt="接入Alipay支付"></p> <p>看完,我直接沉默,代码虽然有些不太合理的地方,但是估计再调一调就能直接用了。</p> <p>这就引入了一个问题,AIGC时代来临,程序员会不会被淘汰?——很有可能。</p> <p>我们今天为ChatGPT欢呼,也许明天就会被它给驱逐。</p> <p>36氪给李彦宏做了一个专访[3],我们看看其中的一段:</p> <blockquote> <p><strong>36氪:今天无数人在提问:AI会不会让打工人没有工作?OpenAI的创始人Sam Altman说,大量的人一定会失业,所以OpenAI会按需收费,给没有工作的人补贴。这其实是有点悲凉的,他自己说也有点可怕,你觉得这个事情会发生吗?这个问题你怎么看。</strong></p> <p>**李彦宏:**今天没有马车夫这样的工作,因为出现了汽车。但是今天世界上存在的工作机会跟100年前相比,不仅是多了,而且多了很多倍。</p> <p>我没有那么悲观,我是乐观的。不管有多少工作被替代,这只是整个图景的一部分。另外一部分是,存在我们现在甚至无法想象的更多新机会。我做一个大胆预测,十年以后,全世界有50%工作会是提示词工程(prompt engineering),不会写提示词(prompt)的人会被淘汰。</p> </blockquote> <p>我甚至在想一个问题——AI会不会淘汰AI工程师呢?</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/09679d9d41da425a9bf43d8b2fd91805.png" alt="AI会不会淘汰AI工程师呢"></p> <p>所以,试着成为一名AI工程师?</p> <br> <br> <hr> <p><big><b>参考:</b></big></p> <p>[1]. https://platform.openai.com/</p> <p>[2].https://www.zhihu.com/question/575983484/answer/2895862383</p> <p>[3].https://36kr.com/p/2184414265262467</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-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div> </article>
关于ChatGPT的一些随笔
作者
sockstack
许可协议
CC BY 4.0
发布于
2024-02-21
修改于
2025-04-01
上一篇:软件:常用 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
前端