Skip to main content

An integration package connecting Neo4j and LangChain

Project description

🦜️🔗 LangChain Neo4j

This package contains the LangChain integration with Neo4j.

📦 Installation

pip install -U langchain-neo4j

💻 Examples

Neo4jGraph

The Neo4jGraph class is a wrapper around Neo4j's Python driver. It provides a simple interface for interacting with a Neo4j database.

from langchain_neo4j import Neo4jGraph

graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="password")
graph.query("MATCH (n) RETURN n LIMIT 1;")

Neo4jChatMessageHistory

The Neo4jChatMessageHistory class is used to store chat message history in a Neo4j database. It stores messages as nodes and creates relationships between them, allowing for easy querying of the conversation history.

from langchain_neo4j import Neo4jChatMessageHistory

history = Neo4jChatMessageHistory(
    url="bolt://localhost:7687",
    username="neo4j",
    password="password",
    session_id="session_id_1",
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages

Neo4jVector

The Neo4jVector class provides functionality for managing a Neo4j vector store. It enables you to create new vector indexes, add vectors to existing indexes, and perform queries on indexes.

from langchain.docstore.document import Document
from langchain_openai import OpenAIEmbeddings

from langchain_neo4j import Neo4jVector

# Create a vector store from some documents and embeddings
docs = [
    Document(
        page_content=(
            "LangChain is a framework to build "
            "with LLMs by chaining interoperable components."
        ),
    )
]
embeddings = OpenAIEmbeddings(
    model="text-embedding-3-large",
    api_key="sk-...",  # Replace with your OpenAI API key
)
db = Neo4jVector.from_documents(
    docs,
    embeddings,
    url="bolt://localhost:7687",
    username="neo4j",
    password="password",
)
# Query the vector store for similar documents
docs_with_score = db.similarity_search_with_score("What is LangChain?", k=1)

GraphCypherQAChain

The CypherQAChain class enables natural language interactions with a Neo4j database. It uses an LLM and the database's schema to translate a user's question into a Cypher query, which is executed against the database. The resulting data is then sent along with the user's question to the LLM to generate a natural language response.

from langchain_openai import ChatOpenAI

from langchain_neo4j import GraphCypherQAChain, Neo4jGraph

llm = ChatOpenAI(
    temperature=0,
    api_key="sk-...",  # Replace with your OpenAI API key
)
graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="password")
chain = GraphCypherQAChain.from_llm(llm=llm, graph=graph, allow_dangerous_requests=True)
chain.run("Who starred in Top Gun?")

🧪 Tests

Install the test dependencies to run the tests:

poetry install --with test,test_integration

Unit Tests

Run the unit tests using:

make tests

Integration Tests

  1. Start the Neo4j instance using Docker:

    cd tests/integration_tests/docker-compose
    docker-compose -f neo4j.yml up
    
  2. Run the tests:

    make integration_tests
    

🧹 Code Formatting and Linting

Install the codespell, lint, and typing dependencies to lint and format your code:

poetry install --with codespell,lint,typing

To format your code, run:

make format

To lint it, run:

make lint

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_neo4j-0.1.0.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

langchain_neo4j-0.1.0-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_neo4j-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_neo4j-0.1.0.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for langchain_neo4j-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e30315985b4012c7151473ffb3159473d0abf83746f31553ce045afc2868b20
MD5 9c8edf06fe085a0ee8321fb1397b9ae0
BLAKE2b-256 abb507ec32f1f7baee33dbe0e2165f32b53d8f5e0c4ed669792feba2c14d5bf4

See more details on using hashes here.

File details

Details for the file langchain_neo4j-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_neo4j-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ba0c1ede3d1294662853eedd07af235e549a16809feca13b77417d7a04b2b94
MD5 b9a9c1ae6fe85b689126c8d39bde89b7
BLAKE2b-256 82f5c5f5cc9b7183c8b7524b151d99a6dfcc5f5273bfd590a5ecf044c1ccd7e8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page