LangChain Kinetica Settings Configuration

Posted: Nov 14, 2024.

KineticaSettings is a configuration class in LangChain that helps you set up and manage connections to Kinetica, a vector database with similarity search capabilities. This guide will show you how to properly configure and use KineticaSettings in your LangChain applications.

What is KineticaSettings?

KineticaSettings is a configuration class that encapsulates all the necessary parameters to establish a connection with a Kinetica database and customize its vector search behavior. It handles connection details like host, port, credentials, as well as vector search-specific settings like the distance metric to use.

Reference

Here are the main configuration parameters available in KineticaSettings:

ParameterDescriptionDefault
hostURL of the Kinetica server'http://127.0.0.1'
portPort number for the connection9191
usernameUsername for authenticationNone
passwordPassword for authenticationNone
databaseName of the database to use'langchain'
tableName of the table to store vectors'langchain_kinetica_embeddings'
metricDistance metric for similarity search (l2, cosine, etc)'l2'

How to use KineticaSettings

Here are some common ways to configure and use KineticaSettings:

Basic Configuration

The simplest way to create a KineticaSettings instance is with default values:

from langchain_community.vectorstores import KineticaSettings

settings = KineticaSettings()

Custom Connection Configuration

For connecting to a specific Kinetica instance:

settings = KineticaSettings(
    host="http://my-kinetica-server.com",
    port=8443,
    username="my_user",
    password="my_password"
)

Database and Table Configuration

To specify custom database and table names:

settings = KineticaSettings(
    database="my_vector_db",
    table="my_embeddings_table"
)

Using with Environment Variables

A common pattern is to load configuration from environment variables:

import os
from dotenv import load_dotenv

load_dotenv()

settings = KineticaSettings(
    host=os.getenv("KINETICA_HOST", "http://127.0.0.1"),
    username=os.getenv("KINETICA_USERNAME"),
    password=os.getenv("KINETICA_PASSWORD")
)

Using with Vector Store

KineticaSettings is typically used when initializing a Kinetica vector store:

from langchain_community.vectorstores import Kinetica
from langchain_openai import OpenAIEmbeddings

# Create settings
settings = KineticaSettings(
    host="http://localhost:9191",
    database="vectors"
)

# Initialize vector store with settings
vectorstore = Kinetica(
    collection_name="my_collection",
    config=settings,
    embedding_function=OpenAIEmbeddings()
)

Customizing Search Metric

You can configure the distance metric used for similarity search:

settings = KineticaSettings(
    metric="cosine"  # Options: l2, cosine, manhattan, hamming, dot
)

Best Practices

  1. Environment Variables: Store sensitive information like credentials in environment variables rather than hardcoding them.

  2. Connection Reuse: Create a helper function to generate your settings to ensure consistent configuration across your application:

def create_kinetica_config() -> KineticaSettings:
    return KineticaSettings(
        host=os.getenv("KINETICA_HOST"),
        username=os.getenv("KINETICA_USERNAME"),
        password=os.getenv("KINETICA_PASSWORD"),
        database=os.getenv("KINETICA_DB", "vectors")
    )
  1. Error Handling: When using KineticaSettings, wrap the initialization in try-except blocks to handle potential configuration errors:
try:
    settings = create_kinetica_config()
    vectorstore = Kinetica(config=settings, ...)
except Exception as e:
    logger.error(f"Failed to initialize Kinetica connection: {e}")
    raise

KineticaSettings provides a clean and organized way to manage your Kinetica database configuration in LangChain applications. By following these patterns and best practices, you can ensure reliable and secure connections to your vector database.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs