这个软件包不再需要任何浏览器黑客攻击——它现在使用官方的 OpenAI 完成 API,其中包含 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)
请升级到(至少 v4.0.0)。与以前的版本相比,更新后的版本更加轻巧和强大。你也不必担心 IP 问题或速率限制。
chatgpt@latest
为发现泄露的聊天模型而向@waylaidwanderer大喊大叫!
如果你遇到任何问题,我们确实有一个非常活跃的Discord,来自Node.js和Python社区的一群ChatGPT黑客。
最后,请考虑主演这个回购,并在推特上关注我,以帮助支持该项目。
谢谢&&干杯,特拉维斯
Node.js非官方ChatGPT API的客户端。
这个包是OpenAI围绕ChatGPT的Node.js包装器。包括 TS 电池。
你可以使用它开始构建由 ChatGPT 提供支持的项目,例如聊天机器人、网站等......
npm install chatgpt
确保你使用的是 so 可用(或者如果你安装了提取 polyfill)。
node >= 18
fetch
node >= 14
注册 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
// timeout after 2 minutes (which will also abort the underlying HTTP request)
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 的内容的详细信息,请在构造函数中设置该选项:
debug: true
ChatGPTAPI
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`
})
请注意,我们会自动处理将以前的消息附加到提示符,并尝试针对可用令牌进行优化(默认为 )。
4096
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)
}
有关方法和参数的详细信息,请参阅自动生成的文档。
要运行包含的演示,请执行以下操作:
OPENAI_API_KEY
包括一个用于测试目的的基本演示:
npx tsx demos/demo.ts
npx tsx demos/demo-on-progress.ts
正在进行的演示使用可选参数来接收中间结果,因为 ChatGPT 正在“键入”。
onProgress
sendMessage
对话演示:
npx tsx demos/demo-conversation.ts
持久性演示展示了如何在 Redis 中存储消息以实现持久性:
npx tsx demos/demo-conversation.ts
支持任何 keyv 适配器的持久性,如果你想使用不同的方式存储/检索消息,则可以覆盖。
请注意,持久化消息对于记住当前 Node.js 进程范围之外的先前对话的上下文是必需的,因为默认情况下,我们只将消息存储在内存中。
所有这些很棒的项目都是使用该软件包构建的。
chatgpt
如果你创建了一个很酷的集成,请随时打开 PR 并将其添加到列表中。
node >= 14
fetch
node >= 18
node < 18
unfetch/polyfill)
chatgpt
© MIT 特拉维斯·费舍尔