-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptchat.js
114 lines (89 loc) · 3.05 KB
/
gptchat.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// GPT Chat Toy -- a toy for the OpenAI GPT-3.5 API
// Credentials
// This script requires a GPT API key.
// You can get one at https://beta.openai.com/
// For doc see: https://platform.openai.com/docs/api-reference/chat
const credential = Credential.create('GPTKey', "Credentials for a GPT Chat toy")
credential.addPasswordField('gptKey', "GPT API key")
credential.authorize()
const gptKey = credential.getValue('gptKey')
const getChatCompletion = (messages) => {
const gptURL = 'https://api.openai.com/v1/chat/completions'
const gptParams = {
"model": "gpt-3.5-turbo",
"messages": messages,
"temperature": 1.0, // more focused 0.0 - 2.0 more random
"top_p": 1.0, // 0 - 1.0 ( 0.1 means only the tokens comprising the top 10% probability mass are considered)
"n": 1,
"max_tokens": 1536
}
const response = HTTP.create().request({
"headers": { "Authorization": `Bearer ${gptKey}` },
"method": 'POST',
"url": gptURL,
"data": gptParams
})
console.log(`=response==> ${JSON.stringify(response.responseData, null, 2)}`)
if (response.success)
return response.responseData.choices[0].message
else {
context.fail(`error ${response.statusCode}`)
return `error ${response.statusCode} -- ${JSON.stringify(response.responseData, null, 2)}`
}
}
const parseRole = (line) => {
let roleRegex = /^[SUA]:/i
let role = null
if (roleRegex.test(line)) {
switch (line[0]) {
case 'S': role = 'system'; break;
case 'U': role = 'user'; break;
case 'A': role = 'assistant'; break;
}
}
console.log(`=line==> ${line}`)
return role
}
const parseContent = (d) => {
let messages = []
let currentMessage = null
for (let line of d.lines) {
let role = parseRole(line)
if (role) {
if (currentMessage)
messages.push(currentMessage)
currentMessage = { role, content: line.slice(2).trim() }
} else {
if (currentMessage) {
currentMessage.content += `\n${line}`
} else {
currentMessage = { role: 'user', content: line }
}
}
console.log(`=currentMessage==> ${JSON.stringify(currentMessage, null, 2)}`)
}
if (currentMessage)
messages.push(currentMessage)
console.log(`=messages==> ${JSON.stringify(messages, null, 2)}`)
return messages
}
const printMessages = (messages) => {
let role
let content = messages.content.trim()
switch (messages.role) {
case 'assistant': role = 'A'; break;
case 'system': role = 'S'; break;
case 'user': role = 'U'; break;
default: role = 'X'; break;
}
return `${role}: ${content}`
}
/****
* Main
****/
const messages = getChatCompletion(parseContent(draft))
const response = printMessages(messages)
draft.saveVersion()
draft.append(response)
draft.addTag('gpt-chat')
draft.update()