Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Neo4j Schema Query Builder Integration #520

Merged
merged 13 commits into from
Sep 24, 2023
Merged

Conversation

shahafp
Copy link
Contributor

@shahafp shahafp commented Sep 14, 2023


Summary:

This pull request introduces the Neo4jQueryToolSpec class which provides an innovative way to query a Neo4j graph database based on a provided schema definition. It incorporates a language model to dynamically generate Cypher queries from user questions and is equipped with a self-healing mechanism for Cypher syntax errors.

Changes:

  • Added Neo4jQueryToolSpec class to enable schema-based querying.
  • Integrated a language model for dynamic and accurate Cypher query generation.
  • Implemented a self-healing mechanism to recover from Cypher syntax errors.
  • Included a README.md to guide users on installation, usage, features, and known limitations.

Testing:

Ensure the Neo4j database is set up and running. Initialize the Neo4jQueryToolSpec with appropriate database credentials and a valid language model, then run various queries to test its functionality and error-handling capabilities.

Impact:

This tool will significantly enhance the user's experience by facilitating intuitive and schema-aware querying of the Neo4j database, while also providing robust error handling.


Copy link
Collaborator

@jerryjliu jerryjliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome! i'm really excited for this.

sorry for the delay in reviewing, a few comments. would be excited to help get this out

@@ -0,0 +1,67 @@

---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove these top-level lines

This class is responsible for querying a Neo4j graph database based on a provided schema definition.
"""

def __init__(self, url, user, password, llm):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the native llamaindex llm abstractions? they include our native integrations (openai, anthropic, etc.) and also integrate with langchain llms:

https://gpt-index.readthedocs.io/en/latest/core_modules/model_modules/llms/root.html

from llama_hub.tools.neo4j_db.base import Neo4jQueryToolSpec
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned below, would be good to have a native llamaindex demo e.g.

from llama_index.llms import OpenAI

llm = OpenAI(model="gpt-4", ...)
query_builder = Neo4jQueryToolSpec('url', 'user', 'password', llm)

Copy link
Collaborator

@jerryjliu jerryjliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually a lot of this logic seems duplicated from our Neo4jGraphStore abstraction in the core LlamaIndex repo. Is there a way we can reuse code from there? E.g. define the tool spec, but just call Neo4jGraphStore under the hood instead of re-implementing the functions here

@shahafp
Copy link
Contributor Author

shahafp commented Sep 18, 2023

Actually a lot of this logic seems duplicated from our Neo4jGraphStore abstraction in the core LlamaIndex repo. Is there a way we can reuse code from there? E.g. define the tool spec, but just call Neo4jGraphStore under the hood instead of re-implementing the functions here

Hey @jerryjliu
Thanks for your feedback!!
I will definitely will re-use your abstraction and update the pull :)

Use Neo4jGraphStore class instead of new graph database
@shahafp
Copy link
Contributor Author

shahafp commented Sep 18, 2023

@jerryjliu I have changed the pull,
I didn't change the query function to the Neo4jGraphStore query because I needed the result keys, I can create another PR to the llama_index repo to add a flag to return the keys from the function if required.

shahafpariente added 5 commits September 18, 2023 13:43
Copy link
Collaborator

@jerryjliu jerryjliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @shahafp, sorry for the delay, some final comments. maybe a general question here is do you have a notebook to show use cases?

the original intent of the tool spec was so that it could be used by agents, but curious how you've been using this so far!

llama_hub/tools/neo4j_db/base.py Show resolved Hide resolved
llama_hub/tools/neo4j_db/base.py Outdated Show resolved Hide resolved
llama_hub/tools/neo4j_db/base.py Outdated Show resolved Hide resolved
add spec_functions
fix string formatting
@shahafp
Copy link
Contributor Author

shahafp commented Sep 23, 2023

thanks @shahafp, sorry for the delay, some final comments. maybe a general question here is do you have a notebook to show use cases?

the original intent of the tool spec was so that it could be used by agents, but curious how you've been using this so far!

Yeah, maybe I kind missed the whole point of the tool at first and thought perhaps it could be used also for RAG in the main repo of llama_index because it might help get information from the graph in a more accurate way.
anyway, I removed one of the functions.

I used this tool with the chat engine to get the information about the data from the graph and some reasoning.
Would you like me to add an example of usage with agent to the README file?

@jerryjliu
Copy link
Collaborator

Yeah, maybe I kind missed the whole point of the tool at first and thought perhaps it could be used also for RAG in the main repo of llama_index because it might help get information from the graph in a more accurate way.

no worries! it's good feedback for how we position these tools - maybe we should make it clear it can be used for both human and agent use :)

I used this tool with the chat engine to get the information about the data from the graph and some reasoning.

I'd actually be really interested if you're able to get an agent up and running with this

@shahafp
Copy link
Contributor Author

shahafp commented Sep 23, 2023

I used this tool with the chat engine to get the information about the data from the graph and some reasoning.

I'd actually be really interested if you're able to get an agent up and running with this

Sure,
I used a simple agent chat with tools and queried the graph with it

llm_chat = OpenAI(
    openai_api_key="XXX-XXX",
    temperature=0,
    model='gpt-4',
)

gds_db = Neo4jQueryToolSpec(
    url="neo4j-url",
    user="neo4j-user",
    password="neo4j=password",
    llm=llm_chat,
    database='neo4j'
)

tools = gds_db.to_tool_list()
agent = OpenAIAgent.from_tools(tools, verbose=True)

# use agent
agent.chat("Where is JFK airport is located?")

Response:

'The port code JFK is located in New York, United States.'

Please let me know if this example is ok if so I will add it to the README file

@jerryjliu
Copy link
Collaborator

I used this tool with the chat engine to get the information about the data from the graph and some reasoning.

I'd actually be really interested if you're able to get an agent up and running with this

Sure, I used a simple agent chat with tools and queried the graph with it

llm_chat = OpenAI(
    openai_api_key="XXX-XXX",
    temperature=0,
    model='gpt-4',
)

gds_db = Neo4jQueryToolSpec(
    url="neo4j-url",
    user="neo4j-user",
    password="neo4j=password",
    llm=llm_chat,
    database='neo4j'
)

tools = gds_db.to_tool_list()
agent = OpenAIAgent.from_tools(tools, verbose=True)

# use agent
agent.chat("Where is JFK airport is located?")

Response:

'The port code JFK is located in New York, United States.'

Please let me know if this example is ok if so I will add it to the README file

yeah this works!

@adriens
Copy link

adriens commented Sep 24, 2023

OMG the code seems so easy to use : do you think this feature could be released soon ? I'd like to use it at #nodes23 :

Btw, I already started to talk about llama_index on one of the datasets, but querying with llama_index could help compare with oter approaches.

What I like the most with llama_index is how simple code is 🤩 ... making it very helpful during demos.

@shahafp
Copy link
Contributor Author

shahafp commented Sep 24, 2023

Thank you both for your feedback!
I have updated the README file with the agent initialization.
@jerryjliu please let me know if something else is missing 🙏

@jerryjliu jerryjliu merged commit 8f24610 into run-llama:main Sep 24, 2023
2 checks passed
@jerryjliu
Copy link
Collaborator

@shahafp merged! https://llamahub.ai/l/tools-neo4j_db

what's your twitter handle? :) promoting soon

@adriens
Copy link

adriens commented Sep 25, 2023

When will it be GA ?

@shahafp
Copy link
Contributor Author

shahafp commented Sep 25, 2023

@shahafp merged! https://llamahub.ai/l/tools-neo4j_db

what's your twitter handle? :) promoting soon

That’s awesome!!! Thank you very much!
My twitter is @ParienteShahaf
I’m honored to be part of it 🤗

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants