Knowledge
The KnowledgeResource handles querying, ingestion, relationship listing, and graph visualization.
Access
user = client.for_user("user-123")
knowledge = user.knowledgeMethods
query(query, **kwargs)
Query the knowledge graph with semantic search, optional reranking, and graph reasoning.
results = knowledge.query("What does the user prefer?")
print(results.context) # Assembled context for LLM prompts
for r in results:
print(f"{r.content} (score: {r.relevance_score})")| Parameter | Type | Default | Description |
|---|---|---|---|
query | str | — | Natural language search query |
top_k | int | 10 | Maximum number of results |
use_cache | bool | True | Enable semantic cache |
use_rerank | bool | True | Enable cross-encoder reranking |
use_emotional_ranking | bool | True | Apply emotional scoring |
use_graph_reasoning | bool | False | Enable multi-hop graph traversal |
graph_max_hops | int | 3 | Maximum traversal depth |
graph_beam_width | int | 5 | Beam search width for graph traversal |
include_community_context | bool | False | Enrich results with community data |
filter_by_community_id | str | None | None | Filter by specific community |
complexity_override | str | None | None | Override complexity: "simple", "medium", or "complex" |
Returns: QueryResponse with fields:
results— List ofQueryResultobjectscontext— Assembled context string for LLM promptsquery_complexity— Detected complexity ("simple","medium","complex")cache_hit— Whether the result was served from cacheresult_count— Total number of resultsreasoning_chains— Multi-hop reasoning paths (when graph reasoning enabled)citations— Source entity citationstiming—TimingBreakdownwith per-stage latencies
ingest(documents)
Ingest content into the knowledge graph. Entity and relationship extraction is automatic.
# Single string
knowledge.ingest("Alice is a software engineer at Acme Corp")
# Multiple documents
knowledge.ingest([
"User prefers dark mode",
"Meeting scheduled for Friday",
])
# Document objects with metadata
from memorer import Document
knowledge.ingest(Document(
content="Quarterly report...",
metadata={"source": "report"},
))| Parameter | Type | Description |
|---|---|---|
documents | str | dict | Document | list | Content to ingest. Accepts a string, dict with content key, Document object, or a list of any of these |
Returns: IngestResponse with fields entities_created, relationships_created, episodes_created, processing_time_ms, status
relationships(limit, offset)
List relationships in the knowledge graph.
rels = knowledge.relationships(limit=50)
for rel in rels.relationships:
print(f"{rel.source_id} --{rel.relationship_type}--> {rel.target_id}")| Parameter | Type | Default | Description |
|---|---|---|---|
limit | int | 100 | Maximum number to return |
offset | int | 0 | Pagination offset |
Returns: RelationshipList with a relationships list of Relationship objects
graph(limit, include_embeddings)
Get full graph data for visualization. Compatible with D3.js, vis.js, Cytoscape, etc.
viz = knowledge.graph(limit=200)
print(f"Nodes: {len(viz.nodes)}")
print(f"Edges: {len(viz.edges)}")
print(f"Communities: {len(viz.communities)}")
if viz.stats:
print(f"Total entities: {viz.stats.total_entities}")| Parameter | Type | Default | Description |
|---|---|---|---|
limit | int | 500 | Maximum number of nodes |
include_embeddings | bool | False | Include embedding vectors |
Returns: GraphVisualization with fields:
nodes— List ofGraphNode(id, label, type, importance, community_id, metadata)edges— List ofGraphEdge(source, target, relationship, weight)communities— List ofGraphCommunity(id, label, summary, entity_count, entity_ids)stats— OptionalKnowledgeStats
Last updated on