Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

58 lines
1.9 KiB
Python

"""Callable memory search: lets the agent run a fresh triplet lookup mid-loop."""
from typing import Any, Dict
from cognee.modules.engine.models import Tool
from cognee.modules.graph.utils import resolve_edges_to_text
from cognee.modules.retrieval.utils.brute_force_triplet_search import (
brute_force_triplet_search,
)
from cognee.modules.tools.errors import ToolInvocationError
from cognee.modules.tools.registry import register_builtin_tool
TOOL = Tool(
name="memory_search",
description=(
"Run a fresh semantic search over the knowledge graph and return the "
"relevant triplets as text. Use when the initial context is insufficient "
"or you need to pivot to a different topic."
),
input_schema={
"type": "object",
"properties": {
"query": {"type": "string", "description": "The search query."},
"top_k": {
"type": "integer",
"description": "Number of triplets to return (default 5).",
"default": 5,
},
},
"required": ["query"],
},
handler_ref="cognee.modules.tools.builtin.memory_search.handler",
permission_required="read",
readonly_hint=True,
)
async def handler(args: Dict[str, Any], **_) -> str:
query = args.get("query")
if not query:
raise ToolInvocationError("memory_search requires a 'query' argument")
raw_top_k = args.get("top_k", 5)
try:
top_k = int(raw_top_k)
except (TypeError, ValueError) as exc:
raise ToolInvocationError("memory_search 'top_k' must be an integer") from exc
if top_k <= 0:
raise ToolInvocationError("memory_search 'top_k' must be greater than 0")
triplets = await brute_force_triplet_search(query, top_k=top_k)
if not triplets:
return "(no results)"
return await resolve_edges_to_text(triplets)
register_builtin_tool(TOOL)