Skip to content

Commit 989f675

Browse files
fix(ollama): handle Length, Unknown, ContentFilter, Other finish reasons (#943)
1 parent 1da1057 commit 989f675

4 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/Providers/Ollama/Handlers/Text.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public function handle(Request $request): Response
4848
return $this->handleToolCalls($data, $request);
4949
}
5050

51-
return match ($this->mapFinishReason($data)) {
52-
FinishReason::Stop => $this->handleStop($data, $request),
51+
$finishReason = $this->mapFinishReason($data);
52+
53+
return match ($finishReason) {
54+
FinishReason::Stop,
55+
FinishReason::Length,
56+
FinishReason::Unknown,
57+
FinishReason::ContentFilter,
58+
FinishReason::Other => $this->handleStop($data, $request),
5359
default => throw new PrismException('Ollama: unknown finish reason'),
5460
};
5561
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"model":"qwen2.5:14b","created_at":"2025-01-29T23:37:41.068257Z","message":{"role":"assistant","content":"I'm Qwen, a large language model developed by Alibaba Cloud. I'm designed to assist with a wide range of tasks including but not limited to answering questions, generating text, offering suggestions, and providing information based on the input I receive. How can I help you today?"},"done_reason":"length","done":true,"total_duration":4819895166,"load_duration":27509166,"prompt_eval_count":33,"prompt_eval_duration":492000000,"eval_count":57,"eval_duration":4298000000}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"model":"qwen2.5:14b","created_at":"2025-01-29T23:37:41.068257Z","message":{"role":"assistant","content":"I'm Qwen, a large language model developed by Alibaba Cloud. I'm designed to assist with a wide range of tasks including but not limited to answering questions, generating text, offering suggestions, and providing information based on the input I receive. How can I help you today?"},"done_reason":"custom","done":true,"total_duration":4819895166,"load_duration":27509166,"prompt_eval_count":33,"prompt_eval_duration":492000000,"eval_count":57,"eval_duration":4298000000}

tests/Providers/Ollama/TextTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,29 @@
266266
});
267267
});
268268
});
269+
describe('Finish reason handling', function (): void {
270+
it('returns response when done_reason is length (max tokens)', function (): void {
271+
FixtureResponse::fakeResponseSequence('api/chat', 'ollama/generate-text-with-done-reason-length');
272+
273+
$response = Prism::text()
274+
->using('ollama', 'qwen2.5:14b')
275+
->withPrompt('Who are you?')
276+
->asText();
277+
278+
expect($response->text)->not->toBeEmpty();
279+
expect($response->steps)->toHaveCount(1);
280+
expect($response->steps[0]->finishReason->value)->toBe('length');
281+
});
282+
283+
it('returns response when done_reason is unknown (treats as stop)', function (): void {
284+
FixtureResponse::fakeResponseSequence('api/chat', 'ollama/generate-text-with-done-reason-unknown');
285+
286+
$response = Prism::text()
287+
->using('ollama', 'qwen2.5:14b')
288+
->withPrompt('Who are you?')
289+
->asText();
290+
291+
expect($response->text)->not->toBeEmpty();
292+
expect($response->steps)->toHaveCount(1);
293+
});
294+
});

0 commit comments

Comments
 (0)