openai-streams v6.2.0
OpenAI Streams
This library returns OpenAI API responses as streams only. Non-stream endpoints
like edits etc. are simply a stream with only one chunk update.
It simplifies the following:
- Prioritizing streaming and type inference.
- Auto-loads
OPENAI_API_KEYfromprocess.env. - Uses the same function for all endpoints, and switches the type based on the
OpenAI(endpoint, ...)signature.
Overall, the library aims to make it as simple to call the API as possible and stream updates in.
Installation
yarn add openai-streams
# or
npm i --save openai-streamsUsage
Set the
OPENAI_API_KEYenv variable (or pass the{ apiKey }option).The library will throw if it cannot find an API key. Your program will load this at runtime from
process.env.OPENAI_API_KEYby default, but you may override this with the{ apiKey }option.IMPORTANT: For security, you should only load this from a
process.envvariable.await OpenAI( "completions", {/* params */}, { apiKey: process.env.MY_SECRET_API_KEY } )Call the API via
await OpenAI(endpoint, params).The
paramstype will be inferred based on theendpointyou provide, i.e. for the"edits"endpoint,import('openai').CreateEditRequestwill be enforced.
Example: Consuming streams in Next.js Edge functions
import { OpenAI } from "openai-streams";
export default async function handler() {
const stream = await OpenAI(
"completions",
{
model: "text-davinci-003",
prompt: "Write a sentence.\n\n",
max_tokens: 100
},
);
return new Response(stream);
}
export const config = {
runtime: "edge"
};See the example in
example/src/pages/api/hello.ts.
See also
src/pages/api/demo.ts
in nextjs-openai.
Notes
- Internally, streams are often manipulated using generators via
for await (const chunk of yieldStream(stream)) { ... }. We recommend following this pattern if you find it intuitive.
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago