forked from TechBot505/ChatGPT-Chrome-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.js
38 lines (37 loc) · 937 Bytes
/
chatgpt.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
export default class ChatGPT {
constructor() {
this.url = "https://api.openai.com/v1";
this.model = "text-davinci-003";
this.temperature = 0.9;
this.max_tokens = 100;
this.top_p = 0;
this.frequency_penalty = 0;
this.presence_penalty = 0;
this.apiKey = process.env.REACT_APP_APIKEY;
}
async getCompletion(prompt) {
const path = `${this.url}/completions`;
const response = await fetch(`${path}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({
model: this.model,
prompt: prompt,
temperature: this.temperature,
max_tokens: this.max_tokens,
top_p: this.top_p,
frequency_penalty: this.frequency_penalty,
presence_penalty: this.presence_penalty,
}),
});
try {
const data = await response.json();
return data.choices[0].text;
} catch (error) {
return `${error}`;
}
}
}