chatgpt-api使用指南詳解教程【官方泄露版】
chatgpt-api是 OpenAI ChatGPT 的非官方的 Node.js 包裝器。 包括 TS 類型定義。 chatgpt-api不再需要任何瀏覽器破解——它使用泄露出來(lái)的OpenAI官方ChatGPT 在后臺(tái)使用的模型。 ??

?你可以使用它開始構(gòu)建由 ChatGPT 支持的項(xiàng)目,例如聊天機(jī)器人、網(wǎng)站等…
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)請(qǐng)升級(jí)到 chatgpt@latest(至少 v4.0.0)。 與以前的版本相比,更新后的版本明顯更加輕巧和健壯,你也不必?fù)?dān)心 IP 問(wèn)題或速率限制。

1、安裝chatgpt-api
確保你使用的是 node >= 18 以便 fetch 可用(node >= 14也可以,但你需要安裝 fetch polyfill)。
使用如下命令安裝 chatgpt-api :
npm install chatgpt
2、chatgpt-api使用方法
首先注冊(cè)獲取 OpenAI API 密鑰并將其存儲(chǔ)在你的環(huán)境中。
下面是簡(jiǎn)單的一次性對(duì)話:
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)
}如果你想進(jìn)行持續(xù)多輪的對(duì)話,需要傳遞 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)可以通過(guò) onProgress 處理程序添加流式響應(yīng):
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 選項(xiàng)添加超時(shí)設(shè)置:
// 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
}
)如果想查看有關(guān)實(shí)際發(fā)送到 OpenAI 完成 API 的內(nèi)容的更多信息,請(qǐng)?jiān)?ChatGPT API 構(gòu)造函數(shù)中設(shè)置 debug: true 選項(xiàng):
const api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY,
debug: true
})你會(huì)注意到我們正在使用反向工程得到的 promptPrefix 和 promptSuffix。 你可以通過(guò) sendMessage 的選項(xiàng)自定義這些:
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`
})請(qǐng)注意,我們會(huì)自動(dòng)處理將先前的消息附加到提示并嘗試優(yōu)化可用token(默認(rèn)為 4096)。
在CommonJS中可以使用動(dòng)態(tài)導(dǎo)入:
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、使用演示程序
要運(yùn)行包含的演示:
- 克隆這個(gè)倉(cāng)庫(kù)
- 安裝node.js依賴
- 在 .env 中設(shè)置 OPENAI_API_KEY
運(yùn)行倉(cāng)庫(kù)中包含的基本演示程序:
npx tsx demos/demo.ts

運(yùn)行倉(cāng)庫(kù)中包含的顯示進(jìn)度處理的演示程序:
npx tsx demos/demo-on-progress.ts
上面這個(gè)演示使用 sendMessage可選的 onProgress 參數(shù)以接收中間結(jié)果,看起來(lái)就像 ChatGPT 正在“輸入”。

運(yùn)行倉(cāng)庫(kù)中包含的多輪對(duì)話演示程序:
npx tsx demos/demo-conversation.ts

倉(cāng)庫(kù)中的持久性演示展示了如何在 Redis 中存儲(chǔ)消息以實(shí)現(xiàn)持久化:
npx tsx demos/demo-conversation.ts
任何 keyv 適配器都支持消息的持久化,如果你想使用不同的方式存儲(chǔ)/檢索消息,則可以進(jìn)行覆蓋。
請(qǐng)注意,需要持久化消息來(lái)記住當(dāng)前 Node.js 進(jìn)程范圍之外的先前對(duì)話的上下文,因?yàn)槟J(rèn)情況下,我們僅將消息存儲(chǔ)在內(nèi)存中。
到此這篇關(guān)于chatgpt-api使用指南【官方泄露版】的文章就介紹到這了,更多相關(guān)chatgpt-api使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
git_stats?web代碼圖形統(tǒng)計(jì)工具詳解
倉(cāng)庫(kù)代碼統(tǒng)計(jì)工具之一,可以按git提交人、提交次數(shù)、修改文件數(shù)、代碼行數(shù)、注釋量在時(shí)間維度上進(jìn)行統(tǒng)計(jì),亦可按各文件類型進(jìn)行簡(jiǎn)單的統(tǒng)計(jì),非常方便.本文給大家介紹git_stats?web代碼圖形統(tǒng)計(jì)工具,需要的朋友參考下吧2021-12-12
Typora?0.11.18免費(fèi)版本安裝使用教程(親測(cè)可用)
Typora是一款非常使用的筆記工具,對(duì)于程序員非常友好,在2021年11月23日,Typora?正式發(fā)布?1.0?版本,進(jìn)入了付費(fèi)時(shí)代,Typora免費(fèi)版本0.11.18(最后的免費(fèi)版),本文給大家分享Typora免費(fèi)獲取方法及安裝使用教程,感興趣的朋友參考下吧2022-07-07

