Skip to content

Commit da53f10

Browse files
committed
feature #267 [AIBundle] Add Ollama support (Guikingone)
This PR was squashed before being merged into the main branch. Discussion ---------- [AIBundle] Add Ollama support | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | yes | Issues | None | License | MIT Hi 👋🏻 Noticed that `Ollama` cannot be used as a platform in the bundle, here's the fix along with tests. Commits ------- f9f4932 [AIBundle] Add Ollama support
2 parents e2c787d + f9f4932 commit da53f10

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/ai-bundle/config/options.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
->scalarNode('host_url')->defaultValue('http://127.0.0.1:1234')->end()
6666
->end()
6767
->end()
68+
->arrayNode('ollama')
69+
->children()
70+
->scalarNode('host_url')->defaultValue('http://127.0.0.1:11434')->end()
71+
->end()
72+
->end()
6873
->end()
6974
->end()
7075
->arrayNode('agent')

src/ai-bundle/config/services.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\AI\AiBundle\Security\EventListener\IsGrantedToolAttributeListener;
2727
use Symfony\AI\Platform\Bridge\Anthropic\Contract\AnthropicContract;
2828
use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
29+
use Symfony\AI\Platform\Bridge\Ollama\Contract\OllamaContract;
2930
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\AudioNormalizer;
3031
use Symfony\AI\Platform\Contract;
3132
use Symfony\AI\Platform\Contract\JsonSchema\DescriptionParser;
@@ -44,6 +45,8 @@
4445
->factory([AnthropicContract::class, 'create'])
4546
->set('ai.platform.contract.google', Contract::class)
4647
->factory([GeminiContract::class, 'create'])
48+
->set('ai.platform.contract.ollama', Contract::class)
49+
->factory([OllamaContract::class, 'create'])
4750
// structured output
4851
->set('ai.agent.response_format_factory', ResponseFormatFactory::class)
4952
->args([

src/ai-bundle/doc/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Configuration
5252
api_version: '%env(AZURE_GPT_VERSION)%'
5353
gemini:
5454
api_key: '%env(GEMINI_API_KEY)%'
55+
ollama:
56+
host_url: '%env(OLLAMA_HOST_URL)%'
5557
agent:
5658
rag:
5759
platform: 'ai.platform.azure.gpt_deployment'

src/ai-bundle/src/AiBundle.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory as GeminiPlatformFactory;
3131
use Symfony\AI\Platform\Bridge\LmStudio\PlatformFactory as LmStudioPlatformFactory;
3232
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory as MistralPlatformFactory;
33+
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
3334
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory as OpenAiPlatformFactory;
3435
use Symfony\AI\Platform\Bridge\OpenRouter\PlatformFactory as OpenRouterPlatformFactory;
3536
use Symfony\AI\Platform\Model;
@@ -271,7 +272,7 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
271272
if ('lmstudio' === $type) {
272273
$platformId = 'symfony_ai.platform.lmstudio';
273274
$definition = (new Definition(Platform::class))
274-
->setFactory(LmStudioPlatformFactory::class.'::create')
275+
->setFactory(LmStudioPlatformFactory::class.'::create')
275276
->setLazy(true)
276277
->addTag('proxy', ['interface' => PlatformInterface::class])
277278
->setArguments([
@@ -286,6 +287,25 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
286287
return;
287288
}
288289

290+
if ('ollama' === $type) {
291+
$platformId = 'ai.platform.ollama';
292+
$definition = (new Definition(Platform::class))
293+
->setFactory(MistralPlatformFactory::class.'::create')
294+
->setFactory(OllamaPlatformFactory::class.'::create')
295+
->setLazy(true)
296+
->addTag('proxy', ['interface' => PlatformInterface::class])
297+
->setArguments([
298+
0 => $platform['host_url'],
299+
2 => new Reference('http_client', ContainerInterface::NULL_ON_INVALID_REFERENCE),
300+
3 => new Reference('ai.platform.contract.ollama'),
301+
])
302+
->addTag('ai.platform');
303+
304+
$container->setDefinition($platformId, $definition);
305+
306+
return;
307+
}
308+
289309
throw new InvalidArgumentException(\sprintf('Platform "%s" is not supported for configuration via bundle at this point.', $type));
290310
}
291311

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ private function getFullConfig(): array
158158
'lmstudio' => [
159159
'host_url' => 'http://127.0.0.1:1234',
160160
],
161+
'ollama' => [
162+
'host_url' => 'http://127.0.0.1:11434',
163+
],
161164
],
162165
'agent' => [
163166
'my_chat_agent' => [

0 commit comments

Comments
 (0)