-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptsummy.js
82 lines (62 loc) · 1.71 KB
/
gptsummy.js
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
// GPT Summarizer -- summarize the content of a website
// for Drafts
// Credentials
// This script requires a GPT API key.
// You can get one at https://beta.openai.com/
const credential = Credential.create('GPTKey', "Credentials for a GPT toy");
credential.addPasswordField('gptKey', "GPT API key");
credential.authorize();
const gptKey = credential.getValue('gptKey');
/****
* OpenAI API
****/
const gptURL = 'https://api.openai.com/v1/completions'
const http = HTTP.create(); // create HTTP object
// POST https://api.openai.com/v1/completions
const getCompletion = (prompt, echo) => {
const response = http.request({
"method": 'POST',
"url": gptURL,
"headers": {
"Authorization": `Bearer ${gptKey}`
},
"data": {
"model": "text-davinci-003",
"prompt": prompt,
"max_tokens": 2048,
"temperature": 0.5,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,
"echo": echo
}
})
console.log(`===> ${JSON.stringify(response.responseData, null, 2)}`)
if (response.success)
return response.responseData.choices[0].text.trim()
else
return `###### ${response.statusCode} -- ${response.error}`
}
/****
* getPrompt(n)
*
*
****/
const getPrompt = (n) => {
n = n || 3
let prompt = `
Go to each of the URLs below and summarize them
${draft.content}
`
return prompt
}
/****
* Main
****/
draft.saveVersion() // save the draft, just in case
let response = getCompletion(getPrompt(5), false)
console.log(`---`)
console.log(JSON.stringify(response, null, 2))
draft.append(response)
draft.update()