-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
41 lines (37 loc) · 1.1 KB
/
index.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
import { Configuration, OpenAIApi } from "openai";
class ChatGPT {
/**
*
* @param {import("./types").ChatGPTOptions} options
*/
constructor(options) {
this.orgKey = options.authId;
this.apiKey = options.apiKey;
this.configuration = new Configuration({
organization: this.orgKey,
apiKey: this.apiKey,
});
this.openai = new OpenAIApi(this.configuration);
}
async chat(content, requester) {
if (!content || !requester) {
throw new Error("Content and requester cannot be null or undefined");
}
try {
let response = await this.openai.createCompletion({
model: "davinci",
prompt: `ChatGPT is a friendly chatbot. \n\
ChatGPT : Hello, how are you?\n\
${requester}: ${content}\n\
ChatGPT:`,
temperature: 0.9,
max_tokens: 100,
stop: ["ChatGPT:", "Kabir Jaipal:"],
});
return response.data.choices.at(0).text;
} catch (error) {
throw new Error(`Failed to complete API call: ${error.message}`);
}
}
}
export { ChatGPT };