快速上手体验ChatGPT
Admin 2023-04-28 群英技术资讯 490 次浏览
使用的 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/introduction chatgpt-api: https://github.com/transitive-bullshit/chatgpt-api 使用方式 node >=18,或者 安装 fetch polyfill, 例如:isomorphic-fetchnpm install chatgpt import { 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-api https://github.com/waylaidwanderer/node-chatgpt-api 1、安装 Follow the same setup instructions for the API server, creating settings.js
npm i @waylaidwanderer/chatgpt-api // 全局安装执行 chatgpt-api // 本地安装执行 npm run cli
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', };
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008