このパッケージはもはやブラウザのハッキングを必要としません - それは今ChatGPTが内部で使用するリークされたモデルで公式のOpenAI補完APIを使用しています。
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への大声で叫びます!
問題が発生した場合は、Node.js&PythonコミュニティのChatGPTハッカーの束とかなり活発なDiscordがあります。
最後に、このリポジトリに出演し、Twitterで私をフォローして、プロジェクトをサポートすることを検討してください。
ありがとう&乾杯、トラビス
ノード.js非公式のChatGPT APIのクライアント。
本パッケージは OpenAI による ChatGPT の Node.js ラッパーです。TSバッテリーが含まれています。
これを使用して、チャットボットやWebサイトなどのChatGPTを利用したプロジェクトの構築を開始できます。
npm install chatgpt
使用していることを確認してください そのように使用可能です (またはフェッチ ポリフィルをインストールする場合)。
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 ©トラビスフィッシャー
このプロジェクトが面白いと思ったら、私を後援するか、ツイッターでフォローすることを検討してください