Couchbase
Code
1import os2from kern.agent import Agent3from kern.knowledge.embedder.openai import OpenAIEmbedder4from kern.knowledge.knowledge import Knowledge5from kern.vectordb.couchbase import CouchbaseSearch6from couchbase.auth import PasswordAuthenticator7from couchbase.management.search import SearchIndex8from couchbase.options import ClusterOptions, KnownConfigProfiles910# Couchbase connection settings11username = os.getenv("COUCHBASE_USER")12password = os.getenv("COUCHBASE_PASSWORD")13connection_string = os.getenv("COUCHBASE_CONNECTION_STRING")1415# Create cluster options with authentication16auth = PasswordAuthenticator(username, password)17cluster_options = ClusterOptions(auth)18cluster_options.apply_profile(KnownConfigProfiles.WanDevelopment)1920# Define the vector search index21search_index = SearchIndex(22 name="vector_search",23 source_type="gocbcore",24 idx_type="fulltext-index",25 source_name="recipe_bucket",26 plan_params={"index_partitions": 1, "num_replicas": 0},27 params={28 "doc_config": {29 "docid_prefix_delim": "",30 "docid_regexp": "",31 "mode": "scope.collection.type_field",32 "type_field": "type",33 },34 "mapping": {35 "default_analyzer": "standard",36 "default_datetime_parser": "dateTimeOptional",37 "index_dynamic": True,38 "store_dynamic": True,39 "default_mapping": {"dynamic": True, "enabled": False},40 "types": {41 "recipe_scope.recipes": {42 "dynamic": False,43 "enabled": True,44 "properties": {45 "content": {46 "enabled": True,47 "fields": [48 {49 "docvalues": True,50 "include_in_all": False,51 "include_term_vectors": False,52 "index": True,53 "name": "content",54 "store": True,55 "type": "text",56 }57 ],58 },59 "embedding": {60 "enabled": True,61 "dynamic": False,62 "fields": [63 {64 "vector_index_optimized_for": "recall",65 "docvalues": True,66 "dims": 1536,67 "include_in_all": False,68 "include_term_vectors": False,69 "index": True,70 "name": "embedding",71 "similarity": "dot_product",72 "store": True,73 "type": "vector",74 }75 ],76 },77 "meta": {78 "dynamic": True,79 "enabled": True,80 "properties": {81 "name": {82 "enabled": True,83 "fields": [84 {85 "docvalues": True,86 "include_in_all": False,87 "include_term_vectors": False,88 "index": True,89 "name": "name",90 "store": True,91 "analyzer": "keyword",92 "type": "text",93 }94 ],95 }96 },97 },98 },99 }100 },101 },102 },103)104vector_db = CouchbaseSearch(105 bucket_name="recipe_bucket",106 scope_name="recipe_scope",107 collection_name="recipes",108 couchbase_connection_string=connection_string,109 cluster_options=cluster_options,110 search_index=search_index,111 embedder=OpenAIEmbedder(112 dimensions=1536,113 ),114 wait_until_index_ready=60,115 overwrite=True,116)117118knowledge = Knowledge(119 name="Couchbase Knowledge Base",120 description="This is a knowledge base that uses a Couchbase DB",121 vector_db=vector_db,122)123124knowledge.insert(125 name="Recipes",126 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",127 metadata={"doc_type": "recipe_book"},128)129agent = Agent(130 knowledge=knowledge,131 search_knowledge=True,132 read_chat_history=True,133)134135agent.print_response("List down the ingredients to make Massaman Gai", markdown=True)136137vector_db.delete_by_name("Recipes")138# or139vector_db.delete_by_metadata({"doc_type": "recipe_book"})Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateStart Couchbase
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:latestThen access http://localhost:8091 and create:
- Bucket:
recipe_bucket - Scope:
recipe_scope - Collection:
recipes
Install dependencies
1uv pip install -U couchbase pypdf openai kern-aiSet environment variables
1export COUCHBASE_USER="Administrator"2export COUCHBASE_PASSWORD="password"3export COUCHBASE_CONNECTION_STRING="couchbase://localhost"4export OPENAI_API_KEY=xxxRun Agent
1python cookbook/08_knowledge/vector_db/couchbase_db/couchbase_db.py