Skip to main content

OpenSearch

OpenSearch is a distributed, RESTful search and analytics engine, capable of performing both vector and lexical search, derived from Elasticsearch 7.10.2.

This notebook shows how to use chat message history functionality with OpenSearch.

Set up OpenSearch

There are two main ways to set up an OpenSearch instance:

  1. AWS. AWS offers a managed OpenSearch service. You can test it with their free credits.

  2. Local OpenSearch installation. Get started with OpenSearch by running it locally. The easiest way is to use the official OpenSearch Docker image. See the OpenSearch Docker documentation for more information.

Install dependencies

%pip install --upgrade --quiet  opensearch-py langchain langchain-community

Authentication

Use the Username/password

opensearch_username = os.environ.get("OPENSEARCH_USERNAME", "opensearch")
opensearch_password = os.environ.get("OPENSEARCH_PASSWORD", "change me...")

history = OpenSearchChatMessageHistory(
opensearch_url=opensearch_url,
opensearch_user=opensearch_username,
opensearch_password=opensearch_password,
index="test-history",
session_id="test-session"
)
NOTE: 
If you want to instantiate the opensearch client separately,
pass it in with the `opensearch_connection` keyword argument.

Initialize OpenSearch client and chat message history

import os

from langchain_community.chat_message_histories import OpenSearchChatMessageHistory

opensearch_url = os.environ.get("OPENSEARCH_URL", "http://localhost:9200")
history = OpenSearchChatMessageHistory(
opensearch_url=opensearch_url, index="test-history", session_id="test-session"
)

Use the chat message history

history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages
[HumanMessage(content='hi!'), AIMessage(content='whats up?')]

Was this page helpful?