-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathopenAiIntegeration.gs
More file actions
84 lines (63 loc) · 2.21 KB
/
Copy pathopenAiIntegeration.gs
File metadata and controls
84 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function onOpen() {
DocumentApp.getUi().createMenu("Auto Blog")
.addItem("Generate Blog", "myFunction")
.addToUi();
}
function myFunction() {
var doc = DocumentApp.getActiveDocument()
var selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText()
var body = doc.getBody()
// Replace YOUR_API_KEY with your actual OpenAI API key
var apiKey = "YOUR_API_KEY";
var prompt = "Generate a detailed full length article about " + selectedText;
// var model = "text-davinci-002";
var model = "text-davinci-003"
temperature= 0
maxTokens = 4060
// Set up the request body with the given parameters
const 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)
}
// Call the OpenAI API
const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestOptions);
// Parse the response and get the generated text
var responseText = response.getContentText();
var json = JSON.parse(responseText);
Logger.log(json['choices'][0]['text'])
para = body.appendParagraph(json['choices'][0]['text'])
//Image Generation
// Set up the request body with the given parameters
var prompt2 = "Generate images for " + selectedText;
const requestBody2 = {
"prompt": prompt2,
"n": 2,
"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);
// Parse the response and get the generated text
var responseText = response2.getContentText();
var json = JSON.parse(responseText);
var url1=json['data'][0]['url']
var url2=json['data'][1]['url']
body.appendImage(UrlFetchApp.fetch(url1).getBlob());
body.appendImage(UrlFetchApp.fetch(url2).getBlob());
}