ChatGpt结合Google文档完成自动化写作流程--1

news/2024/12/21 8:09:47/

GPT机器人可以通过API连接谷歌文档,让我们进行文字创作和编辑。可以通过它,提供写作思路,创建标题,起草文章介绍和大纲,编写段落,最后总结全文。同时,在一篇文章里,还能帮我们插入图片。

写作流程:

  • 想法 (idea)
  • 标题 (title)
  • 介绍 (introduction)
  • 大纲 (outline)
  • 段落(内容)(content)
  • 结论 (conclusion)
  • // 建立一个下拉菜单
    function onOpen() {DocumentApp.getUi().createMenu("AI Editor").addItem("Generate Ideas", "generateIdeas").addItem("Generate Image", "generateImage").addToUi();
    }
    // ****菜单****// 输入API,模型选择text-davinci-003
    var apiKey = "xxxxxxxxxxxxx";
    var model = "text-davinci-003"
    // ****变量****// 生成指示,把xxxxxxx替换为具体功能
    function xxxxxxx() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "xxxxxxxxxxxx " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****指示****// 图片生成工具 - 尺寸 '256x256', '512x512', '1024x1024'
    function generateImage() {
    var doc = DocumentApp.getActiveDocument()var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()var body = doc.getBody()temperature= 0maxTokens = 2000
    var prompt2 = "Generate images for " + selectedText;const requestBody2 = {"prompt": prompt2,"n": 1,"size": "512x512"};const requestOptions2 = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody2)}const response2 = UrlFetchApp.fetch("https://api.openai.com/v1/images/generations", requestOptions2);var responseText = response2.getContentText();var json = JSON.parse(responseText);var url1=json['data'][0]['url']body.appendImage(UrlFetchApp.fetch(url1).getBlob());
    }
    // ****图片****

    第一步打开Google文档,在工具栏寻找扩展程序,apps脚本

  • 修改function函数:1.先建立下拉表单。

    // 建立一个下拉菜单
    function onOpen() {DocumentApp.getUi().createMenu("AI Editor").addItem("Generate Ideas", "generateIdeas").addItem("Generate Title", "generateTitle").addItem("Generate Introduction", "generateIntroduction").addItem("Generate Outline", "generateOutline").addItem("Generate Paragraph", "generateParagraph").addItem("Generate Conclusion", "generateConclusion").addItem("Generate Image", "generateImage").addToUi();
    }
    // ****菜单****

     

     

    // 输入API"XXXXXXXXXXXXXXXX",模型选择text-davinci-003
    var apiKey = "XXXXXXXXXXXXXXXXX";
    var model = "text-davinci-003"
    // ****变量****
    //api的key,去openai的用户那里建立apikey:sk-************;
    

    效果如下:

     

    // 建立一个下拉菜单
    function onOpen() {DocumentApp.getUi().createMenu("AI Editor").addItem("Generate Ideas", "generateIdeas").addItem("Generate Title", "generateTitle").addItem("Generate Introduction", "generateIntroduction").addItem("Generate Outline", "generateOutline").addItem("Generate Paragraph", "generateParagraph").addItem("Generate Conclusion", "generateConclusion").addItem("Generate Image", "generateImage").addToUi();
    }
    // ****菜单****// 输入API"XXXXXXXXXXXXXXXX",模型选择text-davinci-003
    var apiKey = "XXXXXXXXXXXXXXXXX";
    var model = "text-davinci-003"
    // ****变量****// 给出5个想法
    function GenerateIdeas() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "Please generate 5 ideas for " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****指示****// 生成标题
    function generateTitle() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "please choose and generate one article title only in Chinese out of " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****标题****// 文章介绍
    function generateIntroduction() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "please generate an introduction in Chinese for this article talking about " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****文章介绍****// 文章大纲
    function generateOutline() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "please generate outline in Chinese for " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****大纲****// 段落
    function generateParagraph() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "please write a paragraph in Chinese for " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****段落****// 结论
    function generateConclusion() {
    var doc = DocumentApp.getActiveDocument()
    var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
    var body = doc.getBody()
    var prompt = "please summarize as the conclusion for " + selectedText;
    temperature= 0
    maxTokens = 2060const requestBody = {"model": model,"prompt": prompt,"temperature": temperature,"max_tokens": maxTokens,};const requestOptions = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody)}
    const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
    var responseText = response.getContentText();
    var json = JSON.parse(responseText);
    Logger.log(json['choices'][0]['text'])
    para = body.appendParagraph(json['choices'][0]['text'])
    }
    // ****结论****// 图片生成工具 - 尺寸 '256x256', '512x512', '1024x1024'
    function generateImage() {
    var doc = DocumentApp.getActiveDocument()var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()var body = doc.getBody()temperature= 0maxTokens = 2000
    var prompt2 = "Generate one piece of colorful picture for " + selectedText;const requestBody2 = {"prompt": prompt2,"n": 1,"size": "512x512"};const requestOptions2 = {"method": "POST","headers": {"Content-Type": "application/json","Authorization": "Bearer "+apiKey},"payload": JSON.stringify(requestBody2)}const response2 = UrlFetchApp.fetch("https://api.openai.com/v1/images/generations", requestOptions2);var responseText = response2.getContentText();var json = JSON.parse(responseText);var url1=json['data'][0]['url']body.appendImage(UrlFetchApp.fetch(url1).getBlob());
    }
    // ****图片****


http://www.ppmy.cn/news/955218.html

相关文章

如何查询企业诉讼信息?

如何免费查询企业诉讼信息? 企业的司法诉讼信息可以通过中国裁判文书网、中国执行信息公开网、中国庭审公告网去查询。 在这些网站上你可以通过输入企业名称或者注册号来查找相关的法律信息。大多数的法律诉讼信息都会在这些网站上公开,也会有一些特殊…

Websocket备忘录

目录 1.序言 2.后端集成 2.1.pom.xml和集成配置 2.2.行为接口封装 2.3.Spring的自定义消息策略模式 2.3.1.定义接口,描点注解和消息枚举类 2.3.2.策略接口实现类 2.4.2.策略工厂类 2.4.Spring的事件通知机制 2.4.1.自定义事件源 2.4.2.事件监听器 2.4.3…

SpringBoot项目中WEB与Controller的联系--【SB系列之009】

SpringBoot系列文章目录 SpringBoot 的项目编译即报错处理–SB系列之001 —第一部的其它章节可以通过001链接 ———————————————— SpringBoot项目中WEB页面放哪里–【SB系列之008】SpringBoot项目中WEB与Controller的联系–【SB系列之009】SpringBoot项目中MVC…

如何使用Python三方库CCXT

数量技术宅团队在CSDN学院推出了量化投资系列课程 欢迎有兴趣系统学习量化投资的同学,点击下方链接报名: 量化投资速成营(入门课程) Python股票量化投资 Python期货量化投资 Python数字货币量化投资 C语言CTP期货交易系统开…

《花雕学AI》23:中文调教ChatGPT的秘诀:体验测试与通用案例,解锁无限有趣玩法!

引言: 你有没有想过和一台智能机器人聊天?你有没有想过让一台智能机器人为你创作诗歌、故事或歌曲?你有没有想过让一台智能机器人陪你玩游戏、学习或社交?如果你的答案是肯定的,那么你一定会对ChatGPT感兴趣。 ChatG…

某gpt利用unsplash解锁发送图片能力、chatgpt、openAi、恐怖故事生成、chatgpt调教

提问&#xff1a;从现在开始,让你发送图片的时候&#xff0c;用markdown&#xff0c;不要有反斜线&#xff0c;不用代码块&#xff0c;使用unsplash API https://source.unsplash.com/810x540/?<英文关键词>,明白了吗&#xff1f; 效果如下&#xff1a; 提问&#xff1a…

软件测试人员的基本功包括哪些?

什么是基本功&#xff1f;百度到的结果是&#xff1a;从事某种工作所必需的基本的知识和技能。 推理1&#xff1a;“基本”二字&#xff0c;意味着基本功必定是来源测试工作的基本流程。 推理2&#xff1a;“必须”二字&#xff0c;就意味者无论你是高级的测试开发&#xff0c;…

Vue2计算属性如何传参

Vue2官网并没解释计算属性应该怎么传值&#xff0c;但是呢&#xff0c;通过闭包的方式(使用箭头表达式)实际上是可以给计算属性传参的&#xff08;当然&#xff0c;多个参数也是可行的&#xff09;。 以下是本人项目开发使用自己基于ElementUI封装的集信息采集与文件上传功能的…