-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: adds cypher search to retrievers module #648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hajdul88
merged 6 commits into
dev
from
feature/cog-1469-create-a-cypher-query-search-type
Mar 19, 2025
+76
−0
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
756c27b
feat: adds cypher search to retrievers module
hajdul88 628aedb
Merge branch 'dev' into feature/cog-1469-create-a-cypher-query-search…
hajdul88 c54029e
Merge branch 'dev' into feature/cog-1469-create-a-cypher-query-search…
hajdul88 9547cbf
Merge branch 'dev' into feature/cog-1469-create-a-cypher-query-search…
hajdul88 f85a30a
chore: fixes PR comments
hajdul88 d81c1b5
chore: changes Cypher_search to cypher
hajdul88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from typing import Any, Optional | ||
| import logging | ||
| from cognee.infrastructure.databases.graph import get_graph_engine | ||
| from cognee.infrastructure.databases.graph.networkx.adapter import NetworkXAdapter | ||
| from cognee.modules.retrieval.base_retriever import BaseRetriever | ||
| from cognee.modules.retrieval.utils.completion import generate_completion | ||
| from cognee.modules.retrieval.exceptions import SearchTypeNotSupported, CypherSearchError | ||
|
|
||
| logger = logging.getLogger("CypherSearchRetriever") | ||
|
|
||
|
|
||
| class CypherSearchRetriever(BaseRetriever): | ||
| """Retriever for handling cypher-based search""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| user_prompt_path: str = "context_for_question.txt", | ||
| system_prompt_path: str = "answer_simple_question.txt", | ||
| ): | ||
| """Initialize retriever with optional custom prompt paths.""" | ||
| self.user_prompt_path = user_prompt_path | ||
| self.system_prompt_path = system_prompt_path | ||
|
|
||
| async def get_context(self, query: str) -> Any: | ||
| """Retrieves relevant context using a cypher query.""" | ||
| try: | ||
| graph_engine = await get_graph_engine() | ||
|
|
||
| if isinstance(graph_engine, NetworkXAdapter): | ||
| raise SearchTypeNotSupported( | ||
| "CYPHER search type not supported for NetworkXAdapter." | ||
| ) | ||
|
|
||
| result = await graph_engine.query(query) | ||
| except Exception as e: | ||
| logger.error("Failed to execture cypher search retrieval: %s", str(e)) | ||
| raise CypherSearchError() from e | ||
| return result | ||
|
|
||
| async def get_completion(self, query: str, context: Optional[Any] = None) -> Any: | ||
| """Returns the graph connections context.""" | ||
| if context is None: | ||
| context = await self.get_context(query) | ||
| return context | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """ | ||
| Custom exceptions for the Cognee API. | ||
|
|
||
| This module defines a set of exceptions for handling various data errors | ||
| """ | ||
|
|
||
| from .exceptions import SearchTypeNotSupported, CypherSearchError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from cognee.exceptions import CogneeApiError | ||
| from fastapi import status | ||
|
|
||
|
|
||
| class SearchTypeNotSupported(CogneeApiError): | ||
| def __init__( | ||
| self, | ||
| message: str = "CYPHER search type not supported by the adapter.", | ||
| name: str = "SearchTypeNotSupported", | ||
| status_code: int = status.HTTP_400_BAD_REQUEST, | ||
| ): | ||
| super().__init__(message, name, status_code) | ||
|
|
||
|
|
||
| class CypherSearchError(CogneeApiError): | ||
| def __init__( | ||
| self, | ||
| message: str = "An error occurred during the execution of the Cypher query.", | ||
| name: str = "CypherSearchError", | ||
| status_code: int = status.HTTP_400_BAD_REQUEST, | ||
| ): | ||
| super().__init__(message, name, status_code) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.