Skip to content

Commit 961a4fb

Browse files
Aayush KatariaAayush Kataria
authored andcommitted
Resolving comments and adding some code improvements
1 parent 0cf12b5 commit 961a4fb

5 files changed

Lines changed: 116 additions & 7 deletions

File tree

agent_memory_toolkit/_utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,53 @@ def _resolve_embedding_dimensions(val: Optional[int]) -> int:
128128
return parsed
129129

130130

131+
_ALLOWED_EMBEDDING_DATA_TYPES = ("float32", "uint8", "int8")
132+
_ALLOWED_DISTANCE_FUNCTIONS = ("cosine", "dotproduct", "euclidean")
133+
134+
135+
def _resolve_embedding_data_type(val: Optional[str]) -> str:
136+
"""Resolve embedding data type from explicit value or ``AI_FOUNDRY_EMBEDDING_DATA_TYPE`` env var.
137+
138+
Defaults to ``float32``. Raises :class:`ConfigurationError` for unknown values.
139+
"""
140+
raw = (val if val is not None else os.environ.get("AI_FOUNDRY_EMBEDDING_DATA_TYPE") or "float32").strip()
141+
if raw not in _ALLOWED_EMBEDDING_DATA_TYPES:
142+
raise ConfigurationError(
143+
message=(
144+
f"Invalid configuration for embedding_data_type: must be one of "
145+
f"{_ALLOWED_EMBEDDING_DATA_TYPES}, got {raw!r}"
146+
),
147+
parameter="embedding_data_type",
148+
)
149+
return raw
150+
151+
152+
def _resolve_distance_function(val: Optional[str]) -> str:
153+
"""Resolve distance function from explicit value or ``AI_FOUNDRY_EMBEDDING_DISTANCE_FUNCTION`` env var.
154+
155+
Defaults to ``cosine``. Raises :class:`ConfigurationError` for unknown values.
156+
"""
157+
raw = (val if val is not None else os.environ.get("AI_FOUNDRY_EMBEDDING_DISTANCE_FUNCTION") or "cosine").strip()
158+
if raw not in _ALLOWED_DISTANCE_FUNCTIONS:
159+
raise ConfigurationError(
160+
message=(
161+
f"Invalid configuration for distance_function: must be one of "
162+
f"{_ALLOWED_DISTANCE_FUNCTIONS}, got {raw!r}"
163+
),
164+
parameter="distance_function",
165+
)
166+
return raw
167+
168+
169+
def _resolve_full_text_language(val: Optional[str]) -> str:
170+
"""Resolve full-text language from explicit value or ``COSMOS_DB_FULL_TEXT_LANGUAGE`` env var.
171+
172+
Defaults to ``en-US``. Empty values fall back to the default.
173+
"""
174+
raw = (val if val is not None else os.environ.get("COSMOS_DB_FULL_TEXT_LANGUAGE") or "en-US").strip()
175+
return raw or "en-US"
176+
177+
131178
def _resolve_cosmos_throughput_mode(val: Optional[str]) -> str:
132179
"""Resolve throughput mode from explicit value or env var.
133180

agent_memory_toolkit/aio/cosmos_memory_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
_make_memory,
2727
_resolve_cosmos_provisioning_autoscale_max_ru,
2828
_resolve_cosmos_throughput_mode,
29+
_resolve_distance_function,
30+
_resolve_embedding_data_type,
2931
_resolve_embedding_dimensions,
32+
_resolve_full_text_language,
3033
_validate_connection,
3134
_validate_hybrid_search,
3235
)
@@ -482,9 +485,9 @@ async def create_memory_store(
482485
lease_partition_key = PartitionKey(path="/id")
483486
vec_policy, idx_policy, ft_policy = _container_policies(
484487
embedding_dimensions=embedding_dimensions or self._embedding_dimensions or 1536,
485-
embedding_data_type=embedding_data_type or "float32",
486-
distance_function=distance_function or "cosine",
487-
full_text_language=full_text_language or "en-US",
488+
embedding_data_type=_resolve_embedding_data_type(embedding_data_type),
489+
distance_function=_resolve_distance_function(distance_function),
490+
full_text_language=_resolve_full_text_language(full_text_language),
488491
)
489492
offer_throughput = _cosmos_container_offer_throughput(
490493
throughput_mode=self._cosmos_throughput_mode,

agent_memory_toolkit/cosmos_memory_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
_make_memory,
2323
_resolve_cosmos_provisioning_autoscale_max_ru,
2424
_resolve_cosmos_throughput_mode,
25+
_resolve_distance_function,
26+
_resolve_embedding_data_type,
2527
_resolve_embedding_dimensions,
28+
_resolve_full_text_language,
2629
_validate_connection,
2730
_validate_hybrid_search,
2831
)
@@ -456,9 +459,9 @@ def create_memory_store(
456459
lease_partition_key = PartitionKey(path="/id")
457460
vec_policy, idx_policy, ft_policy = _container_policies(
458461
embedding_dimensions=embedding_dimensions or self._embedding_dimensions or 1536,
459-
embedding_data_type=embedding_data_type or "float32",
460-
distance_function=distance_function or "cosine",
461-
full_text_language=full_text_language or "en-US",
462+
embedding_data_type=_resolve_embedding_data_type(embedding_data_type),
463+
distance_function=_resolve_distance_function(distance_function),
464+
full_text_language=_resolve_full_text_language(full_text_language),
462465
)
463466
offer_throughput = _cosmos_container_offer_throughput(
464467
throughput_mode=self._cosmos_throughput_mode,

infra/modules/cosmos.bicep

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ resource memoriesContainer 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/c
113113
{
114114
path: '/embedding/?'
115115
}
116+
{
117+
path: '/source_memory_ids/*'
118+
}
119+
{
120+
path: '/supersedes_ids/*'
121+
}
116122
{
117123
path: '/"_etag"/?'
118124
}

tests/unit/test_utils.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
DEFAULT_TTL_BY_TYPE,
77
_build_container_kwargs,
88
_make_memory,
9+
_resolve_distance_function,
10+
_resolve_embedding_data_type,
11+
_resolve_full_text_language,
912
compute_content_hash,
1013
)
11-
from agent_memory_toolkit.exceptions import ValidationError
14+
from agent_memory_toolkit.exceptions import ConfigurationError, ValidationError
1215

1316

1417
def test_build_container_kwargs_includes_required_fields_and_extras():
@@ -180,3 +183,50 @@ def test_make_memory_invalid_role():
180183
def test_make_memory_invalid_type():
181184
with pytest.raises(ValidationError):
182185
_make_memory(user_id="u1", role="user", content="test", memory_type="invalid")
186+
187+
188+
def test_resolve_embedding_data_type_defaults(monkeypatch):
189+
monkeypatch.delenv("AI_FOUNDRY_EMBEDDING_DATA_TYPE", raising=False)
190+
assert _resolve_embedding_data_type(None) == "float32"
191+
192+
193+
def test_resolve_embedding_data_type_from_env(monkeypatch):
194+
monkeypatch.setenv("AI_FOUNDRY_EMBEDDING_DATA_TYPE", "int8")
195+
assert _resolve_embedding_data_type(None) == "int8"
196+
197+
198+
def test_resolve_embedding_data_type_explicit_overrides_env(monkeypatch):
199+
monkeypatch.setenv("AI_FOUNDRY_EMBEDDING_DATA_TYPE", "int8")
200+
assert _resolve_embedding_data_type("uint8") == "uint8"
201+
202+
203+
def test_resolve_embedding_data_type_invalid_raises(monkeypatch):
204+
monkeypatch.setenv("AI_FOUNDRY_EMBEDDING_DATA_TYPE", "bogus")
205+
with pytest.raises(ConfigurationError):
206+
_resolve_embedding_data_type(None)
207+
208+
209+
def test_resolve_distance_function_defaults(monkeypatch):
210+
monkeypatch.delenv("AI_FOUNDRY_EMBEDDING_DISTANCE_FUNCTION", raising=False)
211+
assert _resolve_distance_function(None) == "cosine"
212+
213+
214+
def test_resolve_distance_function_from_env(monkeypatch):
215+
monkeypatch.setenv("AI_FOUNDRY_EMBEDDING_DISTANCE_FUNCTION", "dotproduct")
216+
assert _resolve_distance_function(None) == "dotproduct"
217+
218+
219+
def test_resolve_distance_function_invalid_raises(monkeypatch):
220+
monkeypatch.setenv("AI_FOUNDRY_EMBEDDING_DISTANCE_FUNCTION", "manhattan")
221+
with pytest.raises(ConfigurationError):
222+
_resolve_distance_function(None)
223+
224+
225+
def test_resolve_full_text_language_defaults(monkeypatch):
226+
monkeypatch.delenv("COSMOS_DB_FULL_TEXT_LANGUAGE", raising=False)
227+
assert _resolve_full_text_language(None) == "en-US"
228+
229+
230+
def test_resolve_full_text_language_from_env(monkeypatch):
231+
monkeypatch.setenv("COSMOS_DB_FULL_TEXT_LANGUAGE", "fr-FR")
232+
assert _resolve_full_text_language(None) == "fr-FR"

0 commit comments

Comments
 (0)