Skip to main content

An integration package connecting Couchbase and LangChain

Project description

langchain-couchbase

This package contains the LangChain integration with Couchbase

Installation

pip install -U langchain-couchbase

Vector Store

CouchbaseVectorStore class enables the usage of Couchbase for Vector Search.

from langchain_couchbase import CouchbaseVectorStore

To use this in an application:

import getpass

# Constants for the connection
COUCHBASE_CONNECTION_STRING = getpass.getpass(
    "Enter the connection string for the Couchbase cluster: "
)
DB_USERNAME = getpass.getpass("Enter the username for the Couchbase cluster: ")
DB_PASSWORD = getpass.getpass("Enter the password for the Couchbase cluster: ")

# Create Couchbase connection object
from datetime import timedelta

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import ClusterOptions

auth = PasswordAuthenticator(DB_USERNAME, DB_PASSWORD)
options = ClusterOptions(auth)
cluster = Cluster(COUCHBASE_CONNECTION_STRING, options)

# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))

vector_store = CouchbaseVectorStore(
    cluster=cluster,
    bucket_name=BUCKET_NAME,
    scope_name=SCOPE_NAME,
    collection_name=COLLECTION_NAME,
    embedding=my_embeddings,
    index_name=SEARCH_INDEX_NAME,
)

See a usage example

LLM Caches

CouchbaseCache

Use Couchbase as a cache for prompts and responses.

See a usage example.

To import this cache:

from langchain_couchbase.cache import CouchbaseCache

To use this cache with your LLMs:

from langchain_core.globals import set_llm_cache

cluster = couchbase_cluster_connection_object

set_llm_cache(
    CouchbaseCache(
        cluster=cluster,
        bucket_name=BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=COLLECTION_NAME,
    )
)

CouchbaseSemanticCache

Semantic caching allows users to retrieve cached prompts based on the semantic similarity between the user input and previously cached inputs. Under the hood it uses Couchbase as both a cache and a vectorstore. The CouchbaseSemanticCache needs a Search Index defined to work. Please look at the usage example on how to set up the index.

See a usage example.

To import this cache:

from langchain_couchbase.cache import CouchbaseSemanticCache

To use this cache with your LLMs:

from langchain_core.globals import set_llm_cache

# use any embedding provider...

from langchain_openai.Embeddings import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
cluster = couchbase_cluster_connection_object

set_llm_cache(
    CouchbaseSemanticCache(
        cluster=cluster,
        embedding = embeddings,
        bucket_name=BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=COLLECTION_NAME,
        index_name=INDEX_NAME,
    )
)

Chat Message History

Use Couchbase as the storage for your chat messages.

See a usage example.

To use the chat message history in your applications:

from langchain_couchbase.chat_message_histories import CouchbaseChatMessageHistory

message_history = CouchbaseChatMessageHistory(
cluster=cluster,
bucket_name=BUCKET_NAME,
scope_name=SCOPE_NAME,
collection_name=COLLECTION_NAME,
session_id="test-session",
)

message_history.add_user_message("hi!")

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_couchbase-0.2.5.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

langchain_couchbase-0.2.5-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_couchbase-0.2.5.tar.gz.

File metadata

  • Download URL: langchain_couchbase-0.2.5.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_couchbase-0.2.5.tar.gz
Algorithm Hash digest
SHA256 3646d1c36ce2a517fe87dc6da9cbc04236f6bb2a405701904ae2d6cdf3826e91
MD5 4cf5308e32fe01a929724720700a935f
BLAKE2b-256 ff59fbcf18cb6c09dae2e9c13abac044593ada8e1942c7f2c60e6eb953c0fa90

See more details on using hashes here.

File details

Details for the file langchain_couchbase-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_couchbase-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 21869daa491d1a7c14061d420b4c5fc62b31f280a0d8ca27e479f8e406e5a164
MD5 b847d3e4cd4b649f8349de50aecaf79e
BLAKE2b-256 071b61452d9d667b386911379c3a60211486ac8fef30fb43185d77c1ffd8dbc9

See more details on using hashes here.

Supported by

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