Skip to main content

An integration package connecting YugabyteDB and LangChain

Project description

langchain-yugabytedb

The langchain-yugabytedb package implementations of core LangChain abstractions using YugabyteDB Distributed SQL Database.

The package is released under the MIT license.

Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.

Requirements

The package supports the asyncpg and psycopg3 drivers.

Installation

pip install -U langchain-yugabytedb

Documentation

Example

from langchain_core.documents import Document
from langchain_core.embeddings import DeterministicFakeEmbedding
from langchain_yugabytedb import YBEngine, YugabyteDBVectorStore

# Replace the connection string with your own YugabyteDB connection string
CONNECTION_STRING = "postgresql+psycopg://yugabyte:@localhost:5433/yugabyte"
engine = YBEngine.from_connection_string(url=CONNECTION_STRING)

# Replace the vector size with your own vector size
VECTOR_SIZE = 768
embedding = DeterministicFakeEmbedding(size=VECTOR_SIZE)

TABLE_NAME = "my_doc_collection"

engine.init_vectorstore_table(
    table_name=TABLE_NAME,
    vector_size=VECTOR_SIZE,
)

store = YugabyteDBVectorStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
    embedding_service=embedding,
)

docs = [
    Document(page_content="Apples and oranges"),
    Document(page_content="Cars and airplanes"),
    Document(page_content="Train")
]

store.add_documents(docs)

query = "I'd like a fruit."
docs = store.similarity_search(query)
print(docs)

[!TIP] All synchronous functions have corresponding asynchronous functions

ChatMessageHistory

The chat message history abstraction helps to persist chat message history in a YugabyteDB table.

YugabyteDBChatMessageHistory is parameterized using a table_name and a session_id.

The table_name is the name of the table in the database where the chat messages will be stored.

The session_id is a unique identifier for the chat session. It can be assigned by the caller using uuid.uuid4().

import uuid

from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_yugabytedb import YugabyteDBChatMessageHistory
import psycopg

# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
conn_info = ... # Fill in with your connection info
sync_connection = psycopg.connect(conn_info)

# Create the table schema (only needs to be done once)
table_name = "chat_history"
YugabyteDBChatMessageHistory.create_tables(sync_connection, table_name)

session_id = str(uuid.uuid4())

# Initialize the chat history manager
chat_history = YugabyteDBChatMessageHistory(
    table_name,
    session_id,
    sync_connection=sync_connection
)

# Add messages to the chat history
chat_history.add_messages([
    SystemMessage(content="Meow"),
    AIMessage(content="woof"),
    HumanMessage(content="bark"),
])

print(chat_history.messages)

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_yugabytedb-0.0.4.tar.gz (248.0 kB view details)

Uploaded Source

Built Distribution

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

langchain_yugabytedb-0.0.4-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file langchain_yugabytedb-0.0.4.tar.gz.

File metadata

  • Download URL: langchain_yugabytedb-0.0.4.tar.gz
  • Upload date:
  • Size: 248.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for langchain_yugabytedb-0.0.4.tar.gz
Algorithm Hash digest
SHA256 caf05f930b7ba0a006ea4e3515b62404d7b42b42eaf2e2eb7a9398da45cc3b0d
MD5 f973817fdaf0ea62d2abd83f9d462851
BLAKE2b-256 8275a1eee8590bd76a920201c51a9b8f26fa6f25c786d79818aa8e4cae8c10f8

See more details on using hashes here.

File details

Details for the file langchain_yugabytedb-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_yugabytedb-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e4990954c4c04c3323a33be2a00f777c38b0b15a8d8e86f95498a92b7fdc42f
MD5 1dd2ca896bf89f5bcc7bb207ea8a443a
BLAKE2b-256 aed38a13dcb4d0a4ad90487e88c50c3d2b3d37c3e173b98d064332f1f793b4ae

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