Symfony’s Experimental AI Store: A Deep Dive into InMemoryStore

Symfony is steadily evolving into a modern AI-enabled framework—and one of its most exciting experimental additions is the symfony/ai-store component. This new package lays the foundation for semantic search, context injection, and Retrieval-Augmented Generation (RAG) workflows directly inside PHP applications.

What Is Symfony AI Store?

The AI Store component is a low-level abstraction for managing vector stores. In simpler terms, it helps store documents as embeddings (numerical vectors) and retrieve them later using similarity search.

This is crucial in RAG scenarios, where an AI agent dynamically pulls relevant facts from a custom knowledge base before answering user queries.

This component is experimental and not subject to Symfony’s backward compatibility promise.

Install it using Composer:

composer require symfony/ai-store

The InMemoryStore: An Overview

The default implementation included in the package is InMemoryStore, which is ideal for local testing or prototyping. It implements two key interfaces:

  • StoreInterface: to add documents
  • VectorStoreInterface: to query documents

Adding Documents

You can add VectorDocument objects using:

$store->add(new VectorDocument($id, $vector));

These documents hold an ID, a Vector, and optional metadata.

Querying with Vectors

To retrieve similar documents:

$results = $store->query($vector, ['maxItems' => 3]);

This will return the top N results sorted by similarity.


Distance Metrics

Symfony AI Store supports various similarity strategies:

StrategyTypeDescription
cosineSimilarityMeasures the angle between vectors
angularDistanceacos(cosine) / π
euclideanDistanceL2 norm (straight-line distance)
manhattanDistanceSum of absolute differences
chebyshevDistanceLargest individual dimension difference

You can define the strategy when initializing InMemoryStore:

$store = new InMemoryStore(InMemoryStore::COSINE_SIMILARITY);

Live Example: Memory-Based Similarity Search

Let’s walk through the examples/store/memory-similarity-search.php:

$store = new InMemoryStore();

foreach (Movies::all() as $movie) {
$documents[] = new TextDocument(
id: Uuid::v4(),
content: "Title: {$movie['title']}\nDirector: {$movie['director']}\nDescription: {$movie['description']}",
metadata: new Metadata($movie),
);
}

$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$indexer = new Indexer(new Vectorizer($platform, new Embeddings()), $store, logger());
$indexer->index($documents);

$similaritySearch = new SimilaritySearch($platform, new Embeddings(), $store);
$agent = new Agent($platform, new GPT(GPT::GPT_4O_MINI), [new AgentProcessor(new Toolbox([$similaritySearch], logger()))]);

$result = $agent->call(new MessageBag(
Message::forSystem("Use only SimilaritySearch."),
Message::ofUser("Which movie fits the theme of the mafia?")
));

echo $result->getContent();

Extending with Bridges

You can also implement your own storage backends:

phpKopierenBearbeitenclass MyStore implements StoreInterface, VectorStoreInterface {
    public function add(VectorDocument ...$documents): void { /* ... */ }
    public function query(Vector $vector, array $options = [], ?float $minScore = null): array { /* ... */ }
}

Bridges can target databases like:

  • ChromaDB
  • Pinecone
  • MongoDB Atlas
  • MariaDB/PostgreSQL
  • Meilisearch
  • Qdrant
  • Azure AI Search

See GitHub Issue #16 for planned additions.

Further Reading

Views: 3