-
Notifications
You must be signed in to change notification settings - Fork 170
Text2Cypher custom prompt: doc, example and bug fix #229
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
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e9712a9
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia b52c45e
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 84c1780
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 47d4782
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia bc7a2f9
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia a945284
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 4e13c23
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 5367bed
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 21d1223
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 3329cd7
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia d8f6364
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 4cec2f3
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 4445b49
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 939b18c
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 1104519
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 1893b85
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 6e4ebda
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 8db7f01
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python
stellasia 085cf10
Doc + bug fix
stellasia c23616b
Merge branch 'main' of https://github.com/neo4j/neo4j-graphrag-python…
stellasia 4cb7842
Do not change the behavior, just document they said
stellasia a615399
Use same order for patched functions and check order of mocked object
stellasia 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
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
76 changes: 76 additions & 0 deletions
76
examples/customize/retrievers/text2cypher_custom_prompt.py
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,76 @@ | ||
| """The example shows how to provide a custom prompt to Text2CypherRetriever. | ||
|
|
||
| Example using the OpenAILLM, hence the OPENAI_API_KEY needs to be set in the | ||
| environment for this example to run. | ||
| """ | ||
|
|
||
| import neo4j | ||
| from neo4j_graphrag.llm import OpenAILLM | ||
| from neo4j_graphrag.retrievers import Text2CypherRetriever | ||
| from neo4j_graphrag.schema import get_schema | ||
|
|
||
| # Define database credentials | ||
| URI = "neo4j+s://demo.neo4jlabs.com" | ||
| AUTH = ("recommendations", "recommendations") | ||
| DATABASE = "recommendations" | ||
|
|
||
| # Create LLM object | ||
| llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0}) | ||
|
|
||
| # (Optional) Specify your own Neo4j schema | ||
| # (also see get_structured_schema and get_schema functions) | ||
| neo4j_schema = """ | ||
| Node properties: | ||
| User {name: STRING} | ||
| Person {name: STRING, born: INTEGER} | ||
| Movie {tagline: STRING, title: STRING, released: INTEGER} | ||
| Relationship properties: | ||
| ACTED_IN {roles: LIST} | ||
| DIRECTED {} | ||
| REVIEWED {summary: STRING, rating: INTEGER} | ||
| The relationships: | ||
| (:Person)-[:ACTED_IN]->(:Movie) | ||
| (:Person)-[:DIRECTED]->(:Movie) | ||
| (:User)-[:REVIEWED]->(:Movie) | ||
| """ | ||
|
|
||
| prompt = """Task: Generate a Cypher statement for querying a Neo4j graph database from a user input. | ||
|
|
||
| Do not use any properties or relationships not included in the schema. | ||
| Do not include triple backticks ``` or any additional text except the generated Cypher statement in your response. | ||
|
|
||
| Always filter movies that have not already been reviewed by the user with name: '{user_name}' using for instance: | ||
| (m:Movie)<-[:REVIEWED]-(:User {{name: <the_user_name>}}) | ||
|
|
||
| Schema: | ||
| {schema} | ||
|
|
||
| Input: | ||
| {query_text} | ||
|
|
||
| Cypher query: | ||
| """ | ||
|
|
||
| with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver: | ||
| # Initialize the retriever | ||
| retriever = Text2CypherRetriever( | ||
| driver=driver, | ||
| llm=llm, | ||
| neo4j_schema=neo4j_schema, | ||
| # here we provide a custom prompt | ||
| custom_prompt=prompt, | ||
| neo4j_database=DATABASE, | ||
| ) | ||
|
|
||
| # Generate a Cypher query using the LLM, send it to the Neo4j database, and return the results | ||
| query_text = "Which movies did Hugo Weaving star in?" | ||
| print( | ||
| retriever.search( | ||
| query_text=query_text, | ||
| prompt_params={ | ||
| # you have to specify all placeholder except the {query_text} one | ||
| "schema": get_schema(driver), | ||
| "user_name": "the user asking question", | ||
| }, | ||
| ) | ||
| ) |
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
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.