Skip to content

Commit ecbe6e5

Browse files
committed
feature #389 [Examples][ClickHouse] Add RAG example (Guikingone)
This PR was merged into the main branch. Discussion ---------- [Examples][ClickHouse] Add RAG example | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | yes | Issues | Fix none | License | MIT Hi 👋🏻 Here's the rag example for `Clickhouse` as discussed in #335 Commits ------- cdbfde7 ref(examples): example added for Clickhouse
2 parents 1ef27c5 + cdbfde7 commit ecbe6e5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

examples/.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ CEREBRAS_API_KEY=
123123

124124
CHROMADB_HOST=http://127.0.0.1
125125
CHROMADB_PORT=8001
126+
127+
# For using Clickhouse (store)
128+
CLICKHOUSE_HOST=http://symfony:[email protected]:8123
129+
CLICKHOUSE_DATABASE=symfony
130+
CLICKHOUSE_TABLE=symfony

examples/rag/clickhouse.php

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

0 commit comments

Comments
 (0)