ChatGPT 快速部署体验版本

作者: cnpim CNPIM 2023年06月09日

前端

开发接口
使用的 Eggjs 进行开发
async chatgpt() {    const { text, model, conversationId, parentMessageId } =      this.ctx.request.body;    const { ctx, app } = this;    const { user } = ctx;    const { ChatGPTClient } = await import('@waylaidwanderer/chatgpt-api');    const clientOptions = {      // (Optional) Support for a reverse proxy for the completions endpoint (private API server).      // Warning: This will expose your `openaiApiKey` to a third-party. Consider the risks before using this.      // reverseProxyUrl: 'https://chatgpt.hato.ai/completions',      // (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: model || 'text-davinci-002', // text-davinci-003(生成更复杂和多样化的完成) text-curie-001(可能不太准确或)      },      // (Optional) Set custom instructions instead of "You are ChatGPT...".      promptPrefix: '你好,我是智能助手',      // (Optional) Set a custom name for the user      // userLabel: 'User',      // (Optional) Set a custom name for ChatGPT      chatGptLabel: '智能助手',      // (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: process.env.STORAGE_FILE_PATH || './cache.json',    };    const chatGptClient = new ChatGPTClient(      process.env.OPENAI_API_KEY,      clientOptions,      cacheOptions,    );    const res = await chatGptClient.sendMessage(text, {      conversationId,      parentMessageId,      // onProgress: (token) => console.log(token),    });    console.log('A:', text, 'Q:', res.response);    this.success({      data: {        text: res.response,        parentMessageId: res.messageId,        conversationId: res.conversationId,      },    });  }

下面的是参考的资料:

Nodejs 能力官方对node 的支持https://platform.openai.com/docs/api-reference/introductionchatgpt-api:https://github.com/transitive-bullshit/chatgpt-api使用方式node >=18,或者 安装 fetch polyfill, 例如:isomorphic-fetch
npm install chatgptimport { ChatGPTAPI } from 'chatgpt'const api = new ChatGPTAPI({ process.env.OPENAI_API_KEY})const arg = process.argv.splice(2)[0]console.log('输入的内容是:',arg);const res = await api.sendMessage(arg)console.log('ChatGPT 的答案是:',res.text)
node-chatgpt-apihttps://github.com/waylaidwanderer/node-chatgpt-api1、安装Follow the samesetup instructionsfor the API server, creatingsettings.js
npm i @waylaidwanderer/chatgpt-api// 全局安装执行chatgpt-api// 本地安装执行npm run cli

  

添加 settings.js
export default {  // Your OpenAI API key (for `ChatGPTClient`)  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-davinci-003',      },      // (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).  // Does not apply when using `BingAIClient`.  cacheOptions: {},  // Options for the Bing client  bingAiClient: {      // The "_U" cookie value from bing.com      userToken: '',      // (Optional) Set to true to enable `console.debug()` logging      debug: false,  },  // 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,      // (Optional) Set to "bing" to use `BingAIClient` instead of `ChatGPTClient`.      // clientToUse: 'bing',  },  // 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',};

 

如果觉得文章不错,可以给小编发个红包给予鼓励.


本文阅读量:

声明:本信息来源于网络,仅用于学习和技术交流,如有侵权或其他问题,请联系本站处理。

技术支持:CNPIM.COM