Skip to content

Commit ea8cd38

Browse files
committed
Added rag examples for Ollama and a small readme
1 parent 3645393 commit ea8cd38

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

examples/ollama/Ollama.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Symfony Ollama Examples
2+
3+
This directory contains various examples of how to use the Symfony AI with [Ollama](https://ollama.com/).
4+
5+
## Getting started
6+
7+
To get started with Ollama please check their [Quickstart guide](https://github.com/ollama/ollama/blob/main/README.md#quickstart).
8+
9+
## Running the examples
10+
11+
To run the examples you will need to download [Llama 3.2](https://ollama.com/library/llama3.2)
12+
and [nomic-embed-text](https://ollama.com/library/nomic-embed-text) models.
13+
14+
Once models are downloaded you can run them with
15+
```bash
16+
ollama run <model-name>
17+
```
18+
for example
19+
20+
```bash
21+
ollama run llama3.2
22+
```
23+
24+
#### Configuration
25+
To run Ollama examples you will need to provide a OLLAMA_HOST_URL key in your env.local file.
26+
27+
For example
28+
```bash
29+
OLLAMA_HOST_URL=http://localhost:11434
30+
```

examples/ollama/indexer.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Platform\Bridge\Ollama\Ollama;
13+
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
14+
use Symfony\AI\Store\Bridge\Local\InMemoryStore;
15+
use Symfony\AI\Store\Document\Loader\TextFileLoader;
16+
use Symfony\AI\Store\Document\Transformer\TextReplaceTransformer;
17+
use Symfony\AI\Store\Document\Transformer\TextSplitTransformer;
18+
use Symfony\AI\Store\Document\Vectorizer;
19+
use Symfony\AI\Store\Indexer;
20+
21+
require_once dirname(__DIR__).'/bootstrap.php';
22+
23+
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
24+
$store = new InMemoryStore();
25+
$vectorizer = new Vectorizer($platform, $embeddings = new Ollama(Ollama::NOMIC_EMBED_TEXT), logger());;
26+
$indexer = new Indexer(
27+
loader: new TextFileLoader(),
28+
vectorizer: $vectorizer,
29+
store: $store,
30+
source: [
31+
dirname(__DIR__, 2).'/fixtures/movies/gladiator.md',
32+
dirname(__DIR__, 2).'/fixtures/movies/inception.md',
33+
dirname(__DIR__, 2).'/fixtures/movies/jurassic-park.md',
34+
],
35+
transformers: [
36+
new TextReplaceTransformer(search: '## Plot', replace: '## Synopsis'),
37+
new TextSplitTransformer(chunkSize: 500, overlap: 100),
38+
],
39+
);
40+
41+
$indexer->index();
42+
43+
$vector = $vectorizer->vectorize('Roman gladiator revenge');
44+
$results = $store->query($vector);
45+
foreach ($results as $i => $document) {
46+
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
47+
}

examples/ollama/rag.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Ollama\PlatformFactory;
18+
use Symfony\AI\Platform\Message\Message;
19+
use Symfony\AI\Platform\Message\MessageBag;
20+
use Symfony\AI\Store\Bridge\Local\InMemoryStore;
21+
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
22+
use Symfony\AI\Store\Document\Metadata;
23+
use Symfony\AI\Store\Document\TextDocument;
24+
use Symfony\AI\Store\Document\Vectorizer;
25+
use Symfony\AI\Store\Indexer;
26+
use Symfony\Component\Uid\Uuid;
27+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
28+
29+
require_once dirname(__DIR__).'/bootstrap.php';
30+
31+
// initialize the store
32+
$store = new InMemoryStore();
33+
$documents = [];
34+
35+
// create embeddings and documents
36+
foreach (Movies::all() as $i => $movie) {
37+
$documents[] = new TextDocument(
38+
id: Uuid::v4(),
39+
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'],
40+
metadata: new Metadata($movie),
41+
);
42+
}
43+
44+
// create embeddings for documents
45+
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
46+
$vectorizer = new Vectorizer($platform, $embeddings = new Ollama(Ollama::NOMIC_EMBED_TEXT), logger());
47+
$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger());
48+
$indexer->index($documents);
49+
50+
$model = new Ollama();
51+
52+
$similaritySearch = new SimilaritySearch($vectorizer, $store);
53+
$toolbox = new Toolbox([$similaritySearch], logger: logger());
54+
$processor = new AgentProcessor($toolbox);
55+
$agent = new Agent($platform, $model, [$processor], [$processor], logger: logger());
56+
57+
$messages = new MessageBag(
58+
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
59+
Message::ofUser('Which movie fits the theme of the mafia?')
60+
);
61+
$result = $agent->call($messages);
62+
63+
echo $result->getContent().\PHP_EOL;

0 commit comments

Comments
 (0)