Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
搜索
关闭
Previous
Previous
Next
Next
chatGPT API调用指南,GPT3.5 turbo API,上下文携带技巧,python函数封装
sockstack
/
196
/
2023-11-17 12:00:29
<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-kimbie-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><h1> <a id="_0"></a>概要</h1> <p>chatGPT是openAI的一款语言类人工智能聊天产品,除了在官网直接使用外,我们还可以通过发起http请求调用官方的gpt3.5turbo API来构建自己的应用产品。<br> <em>内容概述:</em><br> <strong>1本篇博客使用python语言演示了如何简单调用chatGPT接口<br> 2简单描述了chatGPT接口可选的一共12个参数<br> 3从实践案例角度对于API进行函数式封装</strong></p> <h1> <a id="gpt35turbo_6"></a>gpt-3.5-turbo官方文档</h1> <p>https://platform.openai.com/docs/introduction/overview<br> 官方文档链接点击跳转<br> 英文好、有时间可以直接研究官方文档。</p> <h1> <a id="_10"></a>简单例子</h1> <p>首先需要安装python包openai</p> <pre><code class="prism language-shell">pip <span class="token function">install</span> openai </code></pre> <p>pip list查看版本号:openai 0.27.2<br> 下面的例子向openai的chatgpt API发送消息“你好”,然后输出应答。</p> <p>图一:获取api_key</p> <pre><code class="prism language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> openai openai_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> openai<span class="token punctuation">.</span>api_key<span class="token operator">=</span>openai_api_key <span class="token keyword">def</span> <span class="token function">example1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>response <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">"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><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>example1<span class="token punctuation">(</span><span class="token punctuation">)</span> </code></pre> <p>openai.ChatCompletion.create中填写了两个必要参数,模型名、消息。<br> openai_api_key需要在API官网获取,链接在上面已经贴过了。 然后设置为环境变量,或者直接粘贴字符串在这里也可以。</p> <h1> <a id="_38"></a>详细参数示例</h1> <pre><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">example2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>response <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">"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>temperature<span class="token operator">=</span><span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token comment"># 0-2之间,越大越随机,越小越确定</span>top_p<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token comment">#随机取前多少概率的token,0.1意味着取前10%,越小越确定。top_p temperature两个参数推荐只使用一个</span>n<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token comment">#生成几个回答,默认是1个,我这里让它生成2个</span>stream<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span><span class="token comment">#是否流式获得结果,流式就是chatgpt官网那种,结果是一点一点蹦出来的,用于长句子先得到部分结果</span>stop<span class="token operator">=</span><span class="token string">"我"</span><span class="token punctuation">,</span><span class="token comment">#停止词,生成出来“我”就停止生成</span>max_tokens<span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token comment">#最多生成的token数量</span>presence_penalty<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token comment">#(-2.0,2.0) 越大模型就趋向于生成更新的话题,惩罚已经出现过的文本</span>frequency_penalty<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token comment">#(-2.0,2.0) 惩罚出现频率高的文本</span><span class="token comment">#logit_bias=None,#设置token的先验偏置</span>user<span class="token operator">=</span><span class="token string">"会写代码的孙悟空"</span><span class="token comment">#一个表示您的终端用户的唯一标识符,可帮助OpenAI监控和检测滥用行为</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token punctuation">)</span> </code></pre> <pre><code>output: 你好! 你好!有什么 </code></pre> <p>temperature参数示意图<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/d3077657c2a34c75886bf32e5ef980ae.png" alt="在这里插入图片描述"><br> 图:temperature参数改变概率分布,左较小temperature,右较大temperature<br> top_p=1 就是以图中的概率分布取样本,注意低柱子只是取到的概率小<br> top_p=0.2 取前20%的样本,就只有高柱子这一种情况了<br> top_p=0.4 取前40%的样本,从第一高与第二高中取。</p> <h1> <a id="API_73"></a>API封装</h1> <p>说明:从实践案例角度对于API进行函数式封装,方便开发。<br> response是python字典格式:</p> <pre><code class="prism language-txt">{'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve','object': 'chat.completion','created': 1677649420,'model': 'gpt-3.5-turbo','usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},'choices': [{'message': {'role': 'assistant','content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},'finish_reason': 'stop','index': 0}] } </code></pre> <p>字段很多,只使用最关键的content,也就是回复文本内容。</p> <h2> <a id="API_95"></a>一次对话API封装</h2> <p>一问一答,无上下文</p> <pre><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">chat_once</span><span class="token punctuation">(</span>prompt<span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">try</span><span class="token punctuation">:</span>response <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">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> prompt<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">return</span> response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token keyword">except</span><span class="token punctuation">:</span><span class="token keyword">return</span> <span class="token string">""</span> <span class="token keyword">def</span> <span class="token function">test_chat_once</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>res<span class="token operator">=</span>chat_once<span class="token punctuation">(</span><span class="token string">"你好"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>test_chat_once<span class="token punctuation">(</span><span class="token punctuation">)</span> </code></pre> <h2> <a id="_115"></a>一次对话+角色扮演</h2> <pre><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">chat_once_with_sb</span><span class="token punctuation">(</span>prompt<span class="token punctuation">,</span>sb<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment">#sb:some body 不是骂人</span><span class="token keyword">try</span><span class="token punctuation">:</span>response <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> sb<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> prompt<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">return</span> response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span><span class="token keyword">except</span><span class="token punctuation">:</span><span class="token keyword">return</span> <span class="token string">""</span> <span class="token keyword">def</span> <span class="token function">test_chat_once_with_sb</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>prompt<span class="token operator">=</span><span class="token string">"请问你擅长什么?"</span>sb<span class="token operator">=</span><span class="token string">"医生"</span>res<span class="token operator">=</span>chat_once_with_sb<span class="token punctuation">(</span>prompt<span class="token punctuation">,</span>sb<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>test_chat_once_with_sb<span class="token punctuation">(</span><span class="token punctuation">)</span> </code></pre> <p><strong>output:</strong></p> <pre><code class="prism language-text">作为一个 AI 医生,我可以提供广泛的医疗领域的知识和帮助。我可以帮你获取医疗信息、提供急救指导并回答健康问题。 但是,我不能进行具体治疗和诊断,如果您有任何身体不适或医务问题,建议立即咨询专业医生或上医院检查。 </code></pre> <h2> <a id="_142"></a>连续对话保存所有上下文</h2> <pre><code class="prism language-python"><span class="token keyword">class</span> <span class="token class-name">ChatGPT</span><span class="token punctuation">:</span><span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>sb<span class="token operator">=</span><span class="token string">"You are a helpful assistant."</span><span class="token punctuation">)</span><span class="token punctuation">:</span>self<span class="token punctuation">.</span>sb<span class="token operator">=</span>sbself<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>sb<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token keyword">def</span> <span class="token function">send</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>prompt<span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">try</span><span class="token punctuation">:</span>self<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>append<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>prompt<span class="token punctuation">}</span><span class="token punctuation">)</span>response<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>self<span class="token punctuation">.</span>messages<span class="token punctuation">)</span>response_txt<span class="token operator">=</span>response<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"choices"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"message"</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"content"</span><span class="token punctuation">]</span>self<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>append<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">"assistant"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> response_txt<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">return</span> response_txt<span class="token keyword">except</span><span class="token punctuation">:</span><span class="token keyword">return</span> <span class="token string">""</span> <span class="token keyword">def</span> <span class="token function">test_ChatGPT</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>chatgpt<span class="token operator">=</span>ChatGPT<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>text<span class="token operator">=</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">if</span><span class="token punctuation">(</span>text<span class="token operator">==</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">break</span>res<span class="token operator">=</span>chatgpt<span class="token punctuation">.</span>send<span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>test_ChatGPT<span class="token punctuation">(</span><span class="token punctuation">)</span> </code></pre> <p><strong>output:</strong><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1874e68fceb847ce88328c963d1e1d81.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-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
chatGPT API调用指南,GPT3.5 turbo API,上下文携带技巧,python函数封装
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-17
修改于
2024-12-02
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:分享github上比较热门的ChatGPT项目,值得收藏
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
3
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端