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 MagicMock, patch |
| #2 | |
| #3 | from mem0.memory.main import Memory |
| #4 | |
| #5 | |
| #6 | def test_memory_configuration_without_env_vars(): |
| #7 | """Test Memory configuration with mock config instead of environment variables""" |
| #8 | |
| #9 | # Mock configuration without relying on environment variables |
| #10 | mock_config = { |
| #11 | "llm": { |
| #12 | "provider": "openai", |
| #13 | "config": { |
| #14 | "model": "gpt-4", |
| #15 | "temperature": 0.1, |
| #16 | "max_tokens": 1500, |
| #17 | }, |
| #18 | }, |
| #19 | "vector_store": { |
| #20 | "provider": "chroma", |
| #21 | "config": { |
| #22 | "collection_name": "test_collection", |
| #23 | "path": "./test_db", |
| #24 | }, |
| #25 | }, |
| #26 | "embedder": { |
| #27 | "provider": "openai", |
| #28 | "config": { |
| #29 | "model": "text-embedding-ada-002", |
| #30 | }, |
| #31 | }, |
| #32 | } |
| #33 | |
| #34 | # Test messages similar to the main.py file |
| #35 | test_messages = [ |
| #36 | {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, |
| #37 | { |
| #38 | "role": "assistant", |
| #39 | "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions.", |
| #40 | }, |
| #41 | ] |
| #42 | |
| #43 | # Mock the Memory class methods to avoid actual API calls |
| #44 | with patch.object(Memory, "__init__", return_value=None): |
| #45 | with patch.object(Memory, "from_config") as mock_from_config: |
| #46 | with patch.object(Memory, "add") as mock_add: |
| #47 | with patch.object(Memory, "get_all") as mock_get_all: |
| #48 | # Configure mocks |
| #49 | mock_memory_instance = MagicMock() |
| #50 | mock_from_config.return_value = mock_memory_instance |
| #51 | |
| #52 | mock_add.return_value = { |
| #53 | "results": [ |
| #54 | {"id": "1", "text": "Alex is a vegetarian"}, |
| #55 | {"id": "2", "text": "Alex is allergic to nuts"}, |
| #56 | ] |
| #57 | } |
| #58 | |
| #59 | mock_get_all.return_value = [ |
| #60 | {"id": "1", "text": "Alex is a vegetarian", "metadata": {"category": "dietary_preferences"}}, |
| #61 | {"id": "2", "text": "Alex is allergic to nuts", "metadata": {"category": "allergies"}}, |
| #62 | ] |
| #63 | |
| #64 | # Test the workflow |
| #65 | mem = Memory.from_config(config_dict=mock_config) |
| #66 | assert mem is not None |
| #67 | |
| #68 | # Test adding memories |
| #69 | result = mock_add(test_messages, user_id="alice", metadata={"category": "book_recommendations"}) |
| #70 | assert "results" in result |
| #71 | assert len(result["results"]) == 2 |
| #72 | |
| #73 | # Test retrieving memories |
| #74 | all_memories = mock_get_all(user_id="alice") |
| #75 | assert len(all_memories) == 2 |
| #76 | assert any("vegetarian" in memory["text"] for memory in all_memories) |
| #77 | assert any("allergic to nuts" in memory["text"] for memory in all_memories) |
| #78 | |
| #79 | |
| #80 | def test_azure_config_structure(): |
| #81 | """Test that Azure configuration structure is properly formatted""" |
| #82 | |
| #83 | # Test Azure configuration structure (without actual credentials) |
| #84 | azure_config = { |
| #85 | "llm": { |
| #86 | "provider": "azure_openai", |
| #87 | "config": { |
| #88 | "model": "gpt-4", |
| #89 | "temperature": 0.1, |
| #90 | "max_tokens": 1500, |
| #91 | "azure_kwargs": { |
| #92 | "azure_deployment": "test-deployment", |
| #93 | "api_version": "2023-12-01-preview", |
| #94 | "azure_endpoint": "https://test.openai.azure.com/", |
| #95 | "api_key": "test-key", |
| #96 | }, |
| #97 | }, |
| #98 | }, |
| #99 | "vector_store": { |
| #100 | "provider": "azure_ai_search", |
| #101 | "config": { |
| #102 | "service_name": "test-service", |
| #103 | "api_key": "test-key", |
| #104 | "collection_name": "test-collection", |
| #105 | "embedding_model_dims": 1536, |
| #106 | }, |
| #107 | }, |
| #108 | "embedder": { |
| #109 | "provider": "azure_openai", |
| #110 | "config": { |
| #111 | "model": "text-embedding-ada-002", |
| #112 | "api_key": "test-key", |
| #113 | "azure_kwargs": { |
| #114 | "api_version": "2023-12-01-preview", |
| #115 | "azure_deployment": "test-embedding-deployment", |
| #116 | "azure_endpoint": "https://test.openai.azure.com/", |
| #117 | "api_key": "test-key", |
| #118 | }, |
| #119 | }, |
| #120 | }, |
| #121 | } |
| #122 | |
| #123 | # Validate configuration structure |
| #124 | assert "llm" in azure_config |
| #125 | assert "vector_store" in azure_config |
| #126 | assert "embedder" in azure_config |
| #127 | |
| #128 | # Validate Azure-specific configurations |
| #129 | assert azure_config["llm"]["provider"] == "azure_openai" |
| #130 | assert "azure_kwargs" in azure_config["llm"]["config"] |
| #131 | assert "azure_deployment" in azure_config["llm"]["config"]["azure_kwargs"] |
| #132 | |
| #133 | assert azure_config["vector_store"]["provider"] == "azure_ai_search" |
| #134 | assert "service_name" in azure_config["vector_store"]["config"] |
| #135 | |
| #136 | assert azure_config["embedder"]["provider"] == "azure_openai" |
| #137 | assert "azure_kwargs" in azure_config["embedder"]["config"] |
| #138 | |
| #139 | |
| #140 | def test_memory_messages_format(): |
| #141 | """Test that memory messages are properly formatted""" |
| #142 | |
| #143 | # Test message format from main.py |
| #144 | messages = [ |
| #145 | {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, |
| #146 | { |
| #147 | "role": "assistant", |
| #148 | "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions.", |
| #149 | }, |
| #150 | ] |
| #151 | |
| #152 | # Validate message structure |
| #153 | assert len(messages) == 2 |
| #154 | assert all("role" in msg for msg in messages) |
| #155 | assert all("content" in msg for msg in messages) |
| #156 | |
| #157 | # Validate roles |
| #158 | assert messages[0]["role"] == "user" |
| #159 | assert messages[1]["role"] == "assistant" |
| #160 | |
| #161 | # Validate content |
| #162 | assert "vegetarian" in messages[0]["content"].lower() |
| #163 | assert "allergic to nuts" in messages[0]["content"].lower() |
| #164 | assert "vegetarian" in messages[1]["content"].lower() |
| #165 | assert "nut allergy" in messages[1]["content"].lower() |
| #166 | |
| #167 | |
| #168 | def test_safe_update_prompt_constant(): |
| #169 | """Test the SAFE_UPDATE_PROMPT constant from main.py""" |
| #170 | |
| #171 | SAFE_UPDATE_PROMPT = """ |
| #172 | Based on the user's latest messages, what new preference can be inferred? |
| #173 | Reply only in this json_object format: |
| #174 | """ |
| #175 | |
| #176 | # Validate prompt structure |
| #177 | assert isinstance(SAFE_UPDATE_PROMPT, str) |
| #178 | assert "user's latest messages" in SAFE_UPDATE_PROMPT |
| #179 | assert "json_object format" in SAFE_UPDATE_PROMPT |
| #180 | assert len(SAFE_UPDATE_PROMPT.strip()) > 0 |
| #181 |