streaming

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStreamClosed is returned when trying to read from a closed stream
	ErrStreamClosed = errors.New("stream is closed")

	// ErrInvalidSSE is returned when SSE data is malformed
	ErrInvalidSSE = errors.New("invalid SSE format")
)
View Source
var SupportedProviders = map[string]bool{
	"openai":     true,
	"azure":      true,
	"anthropic":  true,
	"fireworks":  true,
	"mancer":     true,
	"recursal":   true,
	"anyscale":   true,
	"lepton":     true,
	"octoai":     true,
	"novita":     true,
	"deepinfra":  true,
	"together":   true,
	"cohere":     true,
	"hyperbolic": true,
	"infermatic": true,
	"avian":      true,
	"xai":        true,
	"cloudflare": true,
	"sfcompute":  true,
	"nineteen":   true,
	"liquid":     true,
	"friendli":   true,
	"chutes":     true,
	"deepseek":   true,
}

SupportedProviders lists providers that support stream cancellation

Functions

func IsProviderSupported

func IsProviderSupported(provider string) bool

IsProviderSupported checks if a provider supports stream cancellation

Types

type CancellableReader

type CancellableReader struct {
	// contains filtered or unexported fields
}

CancellableReader wraps a reader with cancellation support

func NewCancellableReader

func NewCancellableReader(reader io.ReadCloser, ctx context.Context) *CancellableReader

NewCancellableReader creates a new cancellable reader

func (*CancellableReader) Cancel

func (r *CancellableReader) Cancel()

Cancel cancels the stream

func (*CancellableReader) Close

func (r *CancellableReader) Close() error

Close closes the reader and cancels the context

func (*CancellableReader) Read

func (r *CancellableReader) Read(p []byte) (n int, err error)

Read implements io.Reader

type ChatCompletionStreamReader

type ChatCompletionStreamReader struct {
	// contains filtered or unexported fields
}

ChatCompletionStreamReader reads streaming chat completions

func NewChatCompletionStreamReader

func NewChatCompletionStreamReader(reader io.ReadCloser) *ChatCompletionStreamReader

NewChatCompletionStreamReader creates a new stream reader

func (*ChatCompletionStreamReader) Close

func (r *ChatCompletionStreamReader) Close() error

Close closes the stream

func (*ChatCompletionStreamReader) Read

Read reads the next chunk from the stream

type CompletionResponse

type CompletionResponse struct {
	ID      string                   `json:"id"`
	Object  string                   `json:"object"`
	Created int64                    `json:"created"`
	Model   string                   `json:"model"`
	Choices []CompletionStreamChoice `json:"choices"`
	Usage   *models.Usage            `json:"usage,omitempty"`
}

CompletionResponse represents a streaming completion response chunk

type CompletionStreamChoice

type CompletionStreamChoice struct {
	Index        int                 `json:"index"`
	Text         string              `json:"text,omitempty"`
	FinishReason string              `json:"finish_reason,omitempty"`
	Error        *models.ChoiceError `json:"error,omitempty"`
}

CompletionStreamChoice represents a streaming choice

type CompletionStreamReader

type CompletionStreamReader struct {
	// contains filtered or unexported fields
}

CompletionStreamReader reads streaming text completions

func NewCompletionStreamReader

func NewCompletionStreamReader(reader io.ReadCloser) *CompletionStreamReader

NewCompletionStreamReader creates a new completion stream reader

func (*CompletionStreamReader) Close

func (r *CompletionStreamReader) Close() error

Close closes the stream

func (*CompletionStreamReader) Read

Read reads the next chunk from the stream

type SSEEvent

type SSEEvent struct {
	Event string
	Data  string
	ID    string
}

SSEEvent represents a Server-Sent Event

type SSEParser

type SSEParser struct {
	// contains filtered or unexported fields
}

SSEParser parses Server-Sent Events

func NewSSEParser

func NewSSEParser(reader io.Reader) *SSEParser

NewSSEParser creates a new SSE parser

func (*SSEParser) ParseNext

func (p *SSEParser) ParseNext() (*SSEEvent, error)

ParseNext parses the next SSE event

type StreamController

type StreamController struct {
	// contains filtered or unexported fields
}

StreamController provides advanced stream control

func NewStreamController

func NewStreamController(reader io.ReadCloser, ctx context.Context) *StreamController

NewStreamController creates a new stream controller

func (*StreamController) Cancel

func (c *StreamController) Cancel()

Cancel cancels the stream

func (*StreamController) Close

func (c *StreamController) Close() error

Close closes the stream

func (*StreamController) Read

func (c *StreamController) Read() (*SSEEvent, error)

Read reads the next event

Jump to

Keyboard shortcuts

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