Connecting to Neo4j

As you learned in the Initializing the LLM lesson of Neo4j & LLM Fundamentals, Langchain uses the Neo4jGraph class to communicate with Neo4j.

You will need to create an instance of the Neo4jGraph class and connect to the Neo4j Sandbox instance created for you when you enrolled in the course.

To complete this challenge, you will need to:

  1. Add the Neo4j Credentials to .streamlit/secrets.toml

  2. Create a new Neo4jGraph instance using these credentials

Set the Neo4j Secrets

Here are the credentials for your Neo4j Sandbox instance.

Scheme

bolt

Connection URL

{sandbox-ip}

Username

{sandbox-username}

Password

{sandbox-password}

Set these as secrets in the Streamlit app by opening the .streamlit/secrets.toml and adding the following values.

toml
.streamlit/secrets.toml
OPENAI_API_KEY = "sk-..."
OPENAI_MODEL = "gpt-4"

NEO4J_URI = "bolt://{sandbox-ip}:{sandbox-boltPort}"
NEO4J_USERNAME = "{sandbox-username}"
NEO4J_PASSWORD = "{sandbox-password}"

Connect to the database

Open the graph.py file in the project root, import the Neo4jGraph class, and create a new instance with your credentials.

python
graph.py
from langchain_community.graphs import Neo4jGraph

graph = Neo4jGraph(
    url=st.secrets["NEO4J_URI"],
    username=st.secrets["NEO4J_USERNAME"],
    password=st.secrets["NEO4J_PASSWORD"],
)

Using the Neo4jGraph Instance

Once you have completed the steps, you can import the graph and use the Neo4jGraph instance in other modules within the project.

python
from graph import graph

Summary

In this lesson, you added your Neo4j Sandbox credentials to the app and used them to create a Neo4jGraph object.

In the next lesson, you will create an Agent to communicate with the LLM.