Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ class BaseGenerateResponse(SubscriptableBaseModel):
prompt_eval_count: Optional[int] = None
'Number of tokens evaluated in the prompt.'

prompt_eval_cached_count: Optional[int] = None
'Number of prompt tokens that were served from the prompt cache instead of being evaluated.'

prompt_eval_duration: Optional[int] = None
'Duration of evaluating the prompt in nanoseconds.'

Expand Down
43 changes: 42 additions & 1 deletion tests/test_type_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from ollama._types import ChatRequest, CreateRequest, GenerateRequest, Image
from ollama._types import ChatRequest, ChatResponse, CreateRequest, GenerateRequest, GenerateResponse, Image


def test_image_serialization_bytes():
Expand Down Expand Up @@ -122,3 +122,44 @@ def test_think_boolean_serialization():
assert ChatRequest(model='test-model', think=True).model_dump(exclude_none=True)['think'] is True
assert ChatRequest(model='test-model', think=False).model_dump(exclude_none=True)['think'] is False
assert 'think' not in ChatRequest(model='test-model', think=None).model_dump(exclude_none=True)


def test_generate_response_keeps_prompt_eval_cached_count():
response = GenerateResponse.model_validate(
{
'model': 'test-model',
'response': 'hi',
'done': True,
'prompt_eval_count': 30,
'prompt_eval_cached_count': 16,
}
)

assert response.prompt_eval_cached_count == 16
assert response['prompt_eval_cached_count'] == 16
assert 'prompt_eval_cached_count' in response
assert response.model_dump(exclude_none=True)['prompt_eval_cached_count'] == 16


def test_chat_response_keeps_prompt_eval_cached_count():
response = ChatResponse.model_validate(
{
'model': 'test-model',
'message': {'role': 'assistant', 'content': 'hi'},
'done': True,
'prompt_eval_count': 30,
'prompt_eval_cached_count': 16,
}
)

assert response.prompt_eval_cached_count == 16


def test_prompt_eval_cached_count_is_optional():
# Servers predating the field must keep parsing, with the attribute present but unset.
for response in (
GenerateResponse.model_validate({'model': 'test-model'}),
ChatResponse.model_validate({'model': 'test-model', 'message': {'role': 'assistant', 'content': 'hi'}}),
):
assert response.prompt_eval_cached_count is None
assert 'prompt_eval_cached_count' not in response.model_dump(exclude_none=True)