chatgpt api如何使用

发布时间:2023-02-25 10:18:34 作者:iii
来源:亿速云 阅读:556

今天小编给大家分享一下chatgpt api如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

chatgpt-api是 OpenAI ChatGPT 的非官方的 Node.js 包装器。 包括 TS 类型定义。 chatgpt-api不再需要任何浏览器破解,它使用泄露出来的OpenAI官方ChatGPT 在后台使用的模型。

✨你可以使用它开始构建由 ChatGPT 支持的项目,例如聊天机器人、网站等

import { ChatGPTAPI } from 'chatgpt'

const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY
})

const res = await api.sendMessage('Hello World!')
console.log(res.text)

请升级到 chatgpt@latest(至少 v4.0.0)。 与以前的版本相比,更新后的版本明显更加轻巧和健壮,你也不必担心 IP 问题或速率限制。

chatgpt api如何使用

1、安装chatgpt-api

确保你使用的是 node >= 18 以便 fetch 可用(node >= 14也可以,但你需要安装 fetch polyfill)。

使用如下命令安装 chatgpt-api :

npm install chatgpt

2、chatgpt-api使用方法

首先注册获取 OpenAI API 密钥并将其存储在你的环境中。

下面是简单的一次性对话:

import { ChatGPTAPI } from 'chatgpt'

async function example() {
  const api = new ChatGPTAPI({
    apiKey: process.env.OPENAI_API_KEY
  })

  const res = await api.sendMessage('Hello World!')
  console.log(res.text)
}

如果你想进行持续多轮的对话,需要传递 parentMessageid 和 conversationid:

const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })

// send a message and wait for the response
let res = await api.sendMessage('What is OpenAI?')
console.log(res.text)

// send a follow-up
res = await api.sendMessage('Can you expand on that?', {
  conversationId: res.conversationId,
  parentMessageId: res.id
})
console.log(res.text)

// send another follow-up
res = await api.sendMessage('What were we talking about?', {
  conversationId: res.conversationId,
  parentMessageId: res.id
})
console.log(res.text)

可以通过 onProgress 处理程序添加流式响应:

const res = await api.sendMessage('Write a 500 word essay on frogs.', {
  // print the partial response as the AI is "typing"
  onProgress: (partialResponse) => console.log(partialResponse.text)
})

// print the full text at the end
console.log(res.text)

也可以使用 timeoutMs 选项添加超时设置:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const response = await api.sendMessage(
  'write me a really really long essay on frogs',
  {
    timeoutMs: 2 * 60 * 1000
  }
)

如果想查看有关实际发送到 OpenAI 完成 API 的内容的更多信息,请在 ChatGPT API 构造函数中设置 debug: true 选项:

const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY,
  debug: true
})

你会注意到我们正在使用反向工程得到的 promptPrefix 和 promptSuffix。 你可以通过 sendMessage 的选项自定义这些:

const res = await api.sendMessage('what is the answer to the universe?', {
  promptPrefix: `You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don't be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short.
Current date: ${new Date().toISOString()}\n\n`
})

请注意,我们会自动处理将先前的消息附加到提示并尝试优化可用token(默认为 4096)。

在CommonJS中可以使用动态导入:

async function example() {
  // To use ESM in CommonJS, you can use a dynamic import
  const { ChatGPTAPI } = await import('chatgpt')

  const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })

  const res = await api.sendMessage('Hello World!')
  console.log(res.text)
}

完整的使用文档可以在这里查看。

3、使用演示程序

要运行包含的演示:

运行仓库中包含的基本演示程序:

npx tsx demos/demo.ts

chatgpt api如何使用

运行仓库中包含的显示进度处理的演示程序:

npx tsx demos/demo-on-progress.ts

上面这个演示使用 sendMessage可选的 onProgress 参数以接收中间结果,看起来就像 ChatGPT 正在“输入”。

chatgpt api如何使用

运行仓库中包含的多轮对话演示程序:

npx tsx demos/demo-conversation.ts

chatgpt api如何使用

仓库中的持久性演示展示了如何在 Redis 中存储消息以实现持久化:

npx tsx demos/demo-conversation.ts

任何 keyv 适配器都支持消息的持久化,如果你想使用不同的方式存储/检索消息,则可以进行覆盖。

请注意,需要持久化消息来记住当前 Node.js 进程范围之外的先前对话的上下文,因为默认情况下,我们仅将消息存储在内存中。

以上就是“chatgpt api如何使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. ChatGPT是什么及怎么使用
  2. chatgpt怎么使用

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

chatgpt api

上一篇:Python Tkinter Gui运行不卡顿的方法是什么

下一篇:ES6怎么将Set转化为数组

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》