embedding

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

nolint: errcheck // Operations may ignore return values

Package embedding provides vector embedding functionality with caching.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmbeddingFailed indicates embedding generation failed.
	ErrEmbeddingFailed = fmt.Errorf("embedding generation failed")
)

Embedding errors

Functions

This section is empty.

Types

type CacheKey

type CacheKey struct {
	Text   string
	Model  string
	Method string
}

CacheKey represents a cache key for embeddings.

func (*CacheKey) String

func (k *CacheKey) String() string

String returns the string representation of the cache key using BLAKE2b-128 hash. BLAKE2b provides: - Security: Cryptographically secure hash function - Performance: 20-30% faster than SHA256 - Efficiency: 128-bit output is sufficient for cache key collision resistance

type CacheStats

type CacheStats struct {
	Enabled    bool
	RedisKeys  int // Number of keys in Redis
	MemoryKeys int // Number of keys in memory
	TotalKeys  int // Total number of keys
	TTL        time.Duration
}

CacheStats represents cache statistics.

type EmbeddingCache

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

EmbeddingCache provides caching functionality for embeddings. It supports both Redis and in-memory cache as fallback.

func NewEmbeddingCache

func NewEmbeddingCache(redisClient RedisClient, ttl time.Duration) *EmbeddingCache

NewEmbeddingCache creates a new embedding cache. If redisClient is nil, it will use in-memory cache only.

func (*EmbeddingCache) Clear

func (c *EmbeddingCache) Clear(ctx context.Context) error

Clear removes all embeddings from cache.

func (*EmbeddingCache) Close

func (c *EmbeddingCache) Close()

Close stops the cache and cleans up resources. This should be called when the cache is no longer needed to prevent goroutine leaks.

func (*EmbeddingCache) Delete

func (c *EmbeddingCache) Delete(ctx context.Context, key *CacheKey) error

Delete removes an embedding from cache.

func (*EmbeddingCache) Disable

func (c *EmbeddingCache) Disable()

Disable disables the cache.

func (*EmbeddingCache) Enable

func (c *EmbeddingCache) Enable()

Enable enables the cache.

func (*EmbeddingCache) Get

func (c *EmbeddingCache) Get(ctx context.Context, key *CacheKey) ([]float64, bool)

Get retrieves an embedding from cache. It tries Redis first, then falls back to memory cache.

func (*EmbeddingCache) GetStats

func (c *EmbeddingCache) GetStats(ctx context.Context) (*CacheStats, error)

GetStats returns cache statistics.

func (*EmbeddingCache) IsEnabled

func (c *EmbeddingCache) IsEnabled() bool

IsEnabled returns whether the cache is enabled.

func (*EmbeddingCache) Set

func (c *EmbeddingCache) Set(ctx context.Context, key *CacheKey, embedding []float64) error

Set stores an embedding in cache. It stores in both Redis and memory cache (if available).

type EmbeddingClient

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

func NewEmbeddingClient

func NewEmbeddingClient(baseURL, model string, redisClient RedisClient, timeout time.Duration) *EmbeddingClient

func (*EmbeddingClient) Disable

func (c *EmbeddingClient) Disable()

Disable disables the embedding client.

func (*EmbeddingClient) Embed

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

Embed generates vector embedding for a query text (uses "query" prefix). For document storage, use EmbedWithPrefix with "passage:" prefix. Args: ctx - operation context. text - text to embed. Returns embedding vector or error.

func (*EmbeddingClient) EmbedBatch

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

EmbedBatch generates vector embeddings for multiple texts.

func (*EmbeddingClient) EmbedWithPrefix

func (c *EmbeddingClient) EmbedWithPrefix(ctx context.Context, text, prefix string) ([]float64, error)

EmbedWithPrefix generates vector embedding with custom prefix. Use "query:" for search queries and "passage:" for document storage. Args: ctx - operation context. text - text to embed. prefix - prefix to add before text (e.g., "query:", "passage:"). Returns embedding vector or error.

func (*EmbeddingClient) Enable

func (c *EmbeddingClient) Enable()

Enable enables the embedding client.

func (*EmbeddingClient) GetModel

func (c *EmbeddingClient) GetModel() string

GetModel returns the embedding model name.

func (*EmbeddingClient) GetTimeout

func (c *EmbeddingClient) GetTimeout() time.Duration

GetTimeout returns the embedding timeout.

func (*EmbeddingClient) HealthCheck

func (c *EmbeddingClient) HealthCheck(ctx context.Context) error

HealthCheck checks if the embedding service is healthy.

func (*EmbeddingClient) IsEnabled

func (c *EmbeddingClient) IsEnabled() bool

IsEnabled returns whether the embedding client is enabled.

type EmbeddingService

type EmbeddingService interface {
	// Embed generates vector embedding for a query text (uses "query" prefix).
	// For document storage, use EmbedWithPrefix with "passage:" prefix.
	//
	// Args:
	//   ctx - operation context.
	//   text - text to embed.
	//
	// Returns:
	//   []float64 - embedding vector.
	//   error - any error encountered.
	Embed(ctx context.Context, text string) ([]float64, error)

	// EmbedWithPrefix generates vector embedding with custom prefix.
	// Use "query:" for search queries and "passage:" for document storage.
	//
	// Args:
	//   ctx - operation context.
	//   text - text to embed.
	//   prefix - prefix to add before text (e.g., "query:", "passage:").
	//
	// Returns:
	//   []float64 - embedding vector.
	//   error - any error encountered.
	EmbedWithPrefix(ctx context.Context, text, prefix string) ([]float64, error)

	// EmbedBatch generates vector embeddings for multiple texts.
	//
	// Args:
	//   ctx - operation context.
	//   texts - texts to embed.
	//
	// Returns:
	//   [][]float64 - embedding vectors for each text.
	//   error - any error encountered.
	EmbedBatch(ctx context.Context, texts []string) ([][]float64, error)

	// HealthCheck checks if the embedding service is healthy.
	//
	// Args:
	//   ctx - operation context.
	//
	// Returns:
	//   error - any error encountered, nil if healthy.
	HealthCheck(ctx context.Context) error

	// GetModel returns the embedding model name.
	//
	// Returns:
	//   string - the model name.
	GetModel() string

	// GetTimeout returns the embedding timeout.
	//
	// Returns:
	//   time.Duration - the timeout duration.
	GetTimeout() time.Duration
}

EmbeddingService defines the interface for vector embedding operations. This interface allows for mocking in tests and swapping implementations.

type FallbackClient

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

FallbackClient wraps EmbeddingClient with fallback strategies.

func NewFallbackClient

func NewFallbackClient(client *EmbeddingClient, strategy FallbackStrategy) *FallbackClient

NewFallbackClient creates a new fallback client.

func (*FallbackClient) Embed

func (f *FallbackClient) Embed(ctx context.Context, text string) ([]float64, error)

Embed generates embedding with fallback support.

func (*FallbackClient) EmbedBatch

func (f *FallbackClient) EmbedBatch(ctx context.Context, texts []string) ([][]float64, error)

EmbedBatch generates embeddings with fallback support.

func (*FallbackClient) GetStrategy

func (f *FallbackClient) GetStrategy() FallbackStrategy

GetStrategy returns the current fallback strategy.

func (*FallbackClient) SetStrategy

func (f *FallbackClient) SetStrategy(strategy FallbackStrategy)

SetStrategy updates the fallback strategy.

type FallbackStrategy

type FallbackStrategy int

FallbackStrategy defines the fallback behavior when embedding fails.

const (
	// FallbackToCache falls back to cache only.
	FallbackToCache FallbackStrategy = iota

	// FallbackToKeyword falls back to keyword search (returns error).
	FallbackToKeyword

	// FallbackToError returns the error directly.
	FallbackToError
)

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    string
}

HTTPError represents an HTTP request error.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error returns the error message.

type MemoryCache

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

MemoryCache provides in-memory caching as a fallback.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new in-memory cache. The cleanup goroutine runs periodically to remove expired items.

func (*MemoryCache) Close

func (m *MemoryCache) Close()

Close stops the cleanup goroutine and cleans up resources. This should be called when the cache is no longer needed to prevent goroutine leaks.

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(key string)

Delete removes a value from memory cache.

func (*MemoryCache) Get

func (m *MemoryCache) Get(key string) ([]byte, bool)

Get retrieves a value from memory cache.

func (*MemoryCache) Keys

func (m *MemoryCache) Keys(pattern string) []string

Keys returns all keys matching pattern.

func (*MemoryCache) Set

func (m *MemoryCache) Set(key string, value []byte, ttl time.Duration)

Set stores a value in memory cache.

type RedisClient

type RedisClient interface {
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Del(ctx context.Context, keys ...string) error
	Keys(ctx context.Context, pattern string) ([]string, error)
}

RedisClient defines the interface for Redis operations. This allows for optional Redis dependency and easy testing.

Jump to

Keyboard shortcuts

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