Skip to content

Commit 631027f

Browse files
committed
minor #385 [Examples] Add example for RAG with ChromaDB (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Examples] Add example for RAG with ChromaDB | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | | License | MIT Follows #381 Commits ------- e11aab1 Add example for RAG with ChromaDB
2 parents 9803007 + e11aab1 commit 631027f

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

examples/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,6 @@ MILVUS_DATABASE=symfony
120120

121121
# Cerebras
122122
CEREBRAS_API_KEY=
123+
124+
CHROMADB_HOST=http://127.0.0.1
125+
CHROMADB_PORT=8001

examples/compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
chromadb:
3-
image: chromadb/chroma
3+
image: chromadb/chroma:0.5.23
44
environment:
55
IS_PERSISTENT: TRUE
66
PERSIST_DIRECTORY: /chroma/chroma # this is the default path, change it as needed

examples/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"require": {
77
"ext-pdo": "*",
88
"async-aws/bedrock-runtime": "^1.1",
9+
"codewithkyrian/chromadb-php": "^0.4.0",
910
"codewithkyrian/transformers": "^0.6.1",
1011
"doctrine/dbal": "^3.3|^4.0",
1112
"google/auth": "^1.47",

examples/rag/chromadb.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Agent\Toolbox\AgentProcessor;
14+
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
15+
use Symfony\AI\Agent\Toolbox\Toolbox;
16+
use Symfony\AI\Fixtures\Movies;
17+
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
18+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
19+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
20+
use Symfony\AI\Platform\Message\Message;
21+
use Symfony\AI\Platform\Message\MessageBag;
22+
use Symfony\AI\Store\Bridge\ChromaDb\Store;
23+
use Symfony\AI\Store\Document\Metadata;
24+
use Symfony\AI\Store\Document\TextDocument;
25+
use Symfony\AI\Store\Document\Vectorizer;
26+
use Symfony\AI\Store\Indexer;
27+
use Symfony\Component\Uid\Uuid;
28+
29+
require_once dirname(__DIR__).'/bootstrap.php';
30+
31+
// initialize the store
32+
33+
$store = new Store(
34+
(new Codewithkyrian\ChromaDB\Factory())
35+
->withHost(env('CHROMADB_HOST'))
36+
->withPort(env('CHROMADB_PORT'))
37+
->connect(),
38+
'movies',
39+
);
40+
41+
// create embeddings and documents
42+
$documents = [];
43+
foreach (Movies::all() as $i => $movie) {
44+
$documents[] = new TextDocument(
45+
id: Uuid::v4(),
46+
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
47+
metadata: new Metadata($movie),
48+
);
49+
}
50+
51+
// create embeddings for documents
52+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
53+
$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings());
54+
$indexer = new Indexer($vectorizer, $store, logger());
55+
$indexer->index($documents);
56+
57+
$model = new Gpt(Gpt::GPT_4O_MINI);
58+
59+
$similaritySearch = new SimilaritySearch($platform, $embeddings, $store);
60+
$toolbox = new Toolbox([$similaritySearch], logger: logger());
61+
$processor = new AgentProcessor($toolbox);
62+
$agent = new Agent($platform, $model, [$processor], [$processor], logger());
63+
64+
$messages = new MessageBag(
65+
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
66+
Message::ofUser('Which movie fits the theme of technology?')
67+
);
68+
$result = $agent->call($messages);
69+
70+
echo $result->getContent().\PHP_EOL;

0 commit comments

Comments
 (0)