chatgpt-api使用指南詳解教程【官方泄露版】
chatgpt-api是 OpenAI ChatGPT 的非官方的 Node.js 包裝器。 包括 TS 類型定義。 chatgpt-api不再需要任何瀏覽器破解——它使用泄露出來的OpenAI官方ChatGPT 在后臺使用的模型。 ??
?你可以使用它開始構(gòu)建由 ChatGPT 支持的項目,例如聊天機器人、網(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)
請升級到 chatgpt@latest(至少 v4.0.0)。 與以前的版本相比,更新后的版本明顯更加輕巧和健壯,你也不必擔(dān)心 IP 問題或速率限制。
1、安裝chatgpt-api
確保你使用的是 node >= 18 以便 fetch 可用(node >= 14也可以,但你需要安裝 fetch polyfill)。
使用如下命令安裝 chatgpt-api :
npm install chatgpt
2、chatgpt-api使用方法
首先注冊獲取 OpenAI API 密鑰并將其存儲在你的環(huán)境中。
下面是簡單的一次性對話:
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) }
如果你想進行持續(xù)多輪的對話,需要傳遞 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 處理程序添加流式響應(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 選項添加超時設(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)實際發(fā)送到 OpenAI 完成 API 的內(nèi)容的更多信息,請在 ChatGPT API 構(gòu)造函數(shù)中設(shè)置 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` })
請注意,我們會自動處理將先前的消息附加到提示并嘗試優(yōu)化可用token(默認為 4096)。
在CommonJS中可以使用動態(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、使用演示程序
要運行包含的演示:
- 克隆這個倉庫
- 安裝node.js依賴
- 在 .env 中設(shè)置 OPENAI_API_KEY
運行倉庫中包含的基本演示程序:
npx tsx demos/demo.ts
運行倉庫中包含的顯示進度處理的演示程序:
npx tsx demos/demo-on-progress.ts
上面這個演示使用 sendMessage可選的 onProgress 參數(shù)以接收中間結(jié)果,看起來就像 ChatGPT 正在“輸入”。
運行倉庫中包含的多輪對話演示程序:
npx tsx demos/demo-conversation.ts
倉庫中的持久性演示展示了如何在 Redis 中存儲消息以實現(xiàn)持久化:
npx tsx demos/demo-conversation.ts
任何 keyv 適配器都支持消息的持久化,如果你想使用不同的方式存儲/檢索消息,則可以進行覆蓋。
請注意,需要持久化消息來記住當(dāng)前 Node.js 進程范圍之外的先前對話的上下文,因為默認情況下,我們僅將消息存儲在內(nèi)存中。
到此這篇關(guān)于chatgpt-api使用指南【官方泄露版】的文章就介紹到這了,更多相關(guān)chatgpt-api使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
win10環(huán)境下使用Hyper-V進行虛擬機創(chuàng)建的教程(圖解)
這篇文章主要介紹了win10環(huán)境下使用Hyper-V進行虛擬機創(chuàng)建的教程,本文圖文詳解給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11如何用Idea或者webstorm跑一個Vue項目(步驟詳解)
這篇文章主要介紹了如何用Idea或者webstorm跑一個Vue項目,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Bottle部署web服務(wù)及postman接口的方法
這篇文章主要介紹了Bottle部署web服務(wù)及postman接口的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01