Couchbase Vector Database
Use Couchbase as a vector database for your Knowledge Base.
Setup
Local Setup (Docker)
Run Couchbase locally using Docker:
1docker run -d --name couchbase-server \2 -p 8091-8096:8091-8096 \3 -p 11210:11210 \4 -e COUCHBASE_ADMINISTRATOR_USERNAME=Administrator \5 -e COUCHBASE_ADMINISTRATOR_PASSWORD=password \6 couchbase:latest- Access the Couchbase UI at: http://localhost:8091
- Login with username:
Administratorand password:password - Create a bucket named
recipe_bucket, a scoperecipe_scope, and a collectionrecipes
Managed Setup (Capella)
For a managed cluster, use Couchbase Capella:
- Follow Capella's UI to create a database, bucket, scope, and collection
Environment Variables
Set up your environment variables:
1export COUCHBASE_USER="Administrator"2export COUCHBASE_PASSWORD="password"3export COUCHBASE_CONNECTION_STRING="couchbase://localhost"4export OPENAI_API_KEY=xxxFor Capella, set COUCHBASE_CONNECTION_STRING to your Capella connection string.
Install Dependencies
1uv pip install couchbaseExample
1import os2import time3from kern.agent import Agent4from kern.knowledge.embedder.openai import OpenAIEmbedder5from kern.knowledge.knowledge import Knowledge6from kern.vectordb.couchbase import CouchbaseSearch7from couchbase.options import ClusterOptions, KnownConfigProfiles8from couchbase.auth import PasswordAuthenticator9from couchbase.management.search import SearchIndex1011# Couchbase connection settings12username = os.getenv("COUCHBASE_USER")13password = os.getenv("COUCHBASE_PASSWORD")14connection_string = os.getenv("COUCHBASE_CONNECTION_STRING")1516# Create cluster options with authentication17auth = PasswordAuthenticator(username, password)18cluster_options = ClusterOptions(auth)19cluster_options.apply_profile(KnownConfigProfiles.WanDevelopment)2021knowledge_base = Knowledge(22 vector_db=CouchbaseSearch(23 bucket_name="recipe_bucket",24 scope_name="recipe_scope",25 collection_name="recipes",26 couchbase_connection_string=connection_string,27 cluster_options=cluster_options,28 search_index="vector_search_fts_index",29 embedder=OpenAIEmbedder(30 id="text-embedding-3-large",31 dimensions=3072,32 api_key=os.getenv("OPENAI_API_KEY")33 ),34 wait_until_index_ready=60,35 overwrite=True36 ),37)3839# Load the knowledge base40knowledge_base.insert(41 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"42)4344# Wait for the vector index to sync with KV45time.sleep(20)4647# Create and use the agent48agent = Agent(knowledge=knowledge_base)49agent.print_response("How to make Thai curry?", markdown=True)Async Support ⚡
Couchbase also supports asynchronous operations, enabling concurrency and leading to better performance.
1import asyncio2import os3import time4from kern.agent import Agent5from kern.knowledge.embedder.openai import OpenAIEmbedder6from kern.knowledge.knowledge import Knowledge7from kern.vectordb.couchbase import CouchbaseSearch8from couchbase.options import ClusterOptions, KnownConfigProfiles9from couchbase.auth import PasswordAuthenticator10from couchbase.management.search import SearchIndex1112# Couchbase connection settings13username = os.getenv("COUCHBASE_USER")14password = os.getenv("COUCHBASE_PASSWORD")15connection_string = os.getenv("COUCHBASE_CONNECTION_STRING")1617# Create cluster options with authentication18auth = PasswordAuthenticator(username, password)19cluster_options = ClusterOptions(auth)20cluster_options.apply_profile(KnownConfigProfiles.WanDevelopment)2122knowledge_base = Knowledge(23 vector_db=CouchbaseSearch(24 bucket_name="recipe_bucket",25 scope_name="recipe_scope",26 collection_name="recipes",27 couchbase_connection_string=connection_string,28 cluster_options=cluster_options,29 search_index="vector_search_fts_index",30 embedder=OpenAIEmbedder(31 id="text-embedding-3-large",32 dimensions=3072,33 api_key=os.getenv("OPENAI_API_KEY")34 ),35 wait_until_index_ready=60,36 overwrite=True37 ),38)3940# Create and use the agent41agent = Agent(knowledge=knowledge_base)4243async def run_agent():44 await knowledge_base.ainsert(45 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",46 )47 time.sleep(5) # Wait for the vector index to sync with KV48 await agent.aprint_response("How to make Thai curry?", markdown=True)4950if __name__ == "__main__":51 asyncio.run(run_agent())Tip
Use aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.
Key Configuration Notes
Connection Profiles
Use KnownConfigProfiles.WanDevelopment for both local and cloud deployments to handle network latency and timeouts appropriately.
Couchbase Params
| Parameter | Type | Description | Default |
|---|---|---|---|
bucket_name | str | Name of the Couchbase bucket | Required |
scope_name | str | Name of the scope within the bucket | Required |
collection_name | str | Name of the collection within the scope | Required |
couchbase_connection_string | str | Couchbase cluster connection string | Required |
cluster_options | ClusterOptions | Options for configuring the Couchbase cluster connection | Required |
search_index | Union[str, SearchIndex] | Search index configuration, either as index name or SearchIndex definition | Required |
embedder | Embedder | Embedder instance for generating embeddings | OpenAIEmbedder() |
overwrite | bool | Whether to overwrite existing collection | False |
is_global_level_index | bool | Whether the search index is at global level | False |
wait_until_index_ready | Optional[float] | Time in seconds to wait until the index is ready | None |
batch_limit | int | Maximum number of documents to process in a single batch (applies to both sync and async operations) | 500 |