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 that is configured to connect to the Neo4j Sandbox instance was 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

Open in Online IDE →

Set the Neo4j Secrets

Here are the credentials for your Neo4j Sandbox instance.

Scheme

bolt

Connection URL

44.202.210.182

Username

neo4j

Password

programmers-basins-ditches

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

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

NEO4J_URI = "bolt://44.202.210.182:7687"
NEO4J_USERNAME = "neo4j"
NEO4J_PASSWORD = "medicine-errors-sweeper"

A note on Security

For brevity, we are using the neo4j user which has full admin permissions including the ability to manage users and create and drop databases. As you are not in full control of the Cypher statements that are generated by the LLM, this may leave your database open to bad actors.

Therefore, you must ensure that any application that connects to Neo4j must do so with the appropriate user privileges.

For more information on managing users and permissions, see the Authentication & Authorization section of the Neo4j Documentation.

Creating a Neo4j Graph Store Instance

Create a new graph.py file in the project root, import the Neo4jGraph class and create a new instance with the credentials defined in the previous step.

python
graph.py
import streamlit as st
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 will be able to import the graph object into 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 that is capable of communicating with the LLM.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?