From e11aab10e90e398a05f7670d8da4072855c76b4e Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 31 Aug 2025 09:35:49 +0200 Subject: [PATCH] Add example for RAG with ChromaDB --- examples/.env | 3 ++ examples/compose.yaml | 2 +- examples/composer.json | 1 + examples/rag/chromadb.php | 70 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 examples/rag/chromadb.php diff --git a/examples/.env b/examples/.env index 27eabc891..67e0d6395 100644 --- a/examples/.env +++ b/examples/.env @@ -120,3 +120,6 @@ MILVUS_DATABASE=symfony # Cerebras CEREBRAS_API_KEY= + +CHROMADB_HOST=http://127.0.0.1 +CHROMADB_PORT=8001 diff --git a/examples/compose.yaml b/examples/compose.yaml index 4ec191842..b8af871f7 100644 --- a/examples/compose.yaml +++ b/examples/compose.yaml @@ -1,6 +1,6 @@ services: chromadb: - image: chromadb/chroma + image: chromadb/chroma:0.5.23 environment: IS_PERSISTENT: TRUE PERSIST_DIRECTORY: /chroma/chroma # this is the default path, change it as needed diff --git a/examples/composer.json b/examples/composer.json index ce8038b2d..1b3a3089e 100644 --- a/examples/composer.json +++ b/examples/composer.json @@ -6,6 +6,7 @@ "require": { "ext-pdo": "*", "async-aws/bedrock-runtime": "^1.1", + "codewithkyrian/chromadb-php": "^0.4.0", "codewithkyrian/transformers": "^0.6.1", "doctrine/dbal": "^3.3|^4.0", "google/auth": "^1.47", diff --git a/examples/rag/chromadb.php b/examples/rag/chromadb.php new file mode 100644 index 000000000..27e0526b8 --- /dev/null +++ b/examples/rag/chromadb.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\AI\Agent\Agent; +use Symfony\AI\Agent\Toolbox\AgentProcessor; +use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch; +use Symfony\AI\Agent\Toolbox\Toolbox; +use Symfony\AI\Fixtures\Movies; +use Symfony\AI\Platform\Bridge\OpenAi\Embeddings; +use Symfony\AI\Platform\Bridge\OpenAi\Gpt; +use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; +use Symfony\AI\Platform\Message\Message; +use Symfony\AI\Platform\Message\MessageBag; +use Symfony\AI\Store\Bridge\ChromaDb\Store; +use Symfony\AI\Store\Document\Metadata; +use Symfony\AI\Store\Document\TextDocument; +use Symfony\AI\Store\Document\Vectorizer; +use Symfony\AI\Store\Indexer; +use Symfony\Component\Uid\Uuid; + +require_once dirname(__DIR__).'/bootstrap.php'; + +// initialize the store + +$store = new Store( + (new Codewithkyrian\ChromaDB\Factory()) + ->withHost(env('CHROMADB_HOST')) + ->withPort(env('CHROMADB_PORT')) + ->connect(), + 'movies', +); + +// create embeddings and documents +$documents = []; +foreach (Movies::all() as $i => $movie) { + $documents[] = new TextDocument( + id: Uuid::v4(), + content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'], + metadata: new Metadata($movie), + ); +} + +// create embeddings for documents +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); +$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings()); +$indexer = new Indexer($vectorizer, $store, logger()); +$indexer->index($documents); + +$model = new Gpt(Gpt::GPT_4O_MINI); + +$similaritySearch = new SimilaritySearch($platform, $embeddings, $store); +$toolbox = new Toolbox([$similaritySearch], logger: logger()); +$processor = new AgentProcessor($toolbox); +$agent = new Agent($platform, $model, [$processor], [$processor], logger()); + +$messages = new MessageBag( + Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), + Message::ofUser('Which movie fits the theme of technology?') +); +$result = $agent->call($messages); + +echo $result->getContent().\PHP_EOL;