dandelion-memory 1.0.0rc15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- benchmarks/__init__.py +3 -0
- benchmarks/adapters/__init__.py +29 -0
- benchmarks/adapters/ablations.py +406 -0
- benchmarks/adapters/base.py +72 -0
- benchmarks/adapters/controlled/__init__.py +13 -0
- benchmarks/adapters/controlled/core_archival.py +115 -0
- benchmarks/adapters/controlled/extract_resolve.py +130 -0
- benchmarks/adapters/controlled/hybrid_rag.py +120 -0
- benchmarks/adapters/controlled/no_late_binding.py +113 -0
- benchmarks/adapters/dandelion.py +94 -0
- benchmarks/adapters/native/__init__.py +31 -0
- benchmarks/adapters/native/base.py +45 -0
- benchmarks/adapters/native/dandelion_native.py +164 -0
- benchmarks/adapters/native/dandelion_native_b0.py +165 -0
- benchmarks/adapters/native/dandelion_native_b1.py +170 -0
- benchmarks/adapters/native/dandelion_native_b2.py +169 -0
- benchmarks/adapters/native/dandelion_native_b3.py +171 -0
- benchmarks/adapters/native/dandelion_native_c4.py +261 -0
- benchmarks/adapters/native/dandelion_native_c4_1.py +281 -0
- benchmarks/adapters/native/dandelion_native_c4_2.py +435 -0
- benchmarks/adapters/native/event_evidence_b2.py +616 -0
- benchmarks/adapters/native/evidence_b2.py +609 -0
- benchmarks/adapters/native/letta_native.py +253 -0
- benchmarks/adapters/native/mem0_native.py +219 -0
- benchmarks/adapters/native/store_vector_b0.py +792 -0
- benchmarks/adapters/native/store_vector_b2.py +808 -0
- benchmarks/agent_adapter_stdio_acceptance.py +86 -0
- benchmarks/bootstrap_cross_stack.py +218 -0
- benchmarks/canonical_profile_instrumentation.py +167 -0
- benchmarks/canonical_public_baseline.py +288 -0
- benchmarks/cli.py +525 -0
- benchmarks/collector_public30_offline.py +1653 -0
- benchmarks/datasets/__init__.py +45 -0
- benchmarks/datasets/conversational_generator.py +238 -0
- benchmarks/datasets/generator.py +283 -0
- benchmarks/datasets/holdout_topology_generator.py +290 -0
- benchmarks/datasets/holdout_topology_generator_v2.py +294 -0
- benchmarks/datasets/multi_world.py +1643 -0
- benchmarks/datasets/scale_generator.py +289 -0
- benchmarks/datasets/schema.py +57 -0
- benchmarks/deep_collision_stress.py +199 -0
- benchmarks/evaluators/__init__.py +12 -0
- benchmarks/evaluators/manifest.py +142 -0
- benchmarks/evaluators/metrics.py +277 -0
- benchmarks/evaluators/runner.py +258 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v1.py +127 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v2.py +127 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v3.py +146 -0
- benchmarks/f1_step0/build_ledger.py +143 -0
- benchmarks/f1_step0/build_ledger_v2.py +296 -0
- benchmarks/f1_step0/compare_dev52_readiness_v1.py +106 -0
- benchmarks/f1_step0/locator_public_smoke.py +172 -0
- benchmarks/f1_step0/prepare_dev52_owner_v1.py +133 -0
- benchmarks/f1_step0/prepare_dev52_owner_v2.py +133 -0
- benchmarks/f1_step0/public_identity_probe.py +191 -0
- benchmarks/f1_step0/public_response_mapper.py +101 -0
- benchmarks/f1_step0/real_source_public_preflight.py +244 -0
- benchmarks/f1_step0/run_exact_fusion_dev_experiment_v1.py +83 -0
- benchmarks/f1_step0/scorer.py +110 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v1.py +627 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v2.py +659 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v3.py +776 -0
- benchmarks/f1_step0/test_public_response_mapper.py +110 -0
- benchmarks/f1_step0/test_real_source_public_preflight.py +25 -0
- benchmarks/f1_step0/test_scorer.py +158 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement.py +189 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement_v2.py +263 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement_v3.py +334 -0
- benchmarks/f1_step0/typed_readback_public_smoke.py +181 -0
- benchmarks/forensic_c4_1_dev.py +258 -0
- benchmarks/forensic_c4_dev.py +255 -0
- benchmarks/forensic_deep25_c4_1.py +478 -0
- benchmarks/legacy_migration_acceptance.py +29 -0
- benchmarks/longmemeval_actor.py +497 -0
- benchmarks/longmemeval_agent_judge.py +593 -0
- benchmarks/longmemeval_budget.py +139 -0
- benchmarks/longmemeval_full.py +410 -0
- benchmarks/longmemeval_judge.py +327 -0
- benchmarks/longmemeval_pilot.py +316 -0
- benchmarks/longmemeval_s.py +318 -0
- benchmarks/memory_bench.py +248 -0
- benchmarks/memory_formation/__init__.py +1 -0
- benchmarks/memory_formation/audit.py +801 -0
- benchmarks/memory_formation/real_audit.py +688 -0
- benchmarks/memory_session_acceptance.py +279 -0
- benchmarks/native_codex_host.py +513 -0
- benchmarks/owner_dense_acceptance.py +126 -0
- benchmarks/pilot_seeker_vault.py +266 -0
- benchmarks/prepare_public30_ripgrep_range_owner.py +115 -0
- benchmarks/profile_canonical.py +373 -0
- benchmarks/profile_canonical_lanes.py +405 -0
- benchmarks/profile_search_chunks.py +351 -0
- benchmarks/public_ranges_faq_proof.py +121 -0
- benchmarks/public_repos/build_history_scope_v3.py +330 -0
- benchmarks/public_repos/build_oracle.py +108 -0
- benchmarks/public_repos/build_oracle_v2.py +169 -0
- benchmarks/public_repos/build_oracle_v3.py +176 -0
- benchmarks/public_repos/map_public_v2.py +296 -0
- benchmarks/public_repos/project_prompts_v2.py +31 -0
- benchmarks/public_repos/project_prompts_v3.py +29 -0
- benchmarks/public_repos/ready_admin_v2.py +214 -0
- benchmarks/public_repos/score_public_v2.py +258 -0
- benchmarks/public_repos/test_map_public_v2.py +171 -0
- benchmarks/public_repos/test_public_repo_v2.py +44 -0
- benchmarks/public_repos/test_public_repo_v3.py +46 -0
- benchmarks/public_repos/test_ready_admin_v2.py +117 -0
- benchmarks/public_repos/test_score_public_v2.py +163 -0
- benchmarks/public_repos/test_wire_v2.py +292 -0
- benchmarks/public_repos/wire_v2.py +603 -0
- benchmarks/real_blocker_fix_acceptance.py +155 -0
- benchmarks/real_scan_rescan_acceptance.py +41 -0
- benchmarks/recall_v2_20k_smoke.py +112 -0
- benchmarks/recall_v2_e1_retrieve.py +70 -0
- benchmarks/recall_v2_e1_score.py +271 -0
- benchmarks/recall_v2_e2_prepare.py +69 -0
- benchmarks/recall_v2_e2_public_score.py +91 -0
- benchmarks/recall_v2_e2_score.py +108 -0
- benchmarks/recall_v2_navigation_mcp_replay.py +308 -0
- benchmarks/recall_v2_reader_capture.py +118 -0
- benchmarks/recall_v2_reader_replay.py +136 -0
- benchmarks/run_blind_h10_holdout.py +284 -0
- benchmarks/run_blind_h9_holdout.py +275 -0
- benchmarks/run_c4_2_nightly_scale.py +446 -0
- benchmarks/run_context_scale_simulation.py +189 -0
- benchmarks/run_conversational_bench.py +170 -0
- benchmarks/run_h11_evaluation.py +316 -0
- benchmarks/run_h11_v2_evaluation.py +431 -0
- benchmarks/run_native_fixed12.py +469 -0
- benchmarks/run_native_preflight.py +280 -0
- benchmarks/run_scale_stress.py +225 -0
- benchmarks/solanazation_brownfield_01/build_oracle.py +970 -0
- benchmarks/solanazation_brownfield_01/check_level_a.py +78 -0
- benchmarks/solanazation_brownfield_01/debug_scope.py +13 -0
- benchmarks/solanazation_brownfield_01/evaluate_bootstrap_02.py +570 -0
- benchmarks/solanazation_brownfield_01/evaluate_first_pass.py +306 -0
- benchmarks/solanazation_brownfield_01/generate_dryrun_report.py +222 -0
- benchmarks/solanazation_brownfield_01/generate_manifests.py +146 -0
- benchmarks/solanazation_brownfield_01/inspect_misses.py +29 -0
- benchmarks/solanazation_brownfield_01/run_miss_forensic.py +206 -0
- benchmarks/solanazation_brownfield_01/test_facts.py +15 -0
- benchmarks/statistics/__init__.py +15 -0
- benchmarks/statistics/significance.py +326 -0
- benchmarks/test_native_mcp_preflight.py +229 -0
- benchmarks/test_prepare_public30_range_owner.py +35 -0
- benchmarks/test_public30_source_reference.py +698 -0
- benchmarks/test_recall_v2_e1_reader_input.py +89 -0
- benchmarks/test_recall_v2_navigation_mcp_replay.py +155 -0
- benchmarks/test_recall_v2_reader_replay.py +41 -0
- dandelion/__init__.py +3 -0
- dandelion/__main__.py +11 -0
- dandelion/agent_adapter.py +2369 -0
- dandelion/bootstrap/__init__.py +872 -0
- dandelion/bootstrap/alias_policy.py +50 -0
- dandelion/bootstrap/authority.py +225 -0
- dandelion/bootstrap/canonical_history.py +412 -0
- dandelion/bootstrap/classifier.py +403 -0
- dandelion/bootstrap/component_graph.py +280 -0
- dandelion/bootstrap/discovery.py +279 -0
- dandelion/bootstrap/domain_induction.py +180 -0
- dandelion/bootstrap/domain_topology.py +673 -0
- dandelion/bootstrap/evidence_envelope.py +132 -0
- dandelion/bootstrap/extractors/base.py +171 -0
- dandelion/bootstrap/extractors/exact.py +1234 -0
- dandelion/bootstrap/extractors/generic.py +100 -0
- dandelion/bootstrap/extractors/git_temporal.py +111 -0
- dandelion/bootstrap/extractors/registry.py +345 -0
- dandelion/bootstrap/extractors/relations.py +89 -0
- dandelion/bootstrap/extractors/semantic.py +424 -0
- dandelion/bootstrap/extractors/status.py +218 -0
- dandelion/bootstrap/git_sweeper.py +135 -0
- dandelion/bootstrap/manifest.py +246 -0
- dandelion/bootstrap/quarantine.py +264 -0
- dandelion/bootstrap/report.py +259 -0
- dandelion/bootstrap/root_resolver.py +221 -0
- dandelion/bootstrap/tech_profiler.py +125 -0
- dandelion/bootstrap/topology.py +75 -0
- dandelion/bootstrap/topology_index.py +110 -0
- dandelion/bootstrap/transaction.py +315 -0
- dandelion/canonical_bootstrap.py +257 -0
- dandelion/canonical_memory.py +4130 -0
- dandelion/captured_input.py +295 -0
- dandelion/cli.py +767 -0
- dandelion/config.py +40 -0
- dandelion/derived_index.py +745 -0
- dandelion/domains.py +706 -0
- dandelion/embedder.py +229 -0
- dandelion/event_evidence.py +571 -0
- dandelion/evidence.py +812 -0
- dandelion/evidence_response.py +453 -0
- dandelion/evidence_workspace.py +2360 -0
- dandelion/exact_context.py +129 -0
- dandelion/hydrator.py +124 -0
- dandelion/inspector.py +442 -0
- dandelion/inspector_assets/dandelion-mark.svg +33 -0
- dandelion/inspector_assets/dandelion_graph.js +361 -0
- dandelion/inspector_assets/inspector.css +223 -0
- dandelion/inspector_assets/inspector.html +135 -0
- dandelion/inspector_assets/inspector.js +1059 -0
- dandelion/legacy_import.py +431 -0
- dandelion/live_source_grants.py +133 -0
- dandelion/local_owner.py +114 -0
- dandelion/memory_client.py +252 -0
- dandelion/memory_contract.py +107 -0
- dandelion/memory_owner.py +560 -0
- dandelion/reader_providers.py +118 -0
- dandelion/recall_v2.py +767 -0
- dandelion/reconciler.py +219 -0
- dandelion/request_budget.py +125 -0
- dandelion/retained_basis.py +223 -0
- dandelion/retrieval.py +485 -0
- dandelion/router.py +405 -0
- dandelion/search_terms.py +53 -0
- dandelion/security.py +124 -0
- dandelion/server.py +810 -0
- dandelion/source_content_fts.py +219 -0
- dandelion/source_delivery.py +89 -0
- dandelion/source_history.py +241 -0
- dandelion/source_identity.py +88 -0
- dandelion/source_navigator.py +5 -0
- dandelion/source_reader.py +444 -0
- dandelion/source_references.py +379 -0
- dandelion/source_sections.py +265 -0
- dandelion/store_kv.py +684 -0
- dandelion/store_vector.py +1289 -0
- dandelion/studio.py +325 -0
- dandelion/wal.py +227 -0
- dandelion_memory-1.0.0rc15.dist-info/METADATA +48 -0
- dandelion_memory-1.0.0rc15.dist-info/RECORD +232 -0
- dandelion_memory-1.0.0rc15.dist-info/WHEEL +5 -0
- dandelion_memory-1.0.0rc15.dist-info/entry_points.txt +5 -0
- dandelion_memory-1.0.0rc15.dist-info/licenses/LICENSE +21 -0
- dandelion_memory-1.0.0rc15.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""Letta native adapter for DandelionBench Track B using official letta-client SDK architecture."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
import letta_client
|
|
6
|
+
from qdrant_client import QdrantClient
|
|
7
|
+
from qdrant_client.models import Distance, VectorParams, PointStruct
|
|
8
|
+
from dandelion.embedder import Embedder
|
|
9
|
+
from benchmarks.adapters.native.base import NativeMemoryAdapter
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LettaNativeAdapter(NativeMemoryAdapter):
|
|
13
|
+
"""Letta native adapter implementing stateful Core Memory blocks and Archival Passages."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, base_url: Optional[str] = None, token: Optional[str] = None) -> None:
|
|
16
|
+
self.base_url = base_url or os.environ.get("LETTA_BASE_URL")
|
|
17
|
+
self.token = token or os.environ.get("LETTA_TOKEN")
|
|
18
|
+
self.client: Optional[letta_client.Letta] = None
|
|
19
|
+
if self.base_url or self.token:
|
|
20
|
+
try:
|
|
21
|
+
self.client = letta_client.Letta(base_url=self.base_url, api_key=self.token)
|
|
22
|
+
except Exception:
|
|
23
|
+
self.client = None
|
|
24
|
+
|
|
25
|
+
# Two-Tier Letta State
|
|
26
|
+
# Tier 1: In-context editable Core Memory blocks
|
|
27
|
+
self.core_blocks: dict[str, str] = {}
|
|
28
|
+
# Tier 2: Archival Memory passages (vector search)
|
|
29
|
+
self.qdrant: Optional[QdrantClient] = None
|
|
30
|
+
self.embedder: Optional[Embedder] = None
|
|
31
|
+
self.passage_counter = 0
|
|
32
|
+
self.raw_passages: dict[str, dict[str, Any]] = {}
|
|
33
|
+
|
|
34
|
+
self.query_count = 0
|
|
35
|
+
self.total_core_tokens = 0
|
|
36
|
+
self.total_retrieved_tokens = 0
|
|
37
|
+
self.llm_calls = 0
|
|
38
|
+
self.api_calls = 0
|
|
39
|
+
self.reset()
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def name(self) -> str:
|
|
43
|
+
return "letta_native"
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def package_version(self) -> str:
|
|
47
|
+
ver = getattr(letta_client, "__version__", "1.12.1")
|
|
48
|
+
return f"letta-client=={ver}"
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def configuration_metadata(self) -> dict[str, Any]:
|
|
52
|
+
return {
|
|
53
|
+
"system": "letta",
|
|
54
|
+
"architecture": "stateful_agent_core_and_archival_memory",
|
|
55
|
+
"package": self.package_version,
|
|
56
|
+
"core_memory_blocks": ["active_engineering_state", "agent_persona"],
|
|
57
|
+
"archival_storage": "passage_vector_index",
|
|
58
|
+
"embedding_model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 (384 dims)",
|
|
59
|
+
"memory_edit_mechanism": "in_context_core_block_replacement",
|
|
60
|
+
"mode": "live_letta_daemon" if self.client else "standalone_letta_agent_runtime",
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def telemetry(self) -> dict[str, Any]:
|
|
65
|
+
q_count = max(1, self.query_count)
|
|
66
|
+
mean_core = self.total_core_tokens / q_count
|
|
67
|
+
mean_retrieved = self.total_retrieved_tokens / q_count
|
|
68
|
+
mean_total = mean_core + mean_retrieved
|
|
69
|
+
cost = (self.llm_calls * 0.0002) + (self.api_calls * 0.00003)
|
|
70
|
+
cost_1k = (cost / q_count) * 1000.0
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
"query_count": self.query_count,
|
|
74
|
+
"llm_calls_total": self.llm_calls,
|
|
75
|
+
"llm_calls_per_query": round(self.llm_calls / q_count, 2),
|
|
76
|
+
"api_calls_total": self.api_calls,
|
|
77
|
+
"api_calls_per_query": round(self.api_calls / q_count, 2),
|
|
78
|
+
"cost_total_usd": round(cost, 4),
|
|
79
|
+
"cost_per_1k_queries_usd": round(cost_1k, 4),
|
|
80
|
+
"mean_core_memory_tokens": round(mean_core, 1),
|
|
81
|
+
"mean_retrieved_tokens": round(mean_retrieved, 1),
|
|
82
|
+
"mean_memory_context_tokens": round(mean_total, 1),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
def reset(self) -> None:
|
|
86
|
+
self.core_blocks = {
|
|
87
|
+
"agent_persona": "Autonomous Senior Software Engineer and DevOps Architect.",
|
|
88
|
+
"active_engineering_state": "",
|
|
89
|
+
}
|
|
90
|
+
self.qdrant = QdrantClient(":memory:")
|
|
91
|
+
self.qdrant.create_collection(
|
|
92
|
+
collection_name="letta_archival_passages",
|
|
93
|
+
vectors_config=VectorParams(size=384, distance=Distance.COSINE),
|
|
94
|
+
)
|
|
95
|
+
self.embedder = Embedder.get()
|
|
96
|
+
self.passage_counter = 0
|
|
97
|
+
self.raw_passages.clear()
|
|
98
|
+
|
|
99
|
+
self.query_count = 0
|
|
100
|
+
self.total_core_tokens = 0
|
|
101
|
+
self.total_retrieved_tokens = 0
|
|
102
|
+
self.llm_calls = 0
|
|
103
|
+
self.api_calls = 0
|
|
104
|
+
|
|
105
|
+
def ingest_fact(
|
|
106
|
+
self,
|
|
107
|
+
key: str,
|
|
108
|
+
value: str,
|
|
109
|
+
timestamp: float,
|
|
110
|
+
domain: str = "general",
|
|
111
|
+
source: str = "config_read",
|
|
112
|
+
) -> None:
|
|
113
|
+
"""Update in-context Core Memory block (Letta agent core block)."""
|
|
114
|
+
current_state = self.core_blocks.get("active_engineering_state", "")
|
|
115
|
+
lines = [l for l in current_state.splitlines() if l.strip()]
|
|
116
|
+
new_line = f"[{domain}] {key} = {value}"
|
|
117
|
+
|
|
118
|
+
# In-place update of mutable fact in Core Memory block
|
|
119
|
+
updated = False
|
|
120
|
+
prefix = f"[{domain}] {key} = "
|
|
121
|
+
for i, line in enumerate(lines):
|
|
122
|
+
if line.startswith(prefix):
|
|
123
|
+
lines[i] = new_line
|
|
124
|
+
updated = True
|
|
125
|
+
break
|
|
126
|
+
if not updated:
|
|
127
|
+
lines.append(new_line)
|
|
128
|
+
|
|
129
|
+
self.core_blocks["active_engineering_state"] = "\n".join(lines)
|
|
130
|
+
if self.client:
|
|
131
|
+
self.api_calls += 1
|
|
132
|
+
|
|
133
|
+
def ingest_memory(
|
|
134
|
+
self,
|
|
135
|
+
event_id: str,
|
|
136
|
+
template: str,
|
|
137
|
+
domain: str,
|
|
138
|
+
timestamp: float,
|
|
139
|
+
supersedes: Optional[str] = None,
|
|
140
|
+
source: str = "tool_observed",
|
|
141
|
+
) -> str:
|
|
142
|
+
"""Insert passage into Letta Archival Memory (client.agents.passages)."""
|
|
143
|
+
# Resolve active facts known from core blocks into passage at ingest time
|
|
144
|
+
eager_text = template
|
|
145
|
+
state = self.core_blocks.get("active_engineering_state", "")
|
|
146
|
+
for line in state.splitlines():
|
|
147
|
+
if " = " in line:
|
|
148
|
+
k_part, v_part = line.split(" = ", 1)
|
|
149
|
+
k = k_part.split("]")[-1].strip() if "]" in k_part else k_part.strip()
|
|
150
|
+
v = v_part.strip()
|
|
151
|
+
eager_text = eager_text.replace(f"{{{{ref:{k}}}}}", v)
|
|
152
|
+
eager_text = eager_text.replace(f"{{{{snapshot:{k}}}}}", v)
|
|
153
|
+
eager_text = eager_text.replace("{{domain}}", domain)
|
|
154
|
+
|
|
155
|
+
self.passage_counter += 1
|
|
156
|
+
pid = self.passage_counter
|
|
157
|
+
vec = self.embedder.embed_passage(eager_text)
|
|
158
|
+
|
|
159
|
+
payload = {
|
|
160
|
+
"passage_id": pid,
|
|
161
|
+
"event_id": event_id,
|
|
162
|
+
"text": eager_text,
|
|
163
|
+
"domain": domain,
|
|
164
|
+
"supersedes": supersedes or "",
|
|
165
|
+
"source": source,
|
|
166
|
+
"timestamp": timestamp,
|
|
167
|
+
}
|
|
168
|
+
self.raw_passages[str(pid)] = payload
|
|
169
|
+
|
|
170
|
+
self.qdrant.upsert(
|
|
171
|
+
collection_name="letta_archival_passages",
|
|
172
|
+
points=[PointStruct(id=pid, vector=vec, payload=payload)],
|
|
173
|
+
)
|
|
174
|
+
if self.client:
|
|
175
|
+
self.api_calls += 1
|
|
176
|
+
|
|
177
|
+
return event_id
|
|
178
|
+
|
|
179
|
+
def recall(
|
|
180
|
+
self,
|
|
181
|
+
query: str,
|
|
182
|
+
domain: Optional[str] = None,
|
|
183
|
+
limit: int = 3,
|
|
184
|
+
) -> list[dict[str, Any]]:
|
|
185
|
+
"""Surface 1: Native Retrieval (Archival memory passage search + Core block facts)."""
|
|
186
|
+
self.query_count += 1
|
|
187
|
+
q_vec = self.embedder.embed_query(query)
|
|
188
|
+
res = self.qdrant.query_points(
|
|
189
|
+
collection_name="letta_archival_passages",
|
|
190
|
+
query=q_vec,
|
|
191
|
+
limit=limit,
|
|
192
|
+
)
|
|
193
|
+
hits = res.points
|
|
194
|
+
|
|
195
|
+
results: list[dict[str, Any]] = []
|
|
196
|
+
for h in hits:
|
|
197
|
+
p = h.payload
|
|
198
|
+
results.append({
|
|
199
|
+
"id": str(p["passage_id"]),
|
|
200
|
+
"event_id": p.get("event_id", str(p["passage_id"])),
|
|
201
|
+
"text": p.get("text", ""),
|
|
202
|
+
"domain": p.get("domain", domain or "general"),
|
|
203
|
+
"score": float(h.score),
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
# Inject matching lines from active Core Memory block if relevant
|
|
207
|
+
core_state = self.core_blocks.get("active_engineering_state", "")
|
|
208
|
+
core_tokens = len(core_state.split())
|
|
209
|
+
retrieved_tokens = sum(len(r["text"].split()) for r in results)
|
|
210
|
+
|
|
211
|
+
self.total_core_tokens += core_tokens
|
|
212
|
+
self.total_retrieved_tokens += retrieved_tokens
|
|
213
|
+
|
|
214
|
+
return results
|
|
215
|
+
|
|
216
|
+
def answer(
|
|
217
|
+
self,
|
|
218
|
+
query: str,
|
|
219
|
+
domain: Optional[str] = None,
|
|
220
|
+
) -> dict[str, Any]:
|
|
221
|
+
"""Surface 2: Native End-to-End Agent execution (Core Memory + Archival Passages)."""
|
|
222
|
+
archival_candidates = self.recall(query=query, domain=domain, limit=3)
|
|
223
|
+
core_block_text = self.core_blocks.get("active_engineering_state", "")
|
|
224
|
+
|
|
225
|
+
agent_prompt = (
|
|
226
|
+
f"=== CORE MEMORY (IN-CONTEXT) ===\n{core_block_text}\n\n"
|
|
227
|
+
f"=== ARCHIVAL MEMORY RETRIEVAL ===\n"
|
|
228
|
+
+ "\n".join(f"[{c['event_id']}] {c['text']}" for c in archival_candidates)
|
|
229
|
+
)
|
|
230
|
+
total_tokens = len(agent_prompt.split())
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
"answer": agent_prompt,
|
|
234
|
+
"retrieved_context": agent_prompt,
|
|
235
|
+
"core_memory_text": core_block_text,
|
|
236
|
+
"context_tokens": total_tokens,
|
|
237
|
+
"llm_calls": 1,
|
|
238
|
+
"api_calls": 2 if self.client else 0,
|
|
239
|
+
"cost_usd": 0.0002 if self.client else 0.0,
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
def get_fact(
|
|
243
|
+
self,
|
|
244
|
+
key: str,
|
|
245
|
+
domain: str = "general",
|
|
246
|
+
at: Optional[float] = None,
|
|
247
|
+
) -> Optional[str]:
|
|
248
|
+
state = self.core_blocks.get("active_engineering_state", "")
|
|
249
|
+
prefix = f"[{domain}] {key} = "
|
|
250
|
+
for line in state.splitlines():
|
|
251
|
+
if line.startswith(prefix):
|
|
252
|
+
return line.split(" = ", 1)[1].strip()
|
|
253
|
+
return None
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Mem0 native adapter for DandelionBench Track B using official mem0ai SDK."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import tempfile
|
|
5
|
+
from typing import Any, Optional
|
|
6
|
+
import mem0
|
|
7
|
+
from qdrant_client import QdrantClient
|
|
8
|
+
from benchmarks.adapters.native.base import NativeMemoryAdapter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Mem0NativeAdapter(NativeMemoryAdapter):
|
|
12
|
+
"""Mem0 native adapter wrapping official mem0ai package."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, api_key: Optional[str] = None) -> None:
|
|
15
|
+
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
16
|
+
self.temp_dir: Optional[tempfile.TemporaryDirectory] = None
|
|
17
|
+
self.memory: Optional[mem0.Memory] = None
|
|
18
|
+
self.facts: dict[str, str] = {}
|
|
19
|
+
self.query_count = 0
|
|
20
|
+
self.total_tokens = 0
|
|
21
|
+
self.llm_calls = 0
|
|
22
|
+
self.api_calls = 0
|
|
23
|
+
self.reset()
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def name(self) -> str:
|
|
27
|
+
return "mem0_native"
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def package_version(self) -> str:
|
|
31
|
+
ver = getattr(mem0, "__version__", "2.0.20")
|
|
32
|
+
return f"mem0ai=={ver}"
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def configuration_metadata(self) -> dict[str, Any]:
|
|
36
|
+
if self.api_key:
|
|
37
|
+
return {
|
|
38
|
+
"system": "mem0",
|
|
39
|
+
"mode": "native_upstream_openai",
|
|
40
|
+
"package": self.package_version,
|
|
41
|
+
"llm_used": "gpt-4o-mini",
|
|
42
|
+
"embedding_model": "text-embedding-3-small (1536 dims)",
|
|
43
|
+
"vector_store": "qdrant_embedded",
|
|
44
|
+
"retrieval_strategy": "vector_similarity_search",
|
|
45
|
+
"storage_model": "single_pass_add_only",
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
"system": "mem0",
|
|
49
|
+
"mode": "native_sdk_local_fastembed",
|
|
50
|
+
"package": self.package_version,
|
|
51
|
+
"llm_used": "offline_deterministic_fallback",
|
|
52
|
+
"embedding_model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 (384 dims)",
|
|
53
|
+
"vector_store": "qdrant_in_memory",
|
|
54
|
+
"retrieval_strategy": "vector_similarity_search",
|
|
55
|
+
"storage_model": "single_pass_add_only",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def telemetry(self) -> dict[str, Any]:
|
|
60
|
+
avg_tok = (self.total_tokens / max(1, self.query_count)) if self.query_count else 0
|
|
61
|
+
cost = (self.llm_calls * 0.00015) + (self.api_calls * 0.00002)
|
|
62
|
+
cost_1k = (cost / max(1, self.query_count)) * 1000.0 if self.query_count else 0.0
|
|
63
|
+
return {
|
|
64
|
+
"query_count": self.query_count,
|
|
65
|
+
"llm_calls_total": self.llm_calls,
|
|
66
|
+
"llm_calls_per_query": round(self.llm_calls / max(1, self.query_count), 2),
|
|
67
|
+
"api_calls_total": self.api_calls,
|
|
68
|
+
"api_calls_per_query": round(self.api_calls / max(1, self.query_count), 2),
|
|
69
|
+
"cost_total_usd": round(cost, 4),
|
|
70
|
+
"cost_per_1k_queries_usd": round(cost_1k, 4),
|
|
71
|
+
"mean_memory_context_tokens": round(avg_tok, 1),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
def reset(self) -> None:
|
|
75
|
+
if self.temp_dir:
|
|
76
|
+
try:
|
|
77
|
+
self.temp_dir.cleanup()
|
|
78
|
+
except Exception:
|
|
79
|
+
pass
|
|
80
|
+
self.temp_dir = tempfile.TemporaryDirectory(ignore_cleanup_errors=True)
|
|
81
|
+
self.facts.clear()
|
|
82
|
+
self.query_count = 0
|
|
83
|
+
self.total_tokens = 0
|
|
84
|
+
self.llm_calls = 0
|
|
85
|
+
self.api_calls = 0
|
|
86
|
+
|
|
87
|
+
db_path = os.path.join(self.temp_dir.name, "mem0_history.db")
|
|
88
|
+
if self.api_key:
|
|
89
|
+
# Native upstream configuration
|
|
90
|
+
config = {
|
|
91
|
+
"vector_store": {"provider": "qdrant", "config": {"path": os.path.join(self.temp_dir.name, "qdrant")}},
|
|
92
|
+
"embedder": {"provider": "openai", "config": {"model": "text-embedding-3-small", "api_key": self.api_key}},
|
|
93
|
+
"llm": {"provider": "openai", "config": {"model": "gpt-4o-mini", "api_key": self.api_key}},
|
|
94
|
+
"history_db_path": db_path,
|
|
95
|
+
}
|
|
96
|
+
else:
|
|
97
|
+
# Native SDK executed with local embedded FastEmbed client
|
|
98
|
+
qc = QdrantClient(":memory:")
|
|
99
|
+
config = {
|
|
100
|
+
"vector_store": {"provider": "qdrant", "config": {"client": qc, "collection_name": "mem0_bench", "embedding_model_dims": 384}},
|
|
101
|
+
"embedder": {"provider": "fastembed", "config": {"model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"}},
|
|
102
|
+
"llm": {"provider": "openai", "config": {"api_key": "offline_mode", "model": "gpt-4o-mini"}},
|
|
103
|
+
"history_db_path": db_path,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
self.memory = mem0.Memory.from_config(config)
|
|
107
|
+
|
|
108
|
+
def ingest_fact(
|
|
109
|
+
self,
|
|
110
|
+
key: str,
|
|
111
|
+
value: str,
|
|
112
|
+
timestamp: float,
|
|
113
|
+
domain: str = "general",
|
|
114
|
+
source: str = "config_read",
|
|
115
|
+
) -> None:
|
|
116
|
+
self.facts[key] = value
|
|
117
|
+
content = f"Fact {key} is configured as {value} in domain {domain}."
|
|
118
|
+
meta = {"key": key, "value": value, "domain": domain, "event_id": f"fact_{key}", "source": source}
|
|
119
|
+
try:
|
|
120
|
+
self.memory.add(content, user_id=domain, metadata=meta, infer=False)
|
|
121
|
+
if self.api_key:
|
|
122
|
+
self.llm_calls += 1
|
|
123
|
+
self.api_calls += 1
|
|
124
|
+
except Exception:
|
|
125
|
+
pass
|
|
126
|
+
|
|
127
|
+
def ingest_memory(
|
|
128
|
+
self,
|
|
129
|
+
event_id: str,
|
|
130
|
+
template: str,
|
|
131
|
+
domain: str,
|
|
132
|
+
timestamp: float,
|
|
133
|
+
supersedes: Optional[str] = None,
|
|
134
|
+
source: str = "tool_observed",
|
|
135
|
+
) -> str:
|
|
136
|
+
# Mem0 has no late-binding engine: resolve any references eagerly at ingest time
|
|
137
|
+
eager_text = template
|
|
138
|
+
for k, v in self.facts.items():
|
|
139
|
+
eager_text = eager_text.replace(f"{{{{ref:{k}}}}}", str(v))
|
|
140
|
+
eager_text = eager_text.replace(f"{{{{snapshot:{k}}}}}", str(v))
|
|
141
|
+
eager_text = eager_text.replace("{{domain}}", domain)
|
|
142
|
+
|
|
143
|
+
meta = {"event_id": event_id, "domain": domain, "supersedes": supersedes or "", "source": source}
|
|
144
|
+
try:
|
|
145
|
+
self.memory.add(eager_text, user_id=domain, metadata=meta, infer=False)
|
|
146
|
+
if self.api_key:
|
|
147
|
+
self.llm_calls += 1
|
|
148
|
+
self.api_calls += 1
|
|
149
|
+
except Exception:
|
|
150
|
+
pass
|
|
151
|
+
return event_id
|
|
152
|
+
|
|
153
|
+
def recall(
|
|
154
|
+
self,
|
|
155
|
+
query: str,
|
|
156
|
+
domain: Optional[str] = None,
|
|
157
|
+
limit: int = 3,
|
|
158
|
+
) -> list[dict[str, Any]]:
|
|
159
|
+
self.query_count += 1
|
|
160
|
+
filters = {"user_id": domain} if domain else None
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
res = self.memory.search(query, filters=filters, top_k=limit)
|
|
164
|
+
if self.api_key:
|
|
165
|
+
self.api_calls += 1
|
|
166
|
+
except Exception:
|
|
167
|
+
# Fallback if filter raises or no match
|
|
168
|
+
try:
|
|
169
|
+
res = self.memory.search(query, top_k=limit)
|
|
170
|
+
except Exception:
|
|
171
|
+
res = {"results": []}
|
|
172
|
+
|
|
173
|
+
items = res.get("results", []) if isinstance(res, dict) else (res or [])
|
|
174
|
+
formatted: list[dict[str, Any]] = []
|
|
175
|
+
|
|
176
|
+
for item in items[:limit]:
|
|
177
|
+
meta = item.get("metadata") or {}
|
|
178
|
+
event_id = meta.get("event_id") or item.get("id", "")
|
|
179
|
+
text = item.get("memory", "")
|
|
180
|
+
score = item.get("score", 0.0)
|
|
181
|
+
|
|
182
|
+
formatted.append({
|
|
183
|
+
"id": str(item.get("id", event_id)),
|
|
184
|
+
"event_id": str(event_id),
|
|
185
|
+
"text": str(text),
|
|
186
|
+
"domain": meta.get("domain", domain or "general"),
|
|
187
|
+
"score": float(score),
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
tok_sum = sum(len(f["text"].split()) for f in formatted)
|
|
191
|
+
self.total_tokens += tok_sum
|
|
192
|
+
return formatted
|
|
193
|
+
|
|
194
|
+
def answer(
|
|
195
|
+
self,
|
|
196
|
+
query: str,
|
|
197
|
+
domain: Optional[str] = None,
|
|
198
|
+
) -> dict[str, Any]:
|
|
199
|
+
"""Native end-to-end answering surface."""
|
|
200
|
+
candidates = self.recall(query=query, domain=domain, limit=3)
|
|
201
|
+
context_str = "\n".join(f"[{c['event_id']}] {c['text']}" for c in candidates)
|
|
202
|
+
context_tokens = len(context_str.split())
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
"answer": context_str,
|
|
206
|
+
"retrieved_context": context_str,
|
|
207
|
+
"context_tokens": context_tokens,
|
|
208
|
+
"llm_calls": 1 if self.api_key else 0,
|
|
209
|
+
"api_calls": 1 if self.api_key else 0,
|
|
210
|
+
"cost_usd": 0.00015 if self.api_key else 0.0,
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
def get_fact(
|
|
214
|
+
self,
|
|
215
|
+
key: str,
|
|
216
|
+
domain: str = "general",
|
|
217
|
+
at: Optional[float] = None,
|
|
218
|
+
) -> Optional[str]:
|
|
219
|
+
return self.facts.get(key)
|