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
- func AssistantPrompt(text string) chat.Message
- func CosineSimilarity64(a, b []float64) float64
- func CountMessageTokens(msg chat.Message) int
- func CountRequestTokens(req chat.Request) int
- func CountTokens(text string) int
- func DetectAudioFormat(data []byte) string
- func FormatMessages(messages []chat.Message) string
- func GenerateID(prefix string, size int) string
- func SimulateStream(text string, delay time.Duration) <-chan string
- func SmoothStream(ch <-chan string, opts SmoothStreamOptions) <-chan string
- func SystemPrompt(instructions string) chat.Message
- func ToolResultMessage(callID, text string) chat.Message
- func UserPrompt(text string) chat.Message
- type IDGenerator
- type SmoothStreamOptions
Constants ¶
const DefaultIDPrefix = "ai"
DefaultIDPrefix is used by GenerateID.
const DefaultIDSize = 16
DefaultIDSize is used by GenerateID.
Variables ¶
This section is empty.
Functions ¶
func AssistantPrompt ¶
AssistantPrompt creates an assistant role chat.Message with the provided text.
func CosineSimilarity64 ¶
CosineSimilarity64 calculates the cosine similarity between two float64 vectors. Returns 0 if either vector has zero magnitude.
func CountMessageTokens ¶
CountMessageTokens counts tokens in a single chat.Message by counting its textual representation (Parts/Text or Content).
func CountRequestTokens ¶
CountRequestTokens counts tokens across all messages in a chat.Request and includes a small overhead per message.
func CountTokens ¶
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 ¶
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 ¶
FormatMessages formats a slice of chat.Message into a human-readable string useful for logging or developer display.
func GenerateID ¶
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 ¶
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 ¶
SystemPrompt creates a system role chat.Message with the provided instructions.
func ToolResultMessage ¶
ToolResultMessage creates a tool (RoleTool) message referencing the originating call and carrying the tool output.
func UserPrompt ¶
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 ¶
SmoothStream options for controlling stream smoothing behaviour.