diff --git a/examples/.env b/examples/.env index 67e0d6395..f573d2bf1 100644 --- a/examples/.env +++ b/examples/.env @@ -123,3 +123,8 @@ CEREBRAS_API_KEY= CHROMADB_HOST=http://127.0.0.1 CHROMADB_PORT=8001 + +# For using Clickhouse (store) +CLICKHOUSE_HOST=http://symfony:symfony@127.0.0.1:8123 +CLICKHOUSE_DATABASE=symfony +CLICKHOUSE_TABLE=symfony diff --git a/examples/rag/clickhouse.php b/examples/rag/clickhouse.php new file mode 100644 index 000000000..4b4e9fb52 --- /dev/null +++ b/examples/rag/clickhouse.php @@ -0,0 +1,71 @@ + + * + * 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\ClickHouse\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\HttpClient\HttpClient; +use Symfony\Component\Uid\Uuid; + +require_once dirname(__DIR__).'/bootstrap.php'; + +// initialize the store +$store = new Store( + HttpClient::createForBaseUri(env('CLICKHOUSE_HOST')), + env('CLICKHOUSE_DATABASE'), + env('CLICKHOUSE_TABLE'), +); + +// initialize the index +$store->setup(); + +// 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;