node-chatgpt-api - 通过 OpenAI 的 API 使用官方 ChatGPT 模型的 ChatGPT 实现。

Created at: 2023-01-07 12:35:34
Language: JavaScript
License: MIT

命令行演示

更新 (2023-02-02)

尝试将文本聊天-davinci-002-20230126 与 OpenAI API 一起使用现在会返回 404 错误。有人已经找到了新型号名称,但此时不愿意分享。找到新模型后,我将更新此存储库。如果你有任何潜在客户,请打开问题或拉取请求。

同时,我添加了对 text-davinci-003 等模型的支持,你可以将其用作直接替换。请记住,text-davinci-003不如text-chat-davinci-002(通过RHLF训练并微调为对话AI),尽管结果仍然非常好。请注意,使用文本-davinci-003将花费你的积分($)。

Discord 用户 @pig#8932 找到了一个工作模型,.我已经更新了库以使用此模型。

text-chat-davinci-002
text-chat-davinci-002-20221122

ChatGPT API

通过OpenAI的API使用官方ChatGPT模型的ChatGPT实现。

国家防范机制 新人掌  MIT 授权协议 GitHub 回购明星

这是使用官方 ChatGPT 原始模型的 ChatGPT 实现。这个模型名称在我检查官方 ChatGPT 网站发出的网络请求时被短暂泄露,我发现它适用于 OpenAI API使用此模型目前不会花费任何积分。

text-chat-davinci-002
text-chat-davinci-002-20230126

据我所知,我是第一个发现这一点的人,并且该模型的使用已经在acheong08 / ChatGPT等库中实现。

这个库的以前版本使用transitive-bullshit/chatgpt-api仍然在archive/old-version分支上可用。

模型本身没有任何对话支持,因此此库使用缓存来存储对话并将其作为上下文传递给模型。这使你可以以与官方网站几乎相同的方式与 ChatGPT 进行持续对话。

特征

  • 使用官方的 ChatGPT 原始模型,.
    text-chat-davinci-002-20221122
  • 包括一个API服务器(支持Docker),你可以运行以在非Node.js应用程序中使用ChatGPT。
  • 包括一个可以在你自己的 Node.js 应用程序中使用的类。
    ChatGPTClient
  • 包括一个 CLI 界面,你可以在其中与 ChatGPT 聊天。
  • 从 ChatGPT 官方网站复制聊天线程(带有对话 ID 和消息 ID),并使用 Keyv 进行持久对话。
    • 默认情况下,对话存储在内存中,但你可以选择安装存储适配器以将对话保存到数据库。
    • 适配器也包含在此软件包中,如果你使用的是 API 服务器或 CLI,则可用于将对话存储在 JSON 文件中(请参阅)。
      keyv-file
      settings.example.js
  • 支持可配置的提示前缀,以及用户和 ChatGPT 的自定义名称。
    • 从本质上讲,这使你可以将ChatGPT变成不同的角色。
    • 这目前只能在全局级别进行配置,但我计划添加对每个对话自定义的支持。

开始

先决条件

用法

模块

npm i @waylaidwanderer/chatgpt-api
import ChatGPTClient from '@waylaidwanderer/chatgpt-api';

const clientOptions = {
  // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
  modelOptions: {
    // The model is set to text-chat-davinci-002-20221122 by default, but you can override
    // it and any other parameters here
    model: 'text-chat-davinci-002-20221122',
  },
  // (Optional) Set custom instructions instead of "You are ChatGPT...".
  // promptPrefix: 'You are Bob, a cowboy in Western times...',
  // (Optional) Set a custom name for the user
  // userLabel: 'User',
  // (Optional) Set a custom name for ChatGPT
  // chatGptLabel: 'ChatGPT',
  // (Optional) Set to true to enable `console.debug()` logging
  debug: false,
};

const cacheOptions = {
  // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
  // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
  // For example, to use a JSON file (`npm i keyv-file`) as a database:
  // store: new KeyvFile({ filename: 'cache.json' }),
};

const chatGptClient = new ChatGPTClient('OPENAI_API_KEY', clientOptions, cacheOptions);

const response = await chatGptClient.sendMessage('Hello!');
console.log(response); // { response: 'Hi! How can I help you today?', conversationId: '...', messageId: '...' }

const response2 = await chatGptClient.sendMessage('Write a poem about cats.', { conversationId: response.conversationId, parentMessageId: response.messageId });
console.log(response2.response); // Cats are the best pets in the world.

const response3 = await chatGptClient.sendMessage('Now write it in French.', { conversationId: response2.conversationId, parentMessageId: response2.messageId });
console.log(response3.response); // Les chats sont les meilleurs animaux de compagnie du monde.

接口服务器

你可以使用以下方法安装软件包

npm i -g @waylaidwanderer/chatgpt-api

然后使用 运行它。
这采用可选参数,如果未设置,则在当前目录中查找,内容如下:

chatgpt-api
--settings=<path_to_settings.js>
settings.js

module.exports = {
  // Your OpenAI API key
  openaiApiKey: process.env.OPENAI_API_KEY || '',
  chatGptClient: {
    // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
    modelOptions: {
      // The model is set to text-chat-davinci-002-20221122 by default, but you can override
      // it and any other parameters here
      model: 'text-chat-davinci-002-20221122',
    },
    // (Optional) Set custom instructions instead of "You are ChatGPT...".
    // promptPrefix: 'You are Bob, a cowboy in Western times...',
    // (Optional) Set a custom name for the user
    // userLabel: 'User',
    // (Optional) Set a custom name for ChatGPT
    // chatGptLabel: 'ChatGPT',
    // (Optional) Set to true to enable `console.debug()` logging
    debug: false,
  },
  // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
  // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
  cacheOptions: {},
  // Options for the API server
  apiOptions: {
    port: process.env.API_PORT || 3000,
    host: process.env.API_HOST || 'localhost',
    // (Optional) Set to true to enable `console.debug()` logging
    debug: false,
  },
  // If set, ChatGPTClient will use `keyv-file` to store conversations to this JSON file instead of in memory.
  // However, `cacheOptions.store` will override this if set
  storageFilePath: process.env.STORAGE_FILE_PATH || './cache.json',
};

或者,你可以直接安装并运行包。

  1. 克隆此存储库:
    git clone https://github.com/waylaidwanderer/node-chatgpt-api
  2. 安装依赖项(如果不使用 Docker)
    npm install
  3. 重命名为 ,并根据需要更改设置。
    settings.example.js
    settings.js
  4. 启动服务器:
    • 使用 or (如果不使用 Docker)
      npm start
      npm run server
    • 使用(需要 Docker)
      docker-compose up

要与 ChatGPT 开始对话,请使用以下格式的 JSON 正文向服务器的终端节点发送 POST 请求:

/conversation

{
    "message": "Hello, how are you today?",
    "conversationId": "your-conversation-id (optional)",
    "parentMessageId": "your-parent-message-id (optional)"
}

服务器将返回一个包含 ChatGPT 响应的 JSON 对象:

{
    "response": "I'm doing well, thank you! How are you?",
    "conversationId": "your-conversation-id",
    "messageId": "response-message-id"
}

如果请求不成功,服务器将返回一个 JSON 对象,其中包含错误消息和状态代码 503。

如果将消息发送到 ChatGPT 时出错:

{
    "error": "There was an error communicating with ChatGPT."
}

命令行界面

使用与 API 服务器相同的说明安装包。

如果全局安装:

chatgpt-cli

如果在本地安装:

npm run cli

ChatGPT 的回复会自动复制到你的剪贴板,因此你可以将它们粘贴到其他应用程序中。

警告

由于是 ChatGPT 的原始模型,我必须尽最大努力复制 ChatGPT 官方网站使用它的方式。经过广泛的测试和比较响应,我相信 ChatGPT 使用的模型有一些额外的微调。
这意味着我的实现或原始模型在某些方面的行为可能不完全相同:

text-chat-davinci-002-20221122

  • 对话不与任何用户 ID 相关联,因此,如果这对你很重要,则应实现自己的用户 ID 系统。

  • ChatGPT 的模型参数(温度、频率损失等)是未知的,所以我设置了一些我认为合理的默认值。

  • 对话仅限于大约最后 3000 个令牌,因此在较长的对话中可能会忘记较早的消息。

    • 这与 ChatGPT 的工作方式类似,除了我很确定他们在需要时有一些额外的方法来从早期的消息中检索上下文(这可以通过嵌入来实现,但我认为这超出了范围)。
  • 众所周知,作为微调的一部分,ChatGPT 有以下序言:

    “你是ChatGPT,一个由OpenAI训练的大型语言模型。对于每个回答,你回答得尽可能简洁(例如,不要冗长)。尽可能简洁地回答非常重要,因此请记住这一点。如果要生成列表,请不要有太多项目。保持项目数简短。
    知识截止时间:2021-09
    当前日期:2023-01-31”

    随着OpenAI更新ChatGPT,这个序言也可能改变。我的实现中的默认提示前缀尝试复制与当前 ChatGPT 模型类似的行为。

贡献

如果你想参与此项目,请创建一个拉取请求,详细说明你的更改。

许可证

该项目根据 MIT 许可证获得许可。