-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (36 loc) · 1.51 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
const { Configuration, OpenAIApi } = require("openai");
class Client {
constructor(OPENAI_API_KEY) {
this.OPENAI_API_KEY = OPENAI_API_KEY;
if (!this.OPENAI_API_KEY) throw new Error('No OPENAI_API_KEY provided');
const configuration = new Configuration({ apiKey: this.OPENAI_API_KEY });
this.openAi = new OpenAIApi(configuration);
this.prompt = `Given the chessboard state: {{chessboard}},Turn: {{Turn}}, suggest the next move.\nRule: return base on '{ "move" : "<The move>", "des": "<Why this move>" }'`
}
async getMove(Board, Turn, max_tokens) {
if (!Board) throw new Error('No Board provided');
if (!Turn) Turn = "White";
const prompt = this.prompt.replace('{{chessboard}}', `"${Board}"`).replace('{{Turn}}', `"${Turn}"`)
let data = await this.ask(prompt, max_tokens)
return JSON.parse(data);
}
async ask(prompt, max_tokens) {
if (!max_tokens) max_tokens = 100
const completion = await this.openAi
.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: max_tokens,
n: 1,
temperature: 0.7,
})
.catch((err) => {
console.log(err.data);
return err
});
if (!completion) throw new Error("ERROR on openai gpt");
if (!completion.data.choices) throw new Error("ERROR on openai gpt");
return completion.data.choices[0].text.trim();
}
}
module.exports = Client;