/
index.mjs
77 lines (71 loc) · 2.34 KB
/
index.mjs
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
import inquirer from "inquirer";
import { Configuration, OpenAIApi } from "openai";
import { writeFile, appendFile } from "node:fs/promises";
import { readFileSync } from "node:fs";
import { join } from "path";
import chalk from "chalk";
const prompt = inquirer.createPromptModule();
import moment from "moment";
function createSay({ apiKey, model, max_tokens, temperature }) {
const configuration = new Configuration({
apiKey,
});
const openai = new OpenAIApi(configuration);
return async (text) => {
const completion = await openai.createCompletion({
model,
prompt: text,
temperature: Number(temperature),
top_p: Number(1),
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: Number(max_tokens),
});
return completion.data.choices.map(({ text }) => text).join("");
};
}
const getTimeString = () => moment().format("YYYY-MM-D_HH_mm_ss");
export const createTalk = (option) => {
// ''
const getCwdPath = (file) => {
return join(process.cwd(), file);
};
return async function startTalk() {
const file = getCwdPath(`${getTimeString()}.log`);
let { saveKey, apiKey } = option;
if (saveKey && apiKey) {
await writeFile(getCwdPath("chatKey.key"), apiKey);
}
if (!apiKey) {
apiKey = readFileSync(getCwdPath("chatKey.key")).toString();
console.info(chalk.green("read local apikey"), apiKey);
}
if (!apiKey) {
console.error(chalk.red("Apikey is required"));
}
const sayToAI = createSay({ ...option, apiKey });
console.info(chalk.blue(`Start completion`));
async function appendMsg(record) {
const time = getTimeString();
const { text, from } = record;
await appendFile(file, `${from} ${time}\n${text.trim()}\n`);
}
const context = [];
await writeFile(file, ``);
while (true) {
const { your } = await prompt({ type: "input", name: "your" });
context.push(your);
await appendMsg({ from: "me", text: your });
try {
const ai = await sayToAI(context.join("\n\n"));
context.push(ai);
console.info(chalk.green("ai:"), chalk.blue(ai));
await appendMsg({ from: "ai", text: ai });
} catch (e) {
console.error(chalk.red("Open AI access error"), e.message);
process.exit(-1);
}
}
console.info(chalk.blue(`End completion`));
};
};