MCPcopy Index your code
hub / github.com/EvolvingPrograms/nextjs-openai

github.com/EvolvingPrograms/nextjs-openai @7.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 7.2.0 ↗ · + Follow
17 symbols 42 edges 24 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 (



      <StreamingText buffer={buffer} fade={600} />
      <button onClick={refresh} disabled={!done}>Refresh</button>
      <button onClick={cancel} disabled={done}>Cancel</button>



  );
}

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);
}

Extension points exported contracts — how you extend this code

StreamingTextProps (Interface)
(no doc)
src/components/StreamingText/index.tsx
BufferConfig (Interface)
(no doc)
src/hooks/types.ts

Core symbols most depended-on inside this repo

useTextBuffer
called by 4
src/hooks/useTextBuffer/index.ts
useBuffer
called by 1
src/hooks/useBuffer/index.ts
StreamingText
called by 0
src/components/StreamingText/index.tsx
StreamingTextURL
called by 0
src/components/StreamingText/index.tsx
App
called by 0
src/pages/_app.tsx
Home
called by 0
src/pages/node.tsx
setName
called by 0
src/pages/node.tsx
Home
called by 0
src/pages/index.tsx

Shape

Function 15
Interface 2

Languages

TypeScript100%

Modules by API surface

src/components/StreamingText/index.tsx3 symbols
src/pages/node.tsx2 symbols
src/pages/index.tsx2 symbols
src/pages/completion.tsx2 symbols
src/pages/api/node.ts1 symbols
src/pages/api/completion.ts1 symbols
src/pages/api/chat.ts1 symbols
src/pages/_app.tsx1 symbols
src/hooks/useTextBuffer/index.ts1 symbols
src/hooks/useBuffer/state.ts1 symbols
src/hooks/useBuffer/index.ts1 symbols
src/hooks/types.ts1 symbols

For agents

$ claude mcp add nextjs-openai \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact