-
Notifications
You must be signed in to change notification settings - Fork 0
/
davegpt.js
165 lines (147 loc) · 4.28 KB
/
davegpt.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const myVersion = "0.4.0", myProductName = "davegpt";
exports.chatWithChatGpt = chatWithChatGpt;
const utils = require ("daveutils");
const request = require ("request");
//below, i've included the code from commoncode.js here -- 12/18/24 by DW
function httpRequest (url, method, timeout, headers, data, callback) {
var myHttpRequest;
function isNodeEnvironment () {
if ((typeof process !== "undefined") && (typeof process.versions !== "undefined") && (typeof process.versions.node !== "undefined")) {
return (true); //running in Node
}
else {
return (false); //running in the browser
}
}
function nodeHttpRequest (url, method, timeout, headers, data, callback) {
timeout = (timeout === undefined) ? 30000 : timeout;
headers = (headers === undefined) ? new Object () : headers;
const theRequest = {
url,
method,
timeout,
headers,
body: (method.toLowerCase () == "post") ? data : undefined,
json: true
};
request (theRequest, function (err, response, body) {
if (err) {
callback (err);
}
else {
if (response.statusCode >= 200 && response.statusCode <= 299) {
if (body.error === undefined) {
callback (undefined, body); //body is already parsed as JSON due to json: true in theRequest
}
else {
callback (body.error);
}
}
else {
var theError;
try {
theError = body.error; //a strange quirk of OpenAI
}
catch (err) {
const message = "Couldn't make the HTTP request because there was an error == " + response.statusCode + ".";
theError = {message};
}
callback (theError);
}
}
});
}
function browserHttpRequest (url, method, timeout, headers, data, callback) {
timeout = (timeout === undefined) ? 30000 : timeout;
var jxhr = $.ajax ({
url,
method,
headers,
timeout,
data: (method.toLowerCase () == "post") ? JSON.stringify (data) : undefined
})
.success (function (data, status) {
callback (undefined, data);
})
.error (function (status) {
var message;
try { //9/18/21 by DW
message = JSON.parse (status.responseText).message;
}
catch (err) {
message = status.responseText;
}
if ((message === undefined) || (message.length == 0)) { //7/22/22 by DW & 8/31/22 by DW
message = "There was an error communicating with the server.";
}
var err = {
code: status.status,
message
};
callback (err);
});
}
if (isNodeEnvironment ()) {
myHttpRequest = nodeHttpRequest;
}
else {
myHttpRequest = browserHttpRequest;
}
return (myHttpRequest (url, method, timeout, headers, data, callback));
}
function chatWithChatGpt (userOptions) {
const options = {
userPrompt: "Greetings!",
apiUrl: "https://api.openai.com/v1/chat/completions",
apiKey: undefined,
systemRole: "You are a helpful assistant.",
httpRequestCallback: httpRequest //12/17/24 by DW
};
utils.mergeOptions (userOptions, options);
var messages, conversation;
function init () {
messages = [
{role: "system", content: options.systemRole}
];
conversation = [
];
}
function sendMessage (theMessage, callback) {
const newMessage = {role: "user", content: theMessage};
messages.push (newMessage);
conversation.push (newMessage);
const timeout = undefined; //accept the default
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${options.apiKey}`
};
const bodyStruct = {
model: "gpt-4",
messages: messages
};
console.log ("sendMessage: messages == " + utils.jsonStringify (messages));
options.httpRequestCallback (options.apiUrl, "POST", timeout, headers, bodyStruct, function (err, dataFromChatGpt) {
if (err) {
console.log (err.message);
if (callback !== undefined) {
callback (err);
}
}
else {
const responseMessage = dataFromChatGpt.choices [0].message;
console.log ("sendMessage: responseMessage.content == " + utils.jsonStringify (responseMessage.content));
conversation.push (responseMessage.content);
if (callback !== undefined) {
callback (err, utils.jsonStringify (responseMessage.content));
}
}
});
}
function getConversation () {
return (conversation);
}
init ();
this.sendMessage = sendMessage;
this.getConversation = getConversation;
this.init = init;
}