快速上手体验ChatGPT
Admin 2023-04-28 群英技术资讯 742 次浏览
使用的 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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
AI绘画的“小秘密”都在这一篇文章里
智源连甩多个开源王炸!悟道3.0大模型数弹齐发,大模型评测体系上线
Meta「分割一切」超进化版来了,IDEA领衔国内顶尖团队打造:检测、分割、生成一切,狂揽2k星
ChatGPT 大封号、亚洲成重灾区!网友自救喊话:不要登录、不要登录 “通义千问”首发体验:响应速度出色 多轮对话有优势实至名归的晋升:特斯拉迎来权势最高的华人清华大学长聘副教授黄民烈:ChatGPT无法实现非0即1的精确计算清华大学教授陶建华:ChatGPT和GPT-4胜出基于四个方面告别高通骁龙7系!小米Civi 3即将登场:芯片升级为天玑8200有卫星通信 中兴行业终端春季新品发布会定档4月12日装机成本降了! 微星A620M-E主板到手799元:锐龙7 7800X3D绝配4799元 AOC新款27英寸电竞显示器上架:360Hz高刷、1ms响应一不小心就被忽悠 4K投影的猫腻儿可是真不少科大讯飞刘聪:中国造大模型或在某些领域超越ChatGPT龙芯在河南鹤壁发布3D5000服务器CPU百度:目前文心一言无官方App!已起诉苹果公司男子误吞耳机从肚子传出歌声!专家建议睡觉不要戴耳机999元起 中兴远航40上架:紫光展锐T760、10W快充擅长“砍一刀”的拼多多,被商家“砍”了
谁在布局生成式AI?
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008