Back to the Couchbase homepage
Couchbase Developer

  • Docs

    • Integrations
    • SDKs
    • Mobile SDKs

    • AI Developer
    • Backend
    • Full-stack
    • Mobile
    • Ops / DBA

    • Data Modeling
    • Scalability

  • Tutorials

    • Developer Community
    • Ambassador Program
  • Sign In
  • Try Free

RAG with Azure OpenAI and Couchbase Search Vector Index

  • Learn how to build a semantic search engine using Couchbase and Azure OpenAI using Search Vector Index.
  • This tutorial demonstrates how to integrate Couchbase's vector search capabilities with Azure OpenAI embeddings.
  • You'll understand how to perform Retrieval-Augmented Generation (RAG) using LangChain and Couchbase.

View Source

Introduction

In this guide, we will walk you through building a powerful semantic search engine using Couchbase as the backend database, Azure OpenAI as the AI-powered embedding and language model provider. Semantic search goes beyond simple keyword matching by understanding the context and meaning behind the words in a query, making it an essential tool for applications that require intelligent information retrieval. This tutorial is designed to be beginner-friendly, with clear, step-by-step instructions that will equip you with the knowledge to create a fully functional semantic search system using the Search Vector Index from scratch. Alternatively if you want to perform semantic search using the Hyperscale or Composite Vector Index, please take a look at this.

How to run this tutorial

This tutorial is available as a Jupyter Notebook (.ipynb file) that you can run interactively. You can access the original notebook here.

You can either download the notebook file and run it on Google Colab or run it on your system by setting up the Python environment.

Before you start

Get Credentials for Azure OpenAI

Please follow the instructions to generate the Azure OpenAI credentials.

Create and Deploy Your Free Tier Operational cluster on Capella

To get started with Couchbase Capella, create an account and use it to deploy a forever free tier operational cluster. This account provides you with a environment where you can explore and learn about Capella with no time constraint.

To know more, please follow the instructions.

Couchbase Capella Configuration

When running Couchbase using Capella, the following prerequisites need to be met.

  • Create the database credentials to access the travel-sample bucket (Read and Write) used in the application.
  • Allow access to the Cluster from the IP on which the application is running.

Setting the Stage: Installing Necessary Libraries

To build our semantic search engine, we need a robust set of tools. The libraries we install handle everything from connecting to databases to performing complex machine learning tasks. Each library has a specific role: Couchbase libraries manage database operations, LangChain handles AI model integrations, and Azure OpenAI provides advanced AI models for generating embeddings and understanding natural language. By setting up these libraries, we ensure our environment is equipped to handle the data-intensive and computationally complex tasks required for semantic search.

!pip install datasets==3.5.0 langchain-couchbase==0.3.0 langchain-openai==0.3.13 python-dotenv
[Output too long, omitted for brevity]

Importing Necessary Libraries

The script starts by importing a series of libraries required for various tasks, including handling JSON, logging, time tracking, Couchbase connections, embedding generation, and dataset loading. These libraries provide essential functions for working with data, managing database connections, and processing machine learning models.

import os
import getpass
import json
import logging
import sys
import time
import random
from datetime import timedelta
from uuid import uuid4

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.exceptions import (
    CouchbaseException,
    InternalServerFailureException,
    QueryIndexAlreadyExistsException,
)
from couchbase.management.search import SearchIndex
from couchbase.options import ClusterOptions
from datasets import load_dataset
from langchain_core.documents import Document
from langchain_core.globals import set_llm_cache
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_couchbase.cache import CouchbaseCache
from langchain_couchbase.vectorstores import CouchbaseSearchVectorStore
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
from tqdm import tqdm
from dotenv import load_dotenv
/Users/viraj.agarwal/Tasks/2026/Task1.5/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Setup Logging

Logging is configured to track the progress of the script and capture any errors or warnings. This is crucial for debugging and understanding the flow of execution. The logging output includes timestamps, log levels (e.g., INFO, ERROR), and messages that describe what is happening in the script.

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', force=True)

def retry_with_backoff(func, max_retries=3, base_delay=1, max_delay=10):
    """Execute function with exponential backoff retry for rate limiting."""
    for attempt in range(max_retries):
        try:
            return func()
        except InternalServerFailureException as e:
            if "query request rejected" in str(e) or "429" in str(e):
                if attempt < max_retries - 1:
                    delay = min(base_delay * (2 ** attempt) + random.uniform(0, 1), max_delay)
                    logging.warning(f"Rate limited. Retrying in {delay:.2f}s (attempt {attempt + 1}/{max_retries})")
                    time.sleep(delay)
                else:
                    raise
            else:
                raise
        except Exception as e:
            raise
    return None

Loading Sensitive Information

In this section, we prompt the user to input essential configuration settings needed. These settings include sensitive information like API keys, database credentials, and specific configuration names. Instead of hardcoding these details into the script, we request the user to provide them at runtime, ensuring flexibility and security.

The script also validates that all required inputs are provided, raising an error if any crucial information is missing. This approach ensures that your integration is both secure and correctly configured without hardcoding sensitive information, enhancing the overall security and maintainability of your code.

load_dotenv()  # Loads variables from .env into environment

AZURE_OPENAI_KEY = os.getenv('AZURE_OPENAI_KEY') or getpass.getpass('Enter your Azure OpenAI Key: ')
AZURE_OPENAI_ENDPOINT = os.getenv('AZURE_OPENAI_ENDPOINT') or input('Enter your Azure OpenAI Endpoint: ')
AZURE_OPENAI_EMBEDDING_DEPLOYMENT = os.getenv('AZURE_OPENAI_EMBEDDING_DEPLOYMENT') or input('Enter your Azure OpenAI Embedding Deployment: ')
AZURE_OPENAI_CHAT_DEPLOYMENT = os.getenv('AZURE_OPENAI_CHAT_DEPLOYMENT') or input('Enter your Azure OpenAI Chat Deployment: ')

CB_HOST = os.getenv('CB_HOST') or input('Enter your Couchbase host (default: couchbase://localhost): ') or 'couchbase://localhost'
CB_USERNAME = os.getenv('CB_USERNAME') or input('Enter your Couchbase username (default: Administrator): ') or 'Administrator'
CB_PASSWORD = os.getenv('CB_PASSWORD') or getpass.getpass('Enter your Couchbase password (default: password): ') or 'password'
CB_BUCKET_NAME = os.getenv('CB_BUCKET_NAME') or input('Enter your Couchbase bucket name (default: vector-search-testing): ') or 'vector-search-testing'
INDEX_NAME = os.getenv('INDEX_NAME') or input('Enter your index name (default: vector_search_azure): ') or 'vector_search_azure'
SCOPE_NAME = os.getenv('SCOPE_NAME') or input('Enter your scope name (default: shared): ') or 'shared'
COLLECTION_NAME = os.getenv('COLLECTION_NAME') or input('Enter your collection name (default: azure): ') or 'azure'
CACHE_COLLECTION = os.getenv('CACHE_COLLECTION') or input('Enter your cache collection name (default: cache): ') or 'cache'

# Check if the variables are correctly loaded
if not all([AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_EMBEDDING_DEPLOYMENT, AZURE_OPENAI_CHAT_DEPLOYMENT]):
    raise ValueError("Missing required Azure OpenAI variables")

Connecting to the Couchbase Cluster

Connecting to a Couchbase cluster is the foundation of our project. Couchbase will serve as our primary data store, handling all the storage and retrieval operations required for our semantic search engine. By establishing this connection, we enable our application to interact with the database, allowing us to perform operations such as storing embeddings, querying data, and managing collections. This connection is the gateway through which all data will flow, so ensuring it's set up correctly is paramount.

try:
    auth = PasswordAuthenticator(CB_USERNAME, CB_PASSWORD)
    options = ClusterOptions(auth)
    options.apply_profile('wan_development')
    cluster = Cluster(CB_HOST, options)
    cluster.wait_until_ready(timedelta(seconds=5))
    logging.info("Successfully connected to Couchbase")
except Exception as e:
    raise ConnectionError(f"Failed to connect to Couchbase: {str(e)}")
2026-04-06 14:52:41,588 - INFO - Successfully connected to Couchbase

Setting Up Collections in Couchbase

In Couchbase, data is organized in buckets, which can be further divided into scopes and collections. Think of a collection as a table in a traditional SQL database. Before we can store any data, we need to ensure that our collections exist. If they don't, we must create them. This step is important because it prepares the database to handle the specific types of data our application will process. By setting up collections, we define the structure of our data storage, which is essential for efficient data retrieval and management.

Moreover, setting up collections allows us to isolate different types of data within the same bucket, providing a more organized and scalable data structure. This is particularly useful when dealing with large datasets, as it ensures that related data is stored together, making it easier to manage and query.

def setup_collection(cluster, bucket_name, scope_name, collection_name):
    try:
        bucket = cluster.bucket(bucket_name)
        bucket_manager = bucket.collections()

        # Check if collection exists, create if it doesn't
        collections = bucket_manager.get_all_scopes()
        collection_exists = any(
            scope.name == scope_name and collection_name in [col.name for col in scope.collections]
            for scope in collections
        )

        if not collection_exists:
            logging.info(f"Collection '{collection_name}' does not exist. Creating it...")
            bucket_manager.create_collection(scope_name, collection_name)
            logging.info(f"Collection '{collection_name}' created successfully.")
        else:
            logging.info(f"Collection '{collection_name}' already exists.Skipping creation.")

        collection = bucket.scope(scope_name).collection(collection_name)
        time.sleep(2)  # Give the collection time to be ready for queries

        # Ensure primary index exists
        try:
            cluster.query(f"CREATE PRIMARY INDEX IF NOT EXISTS ON `{bucket_name}`.`{scope_name}`.`{collection_name}`").execute()
            logging.info("Primary index present or created successfully.")
        except Exception as e:
            logging.warning(f"Error creating primary index: {str(e)}")

        # Clear all documents in the collection
        try:
            query = f"DELETE FROM `{bucket_name}`.`{scope_name}`.`{collection_name}`"
            cluster.query(query).execute()
            logging.info("All documents cleared from the collection.")
        except Exception as e:
            logging.warning(f"Error while clearing documents: {str(e)}. The collection might be empty.")

        return collection
    except Exception as e:
        raise RuntimeError(f"Error setting up collection: {str(e)}")

setup_collection(cluster, CB_BUCKET_NAME, SCOPE_NAME, COLLECTION_NAME)
setup_collection(cluster, CB_BUCKET_NAME, SCOPE_NAME, CACHE_COLLECTION)

Loading Couchbase Vector Search Index

Semantic search requires an efficient way to retrieve relevant documents based on a user's query. This is where the Couchbase Vector Search Index comes into play. In this step, we load the Vector Search Index definition from a JSON file, which specifies how the index should be structured. This includes the fields to be indexed, the dimensions of the vectors, and other parameters that determine how the search engine processes queries based on vector similarity.

For more information on creating a vector search index, please follow the instructions.

# If you are running this script locally (not in Google Colab), uncomment the following line
# and provide the path to your index definition file.

index_definition_path = './azure_index.json'  # Local setup: specify your file path here

# If you are running in Google Colab, use the following code to upload the index definition file
# from google.colab import files
# print("Upload your index definition file")
# uploaded = files.upload()
# index_definition_path = list(uploaded.keys())[0]

try:
    with open(index_definition_path, 'r') as file:
        index_definition = json.load(file)
    # Update the index definition with the current bucket name and scope/collection
    index_definition['sourceName'] = CB_BUCKET_NAME
    # Update the type mapping to use current scope and collection names
    if 'params' in index_definition and 'mapping' in index_definition['params']:
        if 'types' in index_definition['params']['mapping']:
            types = index_definition['params']['mapping']['types']
            # Replace old scope.collection with new ones
            for old_key in list(types.keys()):
                if '.' in old_key:
                    new_key = f"{SCOPE_NAME}.{COLLECTION_NAME}"
                    types[new_key] = types.pop(old_key)
except Exception as e:
    raise ValueError(f"Error loading index definition from {index_definition_path}: {str(e)}")

Creating or Updating Search Indexes

With the index definition loaded, the next step is to create or update the Vector Search Index in Couchbase. This step is crucial because it optimizes our database for vector similarity search operations, allowing us to perform searches based on the semantic content of documents rather than just keywords. By creating or updating a Vector Search Index, we enable our search engine to handle complex queries that involve finding semantically similar documents using vector embeddings, which is essential for a robust semantic search engine.

try:
    scope_index_manager = cluster.bucket(CB_BUCKET_NAME).scope(SCOPE_NAME).search_indexes()

    # Check if index already exists
    existing_indexes = scope_index_manager.get_all_indexes()
    index_name = index_definition["name"]

    if index_name in [index.name for index in existing_indexes]:
        logging.info(f"Index '{index_name}' found")
    else:
        logging.info(f"Creating new index '{index_name}'...")

    # Create SearchIndex object from JSON definition
    search_index = SearchIndex.from_json(index_definition)

    # Upsert the index (create if not exists, update if exists)
    scope_index_manager.upsert_index(search_index)
    logging.info(f"Index '{index_name}' successfully created/updated.")

except QueryIndexAlreadyExistsException:
    logging.info(f"Index '{index_name}' already exists. Skipping creation/update.")

except InternalServerFailureException as e:
    error_message = str(e)
    logging.error(f"InternalServerFailureException raised: {error_message}")

    try:
        # Accessing the response_body attribute from the context
        error_context = e.context
        response_body = error_context.response_body
        if response_body:
            error_details = json.loads(response_body)
            error_message = error_details.get('error', '')

            if "collection: 'azure' doesn't belong to scope: 'shared'" in error_message:
                raise ValueError("Collection 'azure' does not belong to scope 'shared'. Please check the collection and scope names.")

    except ValueError as ve:
        logging.error(str(ve))
        raise

    except Exception as json_error:
        logging.error(f"Failed to parse the error message: {json_error}")
        raise RuntimeError(f"Internal server error while creating/updating search index: {error_message}")
2026-04-06 14:52:51,019 - INFO - Creating new index 'vector_search_azure'...
2026-04-06 14:52:51,120 - INFO - Index 'vector_search_azure' successfully created/updated.

Load the BBC News Dataset

To build a search engine, we need data to search through. We use the BBC News dataset from RealTimeData, which provides real-world news articles across multiple topics and time periods. Loading the dataset is a crucial step because it provides the raw material that our search engine will work with.

The BBC News dataset allows us to work with authentic news content, enabling us to build and test retrieval quality on realistic long-form articles.

try:
    news_dataset = load_dataset("RealTimeData/bbc_news_alltime", "2024-12", split="train")
    news_articles = news_dataset["content"]
    unique_news_articles = list({article for article in news_articles if article})
    logging.info(f"Successfully loaded BBC News dataset with {len(news_dataset)} rows")
    logging.info(f"Prepared {len(unique_news_articles)} unique BBC news articles for ingestion")
except Exception as e:
    raise ValueError(f"Error loading BBC News dataset: {str(e)}")
2026-04-06 14:52:54,685 - INFO - HTTP Request: HEAD https://huggingface.co/datasets/RealTimeData/bbc_news_alltime/resolve/main/README.md "HTTP/1.1 307 Temporary Redirect"
2026-04-06 14:52:54,904 - INFO - HTTP Request: HEAD https://huggingface.co/api/resolve-cache/datasets/RealTimeData/bbc_news_alltime/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/README.md "HTTP/1.1 200 OK"
2026-04-06 14:52:55,205 - INFO - HTTP Request: HEAD https://huggingface.co/datasets/RealTimeData/bbc_news_alltime/resolve/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/bbc_news_alltime.py "HTTP/1.1 404 Not Found"
2026-04-06 14:52:56,188 - INFO - HTTP Request: HEAD https://s3.amazonaws.com/datasets.huggingface.co/datasets/datasets/RealTimeData/bbc_news_alltime/RealTimeData/bbc_news_alltime.py "HTTP/1.1 404 Not Found"
2026-04-06 14:52:56,602 - INFO - HTTP Request: GET https://huggingface.co/api/datasets/RealTimeData/bbc_news_alltime/revision/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b "HTTP/1.1 200 OK"
2026-04-06 14:52:56,922 - INFO - HTTP Request: HEAD https://huggingface.co/datasets/RealTimeData/bbc_news_alltime/resolve/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/.huggingface.yaml "HTTP/1.1 404 Not Found"
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
2026-04-06 14:52:56,924 - WARNING - Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
2026-04-06 14:52:57,939 - INFO - HTTP Request: GET https://datasets-server.huggingface.co/info?dataset=RealTimeData/bbc_news_alltime "HTTP/1.1 200 OK"
2026-04-06 14:52:58,253 - INFO - HTTP Request: GET https://huggingface.co/api/datasets/RealTimeData/bbc_news_alltime/tree/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/2017-01?recursive=true&expand=false "HTTP/1.1 200 OK"
2026-04-06 14:52:58,547 - INFO - HTTP Request: GET https://huggingface.co/api/datasets/RealTimeData/bbc_news_alltime/tree/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b?recursive=false&expand=false "HTTP/1.1 200 OK"
2026-04-06 14:52:58,851 - INFO - HTTP Request: HEAD https://huggingface.co/datasets/RealTimeData/bbc_news_alltime/resolve/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/dataset_infos.json "HTTP/1.1 404 Not Found"
2026-04-06 14:52:59,178 - INFO - HTTP Request: GET https://huggingface.co/api/datasets/RealTimeData/bbc_news_alltime/tree/8dd1ecdc92ac43f9c04a3da3e945537dbb08179b/2024-12?recursive=true&expand=false "HTTP/1.1 200 OK"
2026-04-06 14:52:59,203 - INFO - Successfully loaded BBC News dataset with 2687 rows
2026-04-06 14:52:59,203 - INFO - Prepared 1749 unique BBC news articles for ingestion

Creating Azure OpenAI Embeddings

Embeddings are at the heart of semantic search. They are numerical representations of text that capture the semantic meaning of the words and phrases. Unlike traditional keyword-based search, which looks for exact matches, embeddings allow our search engine to understand the context and nuances of language, enabling it to retrieve documents that are semantically similar to the query, even if they don't contain the exact keywords. By creating embeddings using Azure OpenAI, we equip our search engine with the ability to understand and process natural language in a way that's much closer to how humans understand language. This step transforms our raw text data into a format that the search engine can use to find and rank relevant documents.

try:
    embeddings = AzureOpenAIEmbeddings(
        deployment=AZURE_OPENAI_EMBEDDING_DEPLOYMENT,
        openai_api_key=AZURE_OPENAI_KEY,
        azure_endpoint=AZURE_OPENAI_ENDPOINT
    )
    # Test the embedding by making a small request
    test_embedding = embeddings.embed_query("test")
    if not test_embedding:
        raise ValueError("Embedding test failed - no embedding returned")
    logging.info("Successfully created AzureOpenAIEmbeddings and verified deployment")
except Exception as e:
    error_msg = str(e)
    if "DeploymentNotFound" in error_msg or "404" in error_msg:
        raise ValueError(
            f"Azure OpenAI deployment '{AZURE_OPENAI_EMBEDDING_DEPLOYMENT}' not found at endpoint '{AZURE_OPENAI_ENDPOINT}'. "
            f"Please verify: 1) The deployment name matches exactly (case-sensitive), "
            f"2) The deployment exists in Azure OpenAI Studio, "
            f"3) The endpoint URL is correct. Original error: {error_msg}"
        )
    raise ValueError(f"Error creating AzureOpenAIEmbeddings: {error_msg}")
2026-04-06 14:53:01,725 - INFO - HTTP Request: POST https://sample-azure-openai-key.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2023-05-15 "HTTP/1.1 200 OK"
2026-04-06 14:53:01,729 - INFO - Successfully created AzureOpenAIEmbeddings and verified deployment

Setting Up the Couchbase Vector Store

The vector store is set up to manage the embeddings created in the previous step. The vector store is essentially a database optimized for storing and retrieving high-dimensional vectors. In this case, the vector store is built on top of Couchbase, allowing the script to store the embeddings in a way that can be efficiently searched.

try:
    vector_store = CouchbaseSearchVectorStore(
        cluster=cluster,
        bucket_name=CB_BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=COLLECTION_NAME,
        embedding=embeddings,
        index_name=INDEX_NAME,
    )
    logging.info("Successfully created vector store")
except Exception as e:
    raise ValueError(f"Failed to create vector store: {str(e)}")
2026-04-06 14:57:00,169 - INFO - Successfully created vector store

Saving Data to the Vector Store

With the vector store set up, the next step is to populate it with data. We save the BBC News dataset to the vector store in batches. This method is efficient and ensures that our search engine can handle large datasets without running into performance issues. By saving the data in this way, we prepare our search engine to quickly and accurately respond to user queries. This step is essential for making the dataset searchable, transforming raw data into a format that can be easily queried by our search engine.

Batch processing is particularly important when dealing with large datasets, as it prevents memory overload and ensures that the data is stored in a structured and retrievable manner. This approach not only optimizes performance but also ensures the scalability of our system.

try:
    batch_size = 50
    logging.disable(sys.maxsize) # Disable logging to prevent tqdm output
    for i in tqdm(range(0, len(unique_news_articles), batch_size), desc="Processing Batches"):
        batch = unique_news_articles[i:i + batch_size]
        documents = [Document(page_content=text) for text in batch]
        uuids = [str(uuid4()) for _ in range(len(documents))]
        vector_store.add_documents(documents=documents, ids=uuids)
    logging.disable(logging.NOTSET) # Re-enable logging
except Exception as e:
    raise RuntimeError(f"Failed to save documents to vector store: {str(e)}")
Processing Batches: 100%|██████████| 35/35 [05:20<00:00,  9.16s/it]

Setting Up a Couchbase Cache

To further optimize our system, we set up a Couchbase-based cache. A cache is a temporary storage layer that holds data that is frequently accessed, speeding up operations by reducing the need to repeatedly retrieve the same information from the database. In our setup, the cache will help us accelerate repetitive tasks, such as looking up similar documents. By implementing a cache, we enhance the overall performance of our search engine, ensuring that it can handle high query volumes and deliver results quickly.

Caching is particularly valuable in scenarios where users may submit similar queries multiple times or where certain pieces of information are frequently requested. By storing these in a cache, we can significantly reduce the time it takes to respond to these queries, improving the user experience.

try:
    cache = CouchbaseCache(
        cluster=cluster,
        bucket_name=CB_BUCKET_NAME,
        scope_name=SCOPE_NAME,
        collection_name=CACHE_COLLECTION,
    )
    logging.info("Successfully created cache")
    set_llm_cache(cache)
except Exception as e:
    raise ValueError(f"Failed to create cache: {str(e)}")
2026-04-06 15:06:49,385 - INFO - Successfully created cache

Using the Azure Chat OpenAI Language Model (LLM)

Language models are AI systems that are trained to understand and generate human language. We'll be using AzureChatOpenAI language model to process user queries and generate meaningful responses. This model is a key component of our semantic search engine, allowing it to go beyond simple keyword matching and truly understand the intent behind a query. By creating this language model, we equip our search engine with the ability to interpret complex queries, understand the nuances of language, and provide more accurate and contextually relevant responses.

The language model's ability to understand context and generate coherent responses is what makes our search engine truly intelligent. It can not only find the right information but also present it in a way that is useful and understandable to the user.

try:
    llm = AzureChatOpenAI(
        deployment_name=AZURE_OPENAI_CHAT_DEPLOYMENT,
        openai_api_key=AZURE_OPENAI_KEY,
        azure_endpoint=AZURE_OPENAI_ENDPOINT,
        openai_api_version="2024-07-01-preview"
    )
    # Test the LLM by making a small request
    test_response = llm.invoke("Say 'test successful'")
    if not test_response:
        raise ValueError("LLM test failed - no response returned")
    logging.info("Successfully created Azure OpenAI Chat model and verified deployment")
except Exception as e:
    error_msg = str(e)
    if "DeploymentNotFound" in error_msg or "404" in error_msg:
        raise ValueError(
            f"Azure OpenAI deployment '{AZURE_OPENAI_CHAT_DEPLOYMENT}' not found at endpoint '{AZURE_OPENAI_ENDPOINT}'. "
            f"Please verify: 1) The deployment name matches exactly (case-sensitive), "
            f"2) The deployment exists in Azure OpenAI Studio, "
            f"3) The endpoint URL is correct. Original error: {error_msg}"
        )
    raise ValueError(f"Error creating Azure OpenAI Chat model: {error_msg}")
2026-04-06 15:06:52,202 - INFO - HTTP Request: POST https://sample-azure-openai-key.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-07-01-preview "HTTP/1.1 200 OK"
2026-04-06 15:06:52,212 - INFO - Successfully created Azure OpenAI Chat model and verified deployment

Perform Semantic Search

Semantic search in Couchbase involves converting queries and documents into vector representations using an embeddings model. These vectors capture the semantic meaning of the text and are stored directly in Couchbase. When a query is made, Couchbase performs a similarity search by comparing the query vector against the stored document vectors. The similarity metric used for this comparison is configurable, allowing flexibility in how the relevance of documents is determined. Common metrics include cosine similarity, Euclidean distance, or dot product, but other metrics can be implemented based on specific use cases. Different embedding models like BERT, Word2Vec, or GloVe can also be used depending on the application's needs, with the vectors generated by these models stored and searched within Couchbase itself.

In the provided code, the search process begins by recording the start time, followed by executing the similarity_search_with_score method of the CouchbaseSearchVectorStore. This method searches Couchbase for the most relevant documents based on the vector similarity to the query. The search results include the document content and a similarity score that reflects how closely each document aligns with the query in the defined semantic space. The time taken to perform this search is then calculated and logged, and the results are displayed, showing the most relevant documents along with their similarity scores. This approach leverages Couchbase as both a storage and retrieval engine for vector data, enabling efficient and scalable semantic searches. The integration of vector storage and search capabilities within Couchbase allows for sophisticated semantic search operations without relying on external services for vector storage or comparison.

query = "What caused the 1929 Great Depression?"

def perform_search():
    """Wrapper function for semantic search to enable retry logic."""
    start_time = time.time()
    results = vector_store.similarity_search_with_score(query, k=10)
    elapsed = time.time() - start_time
    return results, elapsed

try:
    # Perform the semantic search with retry logic
    search_results, search_elapsed_time = retry_with_backoff(perform_search)

    logging.info(f"Semantic search completed in {search_elapsed_time:.2f} seconds")

    # Display search results
    print(f"\nSemantic Search Results (completed in {search_elapsed_time:.2f} seconds):")
    for doc, score in search_results:
        print(f"Distance: {score:.4f}, Text: {doc.page_content}")

except InternalServerFailureException as e:
    if "query request rejected" in str(e):
        raise RuntimeError("Search request was rate limited after multiple retries. Please wait and try again later.")
    raise RuntimeError(f"Error performing semantic search: {str(e)}")
except CouchbaseException as e:
    raise RuntimeError(f"Error performing semantic search: {str(e)}")
except Exception as e:
    raise RuntimeError(f"Unexpected error: {str(e)}")
2026-04-06 15:06:55,740 - INFO - HTTP Request: POST https://sample-azure-openai-key.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2023-05-15 "HTTP/1.1 200 OK"
2026-04-06 15:06:55,770 - INFO - Semantic search completed in 1.05 seconds



Semantic Search Results (completed in 1.05 seconds):
Distance: 0.2631, Text: Stocks slide as US central bank signals slower pace of rate cuts

US share prices slumped after the central bank cut interest rates for the third time in a row but its economic projections signalled a slower pace of cuts next year. In a widely expected move, the Federal Reserve set its key lending rate in a target range of 4.25% to 4.5%. That is down a full percentage point since September, when the bank started lowering borrowing costs, citing progress stabilising prices and a desire to head off economic weakening. Reports since then indicate that the number of jobs being created has been more resilient than expected, while price rises have continued to bubble.

Stocks in the US fell sharply as Federal Reserve chairman Jerome Powell warned the situation would likely result in fewer rate cuts than expected next year. "We are in a new phase of the process," he said at a press conference. "From this point forward, it's appropriate to move cautiously and look for progress on inflation." The Dow Jones Industrial Average closed 2.58% lower, suffering its 10th session of declines in a row and marking its longest streak of daily losses since 1974. The S&P 500 lost almost 3% and the Nasdaq Composite fell 3.6%. In morning trade in Asia on Thursday, Japan's Nikkei 225 was around 1.2% lower, while the Hang Seng in Hong Kong was down by 1.1%.

Inflation, which measures the pace of price increases, has proven stubborn in recent months, ticking up to 2.7% in the US in November. Analysts have also warned that policies backed by president-elect Donald Trump, including plans for tax cuts and widespread import tariffs, could put upward pressure on prices. Analysts say lowering borrowing costs risks adding to that pressure by making it easier to borrow and encouraging businesses and households to take on credit to spend. Mr Powell defended the cut on Wednesday, pointing to cooling in the job market over the last two years. But he conceded that the move was a "closer call" on this occasion and acknowledged there is some uncertainty as the White House changes hands. Olu Sonola, head of US economic research at Fitch Ratings, said it felt like the Fed was signalling a "pause" to cuts as questions about White House policies make it more unsure about the path ahead. "Growth is still good, the labour market is still healthy, but inflationary storms are gathering," he said.

Wednesday's rate cut - formally opposed by one Fed policymaker - is the last by the central bank before president-elect Donald Trump takes office. He won the election in November promising to bring down both prices and interest rates. But mortgage rates have actually climbed since September, reflecting bets that borrowing costs will stay relatively high. Forecasts released by the Fed on Wednesday showed policymakers now expect the bank's key lending rate to fall to just 3.9% by the end of 2025, above the 3.4% predicted just three months ago. They also anticipate inflation staying higher next year than previously forecast, at about 2.5% - still above the bank's 2% target. John Ryding, chief economic advisor at Brean Capital, said he thought it would have been wiser for the Fed to hold off on a cut at this meeting, despite the likelihood it would upset markets. "There has been enormous progress made from the peak in inflation to where the US is now and it risks giving up on that progress, possibly even that progress being partially reversed," he said. "The economy looks strong... What's the rush?"

The Fed announcement comes a day before the Bank of England is due to make its latest interest rates decision in the UK, where price inflation has also recently ticked higher. It is widely expected to hold its benchmark rate steady at 4.75%. Monica George Michail, associate economist at the National Institute of Economic and Social Research, said the Bank of England was facing rates of wage growth and price increases for services that are hotter than in the US. Some of the government's plans, which include hikes to the minimum wage, will also put pressure on inflation, she added. "The Bank of England is trying to remain cautious," she said. But she warned that inflation risks are present in the US as well, pointing to Mr Trump's tariff plans. Mr Ryding said he thought the Bank of England - which unlike the Fed, does not have to consider unemployment as part of its mandate - was more clearly responding to the reality of the situation in front of it. "The Bank [of England] is being more of a prudent central bank than the Fed is right now," he said.
Distance: 0.2324, Text: The European economy is going to have a tough few years, if new forecasts are correct.

Despite the European Central Bank (ECB) cutting interest rates for the third time in a row on Thursday, saying that lowering inflation was "well on track", the outlook for growth is less rosy.

"The element which has changed is the downside risks, particularly the downside risk to growth," ECB president Christine Lagarde said as she announced a further cut in interest rates in the bloc, down to 3%.

The bank said surveys indicated growth was slowing in the current quarter and that the recovery depended on consumers spending more and businesses increasing their investments.

It cut its forecast for growth for the eurozone economy next year to 1.1%, from the 1.3% it had expected in September.

And these forecasts do not include the impact of the Trump tariffs on trade that are on the horizon, after the US president-elect's inauguration in January.

Markets now anticipate a faster series of rate cuts next year.

The key short-term challenge is that the twin engines of the eurozone are not firing.

Germany is beset by a structural challenge to its entire economic model. High energy prices, higher labour costs, a need to shoulder more of a defence burden, and complications in its reliance on exporting capital goods to China have undermined every pillar of its growth and export miracle. Its totemic car industry now faces significant competition from Chinese advances in battery power.

France has been more successful economically, but President Emmanuel Macron's reforms have split the French electorate into three difficult-to-reconcile blocs that have made the nation difficult to govern.

An upcoming German federal election could deliver a mandate for decisive change. Or could Germany's economic challenge be supplemented by a French-style ungovernable political impasse?

There are some brighter spots in Europe.

Spain could be the fastest-growing advanced economy in the world, rivalling even the US, on the back of a tourism boom, ample access to workers and green investment.

The crisis countries of the 2010s - Portugal, Ireland, Greece and Spain - are now the eurozone's outperformers. The so-called "PIGS" are flying.

But there is a wider canvas here. The structural underperformance of the European economy against a booming tech-fuelled and cheap energy-driven US requires some tough political decisions.

Much of this was rather brutally outlined in the report of the former Italian PM and ECB president Mario Draghi, which said the EU faces an "existential challenge" unless it vastly increases investment and reforms its industrial policy.

There is little sign that major European governments have the political capital to make these reforms. And all this occurs before the arrival of a US president who wants to act against jurisdictions which he argues "rip off" the US, including the European Union.

There are big stakes for Europe over the next few months.
Distance: 0.2265, Text: Behind the scenes of the Baltimore bridge collapse

An estimated 3,000 to 4,000 tonnes of debris from the Francis Scott Key Bridge blocked the Patapsco's shipping channel and pinned down the M/V Dali for months

In the dark, early morning hours of 26 March, US transportation secretary Pete Buttigieg's phone rang - and he immediately knew he had a problem. "If my phone rings in the middle of the night, it's not a good thing," he recalled. The Dali, a massive 948ft (289m) cargo ship had slammed into Baltimore's iconic Francis Scott Key Bridge, sending the 1.5-mile (2.4km) bridge into the cold waters of the state's Patapsco River. "It wasn't immediately clear what we were dealing with," Buttigieg told the BBC in an interview. "How many people had been impacted, and how much of the bridge had been destroyed." Six men, all members of a road crew working on the bridge, were killed in the incident, which left the Dali - still afloat - stuck under huge chunks of shredded metal and concrete. Another man was pulled from the icy water, seriously injured. A new documentary from BBC One - available on iPlayer on 5 December - reveals new details of the investigation into the crash, including a possible cause: a cable that shook loose, disrupting the ship's power supply and causing an initial blackout minutes before it struck the bridge. After that, investigators believe they discovered a lack of fuel pressure to the generators may have created power fluctuations that tripped breakers and caused the ship to go dark again, with no time to change course before slamming into the bridge.
• None Watch on iPlayer: Why Bridges Collapse: The Baltimore Disaster
• None Lost power, a mayday call and the crash that brought down a Baltimore bridge Previously unseen bodycam footage taken after the collapse shows first responders and officials struggling to come to grips with the enormity of what they faced in the confusing hours after the crash. "Key Bridge is down. It was last reported that there are at least several vehicles in the water," an officer can be heard saying. "And several people still unaccounted for."

Divers such as Robyn Bianchi were tasked with the difficult, dangerous work of surveying the underwater debris field in the Patapsco

In the aftermath of the collapse, an estimated 45,000 tonnes of debris blocked the Patapsco, a 700ft (213m) wide and 50ft (15m) deep and economically vital shipping channel. Recovering the bodies and removing the twisted, broken steel and concrete from the channel - plus moving the Dali - fell to a massive team including US Army, Navy and Coast Guard members, as well as Maryland authorities and specialist private firms. The woman who led the effort, Colonel Estee Pinchasin, the Army Corps of Engineers Baltimore district commander, said that she had "never seen anything of that magnitude before". "Everything [was] mangled up on top and around," she said. "You had these big, large spans that were just laying in the water. You see four-inch steel that's been bent... how can you even start to think about the force?" The dangerous work of searching the debris field and determining which pieces could be safely removed - and when - fell, in part, to teams of divers who inspected both the ship and the destruction beneath the surface. Never-before-seen footage obtained by the BBC shows the difficult conditions they faced: jagged and potentially dangerous pieces of rubble and muddy, brown water, which often meant they could not see more than a foot or two ahead of them. "There was no visibility. I kind of compared it to a metal jungle gym underwater," recalled Robyn Bianchi, the assistant salvage master for one of the firms involved, Donjon Marine. "You turn off all the lights in the room and try tell me all the pieces of where they connect to that metal jungle gym. Oh, and by the way, the jungle gym is completely twisted and looks nothing like it did when it was built to perfection."

Investigators believe the Dali lost power just moments before it struck the Key Bridge

A preliminary report from the National Transportation Safety Board, or NTSB, found that two electrical blackouts disabled equipment ahead of the incident, and noted that the ship lost power twice in the 10 hours leading up to the crash. In the aftermath, NTSB investigators were tasked with determining what happened. Eventually, they discovered a loose cable, which it is believed may have caused a power surge that tripped two breakers and disrupted electricity. As the crew struggled to restart the engine, a second blackout took place, likely as the result of a pump supplying generators with fuel shutting down and not restarting automatically. The erratic generators may have created additional power fluctuations that trip the ship's breakers - a common occurrence faced by seafarers, but which mostly takes place in open waters. Sal Mercogliano, a maritime historian and former merchant mariner, said that the silence the sailors must have heard in those moments is "the worst sound you ever hear" on a ship. "At that moment, you realise you don't have control of the ship anymore," he added. "It is a nightmare... you have a very short period of time in which to restore power." In the Dali's case, it was already too late. The time between the ship losing power and striking the bridge, it was later determined, was about four minutes. Despite the destruction and loss of life, officials are relieved that the incident was not worse. On the night of the collapse, police were able to stop traffic, with the last car clearing the bridge just 40 seconds before it fell. "Cars would have kept coming," Maryland Governor Wes Moore said. "By the time the people driving the vehicles realised the bridge was gone, it just would have been too late." "I can't tell you how many lives they saved," he added. "Because the answer is countless."
Distance: 0.2239, Text: Trump rings bell on record stock market - but will it last?

Donald Trump stood on Wall Street on Thursday, a euphoric crowd of America's business elite assembled before him, and hammered the bell to kick off morning stock market trading. The mood was celebratory. Not only had the president-elect just been named Time person of the year, but the stock market, already up strongly, has surged to new heights since his election. For Trump, whose public appearances have been relatively sparing since his election win last month, the visit was a reminder of the high value he places on market opinion. But whether history will look back on his appearance as the well-timed heralding of another economic boom - or a death knell - remains to be seen.

This video can not be played To play this video you need to enable JavaScript in your browser. Trump rings bell at NYSE to mark 'Person of the Year' title

Trump is entering office with a US economy that is, in the recent words of America's central bank boss, Jerome Powell, the envy of many other countries, with solid growth of 2.8%, unemployment near historic lows of 4.2% and that mysterious ingredient known as "productivity" on a tear. It has combined to push American stocks to record levels: The Dow Jones Industrial Average is on track to end the year up more than 17%. The S&P 500, a bigger index made up of America's 500 largest companies, has surged 28% since January, while the Nasdaq, where tech firms have a major presence, has rallied more than 40%. Investors are hoping for more, with the Trump administration expected to loosen regulations and green-light takeovers that might have come under harsh review during President Joe Biden's administration. At the stock exchange on Thursday, a who's who of the business world, including Goldman Sachs boss David Solomon and Target chief Brian Cornell, had assembled. The president-elect was greeted with sustained applause and whistles, which later gave way to chants of "USA! USA!" His visit marked a relatively rare appearance by a sitting or former president.

But analysts have warned that current highs may be difficult to sustain in the year ahead. Job creation is already slowing and price inflation is proving stubborn. Many of Trump's priorities - including slashing government spending, erecting stiff, broad-based trade barriers and embarking on mass deportations of migrants - if enacted, could pose further challenges to growth. Though debate is ongoing about how much the president actually will do, the proposals are generating uncertainty and have the potential for significant disruption, said Mark Zandi, chief economist at Moody's Analytics. "The totality of the policies, if implemented to the degree that the president has articulated, I think will be problematic for the economy," he said.

At the stock exchange on Thursday, Trump kept his comments focused on other parts of his agenda more palatable to markets: his promises to lower taxes from 21% to 15% for businesses manufacturing in America, cut regulation and speed up government approvals. "A nuclear power plant - I would say a week would be enough time. What do you think?" he said, to laughter. Similar policies, which Zandi said would be good for the corporate bottom line, if not necessarily the economy, boosted share prices significantly during Trump's first term. He cited those gains repeatedly as a sign of his administration's success, especially at the start. But the market retreated during trade wars with allies and with China - then nose-dived at the onset of the Covid-19 pandemic, before rapidly recovering. Ultimately, the S&P 500 gained more than 67% on his watch - a record bested by presidents Bill Clinton in the 1990s and Barack Obama during his first four years. Under Biden, the index has risen 59% so far. Whether Trump can repeat that performance is something he has declined to predict. The main US stock indexes closed lower on Thursday, with the S&P 500, Dow Jones Industrial Average and Nasdaq dipping by around 0.5%. Asked at the stock exchange what investors should be buying in anticipation of the future, Trump, somewhat uncharacteristically, demurred. "I don't want to get into a situation where they do and we have a dip or something", he said, then added, "Long term, this is going to be a country like no other."
Distance: 0.2119, Text: UK economy had zero growth between July and September

The UK economy had zero growth between July and September, revised official figures show. The revised data comes after a series of disappointing figures including inflation rising at its fastest pace for eight months and the economy unexpectedly shrinking in October. One of the UK's leading business groups, the CBI, said its latest company survey suggested "the economy is headed for the worst of all worlds". Chancellor Rachel Reeves said the challenge to fix the economy "after 15 years of neglect is huge", while shadow chancellor Mel Stride said Monday's figures showed "growth has tanked on Labour's watch".

The revised figure will be a blow to the government which has made boosting the economy its top priority. Labour has promised to deliver the highest sustained economic growth in the G7 group of the world's richest nations. Businesses have already warned that measures announced in October's Budget including a rise in employer national insurance contributions (NICs) and a higher minimum wage could push them into cutting jobs and raising prices. These Budget changes come into effect in April. But Stride said the latest figures from the three months before the October Budget signal "the warning lights are flashing" on how the economy will fare into 2025. Reeves said Labour's Budget would "deliver sustainable long-term growth, putting more money in people's pockets through increased investment and relentless reform." Liberal Democrat treasury spokesperson, Daisy Cooper called on the government to reverse the national insurance tax hike on small businesses and scrap the business rates system.

The CBI, which claims to represent 170,000 firms, said its survey based on responses of 899 firms between 25 November and 12 December, found private sector businesses across all industries expected a "steep decline in activity" in the first three months in 2025. "Expectations are now at their weakest in over two years," said Alpesh Paleja, the CBI's interim deputy chief economist. A separate survey by the British Retail Consortium, which represents UK retailers ranging from Marks and Spencer to Tesco, suggested a "January spending squeeze on the horizon" for consumers. It said "public confidence in the state of the economy took a nosedive" this month, according to its consumer sentiment survey. "With sales growth unable to keep pace, retailers will have no choice but to raise prices or cut costs – closing stores and freezing recruitment," said Helen Dickinson, chief executive of the BRC.

Mick Dore, general manager of the Alexander pub in Wimbledon, said its costs would rise in April due to the rise in national insurance contributions it makes for its 50 to 60 staff. "All of December we've been crazy busy with loads and loads of work parties," he told BBC Breakfast. "There are cost implications going forward and we have to have a good Christmas to insulate ourselves against that." But he said he was optimistic about business next year. "I generally think as a whole we are going to be ok. I've been around a long time now and each time they've told me the end is nigh it never has been."

Paul Dales, chief economist at Capital Economics, said there was "no doubt that some business and households put spending and investment on hold around the Budget". But he said it was "too soon to see any genuine effect of Labour policies". He said it would be a matter of time before the drag from higher interest rates gave way to a boost from lower rates but he expected to see an improvement in the second half of next year. Simon French, chief economist at Panmure Liberum said the revised figures were consistent "with a lot of other indicators we've seen since the July general election that've shown a loss of momentum in the economy". He said there was a question over whether this was a typical slowdown as seen after previous general elections which later picked up or "whether this is something more problematic teeing up a recession next year". The Bank of England voted to hold interest rates on Thursday, stating it thought the UK economy had performed worse than expected, with no growth at all between October and December.

The UK economy is measured by gross domestic product - a measure of all the economic activity of companies, governments and people in the country. The ONS puts out initial estimates on the UK's economic performance and revises them once it receives more data. On Monday it also revised down growth figures for April to June to 0.4% from 0.5%. It said the economy was weaker than initially estimated as bars and restaurants, legal firms and advertising firms performed less well. "Real household disposable income per head showed no growth," ONS director of economic statistics Liz McKeown said.

Have you been impacted by this? Get in touch
Distance: 0.2080, Text: UK economy shrinks for second month in a row

The UK economy shrank for the second month in a row in October as concerns about the Budget continued to weigh on confidence. Official figures showed a 0.1% drop, despite expectations that the economy would return to growth after a fall in September. The Office for National Statistics (ONS) said that activity had stalled or declined with pubs, restaurants and retail among sectors reporting "weak months". Chancellor Rachel Reeves said the figure was "disappointing", but added: "We have put in place policies to deliver long-term economic growth."

Shadow chancellor Mel Stride said: "This fall in growth shows the stark impact of the chancellor's decisions and continually talking down the economy." KPMG's chief economist Yael Selfin said that activity was "held back by uncertainty ahead of the Budget on 30 October" as businesses and consumers held back on spending. But some industries, such as real estate, law firms and accountancy, brought forward work before Reeves announced the Budget, the ONS said. Separately, a survey measuring consumer confidence in December found people were a little more positive about their personal finances for the year ahead. But the findings from market research firm GfK found that "views on the economy are unchanged from November which suggests people don't know where we are going". "In a nutshell, it's the continuing uncharitable view on the UK's general economic situation that's suppressing consumer confidence," said Neil Bellamy, consumer insights director at NIQ GfK. Shortly after becoming prime minister in July, Sir Keir Starmer warned that the Budget would be "painful" following 14 years of Tory government. He later denied that he was talking down the economy. The economy has grown just once over the past five months, ONS figures show.

Capital Economics said GDP was 0.1% lower than before Labour won the election in July. "That suggests it's not just the Budget that is holding the economy back," said Capital's chief UK economist Paul Dales. "Instead, the drag from higher interest rates may be lasting longer than we thought." The Bank of England has cut interest rates twice this year but, at 4.75%, they are still relatively high compared with recent years. The Bank will meet next week for the last interest rate decision of 2024, though it is not widely expected to reduce borrowing costs again until next year. Economists cautioned about placing too much emphasis on the reading for October. It is an initial estimate of economic growth by the ONS and could be revised. Over the three months to October, the economy expanded by 0.1%.

The manufacturing industry recorded the sharpest drop in activity in October, down 0.6%, followed by construction which fell by 0.4%. Meanwhile, the services sector, which makes up the bulk of the UK economy, stalled with zero growth. Rick Gaglio, owner of menswear shop Twisted Fabric in Hitchin, Hertfordshire, said "people are still being cautious", and added that prices are still comparatively high. "That's just down to inflation and customers are feeling those price increases," he said.

Shop owner Rick Gaglio said 2024 has been tough for small businesses

Mr Gaglio also said that retail sales were slow during the summer months due to wetter than usual weather. "It's been tough," he said. "2024 generally for small businesses has been very, very tough and we just want to hear more good news, not bad news." Sir Keir has said he wants the UK to secure the highest sustained economic growth of the G7 group of rich nations. Last week, he set out additional "milestones" to allow people to measure the government's progress. On the economy, he has pledged to increase real household disposable income per person. He also reiterated a promise to build 1.5 million homes in England.

Sign up for our Politics Essential newsletter to read top political analysis, gain insight from across the UK and stay up to speed with the big moments. It'll be delivered straight to your inbox every weekday.
Distance: 0.2054, Text: A winter storm swept across the eastern US and Canada, bringing heavy snow and freezing temperatures to the Great Lakes, Midwest, and Northern Plains. Officials in New York and Pennsylvania declared states of emergency as poor conditions disrupted travel across the region.
Distance: 0.2009, Text: What next for Bitcoin after bursting $100k barrier?

Bitcoin's price has blasted through the much-anticipated threshold of $100,000, raising questions about how much higher it could go - and whether it can shake off its notorious volatility. The world's largest cryptocurrency rose to around $103,400 shortly after 04:00 GMT on Thursday, before falling slightly. Dan Coatsworth, investment analyst at AJ Bell, described it as a "magic moment" for the cryptocurrency and said it had a "clear link" to Donald Trump's election victory. Trump took to social media to celebrate the milestone, posting "congratulations Bitcoiners" and "you're welcome!" The president-elect had previously pledged to make the US the "crypto capital" and "Bitcoin superpower" of the world, helping to push Bitcoin's price higher once he was elected president. It broke through the $100k barrier after Trump said he would nominate former Securities and Exchange Commission (SEC) commissioner Paul Atkins to run the Wall Street regulator. Mr Atkins is seen as being far more pro-cryptocurrency than the current head, Gary Gensler. "Clearly there is anticipation that the new administration is going to be somewhat more favourable to crypto than the old administration was," said Andrew O'Neill, digital assets expert at S&P Global. "So for the price of Bitcoin, I think that that's what's driven the trend so far [and it will] likely continue into the new year," he added. However, Bitcoin has a history of sharp falls as well as rapid rises - and some analysts have cautioned that is unlikely to change. “A lot of people have got rich from the cryptocurrency soaring in value this year, but this high-risk asset isn’t suitable for everyone," said Mr Coatsworth. "It’s volatile, unpredictable and is driven by speculation, none of which makes for a sleep-at-night investment."

During the US presidential election campaign, Trump sought to appeal to cryptocurrency investors with a promise to sack Gary Gensler - chair of the US financial regulator the Securities and Exchange Commission (SEC) - on "day one" of his presidency. Mr Gensler's approach to the cryptocurrency sector has been decidedly less friendly than Trump's. He told the BBC in September it was an industry “rife with fraud and hucksters and grifters”. Under his leadership, the SEC brought a record 46 crypto-related enforcement actions against firms in 2023. Mr Gensler said in November he would step down on 20 January - the day of Trump's inauguration. The choice of Paul Atkins to replace him at the helm of the SEC has been welcomed by crypto advocates. Mike Novogratz, founder and chief executive of US crypto firm Galaxy Digital said he hoped the "clearer regulatory path" would now accelerate the digital currency ecosystem's entry into "the financial mainstream."

Bitcoin has seen fewer drastic falls in value during 2024 than in previous years. In 2022 its price fell sharply below $16,000 after crypto exchange FTX collapsed into bankruptcy. A number of key events besides Trump's victory in the election have helped boost investor confidence that its value will keep going up. The SEC approved several spot Bitcoin exchange traded funds (ETFs) allowing giant investment firms like Blackrock, Fidelity and Grayscale to sell products based on the price of Bitcoin. Some of these products have seen billions of dollars in cash inflows. But its potential to suddenly plummet in value serves as a reminder that it is not like orthodox currencies - and investors have no protection or recourse if they lose money on Bitcoin investments. Carol Alexander, professor of finance at Sussex University, told BBC News that fear of missing out (FOMO) among younger people will see Bitcoin's price continue to rise. But she added that while this could spark a rise in other cryptocurrencies, many of the younger investors investing in meme coins are losing money. Kathleen Breitman, co-founder of another cryptocurrency - Tezos - also had a word of caution for those tempted to invest in Bitcoin. "These are markets that tend to move on momentum so you need to be extraordinarily cautious with it," she told the BBC.

From record-breaking NFT sales to pizza purchases using Bitcoin: Nine key crypto moments.
Distance: 0.1985, Text: Coffee drinkers may soon see their morning treat get more expensive, as the price of coffee on international commodity markets has hit its highest level on record. On Tuesday, the price for Arabica beans, which account for most global production, topped $3.44 a pound (0.45kg), having jumped more than 80% this year. The cost of Robusta beans, meanwhile, hit a fresh high in September. It comes as coffee traders expect crops to shrink after the world's two largest producers, Brazil and Vietnam, were hit by bad weather and the drink's popularity continues to grow. One expert told the BBC coffee brands were considering putting prices up in the new year.

While in recent years major coffee roasters have been able to absorb price hikes to keep customers happy and maintain market share, it looks like that's about to change, according to Vinh Nguyen, the chief executive of Tuan Loc Commodities. "Brands like JDE Peet (the owner of the Douwe Egberts brand), Nestlé and all that, have [previously] taken the hit from higher raw material prices to themselves," he said. "But right now they are almost at a tipping point. A lot of them are mulling a price increase in supermarkets in [the first quarter] of 2025." Italian coffee giant Lavazza said it had gone to great lengths to protect its market share and not pass on higher raw material costs to customers, but soaring coffee prices had eventually forced its hand. "Quality is paramount for us and has always been the cornerstone of our contract of trust with consumers," the company told BBC News. "For us, this means continuing to tackle very high costs. So we have been forced to adjust prices." At an event for investors in November, a top Nestlé executive said the coffee industry was facing "tough times", admitting his company would have to adjust its prices and pack sizes. "We are not immune to the price of coffee, far from it," said David Rennie, Nestlé's head of coffee brands.

The last record high for coffee was set in 1977 after unusual snowfall devastated plantations in Brazil. "Concerns over the 2025 crop in Brazil are the main driver," said Ole Hansen, head of commodity strategy at Saxo Bank. "The country experienced its worst drought in 70 years during August and September, followed by heavy rains in October, raising fears that the flowering crop could fail."

It is not just Brazilian coffee plantations, which mostly produce Arabica beans, that have been hurt by bad weather. Robusta supplies are also set to shrink after plantations in Vietnam, the largest producer of that variety, also faced both drought and heavy rainfall. Coffee is the world's second most traded commodity by volume, after crude oil, and its popularity is increasing. For example, consumption in China has more than doubled in the last decade. "Demand for the commodity remains high, while inventories held by producers and roasters are reported to be at low levels," said Fernanda Okada, a coffee pricing analyst at S&P Global Commodity Insights. "The upward trend in coffee prices is expected to persist for some time," she added.
Distance: 0.1985, Text: Interest rates held as Bank says economy doing worse

UK interest rates have been held at 4.75% after the Bank of England voted to keep borrowing costs unchanged. In an unexpected split, three members of the nine-member rate-setting committee wanted to cut rates to 4.5% to boost growth. The Bank said it thought the economy had performed worse than expected, with no growth at all between October and December. Rates are still expected to fall gradually next year, with the first cut possibly coming in February.

Commenting on the decision, Bank governor Andrew Bailey said: "We think a gradual approach to future interest rate cuts remains right but with the heightened uncertainty in the economy we can't commit to when or by how much we will cut rates in the coming year." Speaking later to reporters, Mr Bailey said he thought the path for interest rates was "downwards", but added: "The world is too uncertain." "We will come back in February at our next meeting and review it [interest rates] again." Figures this week showed that both inflation was higher than the Bank's target and wages were growing faster than expected. But the economy is struggling. Last month, the Bank forecast growth of 0.3% in the final three months of the year, but it now expects 0%.

The revisions will be a blow to Labour which has made boosting economic growth its top priority. It has promised to deliver the highest sustained economic growth in the G7 group of rich nations. In the minutes from the meeting, the Bank said there was uncertainty "around how the measures that had been announced in the autumn Budget were affecting growth". In the Budget, Chancellor Rachel Reeves announced £40bn worth of tax rises, the majority of which will come from an increase in National Insurance contributions from employers. By the time of the Bank's next decision in February, it will have more data on the impact of the Budget changes, as well as Donald Trump's incoming US trade tariff policies. Following the Bank's decision, Chancellor Rachel Reeves said: "We want to put more money in the pockets of working people, but that is only possible if inflation is stable and I fully back the Bank of England to achieve that." Liberal Democrat Treasury spokesperson Daisy Cooper MP said: "The new government needs to work much harder if it's going to turn the economy around any time soon. "That must start by scrapping the self-defeating jobs tax which promises to make the crisis in health and care even worse."

Ruth Gregory, deputy chief UK economist at Capital Economics, said the Bank's policymakers had appeared "to have been more open to cutting interest rates this month than we had expected". She said the comments suggested "that the Bank will cut rates quicker than investors expect".

Danny McGuire, who lives with his parents in Warrington, Cheshire, would like to get on the property ladder, but the deposit size and the lack of properties within his budget range has made it difficult for him. "The idea of owning your own home is preferable to renting for myself," said the 33-year-old, who works for a local council, but added "average house prices are incredibly high". Sarah Coles, head of personal finance at Hargreaves Lansdown, said for people looking to secure a fixed-rate mortgage deal, "the fact the market is pricing in fewer cuts between now and the end of 2025 means we're likely to see mortgage rates rise slightly from here". "Mortgage rates have fluctuated over the past month, as the market struggled to make its mind up about the path of future rate cuts. With so much uncertainty around, it can be a good idea for anyone with a looming remortgage to secure a rate now," she added.

Retrieval-Augmented Generation (RAG) with Couchbase and Langchain

Couchbase and LangChain can be seamlessly integrated to create RAG (Retrieval-Augmented Generation) chains, enhancing the process of generating contextually relevant responses. In this setup, Couchbase serves as the vector store, where embeddings of documents are stored. When a query is made, LangChain retrieves the most relevant documents from Couchbase by comparing the query’s embedding with the stored document embeddings. These documents, which provide contextual information, are then passed to a generative language model within LangChain.

The language model, equipped with the context from the retrieved documents, generates a response that is both informed and contextually accurate. This integration allows the RAG chain to leverage Couchbase’s efficient storage and retrieval capabilities, while LangChain handles the generation of responses based on the context provided by the retrieved documents. Together, they create a powerful system that can deliver highly relevant and accurate answers by combining the strengths of both retrieval and generation.

template = """You are a helpful bot. If you cannot answer based on the context provided, respond with a generic answer. Answer the question as truthfully as possible using the context below:
    {context}
    Question: {question}"""
prompt = ChatPromptTemplate.from_template(template)
rag_chain = (
    {"context": vector_store.as_retriever(), "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)
logging.info("Successfully created RAG chain")
2026-04-06 15:07:01,582 - INFO - Successfully created RAG chain
# Get responses
logging.disable(sys.maxsize) # Disable logging to prevent tqdm output
start_time = time.time()
rag_response = rag_chain.invoke(query)
rag_elapsed_time = time.time() - start_time

print(f"RAG Response: {rag_response}")
print(f"RAG response generated in {rag_elapsed_time:.2f} seconds")
RAG Response: The 1929 Great Depression was primarily caused by the stock market crash of October 1929, which was preceded by a speculative bubble where stock prices soared to unsustainable levels. Contributing factors included excessive stock market speculation, a lack of regulation in the financial sector, an unequal distribution of wealth, and economic policies that favored large businesses and banks. Additionally, widespread bank failures, reductions in consumer spending, and a restrictive monetary policy by the Federal Reserve exacerbated the economic downturn. The combination of these elements led to severe economic decline, high unemployment rates, and a prolonged period of hardship in the following years.
RAG response generated in 3.78 seconds

Using Couchbase as a caching mechanism

Couchbase can be effectively used as a caching mechanism for RAG (Retrieval-Augmented Generation) responses by storing and retrieving precomputed results for specific queries. This approach enhances the system's efficiency and speed, particularly when dealing with repeated or similar queries. When a query is first processed, the RAG chain retrieves relevant documents, generates a response using the language model, and then stores this response in Couchbase, with the query serving as the key.

For subsequent requests with the same query, the system checks Couchbase first. If a cached response is found, it is retrieved directly from Couchbase, bypassing the need to re-run the entire RAG process. This significantly reduces response time because the computationally expensive steps of document retrieval and response generation are skipped. Couchbase's role in this setup is to provide a fast and scalable storage solution for caching these responses, ensuring that frequently asked queries can be answered more quickly and efficiently.

try:
    queries = [
        "Why do heavier objects travel downhill faster?",
        "What is the capital of France?",
        "What caused the 1929 Great Depression?", # Repeated query
        "Why do heavier objects travel downhill faster?",  # Repeated query
    ]

    for i, query in enumerate(queries, 1):
        print(f"\nQuery {i}: {query}")
        start_time = time.time()

        def perform_rag_query():
            """Wrapper for RAG query execution with retry logic."""
            return rag_chain.invoke(query)

        response = retry_with_backoff(perform_rag_query)
        elapsed_time = time.time() - start_time
        print(f"Response: {response}")
        print(f"Time taken: {elapsed_time:.2f} seconds")

        # Add delay between queries to avoid rate limiting
        if i < len(queries):
            time.sleep(0.5)

except InternalServerFailureException as e:
    if "query request rejected" in str(e):
        raise ValueError("Search request was rate limited after multiple retries. Please wait and try again later.")
    raise ValueError(f"Error generating RAG response: {str(e)}")
except Exception as e:
    raise ValueError(f"Error generating RAG response: {str(e)}")
Query 1: Why do heavier objects travel downhill faster?
Response: Heavier objects do not necessarily travel downhill faster than lighter ones when air resistance is not a factor. In a vacuum, all objects fall at the same rate regardless of their weight, according to Galileo's principle of falling bodies. However, in the presence of air resistance, this can change: lighter objects may be more affected by air resistance, causing them to fall slower than heavier objects.
Time taken: 1.48 seconds

Query 2: What is the capital of France?
Response: The capital of France is Paris.
Time taken: 0.95 seconds

Query 3: What caused the 1929 Great Depression?
Response: The 1929 Great Depression was primarily caused by the stock market crash of October 1929, which was preceded by a speculative bubble where stock prices soared to unsustainable levels. Contributing factors included excessive stock market speculation, a lack of regulation in the financial sector, an unequal distribution of wealth, and economic policies that favored large businesses and banks. Additionally, widespread bank failures, reductions in consumer spending, and a restrictive monetary policy by the Federal Reserve exacerbated the economic downturn. The combination of these elements led to severe economic decline, high unemployment rates, and a prolonged period of hardship in the following years.
Time taken: 0.33 seconds

Query 4: Why do heavier objects travel downhill faster?
Response: Heavier objects do not necessarily travel downhill faster than lighter ones when air resistance is not a factor. In a vacuum, all objects fall at the same rate regardless of their weight, according to Galileo's principle of falling bodies. However, in the presence of air resistance, this can change: lighter objects may be more affected by air resistance, causing them to fall slower than heavier objects.
Time taken: 0.29 seconds

Conclusion

You've built a semantic search engine using Couchbase Search Vector Index with Azure OpenAI embeddings and LangChain. For the Hyperscale or Composite Vector Index alternative, see the query_based tutorial.


This tutorial is part of a Couchbase Learning Path:
Contents
Couchbase home page link

3155 Olsen Drive
Suite 150, San Jose
CA 95117, United States

  • Company
  • About
  • Leadership
  • News & Press
  • Careers
  • Events
  • Legal
  • Contact us
  • Support
  • Developer Portal
  • Documentation
  • Forums
  • Professional Services
  • Support Login
  • Support Policy
  • Training
  • Quicklinks
  • Blog
  • Downloads
  • Online Training
  • Resources
  • Why NoSQL
  • Pricing
  • Follow us
  • Social Media Link for TwitterTwitter
  • Social Media Link for LinkedInLinkedIn
  • Social Media Link for YoutubeYouTube
  • Social Media Link for FacebookFacebook
  • Social Media Link for GitHubGitHub
  • Social Media Link for Stack OverflowStack Overflow
  • Social Media Link for DiscordDiscord

© 2026 Couchbase, Inc. Couchbase and the Couchbase logo are registered trademarks of Couchbase, Inc. All third party trademarks (including logos and icons) referenced by Couchbase, Inc. remain the property of their respective owners.

  • Terms of Use
  • Privacy Policy
  • Cookie Policy
  • Support Policy
  • Do Not Sell My Personal Information
  • Marketing Preference Center
  • Trust Center