0% found this document useful (0 votes)
93 views2 pages

Google Docs AI Menu Script

This document contains code for a Google Docs add-on that allows users to generate ideas and images using OpenAI's API. It defines functions to create a menu in Docs, call the API with user-selected text or an image prompt, and insert the API response as new content. Key variables store the API key and model. The functions make POST requests to OpenAI's API to generate text completions or images based on the prompt and insert the results into the user's document.

Uploaded by

Anwar An
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views2 pages

Google Docs AI Menu Script

This document contains code for a Google Docs add-on that allows users to generate ideas and images using OpenAI's API. It defines functions to create a menu in Docs, call the API with user-selected text or an image prompt, and insert the API response as new content. Key variables store the API key and model. The functions make POST requests to OpenAI's API to generate text completions or images based on the prompt and insert the results into the user's document.

Uploaded by

Anwar An
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// DROP DOWN MENU

function onOpen() {
[Link]().createMenu("GPT3 MAGIC")
.addItem("Generate Ideas", "generateIdeas")
.addItem("Generate Image", "generateImage")
.addToUi();
}
// ****END MENU****

// FIXED VARIABLES. Your API and Model Type


var apiKey = "xxxxxxxxxxxxx";
var model = "text-davinci-003"
// ****END VARIABLES****

// GENERATE PROMPT
function xxxxxxx() {
var doc = [Link]()
var selectedText = [Link]().getRangeElements()[0].getElement().asText().getText()
var body = [Link]()
var prompt = "xxxxxxxxxxxx " + selectedText;
temperature= 0
maxTokens = 2060
const requestBody = {
"model": model,
"prompt": prompt,
"temperature": temperature,
"max_tokens": maxTokens,
};
const requestOptions = {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer "+apiKey
},
"payload": [Link](requestBody)
}
const response = [Link]("[Link] requestOptions);
var responseText = [Link]();
var json = [Link](responseText);
[Link](json['choices'][0]['text'])
para = [Link](json['choices'][0]['text'])
}
// ****END PROMPT****

// GENERATE IMAGE - SIZE CAN BE 256x256', '512x512', '1024x1024


function generateImage() {
var doc = [Link]()
var selectedText = [Link]().getRangeElements()[0].getElement().asText().getText()
var body = [Link]()
temperature= 0
maxTokens = 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": [Link](requestBody2)
}
const response2 = [Link]("[Link]
requestOptions2);
var responseText = [Link]();
var json = [Link](responseText);
var url1=json['data'][0]['url']
[Link]([Link](url1).getBlob());
}
// ****END IMAGE****

BUY ME A COFFEE ☕ [Link] <<<<

You might also like