LangChain Connection Parameters for Vector Databases

Posted: Nov 16, 2024.

When working with cloud vector databases like Baidu VectorDB or Tencent VectorDB in LangChain, you'll need to properly configure the connection parameters. The ConnectionParams class provides a standardized way to specify these connection details.

What is ConnectionParams?

ConnectionParams is a utility class in LangChain that encapsulates the connection configuration needed to authenticate and connect to vector databases. It was originally designed for Baidu VectorDB but is also used by other providers like Tencent Cloud VectorDB.

Reference

Here are the key parameters supported by ConnectionParams:

ParameterTypeDescriptionDefault
endpointstrThe server address of the vector databaseRequired
api_keystrAPI key for authenticationRequired
accountstrAccount name for authentication"root"
connection_timeout_in_millsintConnection timeout in milliseconds50000

How to use ConnectionParams

Basic Configuration

The most basic usage is to provide the required endpoint and API key:

from langchain_community.vectorstores.baiduvectordb import ConnectionParams

conn_params = ConnectionParams(
    endpoint="http://192.168.xx.xx:xxxx",
    api_key="your-api-key-here"
)

Custom Timeout Configuration

You can customize the connection timeout if needed:

conn_params = ConnectionParams(
    endpoint="http://192.168.xx.xx:xxxx",
    api_key="your-api-key-here",
    connection_timeout_in_mills=30000  # 30 seconds
)

Using with Baidu VectorDB

Here's how to use ConnectionParams with Baidu's vector database:

from langchain_community.vectorstores import BaiduVectorDB
from langchain_community.embeddings.fake import FakeEmbeddings

# Configure connection
conn_params = ConnectionParams(
    endpoint="http://192.168.xx.xx:xxxx",
    api_key="your-api-key-here"
)

# Initialize embeddings
embeddings = FakeEmbeddings(size=128)

# Create vector store instance
vector_db = BaiduVectorDB(
    embeddings=embeddings,
    connection_params=conn_params
)

Using with Tencent VectorDB

The same ConnectionParams class can be used with Tencent's vector database:

from langchain_community.vectorstores import TencentVectorDB

conn_params = ConnectionParams(
    endpoint="http://10.0.x.x",
    api_key="your-api-key-here",
    account="root",
    connection_timeout_in_mills=20000
)

vector_db = TencentVectorDB(
    embeddings=embeddings,
    connection_params=conn_params
)

When working with these vector databases, make sure to:

  1. Have valid credentials (API key and endpoint)
  2. Properly configure timeouts based on your network conditions
  3. Keep your API keys secure and never commit them to version control

Remember that the exact configuration needed may vary depending on your specific use case and the vector database service you're using.

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