Skip to content

Commit 9b6bd23

Browse files
committed
Ollama tool choice support
1 parent 433e1c6 commit 9b6bd23

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: src/Providers/Ollama/Ollama.php

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use EchoLabs\Prism\Requests\TextRequest;
1212
use EchoLabs\Prism\ValueObjects\ToolCall;
1313
use EchoLabs\Prism\ValueObjects\Usage;
14+
use InvalidArgumentException;
1415
use Throwable;
1516

1617
class Ollama implements Provider
@@ -24,6 +25,8 @@ public function __construct(
2425
public function text(TextRequest $request): ProviderResponse
2526
{
2627
try {
28+
$this->validateTextRequest($request);
29+
2730
$response = $this
2831
->client($request->clientOptions)
2932
->messages(
@@ -68,6 +71,13 @@ public function text(TextRequest $request): ProviderResponse
6871
);
6972
}
7073

74+
protected function validateTextRequest(TextRequest $textRequest): void
75+
{
76+
if ($textRequest->toolChoice) {
77+
throw new InvalidArgumentException('Invalid tool choice');
78+
}
79+
}
80+
7181
/**
7282
* @param array<int, array<string, mixed>> $toolCalls
7383
* @return array<int, ToolCall>

Diff for: tests/Providers/Ollama/OllamaTextTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Providers\Ollama;
66

77
use EchoLabs\Prism\Enums\Provider;
8+
use EchoLabs\Prism\Exceptions\PrismException;
89
use EchoLabs\Prism\Facades\Tool;
910
use EchoLabs\Prism\Prism;
1011
use EchoLabs\Prism\ValueObjects\Messages\Support\Image;
@@ -102,6 +103,17 @@
102103
"Today's Tigers game in Detroit starts at 3 PM. The current temperature is 75°F with clear skies, so you shouldn't need a coat. Enjoy the game!"
103104
);
104105
});
106+
107+
it('throws an exception for ToolChoice', function (): void {
108+
$this->expectException(PrismException::class);
109+
$this->expectExceptionMessage('Invalid tool choice');
110+
111+
Prism::text()
112+
->using(Provider::Ollama, 'qwen2.5:14b')
113+
->withPrompt('Who are you?')
114+
->withToolChoice('weather')
115+
->generate();
116+
});
105117
});
106118

107119
describe('Image support', function (): void {

0 commit comments

Comments
 (0)