streaming

command
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Streaming Example

Real-time response streaming with the PromptKit SDK.

What You'll Learn

  • Using Stream() for real-time responses
  • Processing chunks as they arrive
  • Handling stream completion and errors
  • Progress tracking during generation

Prerequisites

  • Go 1.26+
  • An API key for one of: OpenAI, Anthropic, or Google

Running the Example

The pack does not name a provider — the SDK detects one from the environment, so any of these works:

export OPENAI_API_KEY=your-key       # uses gpt-4o
# or
export ANTHROPIC_API_KEY=your-key
# or
export GOOGLE_API_KEY=your-key

go run .

Code Overview

conv, err := sdk.Open("./streaming.pack.json", "storyteller")
if err != nil {
    log.Fatal(err)
}
defer conv.Close()

ctx := context.Background()

// Stream responses in real-time
for chunk := range conv.Stream(ctx, "Tell me a short story") {
    if chunk.Error != nil {
        log.Printf("Error: %v", chunk.Error)
        break
    }
    if chunk.Type == sdk.ChunkDone {
        fmt.Println("\n[Complete]")
        break
    }
    // Print text as it arrives
    fmt.Print(chunk.Text)
}

Chunk Types

const (
    ChunkText     // Text content arrived
    ChunkToolCall // Tool is being called
    ChunkDone     // Stream completed
)

Pack File Structure

The streaming.pack.json defines:

  • Prompt: A creative storyteller with higher temperature

It deliberately does not pin a provider, so the example runs against whichever of OpenAI / Anthropic / Google you have a key for.

{
  "prompts": {
    "storyteller": {
      "system_template": "You are a creative storyteller...",
      "parameters": {
        "temperature": 0.9,
        "max_tokens": 500
      }
    }
  }
}

Progress Tracking

Track generation progress while streaming:

var charCount int

for chunk := range conv.Stream(ctx, "Tell me about AI") {
    if chunk.Type == sdk.ChunkDone {
        fmt.Printf("\n[Complete - %d characters]\n", charCount)
        break
    }
    fmt.Print(chunk.Text)
    charCount += len(chunk.Text)
}

Key Concepts

  1. Channel-Based - Stream() returns a channel of chunks
  2. Non-Blocking - Print responses as they arrive
  3. Error Handling - Check chunk.Error for issues
  4. Completion - ChunkDone signals end of stream

Next Steps

Documentation

Overview

Package main demonstrates streaming responses with the PromptKit SDK.

This example shows:

  • Using Stream() for real-time response streaming
  • Processing chunks as they arrive

Run with:

export OPENAI_API_KEY=your-key
go run .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL