快速上手体验ChatGPT

Admin 2023-04-28 群英技术资讯 490 次浏览

这篇文章主要讲解了“快速上手体验ChatGPT”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“快速上手体验ChatGPT”吧!

前端



开发接口
使用的 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-fetch
npm 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


添加 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',
};



关于“快速上手体验ChatGPT”就介绍到这了,如果大家觉得不错可以参考了解看看,如果想要了解更多,欢迎关注群英网络,小编每天都会为大家更新不同的知识。 群英智防CDN,智能加速解决方案

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

猜你喜欢

成为群英会员,开启智能安全云计算之旅

立即注册
专业资深工程师驻守
7X24小时快速响应
一站式无忧技术支持
免费备案服务
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
在线客服
微信公众号
返回顶部
返回顶部 返回顶部
在线客服
在线客服