如何使用 Node.js 和 OpenAI API 快速开发一个私有的 ChatGPT 智能聊天机器人程序 All In One
如何使用 Node.js 和 OpenAI API 快速开发一个私有的 ChatGPT 智能聊天机器人程序 All In One
OpenAI API
playground
https://platform.openai.com/playground
code bug ❌
- Fruit Into Baskets
Medium
https://leetcode.com/problems/fruit-into-baskets/
//Q: leetcode 904 fruit-into-baskets answer in typescript
// A:
// Solution 1: Sliding Window
/**
* @param {number[]} tree
* @return {number}
*/
const totalFruit = (tree: number[]): number => {
let left = 0;
let count = 0;
let max = 0;
let types = new Set();
for (let right = 0; right < tree.length; right++) {
const currentType = tree[right];
// Increment count for current type
count++;
types.add(currentType);
// Slide window if two different types are found
if (types.size > 2) {
const leftType = tree[left];
count--;
types.delete(leftType);
left++;
}
// Update max
max = Math.max(max, count);
}
return max;
};
// Solution 2: Greedy
/**
* @param {number[]} tree
* @return {number}
*/
const totalFruit2 = (tree: number[]): number => {
let count = 0;
let max = 0;
let start = 0;
let end = 0;
let types = new Set();
while (start < tree.length && end < tree.length) {
const currentType = tree[end];
count++;
types.add(currentType);
if (types.size > 2) {
const leftType = tree[start];
count--;
types.delete(leftType);
start++;
}
max = Math.max(max, count);
end++;
}
return max;
};
OpenAI API
$ npm install openai
https://www.npmjs.com/package/openai
https://github.com/openai/openai-node
GPT-3
demos (davinci
)
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Say this is a test",
temperature: 0,
max_tokens: 7,
});
https://platform.openai.com/docs/libraries/node-js-library
demos
.env
# private keys
OPENAI_API_KEY=copy_and_replace_your_api_key_in_here
./src/index.ts
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-02-07
* @modified
*
* @description
* @description
* @difficulty Easy
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/17103702.html
* @solutions
*
* @best_solutions
*
*/
// export {};
const log = console.log;
import * as dotenv from 'dotenv';
dotenv.config();
// const { Configuration, OpenAIApi } = require("openai");
import { Configuration, OpenAIApi } from "openai";
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
// Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.ts(1378)
// const response = await openai.createCompletion({
// model: "text-davinci-003",
// prompt: "Say this is a test",
// temperature: 0,
// max_tokens: 7,
// });
async function app(text?: string) {
const res = await openai.createCompletion({
model: "text-davinci-003",
prompt: text ?? "who created ChatGPT",
temperature: 0,
max_tokens: 1000,
});
// log(`res =`, res.data);
log(`res.data.choices =`, res.data.choices);
// res.data.choices = [
// {
// text: '\n' +
// '\n' +
// 'ChatGPT was created by OpenAI, a research laboratory based in San Francisco, California.',
// index: 0,
// logprobs: null,
// finish_reason: 'stop'
// }
// ]
for (const {text} of res.data.choices) {
log(`👻 text =`, text);
}
}
app();
$ yarn app
$ yarn test
https://github.com/xgqfrms/ChatGPT/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
OpenAI API keys
https://platform.openai.com/account/api-keys
API key generated
Please save this secret key
somewhere safe and accessible. For security reasons, you won't be able to view it again
through your OpenAI account. If you lose this secret key, you'll need to generate a new one
.
refs
https://chatgpt.xgqfrms.xyz#chat
https://github.com/humanloop/awesome-chatgpt
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17103702.html
未经授权禁止转载,违者必究!