diff --git a/examples/ollama/Ollama.md b/examples/ollama/Ollama.md new file mode 100644 index 000000000..bc498ebd0 --- /dev/null +++ b/examples/ollama/Ollama.md @@ -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 +``` +for example + +```bash +ollama run llama3.2 +``` + +#### 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 +``` diff --git a/examples/ollama/indexer.php b/examples/ollama/indexer.php new file mode 100644 index 000000000..1912fee15 --- /dev/null +++ b/examples/ollama/indexer.php @@ -0,0 +1,47 @@ + + * + * 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).'...'); +} diff --git a/examples/ollama/rag.php b/examples/ollama/rag.php new file mode 100644 index 000000000..0525cd763 --- /dev/null +++ b/examples/ollama/rag.php @@ -0,0 +1,63 @@ + + * + * 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;