|
1 | 1 | # Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
| 3 | +import base64 |
3 | 4 | import os |
4 | 5 | import uuid |
5 | 6 | from collections.abc import AsyncIterable |
| 7 | +from pathlib import Path |
6 | 8 | from typing import Any, cast |
7 | 9 | from unittest.mock import AsyncMock, MagicMock, patch |
8 | 10 |
|
|
25 | 27 | ) |
26 | 28 | from ollama import AsyncClient |
27 | 29 | from ollama._types import ChatResponse as OllamaChatResponse |
| 30 | +from ollama._types import Image as OllamaImage |
28 | 31 | from ollama._types import Message as OllamaMessage |
29 | 32 | from openai import AsyncStream |
30 | 33 | from pydantic import BaseModel |
@@ -634,18 +637,98 @@ async def test_cmc_with_data_content_type( |
634 | 637 | chat_history: list[Message], |
635 | 638 | mock_chat_completion_response: OllamaChatResponse, |
636 | 639 | ) -> None: |
| 640 | + image_bytes = b"\x89PNG\r\n\x1a\n" |
637 | 641 | mock_chat.return_value = mock_chat_completion_response |
638 | 642 | chat_history.append( |
639 | 643 | Message( |
640 | | - contents=[Content.from_uri(uri="data:image/png;base64,xyz", media_type="image/png")], |
| 644 | + contents=[ |
| 645 | + Content.from_uri( |
| 646 | + uri=f"data:image/png;base64,{base64.b64encode(image_bytes).decode()}", |
| 647 | + media_type="image/png", |
| 648 | + ) |
| 649 | + ], |
641 | 650 | role="user", |
642 | 651 | ) |
643 | 652 | ) |
644 | 653 |
|
645 | 654 | ollama_client = OllamaChatClient() |
646 | 655 |
|
647 | | - result = await ollama_client.get_response(messages=chat_history) |
| 656 | + with ( |
| 657 | + patch.object(Path, "exists", side_effect=AssertionError("Path.exists must not be called")) as path_exists, |
| 658 | + patch.object( |
| 659 | + Path, "read_bytes", side_effect=AssertionError("Path.read_bytes must not be called") |
| 660 | + ) as read_bytes, |
| 661 | + ): |
| 662 | + result = await ollama_client.get_response(messages=chat_history) |
| 663 | + sent_image = mock_chat.call_args.kwargs["messages"][0].images[0] |
| 664 | + assert isinstance(sent_image, OllamaImage) |
| 665 | + assert sent_image.value == image_bytes |
| 666 | + assert sent_image.model_dump() == base64.b64encode(image_bytes).decode() |
| 667 | + |
648 | 668 | assert result.text == "test" |
| 669 | + path_exists.assert_not_called() |
| 670 | + read_bytes.assert_not_called() |
| 671 | + |
| 672 | + |
| 673 | +@pytest.mark.parametrize( |
| 674 | + "uri", |
| 675 | + [ |
| 676 | + "data:image/png,/tmp/image.png", |
| 677 | + "data:image/png;base64,/tmp/image.png", |
| 678 | + r"data:image/png;base64,C:\temp\image.png", |
| 679 | + r"data:image/png;base64,\\server\share\image.png", |
| 680 | + "data:image/png;base64,../image.png", |
| 681 | + "data:image/png;base64,", |
| 682 | + "data:text/plain;base64,aGVsbG8=", |
| 683 | + ], |
| 684 | +) |
| 685 | +def test_cmc_rejects_invalid_image_data_uri( |
| 686 | + uri: str, |
| 687 | + ollama_unit_test_env: dict[str, str], |
| 688 | +) -> None: |
| 689 | + message = Message( |
| 690 | + contents=[Content.from_uri(uri=uri, media_type="image/png")], |
| 691 | + role="user", |
| 692 | + ) |
| 693 | + ollama_client = OllamaChatClient() |
| 694 | + |
| 695 | + with pytest.raises(ChatClientInvalidRequestException): |
| 696 | + ollama_client._format_user_message(message) |
| 697 | + |
| 698 | + |
| 699 | +@patch.object(AsyncClient, "chat", new_callable=AsyncMock) |
| 700 | +async def test_cmc_treats_base64_shaped_path_as_inline_bytes( |
| 701 | + mock_chat: AsyncMock, |
| 702 | + ollama_unit_test_env: dict[str, str], |
| 703 | + chat_history: list[Message], |
| 704 | + mock_chat_completion_response: OllamaChatResponse, |
| 705 | +) -> None: |
| 706 | + payload = "////tmpx" |
| 707 | + expected_bytes = base64.b64decode(payload, validate=True) |
| 708 | + mock_chat.return_value = mock_chat_completion_response |
| 709 | + chat_history.append( |
| 710 | + Message( |
| 711 | + contents=[Content.from_uri(uri=f"data:image/png;base64,{payload}", media_type="image/png")], |
| 712 | + role="user", |
| 713 | + ) |
| 714 | + ) |
| 715 | + |
| 716 | + ollama_client = OllamaChatClient() |
| 717 | + |
| 718 | + with ( |
| 719 | + patch.object(Path, "exists", side_effect=AssertionError("Path.exists must not be called")) as path_exists, |
| 720 | + patch.object( |
| 721 | + Path, "read_bytes", side_effect=AssertionError("Path.read_bytes must not be called") |
| 722 | + ) as read_bytes, |
| 723 | + ): |
| 724 | + await ollama_client.get_response(messages=chat_history) |
| 725 | + sent_image = mock_chat.call_args.kwargs["messages"][0].images[0] |
| 726 | + assert isinstance(sent_image, OllamaImage) |
| 727 | + assert sent_image.value == expected_bytes |
| 728 | + assert sent_image.model_dump() == payload |
| 729 | + |
| 730 | + path_exists.assert_not_called() |
| 731 | + read_bytes.assert_not_called() |
649 | 732 |
|
650 | 733 |
|
651 | 734 | @patch.object(AsyncClient, "chat", new_callable=AsyncMock) |
|
0 commit comments