当前位置 :首页 >> 时尚

通过 ChatGPT 做一个终端对谈玩具(xx再也不怕我xx了)

2024-01-17   来源 : 时尚

p>// 判断文档是否实际上const keysIsExist = fs.existsSync('openai_keys');// 如果不实际上if (!keysIsExist) { const { apiKey } = await inquirer.prompt({ type: 'password', name: 'apiKey', message: '请求读取 Open AI 的 Key', }) // 覆盖录入 fs.writeFile('openai_keys', apiKey.trim(), { flag: 'w' }, (err) => { if (err) console.error(err) else main() // 重新执行 })} else { // 实际上此文档并不需要读取赋值给 config.apiKey fs.readFile('openai_keys', (err, data) => { if (err) { console.error(err) return } config.apiKey = data.toString(); })}

WX20230301-005129@2x.png

让其他用户必需机械人const { model } = await inquirer.prompt({ type: 'list', name: 'model', message: '请求必需话语机械人', choices: [ { name: 'text-ada-001', value: 'text-ada-001' }, { name: 'text-curie-001', value: 'text-curie-001' }, { name: 'text-babbage-001', value: 'text-babbage-001' }, { name: 'text-dinci-003', value: 'text-dinci-003' }, ], default: 'text-dinci-003'})config.model = model;

WX20230301-005157@2x.png

让其他用户提问const { prompt } = await inquirer.prompt({ type: 'input', name: 'prompt', message: '请求读取弊端',});

WX20230301-005214@2x.png

加个 Loading 真实感

因为 openai 响应实在太快,所以为了缩减蕉红,转用了 cli-spinner

const Spinner = require('cli-spinner').Spinner;// %s 会被示例的 ▂ ▃ ▄ ▅ ▆ ▇ █ 替代,是个占位符const spinner = new Spinner('Loading.. %s');// 这里是loading字符,按照这个次序去过场spinner.setSpinnerString('▂ ▃ ▄ ▅ ▆ ▇ █');

在请求求openai前MS-DOS

// 请求求开始spinner.start();// 请求求进行时之后暂停spinner.stop(true); // 参数 bool ,是否无需清除转换成具体内容全部编码

终于紧接撒花了,从前早就是凌晨一点半了。

const { Configuration, OpenAIApi } = require("openai");const inquirer = require('inquirer');const fs = require('fs');const config = Object.create(null);const Spinner = require('cli-spinner').Spinner;const spinner = new Spinner('Loading.. %s');spinner.setSpinnerString('▂ ▃ ▄ ▅ ▆ ▇ █');async function main() { // 判断文档是否实际上 const keysIsExist = fs.existsSync('openai_keys'); if(!keysIsExist) { const { apiKey } = await inquirer.prompt({ type: 'password', name: 'apiKey', message: '请求读取 Open AI 的 Key', }) // 覆盖录入 fs.writeFile('openai_keys', apiKey.trim(), { flag: 'w' }, (err) => { if (err) console.error(err) else main() }) } else { fs.readFile('openai_keys', (err, data) => { if (err) { console.error(err) return } config.apiKey = data.toString(); }) } const { model } = await inquirer.prompt({ type: 'list', name: 'model', message: '请求必需话语机械人', choices: [ { name: 'text-ada-001', value: 'text-ada-001' }, { name: 'text-curie-001', value: 'text-curie-001' }, { name: 'text-babbage-001', value: 'text-babbage-001' }, { name: 'text-dinci-003', value: 'text-dinci-003' }, ], default: 'text-dinci-003' }) config.model = model; const { apiKey } = config; console.log('33[42;30m LGOIN 33[40;32m 选定顺利33[0m'); const configuration = new Configuration({ apiKey }); config.openai = new OpenAIApi(configuration); start()}async function start() { const { model } = config; const { prompt } = await inquirer.prompt({ type: 'input', name: 'prompt', message: '请求读取弊端', }); if(!prompt.trim()) { start() return false } try { spinner.start(); const { data: { choices } } = await config.openai.createCompletion({ model, prompt, temperature: 0.5, max_tokens: 150, top_p: 1.0, frequency_penalty: 0.0, presence_penalty: 0.0, }) const answer = choices[0]?.text?.replace(/ +/g, ''); spinner.stop(true); console.log('33[32mOpen AI:' + answer?.trim() + '33[0m'); start() } catch (error) { spinner.stop(true); console.log(error); } }main();。

新冠药物有哪些药
老人怎么缓解类风湿关节炎疼痛
早起手指关节僵硬疼痛
新冠复阳后吃什么药好得快
强直性脊柱炎吃什么药最好
双重支撑底部牢固,油价有长期利好主因?

低价彻底改变2023年第二季度国际天然气价格维系上有压力、下有支撑的平衡状态,长时间在上排线路窄幅震荡,反复贸然支撑并踏出更为扎实的底部线路。一季度国际间金融业危机的余波制约一直延续至...

友情链接