fetch-stream-observable
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

fetch-stream-observable

Coverage Status npm version npm bundle size

Fetch API which returns an observable.

  • OpenAI stream parsing supported out of the box.
  • Gracefully degrades to calling subscription only once for non streaming response (json and text for eg).
  • Typescript
  • Lightweight (3.7kb min+gzip)

Installation

$ npm i fetch-stream-observable

Usage

import { fetchObservable } from 'fetch-stream-observable';

fetchObservable("https://api.openai.com/v1/chat/completions", {
    headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer <OPEN_AI_API_KEY>`,
    },
    method: "POST",
    body: JSON.stringify({
        /*
            OpenAI body payload.
        */
        // Tells openAI to send streaming response.
        stream: true, 
    }),
    // This disables automatic parsing of open ai response.
    doNotParseOpenAIStream: false, 
    // This will disable returning an additive complete response string.
    doNotConcatTextStream: false, 
})
    .subscribe((data: string) => {
        // Logs the complete string with each event, 
        // helpful with React `setState`.
        console.log(data); 
    });

Readme

Keywords

none

Package Sidebar

Install

npm i fetch-stream-observable

Weekly Downloads

1

Version

1.1.2

License

ISC

Unpacked Size

53.8 kB

Total Files

14

Last publish

Collaborators

  • ashubham