Skip to content

arangoml/langchain-arangodb

Repository files navigation

🦜️🔗🥑 LangChain ArangoDB

CI Docs

PyPI version badge Python versions badge

License Code style: black Downloads

This package contains the ArangoDB integration for LangChain.

📦 Installation

pip install -U langchain-arangodb

💻 Examples

ArangoGraph

The ArangoGraph class is a wrapper around ArangoDB's Python driver. It provides a simple interface for interacting with an ArangoDB database.

from arango import ArangoClient
from langchain_arangodb import ArangoGraph

db = ArangoClient(hosts="http://localhost:8529").db(username="root", password="password")

graph = ArangoGraph(db)

graph.query("RETURN 'hello world'")

ArangoChatMessageHistory

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

from arango import ArangoClient
from langchain_arangodb import ArangoChatMessageHistory

db = ArangoClient(hosts="http://localhost:8529").db(username="root", password="password")

history = ArangoChatMessageHistory(db=db, session_id="session_id_1")
history.add_user_message("hi!")
history.add_ai_message("whats up?")

print(history.messages)

ArangoVector

The ArangoVector class provides functionality for managing an ArangoDB Vector Store. It enables you to create new vector indexes, add vectors to existing indexes, and perform queries on indexes.

from arango import ArangoClient

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

# 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 = ArangoClient(hosts="http://localhost:8529").db(username="root", password="password")

vector_db = ArangoVector.from_documents(
    docs,
    embeddings,
    database=db,
)

# Query the vector store for similar documents
docs_with_score = vector_db.similarity_search_with_score("What is LangChain?", k=1)

ArangoGraphQAChain

The ArangoGraphQAChain class enables natural language interactions with an ArangoDB database. It uses an LLM and the database's schema to translate a user's question into an AQL 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 arango import ArangoClient

from langchain_openai import ChatOpenAI
from langchain_arangodb import ArangoGraph, ArangoGraphQAChain

llm = ChatOpenAI(
    temperature=0,
    api_key="sk-...",  # Replace with your OpenAI API key
)

db = ArangoClient(hosts="http://localhost:8529").db(username="root", password="password")

graph = ArangoGraph(db)

chain = ArangoGraphQAChain.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 ArangoDB instance using Docker:

    cd tests/integration_tests/docker-compose
    docker-compose -f arangodb.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

About

Official LangChain Integration for ArangoDB

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 7

Languages