util

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package util provides shared utilities used across the AI SDK.

This includes ID generation (equivalent to the AI SDK's generateId and createIdGenerator) and stream helpers (equivalent to simulateReadableStream and smoothStream).

Package util provides shared utility functions for the AI SDK.

Index

Constants

View Source
const DefaultIDPrefix = "ai"

DefaultIDPrefix is used by GenerateID.

View Source
const DefaultIDSize = 16

DefaultIDSize is used by GenerateID.

Variables

This section is empty.

Functions

func AssistantPrompt

func AssistantPrompt(text string) chat.Message

AssistantPrompt creates an assistant role chat.Message with the provided text.

func CosineSimilarity64

func CosineSimilarity64(a, b []float64) float64

CosineSimilarity64 calculates the cosine similarity between two float64 vectors. Returns 0 if either vector has zero magnitude.

func CountMessageTokens

func CountMessageTokens(msg chat.Message) int

CountMessageTokens counts tokens in a single chat.Message by counting its textual representation (Parts/Text or Content).

func CountRequestTokens

func CountRequestTokens(req chat.Request) int

CountRequestTokens counts tokens across all messages in a chat.Request and includes a small overhead per message.

func CountTokens

func CountTokens(text string) int

CountTokens approximates token count. Uses a simple heuristic: number of word-like tokens + punctuation tokens. Approx 1 token ≈ 4 chars is a guideline but we return the token count directly.

func DetectAudioFormat

func DetectAudioFormat(data []byte) string

DetectAudioFormat attempts to detect the audio format from the first few bytes of the audio data using magic bytes.

Returns the file extension (e.g. "mp3", "wav", "ogg") or an empty string if the format cannot be determined.

func FormatMessages

func FormatMessages(messages []chat.Message) string

FormatMessages formats a slice of chat.Message into a human-readable string useful for logging or developer display.

func GenerateID

func GenerateID(prefix string, size int) string

GenerateID returns a unique identifier with the given prefix and random portion length. It uses crypto/rand for randomness and hex encoding.

This is the Go equivalent of the AI SDK's generateId function.

func SimulateStream

func SimulateStream(text string, delay time.Duration) <-chan string

SimulateStream creates a channel that emits tokens from text one at a time with the specified delay between each token. Intended for testing and development.

This is the Go equivalent of the AI SDK's simulateReadableStream.

func SmoothStream

func SmoothStream(ch <-chan string, opts SmoothStreamOptions) <-chan string

SmoothStream wraps a string channel and applies inter-chunk delays to create a smoother streaming experience. Intended for providers that return large chunks all at once.

This is the Go equivalent of the AI SDK's smoothStream.

func SystemPrompt

func SystemPrompt(instructions string) chat.Message

SystemPrompt creates a system role chat.Message with the provided instructions.

func ToolResultMessage

func ToolResultMessage(callID, text string) chat.Message

ToolResultMessage creates a tool (RoleTool) message referencing the originating call and carrying the tool output.

func UserPrompt

func UserPrompt(text string) chat.Message

UserPrompt creates a user role chat.Message with the provided text.

Types

type IDGenerator

type IDGenerator func() string

IDGenerator is a function that produces unique IDs on each call. It is the Go equivalent of the AI SDK's createIdGenerator.

func NewIDGenerator

func NewIDGenerator(prefix string, size int) IDGenerator

NewIDGenerator returns an IDGenerator that produces IDs with the given prefix and size.

type SmoothStreamOptions

type SmoothStreamOptions struct {
	// Delay is the base delay between chunks.
	Delay time.Duration
}

SmoothStream options for controlling stream smoothing behaviour.

Jump to

Keyboard shortcuts

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