GitHub | NPM | เอกสาร
ตอนนี้ด้วยการสนับสนุน CHATGPT API! ดู ใช้กับ CHATGPT API (กระซิบเร็ว ๆ นี้!)
ไลบรารีนี้ส่งคืนการตอบกลับ OpenAI API เป็นสตรีมเท่านั้น จุดสิ้นสุดที่ไม่ใช่สตรีมเช่น edits ฯลฯ เป็นเพียงสตรีมที่มีการอัปเดตเพียงก้อนเดียว
OPENAI_API_KEY จาก process.env ใช้ ReadableStream โดยค่าเริ่มต้นสำหรับเบราว์เซอร์, Edge Runtime และ Node 18+ โดยมีเวอร์ชัน NodeJS.Readable ที่ openai-streams/node
yarn add openai-streams
# -or-
npm i --save openai-streams await OpenAI (
/** 'completions', 'chat', etc. */
ENDPOINT ,
/** max_tokens, temperature, messages, etc. */
PARAMS ,
/** apiBase, apiKey, mode, controller, etc */
OPTIONS
) ; ตั้งค่าตัวแปร OPENAI_API_KEY ENV (หรือผ่านตัวเลือก { apiKey } )
ห้องสมุดจะโยนถ้าไม่พบคีย์ API โปรแกรมของคุณจะโหลดสิ่งนี้เมื่อรันไทม์จาก process.env.OPENAI_API_KEY โดยค่าเริ่มต้น แต่คุณอาจแทนที่สิ่งนี้ด้วยตัวเลือก { apiKey }
สำคัญ: เพื่อความปลอดภัยคุณควรโหลดสิ่งนี้จากตัวแปร process.env เท่านั้น
await OpenAI (
"completions" ,
{
/* endpoint params */
} ,
{ apiKey : process . env . MY_SECRET_API_KEY }
) ; โทรหา API ผ่าน await OpenAI(endpoint, params, options?)
ประเภท params จะถูกอนุมานตาม endpoint ที่คุณให้เช่นสำหรับ "edits" จุดสิ้นสุด import('openai').CreateEditRequest จะถูกบังคับใช้
ตัวอย่างด้วยโหมดสตรีม raw :
await OpenAI (
"chat" ,
{
messages : [
/* ... */
] ,
} ,
{ mode : "raw" }
) ; สิ่งนี้จะใช้งานได้ในเบราว์เซอร์ แต่คุณจะต้องใช้ผู้ใช้ในการวางคีย์ OpenAI ของพวกเขาและส่งผ่านในตัวเลือก { apiKey }
import { OpenAI } from "openai-streams" ;
export default async function handler ( ) {
const stream = await OpenAI ( "completions" , {
model : "text-davinci-003" ,
prompt : "Write a happy sentence.nn" ,
max_tokens : 100 ,
} ) ;
return new Response ( stream ) ;
}
export const config = {
runtime : "edge" ,
} ; หากคุณไม่สามารถใช้รันไทม์ขอบหรือต้องการใช้สตรีม Node.js ด้วยเหตุผลอื่นให้ใช้ openai-streams/node :
import type { NextApiRequest , NextApiResponse } from "next" ;
import { OpenAI } from "openai-streams/node" ;
export default async function test ( _ : NextApiRequest , res : NextApiResponse ) {
const stream = await OpenAI ( "completions" , {
model : "text-davinci-003" ,
prompt : "Write a happy sentence.nn" ,
max_tokens : 25 ,
} ) ;
stream . pipe ( res ) ;
} ดูตัวอย่างใน example/src/pages/api/hello.ts
โดยค่าเริ่มต้นด้วย mode = "tokens" คุณจะได้รับเพียง Deltas ข้อความ สำหรับเหตุการณ์เต็มรูปแบบให้ใช้ mode = "raw"
ดู: https://platform.openai.com/docs/guides/chat/introduction
const stream = await OpenAI ( "chat" , {
model : "gpt-3.5-turbo" ,
messages : [
{
role : "system" ,
content : "You are a helpful assistant that translates English to French." ,
} ,
{
role : "user" ,
content : 'Translate the following English text to French: "Hello world!"' ,
} ,
] ,
} ) ; ในโหมด tokens คุณจะได้รับการตอบกลับชิ้นซึ่งมีลักษณะเช่นนี้ (คั่นด้วย newLines สำหรับภาพประกอบ):
Hello
!
How
can
I
assist
you
today
?
ใช้ mode = "raw" สำหรับการเข้าถึงเหตุการณ์ดิบ
for await (const chunk of yieldStream(stream)) { ... } เราขอแนะนำให้ติดตามรูปแบบนี้หากคุณพบว่ามันใช้งานง่าย