7.2.0 • Published 2 years ago

nextjs-openai v7.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

OpenAI for Next.js

Github | NPM | Demo | Docs

Adds hooks and components for working with OpenAI streams.

Installation

nextjs-openai includes frontend tools, and openai-streams includes tools for working with OpenAI streams in your API Routes.

yarn add nextjs-openai openai-streams

# -or-

npm i --save nextjs-openai openai-streams

Hooks

useBuffer() and useTextBuffer() are used to load an incrementing buffer of data (and text) from a given URL.

import { useTextBuffer } from "nextjs-openai";

export default function Demo() {
  const { buffer, refresh, cancel, done } = useTextBuffer({ 
    url: "/api/demo"
  });
  
  return (
    <div>
      <StreamingText buffer={buffer} fade={600} />
      <button onClick={refresh} disabled={!done}>Refresh</button>
      <button onClick={cancel} disabled={done}>Cancel</button>
    </div>
  );
}

Components

<StreamingText> and <StreamingTextURL> render text from a stream buffer using a fade-in animation.

import { StreamingTextURL } from "nextjs-openai";

export default function Demo() {
  return (
    <StreamingTextURL 
      url="/api/demo" 
      fade={600} 
      throttle={100} 
    />
  );
}

Sending data and advanced usage

If you would like to change the type of network request made with <StreamingTextURL> or the useBuffer() and useTextBuffer() hooks, you can set the { method, data } options.

{ data } is sent as the POST request body by default. To use a GET request, set { method = "GET" } and manually set the URL search params on { url }.

See src/pages/index.tsx for a live example.

With <StreamingTextURL>

import { StreamingTextURL } from "nextjs-openai";

export default function Home() {
  const [data, setData] = useState({ name: "John" });
  // ...
  return (
    <StreamingTextURL url="/api/demo" data={data}>
  );
}

With useTextBuffer()

import { useTextBuffer, StreamingText } from "nextjs-openai";

export default function Home() {
  const [data, setData] = useState({ name: "John" });
  const { buffer, refresh, cancel } = useTextBuffer({
    url: "/api/demo",
    throttle: 100,
    data,
    /**
     * Optional: Override params for `fetch(url, params)`.
     */
    options: {
      headers: {
        // ...
      }
    }
  });
  // ...
  return (
    <StreamingText buffer={buffer}>
  );
}

API Routes

Use openai-streams to work with streams in your API Routes.

Edge Runtime

import { OpenAI } from "openai-streams";

export default async function handler() {
  const stream = await OpenAI(
    "completions",
    {
      model: "text-davinci-003",
      prompt: "Write a happy sentence.\n\n",
      max_tokens: 25
    },
  );

  return new Response(stream);
}

export const config = {
  runtime: "edge"
};

Node <18

If you are not using Edge Runtime, import the NodeJS.Readable version from 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.\n\n",
      max_tokens: 25
    }
  );

  stream.pipe(res);
}
6.5.0-canary.0

2 years ago

6.1.0

2 years ago

6.1.2

2 years ago

6.1.1

2 years ago

6.2.3-canary.0

2 years ago

6.1.4

2 years ago

6.6.1-canary.0

2 years ago

6.4.0-canary.0

2 years ago

6.2.4-canary.0

2 years ago

7.1.0

2 years ago

6.8.1-canary.0

2 years ago

6.6.2-canary.0

2 years ago

6.6.2-canary.2

2 years ago

6.6.2-canary.1

2 years ago

6.8.0

2 years ago

5.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.2.1

2 years ago

6.0.3

2 years ago

6.2.0

2 years ago

6.0.2

2 years ago

6.9.1-canary.1

2 years ago

7.2.0

2 years ago

6.9.1-canary.0

2 years ago

6.10.0

2 years ago

6.2.2-canary.3

2 years ago

6.2.2-canary.0

2 years ago

6.7.0

2 years ago

6.1.6

2 years ago

6.1.5

2 years ago

6.9.0

2 years ago

6.2.5-canary.0

2 years ago

6.6.3-canary.1

2 years ago

6.6.3-canary.0

2 years ago

6.6.0-canary.0

2 years ago

6.3.0-canary.0

2 years ago

4.0.0

2 years ago

3.0.9

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

2.0.4-canary.1

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

1.3.4

2 years ago

2.0.0

2 years ago

1.3.4-canary.1

2 years ago

1.3.4-canary.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.14

2 years ago

1.2.13

2 years ago

1.2.12

2 years ago

1.2.11

2 years ago

1.2.10

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.1

2 years ago