Documentation
¶
Index ¶
- func ApplyRuntimeConfig(cfg RuntimeConfig)
- func BuildNamespace(apiKeyID int, endpointFamily, requestedModel string) string
- func Clear()
- func Enabled() bool
- func ExtractNormalizedText(req *transmodel.InternalLLMRequest) (string, bool)
- func GenerateEmbedding(text string) []float64
- func Init(maxEntries int, threshold float64, ttlSec int)
- func Lookup(namespace string, embedding []float64) (responseJSON []byte, found bool)
- func RecordBypass()
- func RecordEvaluated()
- func RecordHit()
- func RecordMiss()
- func RecordStored()
- func Reset()
- func ResetRuntimeStats()
- func RuntimeEnabled() bool
- func Stats() (hits, misses int64, size int)
- func Store(namespace, requestKey string, responseJSON []byte, embedding []float64)
- type CacheEntry
- type EmbeddingClient
- type RuntimeConfig
- type RuntimeStats
- type SemanticCache
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyRuntimeConfig ¶
func ApplyRuntimeConfig(cfg RuntimeConfig)
ApplyRuntimeConfig creates or reconfigures the global semantic cache from runtime settings. When the cache already exists and the size/threshold/TTL parameters are unchanged, the existing cache is reused so stored entries are not discarded.
func BuildNamespace ¶
func Enabled ¶
func Enabled() bool
Enabled returns true if the semantic cache is initialized and active.
func ExtractNormalizedText ¶
func ExtractNormalizedText(req *transmodel.InternalLLMRequest) (string, bool)
func GenerateEmbedding ¶
GenerateEmbedding creates a 64-dimension embedding from the input text. Currently uses a SHA-256 hash for deterministic caching — identical requests produce identical embeddings, while similar (but not identical) requests will not match. A future upgrade can replace this with a real embedding model via the system's embedding relay pipeline.
func Lookup ¶
Lookup finds the best matching cache entry for the given embedding. Returns the response JSON and true if a match above threshold is found.
The cosine-similarity scan runs under a read lock so that concurrent lookups proceed in parallel rather than serializing on a single mutex. Expired entries are skipped inline; structural removal of expired entries is deferred to Store/Stats (which hold the write lock). Hit metadata is updated atomically, so recording a hit does not require the write lock.
func RecordBypass ¶
func RecordBypass()
func RecordEvaluated ¶
func RecordEvaluated()
func RecordMiss ¶
func RecordMiss()
func RecordStored ¶
func RecordStored()
func ResetRuntimeStats ¶
func ResetRuntimeStats()
func RuntimeEnabled ¶
func RuntimeEnabled() bool
Types ¶
type CacheEntry ¶
type CacheEntry struct {
Namespace string
RequestKey string
ResponseJSON []byte
Embedding []float64
CreatedAt time.Time
HitCount atomic.Int64
// contains filtered or unexported fields
}
CacheEntry holds a cached request/response pair with its embedding.
Entries are stored behind pointers (see SemanticCache.entries) and the mutable hit metadata (lastAccessNs / HitCount) is accessed atomically so that Lookup can scan and record hits under a read lock without serializing concurrent lookups. The immutable fields (Namespace, ResponseJSON, Embedding, CreatedAt) are written once at Store time and never mutated in place afterwards, so they are safe to read after releasing the read lock.
type EmbeddingClient ¶
type EmbeddingClient struct {
// contains filtered or unexported fields
}
func NewEmbeddingClient ¶
func NewEmbeddingClient(cfg RuntimeConfig) *EmbeddingClient
func (*EmbeddingClient) CreateEmbedding ¶
type RuntimeConfig ¶
type RuntimeStats ¶
type RuntimeStats struct {
EvaluatedRequests int64
CacheHitResponses int64
CacheMissRequests int64
BypassedRequests int64
StoredResponses int64
}
func GetRuntimeStats ¶
func GetRuntimeStats() RuntimeStats
type SemanticCache ¶
type SemanticCache struct {
// contains filtered or unexported fields
}
SemanticCache is an in-memory vector store with cosine similarity lookup.