repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | from unittest.mock import Mock, patch |
| #2 | |
| #3 | import pytest |
| #4 | |
| #5 | from mem0.configs.llms.azure import AzureOpenAIConfig |
| #6 | from mem0.llms.azure_openai import AzureOpenAILLM |
| #7 | |
| #8 | MODEL = "gpt-4.1-nano-2025-04-14" # or your custom deployment name |
| #9 | TEMPERATURE = 0.7 |
| #10 | MAX_TOKENS = 100 |
| #11 | TOP_P = 1.0 |
| #12 | |
| #13 | |
| #14 | @pytest.fixture |
| #15 | def mock_openai_client(): |
| #16 | with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_openai: |
| #17 | mock_client = Mock() |
| #18 | mock_openai.return_value = mock_client |
| #19 | yield mock_client |
| #20 | |
| #21 | |
| #22 | def test_generate_response_without_tools(mock_openai_client): |
| #23 | config = AzureOpenAIConfig(model=MODEL, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P) |
| #24 | llm = AzureOpenAILLM(config) |
| #25 | messages = [ |
| #26 | {"role": "system", "content": "You are a helpful assistant."}, |
| #27 | {"role": "user", "content": "Hello, how are you?"}, |
| #28 | ] |
| #29 | |
| #30 | mock_response = Mock() |
| #31 | mock_response.choices = [Mock(message=Mock(content="I'm doing well, thank you for asking!"))] |
| #32 | mock_openai_client.chat.completions.create.return_value = mock_response |
| #33 | |
| #34 | response = llm.generate_response(messages) |
| #35 | |
| #36 | mock_openai_client.chat.completions.create.assert_called_once_with( |
| #37 | model=MODEL, messages=messages, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P |
| #38 | ) |
| #39 | assert response == "I'm doing well, thank you for asking!" |
| #40 | |
| #41 | |
| #42 | def test_generate_response_with_tools(mock_openai_client): |
| #43 | config = AzureOpenAIConfig(model=MODEL, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P) |
| #44 | llm = AzureOpenAILLM(config) |
| #45 | messages = [ |
| #46 | {"role": "system", "content": "You are a helpful assistant."}, |
| #47 | {"role": "user", "content": "Add a new memory: Today is a sunny day."}, |
| #48 | ] |
| #49 | tools = [ |
| #50 | { |
| #51 | "type": "function", |
| #52 | "function": { |
| #53 | "name": "add_memory", |
| #54 | "description": "Add a memory", |
| #55 | "parameters": { |
| #56 | "type": "object", |
| #57 | "properties": {"data": {"type": "string", "description": "Data to add to memory"}}, |
| #58 | "required": ["data"], |
| #59 | }, |
| #60 | }, |
| #61 | } |
| #62 | ] |
| #63 | |
| #64 | mock_response = Mock() |
| #65 | mock_message = Mock() |
| #66 | mock_message.content = "I've added the memory for you." |
| #67 | |
| #68 | mock_tool_call = Mock() |
| #69 | mock_tool_call.function.name = "add_memory" |
| #70 | mock_tool_call.function.arguments = '{"data": "Today is a sunny day."}' |
| #71 | |
| #72 | mock_message.tool_calls = [mock_tool_call] |
| #73 | mock_response.choices = [Mock(message=mock_message)] |
| #74 | mock_openai_client.chat.completions.create.return_value = mock_response |
| #75 | |
| #76 | response = llm.generate_response(messages, tools=tools) |
| #77 | |
| #78 | mock_openai_client.chat.completions.create.assert_called_once_with( |
| #79 | model=MODEL, |
| #80 | messages=messages, |
| #81 | temperature=TEMPERATURE, |
| #82 | max_tokens=MAX_TOKENS, |
| #83 | top_p=TOP_P, |
| #84 | tools=tools, |
| #85 | tool_choice="auto", |
| #86 | ) |
| #87 | |
| #88 | assert response["content"] == "I've added the memory for you." |
| #89 | assert len(response["tool_calls"]) == 1 |
| #90 | assert response["tool_calls"][0]["name"] == "add_memory" |
| #91 | assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."} |
| #92 | |
| #93 | |
| #94 | @pytest.mark.parametrize( |
| #95 | "default_headers", |
| #96 | [None, {"Firstkey": "FirstVal", "SecondKey": "SecondVal"}], |
| #97 | ) |
| #98 | def test_generate_with_http_proxies(default_headers): |
| #99 | mock_http_client = Mock() |
| #100 | mock_http_client_instance = Mock() |
| #101 | mock_http_client.return_value = mock_http_client_instance |
| #102 | azure_kwargs = {"api_key": "test"} |
| #103 | if default_headers: |
| #104 | azure_kwargs["default_headers"] = default_headers |
| #105 | |
| #106 | with ( |
| #107 | patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai, |
| #108 | patch("httpx.Client", new=mock_http_client), |
| #109 | ): |
| #110 | config = AzureOpenAIConfig( |
| #111 | model=MODEL, |
| #112 | temperature=TEMPERATURE, |
| #113 | max_tokens=MAX_TOKENS, |
| #114 | top_p=TOP_P, |
| #115 | api_key="test", |
| #116 | http_client_proxies="http://testproxy.mem0.net:8000", |
| #117 | azure_kwargs=azure_kwargs, |
| #118 | ) |
| #119 | |
| #120 | _ = AzureOpenAILLM(config) |
| #121 | |
| #122 | mock_azure_openai.assert_called_once_with( |
| #123 | api_key="test", |
| #124 | http_client=mock_http_client_instance, |
| #125 | azure_deployment=None, |
| #126 | azure_endpoint=None, |
| #127 | azure_ad_token_provider=None, |
| #128 | api_version=None, |
| #129 | default_headers=default_headers, |
| #130 | ) |
| #131 | mock_http_client.assert_called_once_with(proxies="http://testproxy.mem0.net:8000") |
| #132 | |
| #133 | |
| #134 | def test_init_with_api_key(monkeypatch): |
| #135 | # Patch environment variables to None to force config usage |
| #136 | monkeypatch.delenv("LLM_AZURE_OPENAI_API_KEY", raising=False) |
| #137 | monkeypatch.delenv("LLM_AZURE_DEPLOYMENT", raising=False) |
| #138 | monkeypatch.delenv("LLM_AZURE_ENDPOINT", raising=False) |
| #139 | monkeypatch.delenv("LLM_AZURE_API_VERSION", raising=False) |
| #140 | |
| #141 | config = AzureOpenAIConfig( |
| #142 | model=MODEL, |
| #143 | temperature=TEMPERATURE, |
| #144 | max_tokens=MAX_TOKENS, |
| #145 | top_p=TOP_P, |
| #146 | ) |
| #147 | # Set Azure kwargs directly |
| #148 | config.azure_kwargs.api_key = "test-key" |
| #149 | config.azure_kwargs.azure_deployment = "test-deployment" |
| #150 | config.azure_kwargs.azure_endpoint = "https://test-endpoint" |
| #151 | config.azure_kwargs.api_version = "2024-01-01" |
| #152 | config.azure_kwargs.default_headers = {"x-test": "header"} |
| #153 | config.http_client = None |
| #154 | |
| #155 | with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai: |
| #156 | llm = AzureOpenAILLM(config) |
| #157 | mock_azure_openai.assert_called_once_with( |
| #158 | azure_deployment="test-deployment", |
| #159 | azure_endpoint="https://test-endpoint", |
| #160 | azure_ad_token_provider=None, |
| #161 | api_version="2024-01-01", |
| #162 | api_key="test-key", |
| #163 | http_client=None, |
| #164 | default_headers={"x-test": "header"}, |
| #165 | ) |
| #166 | assert llm.config.model == MODEL |
| #167 | |
| #168 | |
| #169 | def test_init_with_env_vars(monkeypatch): |
| #170 | monkeypatch.setenv("LLM_AZURE_OPENAI_API_KEY", "env-key") |
| #171 | monkeypatch.setenv("LLM_AZURE_DEPLOYMENT", "env-deployment") |
| #172 | monkeypatch.setenv("LLM_AZURE_ENDPOINT", "https://env-endpoint") |
| #173 | monkeypatch.setenv("LLM_AZURE_API_VERSION", "2024-02-02") |
| #174 | |
| #175 | config = AzureOpenAIConfig(model=None) |
| #176 | config.azure_kwargs.api_key = None |
| #177 | config.azure_kwargs.azure_deployment = None |
| #178 | config.azure_kwargs.azure_endpoint = None |
| #179 | config.azure_kwargs.api_version = None |
| #180 | config.azure_kwargs.default_headers = None |
| #181 | config.http_client = None |
| #182 | |
| #183 | with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai: |
| #184 | llm = AzureOpenAILLM(config) |
| #185 | mock_azure_openai.assert_called_once_with( |
| #186 | azure_deployment="env-deployment", |
| #187 | azure_endpoint="https://env-endpoint", |
| #188 | azure_ad_token_provider=None, |
| #189 | api_version="2024-02-02", |
| #190 | api_key="env-key", |
| #191 | http_client=None, |
| #192 | default_headers=None, |
| #193 | ) |
| #194 | # Should default to "gpt-4.1-nano-2025-04-14" if model is None |
| #195 | assert llm.config.model == "gpt-4.1-nano-2025-04-14" |
| #196 | |
| #197 | |
| #198 | def test_init_with_default_azure_credential(monkeypatch): |
| #199 | # No API key in config or env, triggers DefaultAzureCredential |
| #200 | monkeypatch.delenv("LLM_AZURE_OPENAI_API_KEY", raising=False) |
| #201 | config = AzureOpenAIConfig(model=MODEL) |
| #202 | config.azure_kwargs.api_key = None |
| #203 | config.azure_kwargs.azure_deployment = "dep" |
| #204 | config.azure_kwargs.azure_endpoint = "https://endpoint" |
| #205 | config.azure_kwargs.api_version = "2024-03-03" |
| #206 | config.azure_kwargs.default_headers = None |
| #207 | config.http_client = None |
| #208 | |
| #209 | with ( |
| #210 | patch("mem0.llms.azure_openai.DefaultAzureCredential") as mock_cred, |
| #211 | patch("mem0.llms.azure_openai.get_bearer_token_provider") as mock_token_provider, |
| #212 | patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai, |
| #213 | ): |
| #214 | mock_cred_instance = mock_cred.return_value |
| #215 | mock_token_provider.return_value = "token-provider" |
| #216 | AzureOpenAILLM(config) |
| #217 | mock_cred.assert_called_once() |
| #218 | mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default") |
| #219 | mock_azure_openai.assert_called_once_with( |
| #220 | azure_deployment="dep", |
| #221 | azure_endpoint="https://endpoint", |
| #222 | azure_ad_token_provider="token-provider", |
| #223 | api_version="2024-03-03", |
| #224 | api_key=None, |
| #225 | http_client=None, |
| #226 | default_headers=None, |
| #227 | ) |
| #228 | |
| #229 | |
| #230 | def test_init_with_placeholder_api_key(monkeypatch): |
| #231 | # Placeholder API key should trigger DefaultAzureCredential |
| #232 | config = AzureOpenAIConfig(model=MODEL) |
| #233 | config.azure_kwargs.api_key = "your-api-key" |
| #234 | config.azure_kwargs.azure_deployment = "dep" |
| #235 | config.azure_kwargs.azure_endpoint = "https://endpoint" |
| #236 | config.azure_kwargs.api_version = "2024-04-04" |
| #237 | config.azure_kwargs.default_headers = None |
| #238 | config.http_client = None |
| #239 | |
| #240 | with ( |
| #241 | patch("mem0.llms.azure_openai.DefaultAzureCredential") as mock_cred, |
| #242 | patch("mem0.llms.azure_openai.get_bearer_token_provider") as mock_token_provider, |
| #243 | patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai, |
| #244 | ): |
| #245 | mock_cred_instance = mock_cred.return_value |
| #246 | mock_token_provider.return_value = "token-provider" |
| #247 | AzureOpenAILLM(config) |
| #248 | mock_cred.assert_called_once() |
| #249 | mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default") |
| #250 | mock_azure_openai.assert_called_once_with( |
| #251 | azure_deployment="dep", |
| #252 | azure_endpoint="https://endpoint", |
| #253 | azure_ad_token_provider="token-provider", |
| #254 | api_version="2024-04-04", |
| #255 | api_key=None, |
| #256 | http_client=None, |
| #257 | default_headers=None, |
| #258 | ) |
| #259 |