Documentation
¶
Overview ¶
nolint: errcheck // Operations may ignore return values
Package embedding provides vector embedding functionality with caching.
Index ¶
- Variables
- type CacheKey
- type CacheStats
- type EmbeddingCache
- func (c *EmbeddingCache) Clear(ctx context.Context) error
- func (c *EmbeddingCache) Close()
- func (c *EmbeddingCache) Delete(ctx context.Context, key *CacheKey) error
- func (c *EmbeddingCache) Disable()
- func (c *EmbeddingCache) Enable()
- func (c *EmbeddingCache) Get(ctx context.Context, key *CacheKey) ([]float64, bool)
- func (c *EmbeddingCache) GetStats(ctx context.Context) (*CacheStats, error)
- func (c *EmbeddingCache) IsEnabled() bool
- func (c *EmbeddingCache) Set(ctx context.Context, key *CacheKey, embedding []float64) error
- type EmbeddingClient
- func (c *EmbeddingClient) Disable()
- func (c *EmbeddingClient) Embed(ctx context.Context, text string) ([]float64, error)
- func (c *EmbeddingClient) EmbedBatch(ctx context.Context, texts []string) ([][]float64, error)
- func (c *EmbeddingClient) EmbedWithPrefix(ctx context.Context, text, prefix string) ([]float64, error)
- func (c *EmbeddingClient) Enable()
- func (c *EmbeddingClient) GetModel() string
- func (c *EmbeddingClient) GetTimeout() time.Duration
- func (c *EmbeddingClient) HealthCheck(ctx context.Context) error
- func (c *EmbeddingClient) IsEnabled() bool
- type EmbeddingService
- type FallbackClient
- type FallbackStrategy
- type HTTPError
- type MemoryCache
- type RedisClient
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmbeddingFailed indicates embedding generation failed. ErrEmbeddingFailed = fmt.Errorf("embedding generation failed") )
Embedding errors
Functions ¶
This section is empty.
Types ¶
type CacheKey ¶
CacheKey represents a cache key for embeddings.
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) Get ¶
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.
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 ¶
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 ¶
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) EmbedBatch ¶
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 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.
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.