JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Gallery
Share
Explore
ChatGPT & Google Doc
Amir Banihashemi
Creating our own AI Content Writer in Google Doc
// Creates our custom menu in our doc
function
onOpen() {
DocumentApp
.getUi().createMenu(
"ChatGPT"
)
.addItem(
"Generate Twitter Post"
,
"generateTwitterPost"
)
.addItem(
"Generate Linkedin Post"
,
"generateLinkedinPost"
)
.addItem(
"Marketing Post"
,
"marketingPost"
)
.addToUi();
}
// API Key & model type
const
API_KEY
=
"sk-xxxx"
const
MODEL_TYPE
=
"gpt-3.5-turbo"
;
function
marketingPost() {
const
doc =
DocumentApp
.getActiveDocument();
const
selectedText = doc.getSelection().getRangeElements()[
0
].getElement().asText().getText();
const
body = doc.getBody();
const
prompt =
"Help me create a marketing post on "
+ selectedText;
const
temperature =
0
;
const
maxTokens =
2060
;
const
requestBody = {
model:
MODEL_TYPE
,
messages: [{role:
"user"
, content: prompt}],
temperature,
max_tokens: maxTokens,
};
const
requestOptions = {
method:
"POST"
,
headers: {
"Content-Type"
:
"application/json"
,
Authorization
:
"Bearer "
+
API_KEY
,
},
payload:
JSON
.stringify(requestBody),
};
const
response =
UrlFetchApp
.fetch(
"https://api.openai.com/v1/chat/completions"
, requestOptions);
const
responseText = response.getContentText();
const
json =
JSON
.parse(responseText);
const
generatedText = json[
'choices'
][
0
][
'message'
][
'content'
];
Logger
.log(generatedText);
body.appendParagraph(generatedText.toString());
}
// Generate Linkedin Post
function
generateLinkedinPost() {
const
doc =
DocumentApp
.getActiveDocument();
const
selectedText = doc.getSelection().getRangeElements()[
0
].getElement().asText().getText();
const
body = doc.getBody();
const
prompt =
"Help me write a Linkedin post on "
+ selectedText;
const
temperature =
0
;
const
maxTokens =
2060
;
const
requestBody = {
model:
MODEL_TYPE
,
messages: [{role:
"user"
, content: prompt}],
temperature,
max_tokens: maxTokens,
};
const
requestOptions = {
method:
"POST"
,
headers: {
"Content-Type"
:
"application/json"
,
Authorization
:
"Bearer "
+
API_KEY
,
},
payload:
JSON
.stringify(requestBody),
};
const
response =
UrlFetchApp
.fetch(
"https://api.openai.com/v1/chat/completions"
, requestOptions);
const
responseText = response.getContentText();
const
json =
JSON
.parse(responseText);
const
generatedText = json[
'choices'
][
0
][
'message'
][
'content'
];
Logger
.log(generatedText);
body.appendParagraph(generatedText.toString());
}
// Generate Twitter Post
function
generateTwitterPost() {
const
doc =
DocumentApp
.getActiveDocument();
const
selectedText = doc.getSelection().getRangeElements()[
0
].getElement().asText().getText();
const
body = doc.getBody();
const
prompt =
"Generate an essay on "
+ selectedText;
const
temperature =
0
;
const
maxTokens =
2060
;
const
requestBody = {
model:
MODEL_TYPE
,
messages: [{role:
"user"
, content: prompt}],
temperature,
max_tokens: maxTokens,
};
const
requestOptions = {
method:
"POST"
,
headers: {
"Content-Type"
:
"application/json"
,
Authorization
:
"Bearer "
+
API_KEY
,
},
payload:
JSON
.stringify(requestBody),
};
const
response =
UrlFetchApp
.fetch(
"https://api.openai.com/v1/chat/completions"
, requestOptions);
const
responseText = response.getContentText();
const
json =
JSON
.parse(responseText);
const
generatedText = json[
'choices'
][
0
][
'message'
][
'content'
];
Logger
.log(generatedText);
body.appendParagraph(generatedText.toString());
}
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.