embedding

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyInput     = errors.New("empty input text")
	ErrBatchTooLarge  = errors.New("batch size exceeds limit")
	ErrProviderFailed = errors.New("embedding provider failed")
)

Common errors returned by embedding providers.

Functions

This section is empty.

Types

type CachedProvider

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

CachedProvider wraps a Provider with an LRU cache for embeddings. This reduces redundant API calls for repeated queries.

func NewCachedProvider

func NewCachedProvider(provider Provider, cacheSize int) (*CachedProvider, error)

NewCachedProvider creates a new cached embedding provider. cacheSize specifies the maximum number of embeddings to cache.

func (*CachedProvider) CacheStats

func (c *CachedProvider) CacheStats() (size int, capacity int)

CacheStats returns cache statistics.

func (*CachedProvider) ClearCache

func (c *CachedProvider) ClearCache()

ClearCache removes all entries from the cache.

func (*CachedProvider) Close

func (c *CachedProvider) Close() error

Close closes the underlying provider.

func (*CachedProvider) Dimensions

func (c *CachedProvider) Dimensions() int

Dimensions returns the embedding dimensions from the underlying provider.

func (*CachedProvider) Embed

func (c *CachedProvider) Embed(ctx context.Context, text string) ([]float32, error)

Embed generates an embedding, using the cache if available.

func (*CachedProvider) EmbedBatch

func (c *CachedProvider) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

EmbedBatch generates embeddings for multiple texts, using cache where available. Non-cached texts are fetched from the underlying provider in a single batch.

type EmbeddingRequest

type EmbeddingRequest struct {
	Provider string   `json:"provider"` // e.g., "openai", "anthropic", "cohere"
	Model    string   `json:"model"`    // e.g., "text-embedding-3-small"
	Input    []string `json:"input"`    // Text inputs to embed
}

EmbeddingRequest represents a request to generate embeddings.

type EmbeddingResponse

type EmbeddingResponse struct {
	Embeddings [][]float32 `json:"embeddings"` // One embedding per input
	Model      string      `json:"model"`      // Model used
	Usage      UsageInfo   `json:"usage"`      // Token usage information
}

EmbeddingResponse represents the response from an embedding request.

type IrisClient

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

IrisClient implements the Provider interface using the iris SDK.

func NewIrisClient

func NewIrisClient(cfg *config.Config) (*IrisClient, error)

NewIrisClient creates a new Iris embedding client using the iris SDK.

func (*IrisClient) Close

func (c *IrisClient) Close() error

Close releases resources. For iris SDK, this is a no-op.

func (*IrisClient) Dimensions

func (c *IrisClient) Dimensions() int

Dimensions returns the configured embedding dimensions.

func (*IrisClient) Embed

func (c *IrisClient) Embed(ctx context.Context, text string) ([]float32, error)

Embed generates an embedding for a single text input.

func (*IrisClient) EmbedBatch

func (c *IrisClient) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

EmbedBatch generates embeddings for multiple texts in a single call.

type Provider

type Provider interface {
	// Embed generates an embedding vector for a single text input.
	// Returns a float32 slice of the configured dimensions.
	Embed(ctx context.Context, text string) ([]float32, error)

	// EmbedBatch generates embeddings for multiple texts in a single call.
	// This is more efficient than calling Embed repeatedly for bulk operations.
	// Implementations should respect configured batch size limits.
	EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

	// Dimensions returns the embedding vector dimensions.
	Dimensions() int

	// Close releases any resources held by the provider.
	Close() error
}

Provider defines the interface for embedding generation. Implementations may call external services (Iris, OpenAI) or provide local embeddings.

type UsageInfo

type UsageInfo struct {
	PromptTokens int `json:"prompt_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

UsageInfo contains token usage information for billing/monitoring.

Jump to

Keyboard shortcuts

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