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()); } // ****图片****