-
Notifications
You must be signed in to change notification settings - Fork 999
feat: Text to Cypher Retriever #646
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
Conversation
WalkthroughThis pull request integrates a new Cypher query search option into the application. The frontend SearchView component now offers a "Cypher prompting" choice. On the backend, a new Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant SV as SearchView
participant SM as Search Module
participant CR as CypherQueryRetriever
participant GE as Graph Engine
U->>SV: Selects "Cypher prompting" option
SV->>SM: Initiates specific_search(query, type: CYPHER_QUERY)
SM->>CR: Calls get_completion(query)
alt No context available
CR->>CR: Invoke get_context(query)
CR->>GE: Retrieve graph schema & execute Cypher query
end
CR-->>SM: Return query context/result
SM-->>SV: Return search result
SV-->>U: Display result
Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
️✅ There are no secrets present in this pull request anymore.If these secrets were true positive and are still valid, we highly recommend you to revoke them. 🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
cognee-frontend/src/ui/Partials/SearchView/SearchView.tsx(1 hunks)cognee/modules/retrieval/cypher_query_retriever.py(1 hunks)cognee/modules/search/methods/search.py(2 hunks)cognee/modules/search/types/SearchType.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
cognee/modules/retrieval/cypher_query_retriever.py
36-43: f-string without any placeholders
Remove extraneous f prefix
(F541)
🪛 GitHub Actions: lint | ruff lint
cognee/modules/retrieval/cypher_query_retriever.py
[error] 36-36: Ruff: F541 - f-string without any placeholders. Remove extraneous f prefix.
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: windows-latest
- GitHub Check: run_simple_example_test
🔇 Additional comments (7)
cognee/modules/search/types/SearchType.py (1)
12-12: Add new enum entry for CYPHER_QUERYThis addition nicely expands the search functionality.
cognee-frontend/src/ui/Partials/SearchView/SearchView.tsx (1)
39-41: New 'Cypher prompting' search optionGreat job adding this to the dropdown list, ensuring consistency with the existing search types.
cognee/modules/search/methods/search.py (2)
15-15: Import for CypherQueryRetrieverImporting the new retriever aligns with the existing retrieval modules and is clearly placed.
69-69: Extended the search_tasks mappingProperly incorporates
SearchType.CYPHER_QUERYwith the new retriever. This addition looks correct.cognee/modules/retrieval/cypher_query_retriever.py (3)
1-16: Class definition and constructorCreating a maximum attempt count and initializing the LLM client in the constructor is well-structured.
17-32: Gathering node and edge schemasFetching the graph schema before generating queries ensures a context-aware approach. Nicely done.
73-78: get_completion methodDelegating context retrieval to
get_contextif not already provided is straightforward and aligns well with the retriever pattern.
| previous_attempt_context = None | ||
| for attempt in range(self.max_attempts): | ||
| llm_client = get_llm_client() | ||
| system_prompt = f""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extraneous f-string prefix
No placeholders are used in this multiline string. Removing the f prefix resolves the lint error.
- system_prompt = f"""
+ system_prompt = """📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| system_prompt = f""" | |
| system_prompt = """ |
🧰 Tools
🪛 Ruff (0.8.2)
36-43: f-string without any placeholders
Remove extraneous f prefix
(F541)
🪛 GitHub Actions: lint | ruff lint
[error] 36-36: Ruff: F541 - f-string without any placeholders. Remove extraneous f prefix.
| previous_attempt_context = None | ||
| for attempt in range(self.max_attempts): | ||
| llm_client = get_llm_client() | ||
| system_prompt = f""" | ||
| You are an advanced Cypher query generator for Neo4j. Your task: | ||
| - Based on the following graph schema (nodes and edges) and a natural language query, generate a valid Cypher query that retrieves the requested information. | ||
| - Provide only the Cypher query, no explanations or additional formatting. | ||
| - Take into account the previous failed attempts and adjust the query accordingly. | ||
|
|
||
| Return only a valid, optimized Cypher query. | ||
| """ | ||
|
|
||
| cypher_query = await llm_client.acreate_structured_output( | ||
| text_input=f""" | ||
| User query: | ||
| {query} | ||
|
|
||
| Node schema (labels and properties): | ||
| {node_schemas} | ||
|
|
||
| Edge schema (relationship properties): | ||
| {edge_schemas} | ||
|
|
||
| Previous attempts: | ||
| {previous_attempt_context} | ||
| """, | ||
| system_prompt=system_prompt, | ||
| response_model=str, | ||
| ) | ||
| try: | ||
| context = await graph_engine.query(cypher_query) | ||
| if context: | ||
| return context | ||
| else: | ||
| previous_attempt_context += f"Query: {cypher_query} -> Result: None\n" | ||
| continue | ||
| except Exception as e: | ||
| previous_attempt_context += f"Query: {cypher_query} -> Executed with error: {e}\n" | ||
| return None | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple attempts loop
previous_attempt_context is initialized to None, but string concatenation on the first attempt can cause an error. Please initialize it as an empty string or handle it conditionally.
- previous_attempt_context = None
+ previous_attempt_context = ""📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| previous_attempt_context = None | |
| for attempt in range(self.max_attempts): | |
| llm_client = get_llm_client() | |
| system_prompt = f""" | |
| You are an advanced Cypher query generator for Neo4j. Your task: | |
| - Based on the following graph schema (nodes and edges) and a natural language query, generate a valid Cypher query that retrieves the requested information. | |
| - Provide only the Cypher query, no explanations or additional formatting. | |
| - Take into account the previous failed attempts and adjust the query accordingly. | |
| Return only a valid, optimized Cypher query. | |
| """ | |
| cypher_query = await llm_client.acreate_structured_output( | |
| text_input=f""" | |
| User query: | |
| {query} | |
| Node schema (labels and properties): | |
| {node_schemas} | |
| Edge schema (relationship properties): | |
| {edge_schemas} | |
| Previous attempts: | |
| {previous_attempt_context} | |
| """, | |
| system_prompt=system_prompt, | |
| response_model=str, | |
| ) | |
| try: | |
| context = await graph_engine.query(cypher_query) | |
| if context: | |
| return context | |
| else: | |
| previous_attempt_context += f"Query: {cypher_query} -> Result: None\n" | |
| continue | |
| except Exception as e: | |
| previous_attempt_context += f"Query: {cypher_query} -> Executed with error: {e}\n" | |
| return None | |
| previous_attempt_context = "" | |
| for attempt in range(self.max_attempts): | |
| llm_client = get_llm_client() | |
| system_prompt = f""" | |
| You are an advanced Cypher query generator for Neo4j. Your task: | |
| - Based on the following graph schema (nodes and edges) and a natural language query, generate a valid Cypher query that retrieves the requested information. | |
| - Provide only the Cypher query, no explanations or additional formatting. | |
| - Take into account the previous failed attempts and adjust the query accordingly. | |
| Return only a valid, optimized Cypher query. | |
| """ | |
| cypher_query = await llm_client.acreate_structured_output( | |
| text_input=f""" | |
| User query: | |
| {query} | |
| Node schema (labels and properties): | |
| {node_schemas} | |
| Edge schema (relationship properties): | |
| {edge_schemas} | |
| Previous attempts: | |
| {previous_attempt_context} | |
| """, | |
| system_prompt=system_prompt, | |
| response_model=str, | |
| ) | |
| try: | |
| context = await graph_engine.query(cypher_query) | |
| if context: | |
| return context | |
| else: | |
| previous_attempt_context += f"Query: {cypher_query} -> Result: None\n" | |
| continue | |
| except Exception as e: | |
| previous_attempt_context += f"Query: {cypher_query} -> Executed with error: {e}\n" | |
| return None |
🧰 Tools
🪛 Ruff (0.8.2)
36-43: f-string without any placeholders
Remove extraneous f prefix
(F541)
🪛 GitHub Actions: lint | ruff lint
[error] 36-36: Ruff: F541 - f-string without any placeholders. Remove extraneous f prefix.
I will talk to @borisarzentar about this. I think it's slightly different. In my PR, it's about exposing the cypher search parameter of the DB adapter, so the query is ultimately just a cypher query, not a natural language query. Text to cypher means the user is forming a natural language query and you have to create a corresponding cypher right? |
@hajdul88 @dm1tryG Yes, this one is different from the one we implemented last week. I would name it differently, so we know that it is a natural language search. Cypher is the implementation detail, right? Users don't care what happens behind the scene, they just send the natural language query and get results. |
|
@borisarzentar @hajdul88 I will rename the retriever, maybe good idea to inherit from new cypher retriever from #648 will check |
Description
New Text to Cypher Retriever
DCO Affirmation
I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin
Summary by CodeRabbit