Documentation
¶
Overview ¶
Package semantic provides a Redis Stack (RediSearch) vector store used by the semantic cache plugin to look up and persist responses by embedding similarity, scoped per rule via a hashed tag.
Index ¶
- type Candidate
- type Deps
- type Entry
- type MemoryStore
- func (s *MemoryStore) EnsureIndex(ctx context.Context, dimension int) error
- func (s *MemoryStore) GetExact(ctx context.Context, ruleID, key string) (string, bool, error)
- func (s *MemoryStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
- func (s *MemoryStore) PutExact(ctx context.Context, ruleID, key, response string, ttl time.Duration) error
- func (s *MemoryStore) Store(ctx context.Context, entry Entry) error
- type Option
- type PgvectorStore
- func (s *PgvectorStore) EnsureIndex(ctx context.Context, dimension int) error
- func (s *PgvectorStore) GetExact(ctx context.Context, ruleID, key string) (string, bool, error)
- func (s *PgvectorStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
- func (s *PgvectorStore) PutExact(ctx context.Context, ruleID, key, response string, ttl time.Duration) error
- func (s *PgvectorStore) Store(ctx context.Context, entry Entry) error
- type RedisStore
- func (s *RedisStore) EnsureIndex(ctx context.Context, dimension int) error
- func (s *RedisStore) GetExact(ctx context.Context, ruleID, key string) (string, bool, error)
- func (s *RedisStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
- func (s *RedisStore) PutExact(ctx context.Context, ruleID, key, response string, ttl time.Duration) error
- func (s *RedisStore) Store(ctx context.Context, entry Entry) error
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Deps ¶ added in v0.2.4
Deps carries the backend handles a Store may need; only the field matching the selected kind is required.
type Entry ¶
type Entry struct {
RuleID string
Embedding *embedding.Embedding
Response string
TTL time.Duration
}
Entry is a response to persist for future similarity lookups.
type MemoryStore ¶ added in v0.2.4
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-process Store backed by a TTL map and brute-force cosine similarity. It suits development and small single-replica deployments; eviction is purely TTL-based.
func NewMemoryStore ¶ added in v0.2.4
func NewMemoryStore(logger *slog.Logger) *MemoryStore
NewMemoryStore builds an empty in-memory Store.
func (*MemoryStore) EnsureIndex ¶ added in v0.2.4
func (s *MemoryStore) EnsureIndex(ctx context.Context, dimension int) error
EnsureIndex is a no-op for the in-memory backend.
func (*MemoryStore) GetExact ¶ added in v0.2.4
GetExact returns the response cached under the exact key for the rule when it is still live, filtering out expired entries on read.
func (*MemoryStore) Lookup ¶ added in v0.2.4
func (s *MemoryStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
Lookup returns the topK live cached responses for the rule, ordered by descending cosine similarity.
type Option ¶
type Option func(*RedisStore)
Option customizes a RedisStore.
func WithIndexName ¶
WithIndexName overrides the RediSearch index name.
type PgvectorStore ¶ added in v0.2.4
type PgvectorStore struct {
// contains filtered or unexported fields
}
PgvectorStore implements Store against PostgreSQL with the pgvector extension. The schema is created lazily on first use so the backend stays inert (and never blocks process startup) unless it is the configured vector store.
func NewPgvectorStore ¶ added in v0.2.4
func NewPgvectorStore(pool *pgxpool.Pool, logger *slog.Logger) *PgvectorStore
NewPgvectorStore builds a vector store over the given pgx pool.
func (*PgvectorStore) EnsureIndex ¶ added in v0.2.4
func (s *PgvectorStore) EnsureIndex(ctx context.Context, dimension int) error
EnsureIndex validates the requested dimension. A dimension other than the supported width is rejected so the plugin degrades to pass-through. Schema creation is deferred to the first data operation via ensureSchema.
func (*PgvectorStore) GetExact ¶ added in v0.2.4
GetExact returns the response cached under the exact key for the rule. A miss or a transient backend error degrades to ("", false, nil) so the caller falls through to the upstream.
func (*PgvectorStore) Lookup ¶ added in v0.2.4
func (s *PgvectorStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
Lookup returns the topK nearest cached responses for the rule, ordered by descending cosine similarity. Any query error degrades to no candidates so the caller falls through to the upstream.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements Store against Redis Stack RediSearch.
func NewRedisStore ¶
NewRedisStore builds a vector store over the given Redis client.
func (*RedisStore) EnsureIndex ¶
func (s *RedisStore) EnsureIndex(ctx context.Context, dimension int) error
EnsureIndex creates the vector index once, tolerating a pre-existing index.
func (*RedisStore) GetExact ¶ added in v0.2.4
GetExact returns the response cached under the exact key for the rule. A miss or a transient backend error degrades to ("", false, nil) so the caller falls through to the upstream, matching Lookup's degrade-on-error contract.
func (*RedisStore) Lookup ¶
func (s *RedisStore) Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
Lookup returns the topK nearest cached responses for the rule, ordered by descending similarity. A query error degrades to no candidates so the caller can fall through to the upstream.
type Store ¶
type Store interface {
EnsureIndex(ctx context.Context, dimension int) error
Lookup(ctx context.Context, ruleID string, emb *embedding.Embedding, topK int) ([]Candidate, error)
Store(ctx context.Context, entry Entry) error
GetExact(ctx context.Context, ruleID, key string) (string, bool, error)
PutExact(ctx context.Context, ruleID, key, response string, ttl time.Duration) error
}
Store is the vector cache backing the semantic cache plugin.