Skip to content

Commit 5bb1501

Browse files
committed
Address review comments and add Retriever to demo
- Remove duplicate class comment from Retriever (already on interface) - Rename example file from with-options.php to movies.php (no options used) - Add retriever configuration to demo application - Add RetrieveCommand to demonstrate Retriever usage
1 parent a10ab5e commit 5bb1501

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

demo/config/packages/ai.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ ai:
100100
- 'Symfony\AI\Store\Document\Transformer\TextTrimTransformer'
101101
vectorizer: 'ai.vectorizer.openai'
102102
store: 'ai.store.chroma_db.symfonycon'
103+
retriever:
104+
blog:
105+
vectorizer: 'ai.vectorizer.openai'
106+
store: 'ai.store.chroma_db.symfonycon'
103107

104108
services:
105109
_defaults:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Blog\Command;
13+
14+
use Symfony\AI\Store\RetrieverInterface;
15+
use Symfony\Component\Console\Attribute\AsCommand;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
18+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
19+
20+
#[AsCommand('app:blog:retrieve', description: 'Retrieve blog posts using the Retriever component.')]
21+
final readonly class RetrieveCommand
22+
{
23+
public function __construct(
24+
#[Autowire(service: 'ai.retriever.blog')]
25+
private RetrieverInterface $retriever,
26+
) {
27+
}
28+
29+
public function __invoke(SymfonyStyle $io): int
30+
{
31+
$io->title('Retrieve Blog Posts');
32+
33+
$search = $io->ask('What do you want to search for?', 'New Symfony Features');
34+
$io->comment(\sprintf('Searching for "%s" using the Retriever...', $search));
35+
36+
$documents = $this->retriever->retrieve((string) $search);
37+
38+
$count = 0;
39+
foreach ($documents as $document) {
40+
++$count;
41+
$title = $document->metadata->get('title') ?? 'Unknown Title';
42+
$description = $document->metadata->get('description') ?? 'No description';
43+
44+
$io->section($title);
45+
$io->block($description);
46+
47+
if (4 === $count) {
48+
break;
49+
}
50+
}
51+
52+
if (0 === $count) {
53+
$io->error('No results found!');
54+
55+
return Command::FAILURE;
56+
}
57+
58+
$io->success(\sprintf('Found %d result(s) using the Retriever!', $count));
59+
60+
return Command::SUCCESS;
61+
}
62+
}

docs/components/store.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Example Usage
101101
~~~~~~~~~~~~~
102102

103103
* `Basic Retriever Example`_
104-
* `Retriever with Options Example`_
104+
* `Movies Retriever Example`_
105105

106106
Similarity Search Examples
107107
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -201,7 +201,7 @@ This leads to a store implementing two methods::
201201

202202
.. _`Retrieval Augmented Generation`: https://en.wikipedia.org/wiki/Retrieval-augmented_generation
203203
.. _`Basic Retriever Example`: https://github.com/symfony/ai/blob/main/examples/retriever/basic.php
204-
.. _`Retriever with Options Example`: https://github.com/symfony/ai/blob/main/examples/retriever/with-options.php
204+
.. _`Movies Retriever Example`: https://github.com/symfony/ai/blob/main/examples/retriever/movies.php
205205
.. _`Similarity Search with Cloudflare (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/cloudflare.php
206206
.. _`Similarity Search with Manticore (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/manticore.php
207207
.. _`Similarity Search with MariaDB (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/mariadb-gemini.php

examples/retriever/with-options.php renamed to examples/retriever/movies.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
// create retriever
4747
$retriever = new Retriever($vectorizer, $store, logger());
4848

49-
// retrieve documents with options (store-specific)
50-
// For InMemoryStore, you can pass options like 'limit' or 'threshold'
49+
// retrieve documents similar to the query
5150
echo "Searching for movies about 'crime family mafia'\n";
5251
echo "================================================\n\n";
5352

src/store/src/Retriever.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
use Symfony\AI\Store\Document\VectorizerInterface;
1818

1919
/**
20-
* Retrieves documents from a vector store based on a query string.
21-
*
22-
* The opposite of Indexer - while the Indexer loads, transforms, vectorizes and stores documents,
23-
* the Retriever vectorizes a query and retrieves similar documents from the store.
24-
*
2520
* @author Oskar Stark <[email protected]>
2621
*/
2722
class Retriever implements RetrieverInterface

0 commit comments

Comments
 (0)