Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
如何实现Chatgpt写文章(附chatgpt3.5免费接口)
sockstack
/
248
/
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-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><p>申明:本次只是说一下实现思路,官方的接口以及如何实现方式,本文没有提及,这次只是一个思路,若想代替人工完成质量还差的很远,请审核大大放行</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/af53cabd3c8b40b7b46c7a9ae162a17e.png#pic_center" alt="在这里插入图片描述"></p> <p><code>今天再次优化了代码,修复了一些bug</code>,考虑到不完善且文章质量不是很高的情况(<code>连个图文都没有</code>),打算将代码分离开来,一起使用可能会对后面维护开发产生不必要的麻烦,<mark>欢迎大佬的关注,我会自动回关!</mark>,<code>等到真正实现自动化,会打包成exe分享</code></p> <p>实现效果看我的博客:Python3 循环语句</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/55c550795afa47d58ebdd608fc27a618.png" alt="在这里插入图片描述"></p> <p>缺点:</p> <ol> <li><code>gpt3.5没有图文并茂</code></li> <li><code>"复制代码"转换过程中处理成了文字</code></li> <li>未知原因requests返回的数据和真实的数据有出入</li> </ol> <h1> <a id="_23"></a>已完成的工作</h1> <p>20230405</p> <ul> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 支持点赞、收藏回访(被动模式)</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 关注回访(需要发表过博客)</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 评论回访</li> </ul> <p>20230406优化</p> <ul> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 私信检测到群发消息自动三连/满足模板设定给予三连触发</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 优化检测模式,防止出现多触发情况</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 为了防止操作额度不够,只处理最新发表的博客(第一条),不在继续往下操作</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 暂未对动态blink进行调整,理论可以</li> <li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 评论多评检测(防止有人故意而为之)</li> </ul> <p>20230408</p> <ul><li class="task-list-item"> <input type="checkbox" class="task-list-item-checkbox" disabled> 接入chatgpt,可对话、可自动评论</li></ul> <h1> <a id="_39"></a>实现流程</h1> <h2> <a id="chatgpt35api_41"></a>第三方chatgpt3.5api接口</h2> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0faf7d083c26464bad8f78b5e20fc5ec.png" alt="在这里插入图片描述"><br> 这里接口有需要的伙伴,私聊领取哈,免费的</p> <h2> <a id="_46"></a>构造标题、关键字等</h2> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/02081d6175da4b7a9859992d6b4f9f6f.png" alt="在这里插入图片描述"><br> 构造完成整理为发送gpt的模块中</p> <pre><code class="prism language-python"> title<span class="token operator">=</span><span class="token string">"Python3 循环语句"</span>yaoqiu<span class="token operator">=</span><span class="token string">"要求:代码实例和执行结果解释多一点"</span>text_val <span class="token operator">=</span> <span class="token string">"请写一篇标题为:"</span><span class="token operator">+</span>title<span class="token operator">+</span><span class="token string">"的博客文章,以markdown给我,字数在1800字左右"</span><span class="token operator">+</span>yaoqiucontent<span class="token operator">=</span>chatgpt<span class="token punctuation">(</span>title<span class="token punctuation">)</span> </code></pre> <h2> <a id="Cmarkdown_58"></a>分析C的markdown接口请求参数</h2> <p>文章保存参数:</p> <table> <thead><tr> <th>参数名</th> <th>值</th> <th>说明</th> </tr></thead> <tbody> <tr> <td>title</td> <td>Python3 循环语句</td> <td>标题</td> </tr> <tr> <td>description</td> <td>摘要</td> <td>摘要可为空</td> </tr> <tr> <td>content</td> <td>内容</td> <td>需要使用markdown转换,不转换就是一行</td> </tr> <tr> <td>tags</td> <td>文章标签</td> <td>例如:机器学习</td> </tr> <tr> <td>categories</td> <td>空</td> <td>暂不清楚,猜测分类</td> </tr> <tr> <td>type</td> <td>original</td> <td>暂不清楚</td> </tr> <tr> <td>status</td> <td>2</td> <td>暂不清楚</td> </tr> <tr> <td>read_type</td> <td>public</td> <td>是否公开</td> </tr> <tr> <td>reason</td> <td></td> <td>暂不清楚</td> </tr> <tr> <td>cover_images</td> <td>imgurl</td> <td>头图支持多个</td> </tr> <tr> <td>is_new</td> <td>1</td> <td>新文</td> </tr> <tr> <td>等等…</td> <td></td> <td></td> </tr> </tbody> </table> <p>知道请求参数,关键在于标题、内容、标签、图片,于是接下来着手获取</p> <h3> <a id="_78"></a>内容</h3> <p>输入标题,完成构造对话发送等待返回文章,通过markdown处理请求</p> <pre><code class="prism language-python"><span class="token keyword">def</span> <span class="token function">convert_markdown_to_html</span><span class="token punctuation">(</span>markdown_text<span class="token punctuation">)</span><span class="token punctuation">:</span>html <span class="token operator">=</span> markdown<span class="token punctuation">.</span>markdown<span class="token punctuation">(</span>markdown_text<span class="token punctuation">)</span><span class="token keyword">return</span> html </code></pre> <p>处理后获得title标题、content内容</p> <h3> <a id="_87"></a>获取标签、头图</h3> <p>C站标签和头图属于一个接口,通过请求</p> <pre><code class="prism language-python"> result<span class="token operator">=</span>get_recommend_tags<span class="token punctuation">(</span>title<span class="token punctuation">,</span>content_mark<span class="token punctuation">)</span> </code></pre> <p>获取到标签和头图,这里由于比较忙我就不整理参数了</p> <p>以下为接口返回数据(请求标题与numpy相关):</p> <pre><code class="prism language-python"><span class="token punctuation">{<!-- --></span><span class="token string">"code"</span><span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">,</span><span class="token string">"message"</span><span class="token punctuation">:</span> <span class="token string">"success"</span><span class="token punctuation">,</span><span class="token string">"traceId"</span><span class="token punctuation">:</span> <span class="token string">"xxxxxx"</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 string">"common"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"bug"</span><span class="token punctuation">,</span> <span class="token string">"php"</span><span class="token punctuation">,</span> <span class="token string">"开发语言"</span><span class="token punctuation">,</span> <span class="token string">"thinkphp5"</span><span class="token punctuation">,</span> <span class="token string">"微信小程序"</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"list"</span><span class="token punctuation">:</span> <span class="token punctuation">{<!-- --></span><span class="token string">"推荐"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"numpy"</span><span class="token punctuation">,</span> <span class="token string">"python"</span><span class="token punctuation">,</span> <span class="token string">"机器学习"</span><span class="token punctuation">,</span> <span class="token string">"开发语言"</span><span class="token punctuation">,</span> <span class="token string">"人工智能"</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"Python"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"python"</span><span class="token punctuation">,</span> <span class="token string">"django"</span><span class="token punctuation">,</span> <span class="token string">"pygame"</span><span class="token punctuation">,</span> <span class="token string">"virtualenv"</span><span class="token punctuation">,</span> <span class="token string">"tornado"</span><span class="token punctuation">,</span> <span class="token string">"flask"</span><span class="token punctuation">,</span> <span class="token string">"scikit-learn"</span><span class="token punctuation">,</span> <span class="token string">"plotly"</span><span class="token punctuation">,</span> <span class="token string">"dash"</span><span class="token punctuation">,</span> <span class="token string">"fastapi"</span><span class="token punctuation">,</span> <span class="token string">"pyqt"</span><span class="token punctuation">,</span> <span class="token string">"scrapy"</span><span class="token punctuation">,</span> <span class="token string">"beautifulsoup"</span><span class="token punctuation">,</span> <span class="token string">"numpy"</span><span class="token punctuation">,</span> <span class="token string">"scipy"</span><span class="token punctuation">,</span> <span class="token string">"pandas"</span><span class="token punctuation">,</span> <span class="token string">"matplotlib"</span><span class="token punctuation">,</span> <span class="token string">"httpx"</span><span class="token punctuation">,</span> <span class="token string">"web3.py"</span><span class="token punctuation">,</span> <span class="token string">"pytest"</span><span class="token punctuation">,</span> <span class="token string">"pillow"</span><span class="token punctuation">,</span> <span class="token string">"gunicorn"</span><span class="token punctuation">,</span> <span class="token string">"pip"</span><span class="token punctuation">,</span> <span class="token string">"conda"</span><span class="token punctuation">,</span> <span class="token string">"ipython"</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"images"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"https://xxxxxx/img_convert/7bc0b398d41f4dbc81d31b9dedc4e172.png"</span><span class="token punctuation">,</span> <span class="token string">"https://xxxxxx/img_convert/77fcfea2a41749d7867f62f0e98b01ca.png"</span><span class="token punctuation">,</span> <span class="token string">"https://xxxxxx/img_convert/fad536e972e14ce4b37803185dc3b00c.png"</span><span class="token punctuation">,</span> <span class="token string">"https://xxxxxx/img_convert/5bbc102f2243430991aedee1be20b4f3.png"</span><span class="token punctuation">,</span> <span class="token string">"https://xxxxxx/img_convert/cf52fbe57e404f30babcdda6f1ef2c08.png"</span><span class="token punctuation">]</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> </code></pre> <p>标签是[‘推荐’]字段,使用python转换</p> <pre><code class="prism language-python">tags <span class="token operator">=</span> result<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'list'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'推荐'</span><span class="token punctuation">]</span> tag_str <span class="token operator">=</span> <span class="token string">', '</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span>tag<span class="token punctuation">)</span> <span class="token keyword">for</span> tag <span class="token keyword">in</span> tags<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>tag_str<span class="token punctuation">)</span> </code></pre> <p>打印结果为:<code>numpy, python, 机器学习, 开发语言, 人工智能</code></p> <p>以上已经获取到了标签和头图以及markdown格式的内容,接下来结合起来像C站文章保存接口请求一次</p> <pre><code class="prism language-python">saveArticle<span class="token punctuation">(</span>tag_str<span class="token punctuation">,</span>imgurl<span class="token punctuation">,</span>title<span class="token punctuation">,</span>content_mark<span class="token punctuation">)</span> </code></pre> <p>部分请求成功构造:</p> <pre><code class="prism language-python"> json_data <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token comment"># 'article_id': 130040595,</span><span class="token string">'title'</span><span class="token punctuation">:</span> title<span class="token punctuation">,</span><span class="token string">'description'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'content'</span><span class="token punctuation">:</span> content<span class="token punctuation">,</span><span class="token string">'tags'</span><span class="token punctuation">:</span> tag_str<span class="token punctuation">,</span><span class="token string">'categories'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'type'</span><span class="token punctuation">:</span> <span class="token string">'original'</span><span class="token punctuation">,</span><span class="token string">'status'</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token string">'read_type'</span><span class="token punctuation">:</span> <span class="token string">'public'</span><span class="token punctuation">,</span><span class="token string">'reason'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'resource_url'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'original_link'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'authorized_status'</span><span class="token punctuation">:</span> <span class="token boolean">False</span><span class="token punctuation">,</span><span class="token string">'check_original'</span><span class="token punctuation">:</span> <span class="token boolean">False</span><span class="token punctuation">,</span><span class="token string">'source'</span><span class="token punctuation">:</span> <span class="token string">'pc_postedit'</span><span class="token punctuation">,</span><span class="token string">'not_auto_saved'</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'creator_activity_id'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span><span class="token string">'cover_images'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>imgurl<span class="token punctuation">,</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">'cover_type'</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'vote_id'</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token string">'scheduled_time'</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token string">'is_new'</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">}</span>response <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">'xxxxx'</span><span class="token punctuation">,</span>cookies<span class="token operator">=</span>cookies<span class="token punctuation">,</span>headers<span class="token operator">=</span>headers<span class="token punctuation">,</span>json<span class="token operator">=</span>json_data<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>text<span class="token punctuation">)</span> </code></pre> <p>请求成功,刷新我的文章查看</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/010ea998beb84dc19703d414c67dba88.png#pic_center" alt="在这里插入图片描述"><br> 由于这里还在对文章内容排版优化,所以就不直接发表,需要核查一遍,等后面对接了文心一言考虑自动发表!</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写文章(附chatgpt3.5免费接口)
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-23
修改于
2024-12-22
上一篇:软件:常用 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
前端