xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

如何使用 Node.js 和 OpenAI API 快速开发一个私有的 ChatGPT 智能聊天机器人程序 All In One

如何使用 Node.js 和 OpenAI API 快速开发一个私有的 ChatGPT 智能聊天机器人程序 All In One

OpenAI API playground

image

https://platform.openai.com/playground

code bug ❌

  1. 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

image

https://github.com/xgqfrms/ChatGPT/

OpenAI API keys

https://platform.openai.com/account/api-keys

image

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://chat.xgqfrms.xyz/

https://github.com/humanloop/awesome-chatgpt



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(2514)  评论(8编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2022-02-08 js Tree Data Structure & Tree Traversal Algorithm All In One
2022-02-08 前端纯 js 实现一个旭日图组件 All In One
2022-02-08 vue table dynamic column resize bug All In One
2021-02-08 vue 单文件组件最佳实践
2021-02-08 Object Destructuring Assignment vs Object.assign
2021-02-08 vue & this.$router.resolve
2021-02-08 breadcrumb 面包屑
点击右上角即可分享
微信分享提示