Skip to main content

Library with an OpenSearch implementation of LangGraph checkpoint saver.

Project description

LangGraph OpenSearch

This repository contains OpenSearch implementations for LangGraph, providing Checkpoint Savers functionality.

Overview

The project consists of:

  1. OpenSearch Checkpoint Savers: Implementations for storing and managing checkpoints using OpenSearch

Dependencies

Python Dependencies

The project requires the following main Python dependencies:

  • opensearch-py>=2.0.0
  • langgraph-checkpoint>=2.0.24

OpenSearch Requirements

Ensure your OpenSearch instance is properly configured and accessible.

Installation

Install the library using pip:

pip install langgraph-opensearch

OpenSearch Checkpoint Savers

Standard Implementation

# Initialize OpenSearchSaver with the graph
import os
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END, MessagesState
from langgraph.checkpoint.opensearch import OpenSearchSaver

from langchain_openai import ChatOpenAI

from langchain_core.messages import (
    AIMessage,
    HumanMessage,
    SystemMessage,
)

from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import boto3

region = "us-east-1"

# Get AWS credentials (from environment, IAM role, or ~/.aws/credentials)
session = boto3.Session()
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, 'es', session_token=credentials.token)

llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0.0,
    max_tokens=1000,
    streaming=True,
)

graph = StateGraph(MessagesState)

def ask(state: MessagesState) -> MessagesState:
    """
    Ask a question to the LLM and return the response.
    """
    question = state['messages'][-1].content
    response = llm.invoke(
        [*state['messages']]
    )
    return { 'messages': [ AIMessage(content=response.content) ] }

graph.add_node('ask', ask)
graph.add_edge(START, 'ask')
graph.add_edge('ask', END)

with OpenSearchSaver.from_conn_string(client_kwargs={
    'hosts': [{'host': os.getenv('OSS_HOST'), 'port': 443}],
    'http_auth': awsauth,
    'use_ssl': True,
    'verify_certs': True,
    'connection_class': RequestsHttpConnection
}) as checkpointer:
    
    config = {
        'configurable': {
            'thread_id': '3'
        }
    }
    graph = graph.compile(checkpointer=checkpointer)
    # Run the graph with an initial message
    response = graph.invoke(
        {
            "messages": [
                HumanMessage(content="What is the capital of France?")
            ]
        },
        config
    )
    print(response)

    response = graph.invoke(
        {
            "messages": [
                HumanMessage(content="What are its key attractions?")
            ]
        },
        config
    )
    print(response)

Examples

The examples directory contains Jupyter notebooks demonstrating the usage of OpenSearch with LangGraph:

  • create-basic-checkpoint.ipynb: Demonstrates the usage of OpenSearch checkpoint savers with LangGraph

Implementation Details

OpenSearch Indexing

The OpenSearch implementation creates these main indices:

  1. Checkpoints Index: Stores checkpoint metadata and versioning
  2. Writes Index: Tracks pending writes and intermediate states

Contributing

We welcome contributions! Here's how you can help:

Development Setup

  1. Clone the repository:

    git clone https://github.com/opensearch-developer/langgraph-opensearch
    cd langgraph-opensearch
    
  2. Install dependencies:

    `poetry install --all-extras`
    

Contribution Guidelines

  1. Create a new branch for your changes
  2. Write tests for new functionality
  3. Submit a pull request with a clear description of your changes
  4. Follow Conventional Commits for commit messages

License

This project is licensed under the MIT License.

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

langgraph_opensearch-0.1.1.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langgraph_opensearch-0.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file langgraph_opensearch-0.1.1.tar.gz.

File metadata

  • Download URL: langgraph_opensearch-0.1.1.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for langgraph_opensearch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6a1227cf09eac4d9009c6b1897620007c5a8d4cb513b78e514391fbf2ff680b2
MD5 0710cfb2f8bce620d3fefa3843d618b2
BLAKE2b-256 9fc0e0b01434b1b553d0c693ae3e3ccde6a84690c400bced9272045b3cb982a9

See more details on using hashes here.

File details

Details for the file langgraph_opensearch-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langgraph_opensearch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ddae966aec8b6c6c3baab1ad06af2bfdd39ca4e4e6cb53b54cb4da894953cdc
MD5 332028907cf2e49242c0827c22d51301
BLAKE2b-256 94e75072befb793207e578887a01b68ebc149a88f6bb85a84a4ae507dbe7de63

See more details on using hashes here.

Supported by

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