-
Notifications
You must be signed in to change notification settings - Fork 91
/
client.ts
31 lines (26 loc) · 849 Bytes
/
client.ts
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
// Adapted from: https://github.com/wong2/chat-gpt-google-extension/blob/main/background/index.mjs
import { Configuration, OpenAIApi } from "openai";
import { getApiKey, getPromptOptions } from "./config.js";
import { getConfig } from "./config_storage.js";
const configuration = new Configuration({
apiKey: await getApiKey(),
});
const openai = new OpenAIApi(configuration);
export class ChatGPTClient {
async getAnswer(question: string): Promise<string> {
const { model, maxTokens, temperature } = await getPromptOptions();
try {
const result = await openai.createCompletion({
model,
prompt: question,
max_tokens: maxTokens,
temperature,
});
return result.data.choices[0].text;
} catch (e) {
console.error(e?.response ?? e);
throw e;
}
// @ts-ignore
}
}