This repository contains a Python notebook and a FastAPI application that demonstrate how to use the llama-index
library to interact with a Neo4j graph database for storing, indexing, and querying documents.
- Python 3.8+
- Neo4j database
- OpenAI API key
- FastAPI
- Uvicorn (or any other ASGI server)
- Clone this repository:
git clone https://github.com/your-username/llama-index-neo4j-starter-kit.git
cd llama-index-neo4j-starter-kit
- Install the required Python packages using Poetry:
poetry install
Alternatively, if you prefer to use pip
, you can install the dependencies using requirements.txt
:
pip install -r requirements.txt
- Set up your Neo4j credentials:
Update your Neo4j credentials in the credentials.json
file in the project directory. If you already have a graphdatabase set up you will update with your current Neo4j DB Credentials and OpenAI api key. If you are looking to test out a sample dataset, the following Neo4j credentials allow for read-only access to a hosted sample dataset. Your own OpenAI key will be needed to run this server.
NEO4J_URI=neo4j+ssc://9fcf58c6.databases.neo4j.io
NEO4J_DATABASE=neo4j
NEO4J_USERNAME=public
NEO4J_PASSWORD=read_only
OPENAI_API_KEY=<add_your_openai_key_here>
NOTE: the NEO4J_URI
value can use either the neo4j or bolt uri scheme. For more details on which to use, see this example.
-
Open the
starter_kit_notebook.ipynb
notebook in Jupyter Notebook or Google Colab. -
Run the notebook cells sequentially to:
- Install necessary libraries
- Import required modules and classes
- Load Neo4j credentials
- Set up the OpenAI API key and other settings
- Create graph and vector stores
- Retrieve documents from Neo4j nodes with text properties
- Create a KnowledgeGraphIndex from the retrieved documents and store it back in the Neo4j database with embeddings
- Query the KnowledgeGraphIndex using a query engine
- Create a GPTVectorStoreIndex from the documents and query it
- Use a KnowledgeGraphRAGRetriever to query the graph database
- Query the Neo4j database directly using Cypher queries with the help of the Neo4jQueryToolSpec and OpenAIAgent classes
- Create a VectorStoreIndex from the documents and query it
The starter kit also includes a FastAPI application that provides an API endpoint for sending and receiving chat messages. The application is defined in the app.py
file.
To run the FastAPI application using Poetry, follow these steps:
-
Open a terminal or command prompt and navigate to the project directory.
-
Run the following command to start the FastAPI server:
poetry run uvicorn llamaindex_starter_kit.main:app --reload
The default local address will be: http://127.0.0.1:8000
To change the port, append the command with --port=<port>
. For example:
poetry run uvicorn llamaindex_starter_kit.main:app --reload --port=9000
-
The FastAPI application will be available at the specified address and port. You can interact with the API using tools like cURL, Postman, or by making HTTP requests from your code.
-
To send a chat message, make a POST request to the
/api/chat
endpoint with a JSON payload containing the message. For example:
{
"message": "Hello, how can I assist you today?"
}
Note: The current implementation of the send_chat_message
function is a placeholder that echoes the received message. You need to replace it with the actual implementation that calls the underlying language model (LLM) using the target framework.
The FastAPI application uses Pydantic models to define the structure and validation of the request and response data. These models are defined in the models.py
file.
The following models are used:
-
ApiChatPostRequest
: Represents the request data for sending a chat message. It has the following field:message
(str): The chat message to send.
-
ApiChatPostResponse
: Represents the response data for a chat message. It has the following field:message
(Optional[str]): The chat message response, which can be None.
The Pydantic models ensure that the incoming request data and outgoing response data conform to the specified structure and types. They also provide automatic validation and serialization/deserialization of the data.
Feel free to submit issues or pull requests to improve this starter kit or add new features.
This project is licensed under the MIT License. See the LICENSE file for details.
This starter kit is built using the llama-index library by Jerry Liu and the Neo4j graph database.