Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/ollama/Ollama.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Symfony Ollama Examples

This directory contains various examples of how to use the Symfony AI with [Ollama](https://ollama.com/).

## Getting started

To get started with Ollama please check their [Quickstart guide](https://github.com/ollama/ollama/blob/main/README.md#quickstart).

## Running the examples

To run the examples you will need to download [Llama 3.2](https://ollama.com/library/llama3.2)
and [nomic-embed-text](https://ollama.com/library/nomic-embed-text) models.

Once models are downloaded you can run them with
```bash
ollama run <model-name>
```
for example

```bash
ollama run llama3.2
```
Comment on lines +14 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running models is not mandatory, but serving them is. Using ollama serve command. BTW running an embedding model is not possible using Ollama, output is not human readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, so to run the examples it would be:

ollama pull llama3.2
ollama pull nomic-embed-text
ollama serve


#### Configuration
To run Ollama examples you will need to provide a OLLAMA_HOST_URL key in your env.local file.

For example
```bash
OLLAMA_HOST_URL=http://localhost:11434
```
47 changes: 47 additions & 0 deletions examples/ollama/indexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\Ollama\Ollama;
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
use Symfony\AI\Store\Bridge\Local\InMemoryStore;
use Symfony\AI\Store\Document\Loader\TextFileLoader;
use Symfony\AI\Store\Document\Transformer\TextReplaceTransformer;
use Symfony\AI\Store\Document\Transformer\TextSplitTransformer;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\Indexer;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
$store = new InMemoryStore();
$vectorizer = new Vectorizer($platform, $embeddings = new Ollama(Ollama::NOMIC_EMBED_TEXT), logger());;
$indexer = new Indexer(
loader: new TextFileLoader(),
vectorizer: $vectorizer,
store: $store,
source: [
dirname(__DIR__, 2).'/fixtures/movies/gladiator.md',
dirname(__DIR__, 2).'/fixtures/movies/inception.md',
dirname(__DIR__, 2).'/fixtures/movies/jurassic-park.md',
],
transformers: [
new TextReplaceTransformer(search: '## Plot', replace: '## Synopsis'),
new TextSplitTransformer(chunkSize: 500, overlap: 100),
],
);

$indexer->index();

$vector = $vectorizer->vectorize('Roman gladiator revenge');
$results = $store->query($vector);
foreach ($results as $i => $document) {
echo sprintf("%d. %s\n", $i + 1, substr($document->id, 0, 40).'...');
}
63 changes: 63 additions & 0 deletions examples/ollama/rag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Ollama\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Store\Bridge\Local\InMemoryStore;
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
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;
use Symfony\AI\Platform\Bridge\Ollama\Ollama;

require_once dirname(__DIR__).'/bootstrap.php';

// initialize the store
$store = new InMemoryStore();
$documents = [];

// create embeddings and 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('OLLAMA_HOST_URL'), http_client());
$vectorizer = new Vectorizer($platform, $embeddings = new Ollama(Ollama::NOMIC_EMBED_TEXT), logger());
$indexer = new Indexer(new InMemoryLoader($documents), $vectorizer, $store, logger: logger());
$indexer->index($documents);

$model = new Ollama();

$similaritySearch = new SimilaritySearch($vectorizer, $store);
$toolbox = new Toolbox([$similaritySearch], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, $model, [$processor], [$processor], logger: logger());

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Message::ofUser('Which movie fits the theme of the mafia?')
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;