Skip to content

Commit 239b08e

Browse files
authored
fix: azure openai key and test error (camel-ai#2150)
1 parent e577058 commit 239b08e

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

.github/workflows/pytest_package.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ jobs:
3535
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
3636
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
3737
HF_TOKEN: "${{ secrets.HF_TOKEN }}"
38-
AZURE_OPENAI_API_KEY: "to-be-filled"
39-
AZURE_API_VERSION: "to-be-filled"
40-
AZURE_DEPLOYMENT_NAME: "to-be-filled"
41-
AZURE_OPENAI_BASE_URL: "https://camel.openai.azure.com/"
38+
AZURE_OPENAI_API_KEY: "${{ secrets.AZURE_OPENAI_API_KEY }}"
39+
AZURE_API_VERSION: "${{ secrets.AZURE_API_VERSION }}"
40+
AZURE_DEPLOYMENT_NAME: "${{ secrets.AZURE_DEPLOYMENT_NAME }}"
41+
AZURE_OPENAI_BASE_URL: "${{ secrets.AZURE_OPENAI_BASE_URL }}"
4242
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
4343
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
4444
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
@@ -95,10 +95,10 @@ jobs:
9595
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
9696
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
9797
HF_TOKEN: "${{ secrets.HF_TOKEN }}"
98-
AZURE_OPENAI_API_KEY: "to-be-filled"
99-
AZURE_API_VERSION: "to-be-filled"
100-
AZURE_DEPLOYMENT_NAME: "to-be-filled"
101-
AZURE_OPENAI_BASE_URL: "https://camel.openai.azure.com/"
98+
AZURE_OPENAI_API_KEY: "${{ secrets.AZURE_OPENAI_API_KEY }}"
99+
AZURE_API_VERSION: "${{ secrets.AZURE_API_VERSION }}"
100+
AZURE_DEPLOYMENT_NAME: "${{ secrets.AZURE_DEPLOYMENT_NAME }}"
101+
AZURE_OPENAI_BASE_URL: "${{ secrets.AZURE_OPENAI_BASE_URL }}"
102102
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
103103
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
104104
NEO4J_URI: "${{ secrets.NEO4J_URI }}"

test/embeddings/test_azure_embedding.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,73 @@
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414

1515

16+
from typing import List
17+
from unittest.mock import MagicMock, patch
18+
19+
import pytest
20+
1621
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+
1734

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")
1843

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
2060
embedding_model = AzureEmbedding()
2161
text = "test 1."
2262
vector = embedding_model.embed(text)
63+
64+
# Verify mock was called correctly
65+
mock_embeddings.create.assert_called_once()
2366
assert len(vector) == embedding_model.get_output_dim()
2467

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)
2680
text = "test 2"
2781
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

Comments
 (0)