Skip to content

[Platform] Add Ollama embeddings #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025

Conversation

Guikingone
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues None
License MIT

Hi 👋🏻

This PR aims to add the support for embeddings via Ollama, the platform is updated to handle the new embedding client.

@Guikingone Guikingone changed the title feat(platform): ollama embeddings added [Platform] Add Ollama embeddings Jul 31, 2025
@Guikingone Guikingone force-pushed the platform/ollama_embeddings branch from 5cd354e to 096f582 Compare July 31, 2025 12:00
@Guikingone
Copy link
Contributor Author

Ok, should be better 😅

@JoshuaBehrens
Copy link
Contributor

JoshuaBehrens commented Jul 31, 2025

Looks like I need to be faster with proposing my changes :D glad that you already have tests at hand

This is my example to proof, that Ollama can solve the same movie selection task

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * 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\Meta\Llama;
use Symfony\AI\Platform\Bridge\Ollama\Embeddings;
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
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\AI\Store\InMemoryStore;
use Symfony\Component\Uid\Uuid;

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

// initialize the store
$store = new InMemoryStore();

// 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 Embeddings(Embeddings::MXBAI_EMBED_LARGE));
$indexer = new Indexer($vectorizer, $store, logger());
$indexer->index($documents);

$model = new Llama(Llama::V3_2_1B);

$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 the mafia?')
);
$result = $agent->call($messages);

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

@Guikingone
Copy link
Contributor Author

Hi @JoshuaBehrens 👋🏻

Ah, didn't know that you were working on it, sorry 😅

Regarding your example, I just listed the model that are commonly used, if you have any other model in mind, feel free to list them, we can add them in the class 🙂

BTW, I agree (if the suggestion is to add an example via Ollama), we should add one with the Ollama stack

@JoshuaBehrens
Copy link
Contributor

JoshuaBehrens commented Jul 31, 2025

No worries @Guikingone I did not have any real dips on that ^^'

Implementation looks similar:

class Embeddings extends Model
{
    public const MXBAI_EMBED_LARGE = 'mxbai-embed-large';
    public const NOMIC_EMBED_TEXT = 'nomic-embed-text';
    public const ALL_MINILM = 'all-minilm';

    /**
     * @param array<string, mixed> $options
     */
    public function __construct(string $name = self::ALL_MINILM, array $options = [])
    {
        parent::__construct($name, [], $options);
    }
}
grafik

Right now I am refactoring the use of the modal class so I wanted to wait for my refactoring but I will just amend yours later :)

Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

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

Hey @Guikingone, thanks for working on that and extending our Ollama support!

My initial thought was that this is straight forward and a matching implementation, but checking it out and testing it got me thinking.

I think it would be great, if we can ease the Ollama usage a bit and reduce it to one model class and client. For deciding on the endpoint and result converter, we could use the model's capabilities. Either hard code them in the model class or dynamically fetch them from /api/show endpoint.

how does that sound?

@@ -14,7 +14,7 @@ CHANGELOG
- AWS Bedrock (Anthropic Claude, Meta Llama, Amazon Nova)
- Mistral AI (language models and embeddings)
- Meta Llama (via Azure, Ollama, Replicate, AWS Bedrock)
- Ollama (local model hosting)
- Ollama (local model hosting and embeddings)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this addition is meaningful, since we create the embeddings also with models and the main feature of ollama is running models locally

@Guikingone
Copy link
Contributor Author

Agree on the refactoring, the show endpoint could be a solution, I'll take a look on it.

@Guikingone Guikingone force-pushed the platform/ollama_embeddings branch 3 times, most recently from 9ce976e to 4018185 Compare August 4, 2025 11:53
@Guikingone Guikingone requested a review from OskarStark August 4, 2025 11:54
@Guikingone Guikingone requested a review from chr-hertel August 4, 2025 11:54
@fabpot fabpot changed the title [Platform] Add Ollama embeddings Add Ollama embeddings Aug 4, 2025
@Guikingone Guikingone changed the title Add Ollama embeddings Platform] Add Ollama embeddings Aug 4, 2025
@Guikingone Guikingone changed the title Platform] Add Ollama embeddings [Platform] Add Ollama embeddings Aug 4, 2025
@chr-hertel chr-hertel added the Platform Issues & PRs about the AI Platform component label Aug 4, 2025
@Guikingone
Copy link
Contributor Author

Should be better, nice catch around the support method @chr-hertel 👍🏻

Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

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

Looks good & works now - thanks! 👍

@chr-hertel chr-hertel force-pushed the platform/ollama_embeddings branch from 37a4b76 to 005ef2f Compare August 4, 2025 19:44
@chr-hertel chr-hertel merged commit dd03c20 into symfony:main Aug 4, 2025
7 checks passed
@Guikingone Guikingone deleted the platform/ollama_embeddings branch August 4, 2025 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform Issues & PRs about the AI Platform component Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants