Skip to content

feat(cache): add SemanticCache component backed by RagProviderInterface #144

Description

@usernane

Summary

Add a SemanticCache component that matches chat requests by vector similarity, backed by an existing RagProviderInterface (ADR-0045) for storage/retrieval and a ProviderInterface for query embeddings. See ADR-0049.

Design

SemanticCache is a standalone class, not a CacheInterface implementation, because the lookup model differs fundamentally (text similarity vs. string-key equality). See ADR-0049 "Alternatives Considered".

class SemanticCache {
    public function __construct(
        RagProviderInterface $store,
        ProviderInterface $embedder,
    ) {}

    public function lookup(string $query, SemanticCacheConfig $config): ?ChatResponse {}
    public function store(string $query, ChatResponse $response, SemanticCacheConfig $config): void {}
    public function clear(): void {}
}

lookup()

  1. Call $store->retrieve($query, topK: 1).
  2. If no result or result.score < $config->getThreshold() → return null (miss).
  3. Reconstruct ChatResponse from result.getMetadata()['cached_response'] via ChatResponse::fromArray().

store()

  1. Serialize the response via ChatResponse::toArray().
  2. $store->ingest($query, ['cached_response' => $arr, 'model' => ..., 'provider' => ..., 'created_at' => time()]).

Dependencies

Acceptance Criteria

  • SemanticCache class with lookup(), store(), clear()
  • Threshold check uses RetrievalResult::getScore()
  • Response reconstructed from metadata
  • TTL respected (via config; store-level or metadata timestamp check)
  • Unit tests with a fake RagProviderInterface covering: hit above threshold, miss below threshold, empty store, round-trip of stored response
  • composer test green

References

  • ADR-0049: AI: Semantic Response Caching
  • ADR-0045: RagProviderInterface

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions