fleet-memory/hindsight-docs/examples/api/memory-banks.py
Nicolò Boschi 90e370ef35
fix: misc fixes for observations and mental models (#209)
* fix: misc fixes for observations and mental models

* feat: improve graph retrieval for observations

- Update LinkExpansionRetriever to traverse through source_memory_ids
  for observation entity connections (avoiding data duplication)
- Remove entity link copy from world facts to observations in consolidator
- Add tests for link expansion graph retrieval
- Add directives_applied field to ReflectResult
- Include user's other changes (CLI, docs, client updates)

* fix: CI test failures

- Add mental_model_id parameter to create_mental_model function
- Fix ToolCallTrace not including reason field from ToolCall
- Improve test_link_expansion_observation_graph_retrieval to wait for consolidation with retry

* chore: reduce link expansion log verbosity

* Revert "chore: reduce link expansion log verbosity"

This reverts commit 3ce759391cead1012157785fa78fef16ef9bfe3b.

* feat: add semantic/temporal/entity links as fallback in graph retrieval

- Add fallback query for semantic, temporal, and entity links from memory_links
- Check both directions (outgoing and incoming links)
- Weight fallback results at 0.5x to prioritize entity links via unit_entities
- Fixes graph retrieval returning 0 when data has cross-cluster temporal connections

* fix: enable observations fixture for link expansion test

- Add enable_observations fixture to ensure observations are created
- Increase wait time from 10 to 30 seconds for CI reliability
2026-01-27 15:37:57 +01:00

77 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Memory Banks API examples for Hindsight.
Run: python examples/api/memory-banks.py
"""
import os
import requests
HINDSIGHT_URL = os.getenv("HINDSIGHT_API_URL", "http://localhost:8888")
# =============================================================================
# Setup (not shown in docs)
# =============================================================================
from hindsight_client import Hindsight
client = Hindsight(base_url=HINDSIGHT_URL)
# =============================================================================
# Doc Examples
# =============================================================================
# [docs:create-bank]
client.create_bank(
bank_id="my-bank",
name="Research Assistant",
mission="You're a research assistant specializing in machine learning - keep track of papers, methods, and findings.",
disposition={
"skepticism": 4,
"literalism": 3,
"empathy": 3
}
)
# [/docs:create-bank]
# [docs:bank-mission]
client.create_bank(
bank_id="financial-advisor",
name="Financial Advisor",
mission="""You're a conservative financial advisor - keep track of client risk tolerance,
investment preferences, and market conditions. Prioritize capital preservation over growth."""
)
# [/docs:bank-mission]
# [docs:bank-with-disposition]
client.create_bank(
bank_id="architect-bank",
mission="You're a senior software architect - keep track of system designs, "
"technology decisions, and architectural patterns. Prefer simplicity over cutting-edge.",
disposition={
"skepticism": 4, # Questions new technologies
"literalism": 4, # Focuses on concrete specs
"empathy": 2 # Prioritizes technical facts
}
)
# [/docs:bank-with-disposition]
# [docs:bank-support-agent]
client.create_bank(
bank_id="support-agent",
mission="You're a customer support agent - keep track of "
"customer preferences, past issues, and communication styles."
)
# [/docs:bank-support-agent]
# =============================================================================
# Cleanup (not shown in docs)
# =============================================================================
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/my-bank")
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/financial-advisor")
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/architect-bank")
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/support-agent")
print("memory-banks.py: All examples passed")