Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
ChatGPT为什么会一本正经胡说八道?我们如何改进它?| 文内附有代码
sockstack
/
218
/
2023-12-30 12:02:37
<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-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="ChatGPT__0"></a>ChatGPT为什么会一本正经胡说八道?我们如何改进它?| 文内附有代码</h1> <blockquote> <p> 众所周知,在OpenAI平台上的<code>ChatGPT</code>模型目前有两大痛点:1. 它所学习的数据资料都是截止到2021年为止的,因此无法给出2022年之后的发生的事情。2. 有些时候会出现一些常识性的错误,也就是它会一本正经的胡说八道,比如问它一些历史、数学问题,它可能就会回答出<strong>像那么回事儿的错误答案</strong>。<br> 究其原因,主要是ChatGPT是“生成式回复”。它事先学习了大量的人类的知识以及人类对它的引导和奖励的策略。它本质上依托于一个参数量巨大的神经网络模型,其训练过程是基于它的语料库进行的,而<strong>这些训练数据本身良莠不齐,并不全都是优质文本,因此有可能出现事实性错误问题。</strong> <code>ChatGPT</code>给出的答案只是基于其理解生成的最佳结果,然而对于某些训练的知识盲区问题,它可能根据字面意思进行推理,从而出现了“一本正经胡说八道”的现象。<br> 所以为了解决上面的两个问题,我们可以利用<strong>搜索引擎</strong>向<code>ChatGPT</code>提供准确、及时的辅助信息。<font color="magenta"><strong>总而言之,整体方案就是把在搜索引擎上爬取的问题相关资料丢给CharGPT,让它帮我们整合并输出我们想要的答案。</strong></font></p> </blockquote> <p></p> <div class="toc"> <h3>文章目录</h3> <ul> <li>ChatGPT为什么会一本正经胡说八道?我们如何改进它?| 文内附有代码</li> <li> <ul> <li>1.快速通道</li> <li>2. ChatGPT历史的局限性与事实性错误</li> <li>3. 效果演示</li> <li>5. 代码示例</li> <li>4. 参考资料</li> </ul> </li> </ul> </div> <p></p> <h2> <a id="1_8"></a>1.快速通道</h2> <p> 要是有代码熟练度的读者想直接调试完整代码,可以到我的这个repository直接下载完整可运行的,并且由于CSDN社区规定的原因,完整内容无法写在上面,所以博主将代码和博客全都放在这个repo上了<font color="magenta"><strong>求Star!</strong></font>:https://github.com/lsl1229840757/chatbot。</p> <h2> <a id="2_ChatGPT_11"></a>2. ChatGPT历史的局限性与事实性错误</h2> <p> 在摘要之中博主已经详细分析了ChatGPT(1)无法回答2022年之后发生的事情;(2)会犯一些常识性的错误,这两大问题。下面让我们举个栗子来看看:</p> <p> 当我们问他2022世界杯冠军队伍是哪支的时候,它说它无法预测。<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/479ed6a733ad4666b29fe1fbbf8f02b5.png" alt="在这里插入图片描述"><br> 当我们问他勾三股四弦五是什么的时候,它会一本正经的告诉我们这是中国古代乐器琴的调弦方法:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/18a545e811e34846bc9b36883754dc02.png" alt="在这里插入图片描述"></p> <h2> <a id="3__19"></a>3. 效果演示</h2> <p> 没有对比就没有伤害,咱们话不多说,直接上效果展示:</p> <ul> <li>询问2022世界杯冠军队伍<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/41e708675f404477b976c1237e020947.png" alt="在这里插入图片描述"> </li> <li>询问勾股定理<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/b22bd9fd65d9473195fbb1eb3c93587f.png" alt="在这里插入图片描述"> </li> <li>查看近期的新闻<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8f29fd74f78b4bb1ba3d81229f8edff7.png" alt="在这里插入图片描述"><br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3a63a3f50bed4093922c204bbef030b7.png" alt="在这里插入图片描述"> </li> </ul> <h2> <a id="5__29"></a>5. 代码示例</h2> <pre><code class="prism language-python"><span class="token keyword">import</span> warnings<span class="token keyword">import</span> openai <span class="token keyword">from</span> selenium <span class="token keyword">import</span> webdriver <span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>common<span class="token punctuation">.</span>by <span class="token keyword">import</span> Bywarnings<span class="token punctuation">.</span>filterwarnings<span class="token punctuation">(</span><span class="token string">'ignore'</span><span class="token punctuation">)</span><span class="token keyword">class</span> <span class="token class-name">ChatGPT</span><span class="token punctuation">(</span><span class="token builtin">object</span><span class="token punctuation">)</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> api_key<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token boolean">None</span><span class="token punctuation">:</span>self<span class="token punctuation">.</span>_api_key <span class="token operator">=</span> <span class="token boolean">None</span>self<span class="token punctuation">.</span>api_key <span class="token operator">=</span> api_keyoptions <span class="token operator">=</span> webdriver<span class="token punctuation">.</span>ChromeOptions<span class="token punctuation">(</span><span class="token punctuation">)</span>options<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'headless'</span><span class="token punctuation">)</span> <span class="token comment"># 设置不弹出浏览器</span>options<span class="token punctuation">.</span>add_experimental_option<span class="token punctuation">(</span><span class="token string">'excludeSwitches'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'enable-logging'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 控制台不输出</span>self<span class="token punctuation">.</span>browser <span class="token operator">=</span> webdriver<span class="token punctuation">.</span>Chrome<span class="token punctuation">(</span>options<span class="token operator">=</span>options<span class="token punctuation">)</span><span class="token decorator annotation punctuation">@property</span><span class="token keyword">def</span> <span class="token function">api_key</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">return</span> self<span class="token punctuation">.</span>_api_key<span class="token decorator annotation punctuation">@api_key<span class="token punctuation">.</span>setter</span><span class="token keyword">def</span> <span class="token function">api_key</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> api_key<span class="token punctuation">)</span><span class="token punctuation">:</span>self<span class="token punctuation">.</span>_api_key <span class="token operator">=</span> api_key<span class="token comment"># Load your API key</span>openai<span class="token punctuation">.</span>api_key <span class="token operator">=</span> api_key<span class="token keyword">def</span> <span class="token function">_grabbing_data</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> wd<span class="token punctuation">)</span><span class="token punctuation">:</span>url <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f'https://www.baidu.com/s?wd=</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>wd<span class="token punctuation">}</span></span><span class="token string">'</span></span>self<span class="token punctuation">.</span>browser<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">)</span>self<span class="token punctuation">.</span>browser<span class="token punctuation">.</span>execute_script<span class="token punctuation">(</span><span class="token string">'window.scrollTo(0, document.body.scrollHeight)'</span><span class="token punctuation">)</span>data <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>results <span class="token operator">=</span> self<span class="token punctuation">.</span>browser<span class="token punctuation">.</span>find_elements<span class="token punctuation">(</span>By<span class="token punctuation">.</span>CLASS_NAME<span class="token punctuation">,</span> <span class="token string">'c-border'</span><span class="token punctuation">)</span> \<span class="token operator">+</span> self<span class="token punctuation">.</span>browser<span class="token punctuation">.</span>find_elements<span class="token punctuation">(</span>By<span class="token punctuation">.</span>CLASS_NAME<span class="token punctuation">,</span> <span class="token string">'result-op'</span><span class="token punctuation">)</span> \<span class="token operator">+</span> self<span class="token punctuation">.</span>browser<span class="token punctuation">.</span>find_elements<span class="token punctuation">(</span>By<span class="token punctuation">.</span>CLASS_NAME<span class="token punctuation">,</span> <span class="token string">'result'</span><span class="token punctuation">)</span><span class="token keyword">for</span> result <span class="token keyword">in</span> results<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span>a_link <span class="token operator">=</span> result<span class="token punctuation">.</span>find_element<span class="token punctuation">(</span>By<span class="token punctuation">.</span>TAG_NAME<span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">)</span>href <span class="token operator">=</span> a_link<span class="token punctuation">.</span>get_attribute<span class="token punctuation">(</span><span class="token string">'href'</span><span class="token punctuation">)</span>data<span class="token punctuation">.</span>append<span class="token punctuation">(</span>result<span class="token punctuation">.</span>text <span class="token operator">+</span> <span class="token string-interpolation"><span class="token string">f'来源:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>href<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span><span class="token keyword">return</span> data<span class="token keyword">def</span> <span class="token function">_format_prompt</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">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'正在百度中搜索</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>prompt<span class="token punctuation">}</span></span><span class="token string">相关资料...'</span></span><span class="token punctuation">)</span>data <span class="token operator">=</span> self<span class="token punctuation">.</span>_grabbing_data<span class="token punctuation">(</span>prompt<span class="token punctuation">)</span>data <span class="token operator">=</span> <span class="token string">'\n'</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>data<span class="token punctuation">)</span>prompt <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f'请根据以下辅助信息回答问题: </span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>data<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token format-spec">1000]</span><span class="token punctuation">}</span></span><span class="token string">\n 请问:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>prompt<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token keyword">return</span> prompt<span class="token keyword">def</span> <span class="token function">get_answer</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> prompt<span class="token punctuation">)</span><span class="token punctuation">:</span>prompt <span class="token operator">=</span> self<span class="token punctuation">.</span>_format_prompt<span class="token punctuation">(</span>prompt<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>prompt<span class="token punctuation">,</span>temperature<span class="token operator">=</span><span class="token number">0.6</span><span class="token punctuation">,</span>max_tokens<span class="token operator">=</span><span class="token number">2048</span><span class="token punctuation">)</span><span class="token keyword">return</span> response<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">'text'</span><span class="token punctuation">]</span><span class="token keyword">def</span> <span class="token function">chat</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>flag <span class="token operator">=</span> <span class="token boolean">True</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'欢迎使用百度版ChatGPT, 我使用了百度作为资料搜索支撑引擎, 如果您想退出请输入exit!'</span><span class="token punctuation">)</span><span class="token keyword">while</span> flag<span class="token punctuation">:</span>prompt <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> prompt <span class="token operator">!=</span> <span class="token string">'exit'</span><span class="token punctuation">:</span><span class="token keyword">print</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>get_answer<span class="token punctuation">(</span>prompt<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token keyword">else</span><span class="token punctuation">:</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'再见!'</span><span class="token punctuation">)</span>flag <span class="token operator">=</span> <span class="token boolean">False</span> </code></pre> <h2> <a id="4__108"></a>4. 参考资料</h2> <ul> <li>https://cloud.tencent.com/developer/article/2190154</li> <li>https://platform.openai.com/account/api-keys</li> <li>https://platform.openai.com/docs/introduction/overview</li> </ul> </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>
ChatGPT为什么会一本正经胡说八道?我们如何改进它?| 文内附有代码
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-12-30
修改于
2024-12-26
上一篇:软件:常用 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
前端