Skip to content

Commit 341184a

Browse files
committed
Establish Ollama class for better model differences to Llama
1 parent 5964386 commit 341184a

File tree

9 files changed

+88
-17
lines changed

9 files changed

+88
-17
lines changed

examples/ollama/chat-llama.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
*/
1111

1212
use Symfony\AI\Agent\Agent;
13-
use Symfony\AI\Platform\Bridge\Meta\Llama;
13+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1414
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
1515
use Symfony\AI\Platform\Message\Message;
1616
use Symfony\AI\Platform\Message\MessageBag;
1717

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

2020
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
21-
$model = new Llama('llama3.2');
21+
$model = new Ollama();
2222

2323
$agent = new Agent($platform, $model, logger: logger());
2424
$messages = new MessageBag(

examples/ollama/toolcall.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
use Symfony\AI\Agent\Toolbox\AgentProcessor;
1414
use Symfony\AI\Agent\Toolbox\Tool\Clock;
1515
use Symfony\AI\Agent\Toolbox\Toolbox;
16-
use Symfony\AI\Platform\Bridge\Meta\Llama;
16+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1717
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
1818
use Symfony\AI\Platform\Message\Message;
1919
use Symfony\AI\Platform\Message\MessageBag;
2020

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

2323
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client());
24-
$model = new Llama('llama3.2');
24+
$model = new Ollama();
2525

2626
$toolbox = new Toolbox([new Clock()], logger: logger());
2727
$processor = new AgentProcessor($toolbox);

src/platform/src/Bridge/Meta/Llama.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function __construct(string $name = self::V3_1_405B_INSTRUCT, array $opti
4343
$capabilities = [
4444
Capability::INPUT_MESSAGES,
4545
Capability::OUTPUT_TEXT,
46-
Capability::TOOL_CALLING,
4746
];
4847

4948
parent::__construct($name, $capabilities, $options);

src/platform/src/Bridge/Ollama/Contract/AssistantMessageNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Ollama\Contract;
1313

14-
use Symfony\AI\Platform\Bridge\Meta\Llama;
14+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1515
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
1616
use Symfony\AI\Platform\Message\AssistantMessage;
1717
use Symfony\AI\Platform\Message\Role;
@@ -34,7 +34,7 @@ protected function supportedDataClass(): string
3434

3535
protected function supportsModel(Model $model): bool
3636
{
37-
return $model instanceof Llama;
37+
return $model instanceof Ollama;
3838
}
3939

4040
/**
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\AI\Platform\Bridge\Ollama;
13+
14+
use Symfony\AI\Platform\Capability;
15+
use Symfony\AI\Platform\Model;
16+
17+
/**
18+
* @author Joshua Behrens <[email protected]>
19+
*/
20+
class Ollama extends Model
21+
{
22+
public const DEEPSEEK_R_1 = 'deepseek-r1'; // TOOL
23+
public const GEMMA_3_N = 'gemma3n';
24+
public const GEMMA_3 = 'gemma3';
25+
public const QWEN_3 = 'qwen3'; // TOOL
26+
public const QWEN_2_5_VL = 'qwen2.5vl';
27+
public const LLAMA_3_1 = 'llama3.1'; // TOOL
28+
public const LLAMA_3_2 = 'llama3.2'; // TOOL
29+
public const MISTRAL = 'mistral'; // TOOL
30+
public const QWEN_2_5 = 'qwen2.5'; // TOOL
31+
public const LLAMA_3 = 'llama3';
32+
public const LLAVA = 'llava';
33+
public const PHI_3 = 'phi3';
34+
public const GEMMA_2 = 'gemma2';
35+
public const QWEN_2_5_CODER = 'qwen2.5-coder'; // TOOL
36+
public const GEMMA = 'gemma';
37+
public const QWEN = 'qwen';
38+
public const QWEN_2 = 'qwen2'; // TOOL
39+
public const LLAMA_2 = 'llama2';
40+
41+
private const TOOL_PATTERNS = [
42+
'.' => [
43+
Capability::INPUT_MESSAGES,
44+
Capability::OUTPUT_TEXT,
45+
],
46+
'^llama\D*3(\D*\d+)' => [
47+
Capability::TOOL_CALLING,
48+
],
49+
'^qwen\d(\.\d)?(-coder)?$' => [
50+
Capability::TOOL_CALLING,
51+
],
52+
'^(deepseek|mistral)' => [
53+
Capability::TOOL_CALLING,
54+
],
55+
];
56+
57+
/**
58+
* @param array<string, mixed> $options
59+
*/
60+
public function __construct(string $name = self::LLAMA_3_2, array $options = [])
61+
{
62+
$capabilities = [];
63+
64+
foreach (self::TOOL_PATTERNS as $pattern => $options) {
65+
if (\preg_match($pattern, $name) === 1) {
66+
$capabilities = [...$capabilities, ...$options];
67+
}
68+
}
69+
70+
$capabilities = \array_unique($capabilities);
71+
72+
parent::__construct($name, $capabilities, $options);
73+
}
74+
}

src/platform/src/Bridge/Ollama/OllamaModelClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Ollama;
1313

14-
use Symfony\AI\Platform\Bridge\Meta\Llama;
1514
use Symfony\AI\Platform\Model;
1615
use Symfony\AI\Platform\ModelClientInterface;
1716
use Symfony\AI\Platform\Result\RawHttpResult;
@@ -30,7 +29,7 @@ public function __construct(
3029

3130
public function supports(Model $model): bool
3231
{
33-
return $model instanceof Llama;
32+
return $model instanceof Ollama;
3433
}
3534

3635
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult

src/platform/src/Bridge/Ollama/OllamaResultConverter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Ollama;
1313

14-
use Symfony\AI\Platform\Bridge\Meta\Llama;
1514
use Symfony\AI\Platform\Exception\RuntimeException;
1615
use Symfony\AI\Platform\Model;
1716
use Symfony\AI\Platform\Result\RawResultInterface;
@@ -28,7 +27,7 @@
2827
{
2928
public function supports(Model $model): bool
3029
{
31-
return $model instanceof Llama;
30+
return $model instanceof Ollama;
3231
}
3332

3433
public function convert(RawResultInterface $result, array $options = []): ResultInterface

src/platform/tests/Bridge/Ollama/Contract/AssistantMessageNormalizerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use PHPUnit\Framework\Attributes\Small;
1717
use PHPUnit\Framework\Attributes\UsesClass;
1818
use PHPUnit\Framework\TestCase;
19-
use Symfony\AI\Platform\Bridge\Meta\Llama;
2019
use Symfony\AI\Platform\Bridge\Ollama\Contract\AssistantMessageNormalizer;
20+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
2121
use Symfony\AI\Platform\Contract;
2222
use Symfony\AI\Platform\Message\AssistantMessage;
2323
use Symfony\AI\Platform\Message\Role;
@@ -26,7 +26,7 @@
2626

2727
#[Small]
2828
#[CoversClass(AssistantMessageNormalizer::class)]
29-
#[UsesClass(Llama::class)]
29+
#[UsesClass(Ollama::class)]
3030
#[UsesClass(AssistantMessage::class)]
3131
#[UsesClass(Model::class)]
3232
#[UsesClass(ToolCall::class)]
@@ -42,7 +42,7 @@ protected function setUp(): void
4242
public function testSupportsNormalization(): void
4343
{
4444
$this->assertTrue($this->normalizer->supportsNormalization(new AssistantMessage('Hello'), context: [
45-
Contract::CONTEXT_MODEL => new Llama(),
45+
Contract::CONTEXT_MODEL => new Ollama(),
4646
]));
4747
$this->assertFalse($this->normalizer->supportsNormalization(new AssistantMessage('Hello'), context: [
4848
Contract::CONTEXT_MODEL => new Model('any-model'),

src/platform/tests/Bridge/Ollama/OllamaResultConverterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\Attributes\Small;
1616
use PHPUnit\Framework\Attributes\UsesClass;
1717
use PHPUnit\Framework\TestCase;
18-
use Symfony\AI\Platform\Bridge\Meta\Llama;
18+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1919
use Symfony\AI\Platform\Bridge\Ollama\OllamaResultConverter;
2020
use Symfony\AI\Platform\Exception\RuntimeException;
2121
use Symfony\AI\Platform\Model;
@@ -26,7 +26,7 @@
2626

2727
#[CoversClass(OllamaResultConverter::class)]
2828
#[Small]
29-
#[UsesClass(Llama::class)]
29+
#[UsesClass(Ollama::class)]
3030
#[UsesClass(TextResult::class)]
3131
#[UsesClass(ToolCall::class)]
3232
#[UsesClass(ToolCallResult::class)]
@@ -36,7 +36,7 @@ public function testSupportsLlamaModel(): void
3636
{
3737
$converter = new OllamaResultConverter();
3838

39-
$this->assertTrue($converter->supports(new Llama()));
39+
$this->assertTrue($converter->supports(new Ollama()));
4040
$this->assertFalse($converter->supports(new Model('any-model')));
4141
}
4242

0 commit comments

Comments
 (0)