NovaKitv1.0

Streaming Responses

Handle streaming responses for real-time chat output

Streaming Responses

Streaming allows you to receive chat responses token-by-token as they're generated, providing a better user experience for real-time applications.

Basic Streaming

from openai import OpenAI

client = OpenAI(
    api_key="sk_your_api_key",
    base_url="https://www.novakit.ai/api/v1"
)

stream = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "sk_your_api_key",
  baseURL: "https://www.novakit.ai/api/v1",
});

const stream = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  messages: [{ role: "user", content: "Tell me a story" }],
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || "";
  process.stdout.write(content);
}
curl -N https://www.novakit.ai/api/v1/chat/completions \
  -H "Authorization: Bearer sk_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o-mini",
    "messages": [{"role": "user", "content": "Tell me a story"}],
    "stream": true
  }'

React/Next.js Integration

Using the Vercel AI SDK:

// app/api/chat/route.ts
import { streamText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";

const novakit = createOpenAI({
  apiKey: process.env.NOVAKIT_API_KEY,
  baseURL: "https://www.novakit.ai/api/v1",
});

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: novakit("openai/gpt-4o-mini"),
    messages,
  });

  return result.toDataStreamResponse();
}
// components/chat.tsx
"use client";

import { useChat } from "ai/react";

export function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat();

  return (
    <div>
      {messages.map((m) => (
        <div key={m.id}>
          {m.role}: {m.content}
        </div>
      ))}

      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
        <button type="submit">Send</button>
      </form>
    </div>
  );
}

SSE Event Format

The stream consists of Server-Sent Events (SSE):

data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"Hello"}}]}

data: {"id":"chatcmpl-123","choices":[{"delta":{"content":" there"}}]}

data: {"id":"chatcmpl-123","choices":[{"delta":{"content":"!"}}]}

data: [DONE]

Handling Stream Completion

from openai import OpenAI

client = OpenAI(
    api_key="sk_your_api_key",
    base_url="https://www.novakit.ai/api/v1"
)

full_response = ""
stream = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a haiku"}],
    stream=True
)

for chunk in stream:
    content = chunk.choices[0].delta.content or ""
    full_response += content
    print(content, end="", flush=True)

print(f"\n\nFull response: {full_response}")

On this page