Add Support for Customizing default_headers in Azure OpenAI (#1925)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
from mem0.embeddings.azure_openai import AzureOpenAIEmbedding
|
||||
|
||||
import pytest
|
||||
|
||||
from mem0.configs.embeddings.base import BaseEmbedderConfig
|
||||
from mem0.embeddings.azure_openai import AzureOpenAIEmbedding
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -29,18 +31,26 @@ def test_embed_text(mock_openai_client):
|
||||
assert embedding == [0.1, 0.2, 0.3]
|
||||
|
||||
|
||||
def test_embed_text_with_newlines(mock_openai_client):
|
||||
config = BaseEmbedderConfig(model="text-embedding-ada-002")
|
||||
embedder = AzureOpenAIEmbedding(config)
|
||||
|
||||
mock_embedding_response = Mock()
|
||||
mock_embedding_response.data = [Mock(embedding=[0.4, 0.5, 0.6])]
|
||||
mock_openai_client.embeddings.create.return_value = mock_embedding_response
|
||||
|
||||
text = "Hello,\nthis is a test\nwith newlines."
|
||||
embedding = embedder.embed(text)
|
||||
|
||||
mock_openai_client.embeddings.create.assert_called_once_with(
|
||||
input=["Hello, this is a test with newlines."], model="text-embedding-ada-002"
|
||||
@pytest.mark.parametrize(
|
||||
"default_headers, expected_header",
|
||||
[
|
||||
(None, None),
|
||||
({"Test": "test_value"}, "test_value"),
|
||||
({}, None)
|
||||
],
|
||||
)
|
||||
def test_embed_text_with_default_headers(default_headers, expected_header):
|
||||
config = BaseEmbedderConfig(
|
||||
model="text-embedding-ada-002",
|
||||
azure_kwargs={
|
||||
"api_key": "test",
|
||||
"api_version": "test_version",
|
||||
"azure_endpoint": "test_endpoint",
|
||||
"azuer_deployment": "test_deployment",
|
||||
"default_headers": default_headers
|
||||
}
|
||||
)
|
||||
assert embedding == [0.4, 0.5, 0.6]
|
||||
embedder = AzureOpenAIEmbedding(config)
|
||||
assert embedder.client.api_key == "test"
|
||||
assert embedder.client._api_version == "test_version"
|
||||
assert embedder.client.default_headers.get("Test") == expected_header
|
||||
|
||||
@@ -92,10 +92,17 @@ def test_generate_response_with_tools(mock_openai_client):
|
||||
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}
|
||||
|
||||
|
||||
def test_generate_with_http_proxies():
|
||||
@pytest.mark.parametrize(
|
||||
"default_headers",
|
||||
[None, {"Firstkey": "FirstVal", "SecondKey": "SecondVal"}],
|
||||
)
|
||||
def test_generate_with_http_proxies(default_headers):
|
||||
mock_http_client = Mock(spec=httpx.Client)
|
||||
mock_http_client_instance = Mock(spec=httpx.Client)
|
||||
mock_http_client.return_value = mock_http_client_instance
|
||||
azure_kwargs = {"api_key": "test"}
|
||||
if default_headers:
|
||||
azure_kwargs["default_headers"] = default_headers
|
||||
|
||||
with (
|
||||
patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai,
|
||||
@@ -108,7 +115,7 @@ def test_generate_with_http_proxies():
|
||||
top_p=TOP_P,
|
||||
api_key="test",
|
||||
http_client_proxies="http://testproxy.mem0.net:8000",
|
||||
azure_kwargs={"api_key": "test"},
|
||||
azure_kwargs=azure_kwargs,
|
||||
)
|
||||
|
||||
_ = AzureOpenAILLM(config)
|
||||
@@ -119,5 +126,6 @@ def test_generate_with_http_proxies():
|
||||
azure_deployment=None,
|
||||
azure_endpoint=None,
|
||||
api_version=None,
|
||||
default_headers=default_headers,
|
||||
)
|
||||
mock_http_client.assert_called_once_with(proxies="http://testproxy.mem0.net:8000")
|
||||
|
||||
Reference in New Issue
Block a user