|
13 | 13 | # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
14 | 14 |
|
15 | 15 |
|
| 16 | +from typing import List |
| 17 | +from unittest.mock import MagicMock, patch |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
16 | 21 | from camel.embeddings import AzureEmbedding |
| 22 | +from camel.types import EmbeddingModelType |
| 23 | + |
| 24 | + |
| 25 | +class MockEmbeddingData: |
| 26 | + def __init__(self, embedding: List[float]): |
| 27 | + self.embedding = embedding |
| 28 | + |
| 29 | + |
| 30 | +class MockResponse: |
| 31 | + def __init__(self, data: List[MockEmbeddingData]): |
| 32 | + self.data = data |
| 33 | + |
17 | 34 |
|
| 35 | +@pytest.fixture |
| 36 | +def mock_env_vars(monkeypatch): |
| 37 | + r"""Set up environment variables to prevent API calls during testing.""" |
| 38 | + monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test_api_key") |
| 39 | + monkeypatch.setenv( |
| 40 | + "AZURE_OPENAI_BASE_URL", "https://test.openai.azure.com" |
| 41 | + ) |
| 42 | + monkeypatch.setenv("AZURE_API_VERSION", "2023-05-15") |
18 | 43 |
|
19 | | -def test_azure_embedding(): |
| 44 | + |
| 45 | +@patch('camel.embeddings.azure_embedding.AzureOpenAI') |
| 46 | +def test_azure_embedding(mock_azure_openai, mock_env_vars): |
| 47 | + # Create mock client and mock embeddings object |
| 48 | + mock_client = MagicMock() |
| 49 | + mock_embeddings = MagicMock() |
| 50 | + mock_client.embeddings = mock_embeddings |
| 51 | + mock_azure_openai.return_value = mock_client |
| 52 | + |
| 53 | + # Default model test with mocked response (1536 dimensions) |
| 54 | + default_dim = EmbeddingModelType.TEXT_EMBEDDING_3_SMALL.output_dim |
| 55 | + mock_embedding = [0.1] * default_dim |
| 56 | + mock_response = MockResponse([MockEmbeddingData(mock_embedding)]) |
| 57 | + mock_embeddings.create.return_value = mock_response |
| 58 | + |
| 59 | + # Test with default dimensions |
20 | 60 | embedding_model = AzureEmbedding() |
21 | 61 | text = "test 1." |
22 | 62 | vector = embedding_model.embed(text) |
| 63 | + |
| 64 | + # Verify mock was called correctly |
| 65 | + mock_embeddings.create.assert_called_once() |
23 | 66 | assert len(vector) == embedding_model.get_output_dim() |
24 | 67 |
|
25 | | - embedding_model = AzureEmbedding(dimensions=256) |
| 68 | + # Reset mock for second test |
| 69 | + mock_embeddings.create.reset_mock() |
| 70 | + |
| 71 | + # Test with custom dimensions |
| 72 | + custom_dim = 256 |
| 73 | + mock_embedding_custom = [0.1] * custom_dim |
| 74 | + mock_response_custom = MockResponse( |
| 75 | + [MockEmbeddingData(mock_embedding_custom)] |
| 76 | + ) |
| 77 | + mock_embeddings.create.return_value = mock_response_custom |
| 78 | + |
| 79 | + embedding_model = AzureEmbedding(dimensions=custom_dim) |
26 | 80 | text = "test 2" |
27 | 81 | vector = embedding_model.embed(text) |
28 | | - assert len(vector) == embedding_model.get_output_dim() == 256 |
| 82 | + |
| 83 | + # Verify dimensions were passed correctly |
| 84 | + mock_embeddings.create.assert_called_once() |
| 85 | + assert len(vector) == embedding_model.get_output_dim() == custom_dim |
0 commit comments