Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openai-streams

Package Overview
Dependencies
Maintainers
1
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openai-streams

Streams-first OpenAI API client, written in TypeScript.

  • 1.0.11
  • npm
  • Socket score

Version published
Weekly downloads
419
decreased by-49.94%
Maintainers
1
Weekly downloads
 
Created
Source

OpenAI Streams

Github | NPM | Docs

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:

  1. Prioritizing streaming and type inference.
  2. Auto-loads OPENAI_API_KEY from process.env.
  3. 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.

Usage

  1. Set the OPENAI_API_KEY env variable.

    The runtime will throw if this is not available.

  2. Call the API via await OpenAI(endpoint, params).

    The params type will be inferred based on the endpoint you provide, i.e. for the "edits" endpoint, import('openai').CreateEditRequest will be enforced.

Example: Consuming streams in Next.js Edge functions
export default async function test() {
  const stream = await OpenAI(
    "completions",
    {
      model: "text-davinci-003",
      prompt: "Write a two-sentence paragraph.\n\n",
      temperature: 1,
      max_tokens: 100,
    },
  );

  return new Response(completionsStream);
}

Notes

  1. 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.

Keywords

FAQs

Package last updated on 19 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc