- Learn how to build a semantic search engine using Couchbase, OpenAI, and Voyage
- This tutorial demonstrates how to integrate Couchbase’s vector search capabilities with Voyage embeddings and use OpenAI as the language model.
- LangChainとCouchbaseを使用して、Retrieval-Augmented Generation(RAG)を実行する方法を理解します。.
このガイドでは、バックエンドデータベースとして Couchbase を使用して強力なセマンティック検索エンジンを構築する方法を説明します。 Voyage as the AI-powered embedding and OpenAI as the 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 from scratch.
このチュートリアルはJupyter Notebookとして利用可能です(.ipynb ファイル) を実行できます。オリジナルのノートブックにアクセスできます。 これ.
ノートブックファイルをダウンロードして実行するか、 Google Colab または、Python環境をセットアップしてシステム上で実行してください。.
Get Credentials for VoyageAI and OpenAI
Capellaで無料Tier運用クラスターを作成およびデプロイ
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.
詳細については、以下をご覧ください。 説明書.
Couchbase Capellaの設定
「couchbase」を以下のように実行すると、 Capella, 以下のような前提条件を満たしている必要があります。.
- Create the データベース認証情報 アプリケーションで使用される必要なバケット(読み取りおよび書き込み)にアクセスします。.
- アクセス許可を許可する アプリケーションが実行されている IP からのクラスタへの接続です。.
当社のセマンティック検索エンジンを構築するには、強力なツールセットが必要です。インストールしたライブラリは、データベースへの接続から複雑な機械学習タスクの実行まで、あらゆる作業に対応します。.
%pip install --quiet datasets==3.5.0 langchain-couchbase==0.3.0 langchain-voyageai==0.1.4 langchain-openai==0.3.13
注:更新されたパッケージを使用するには、カーネルを再起動する必要がある場合があります。.
This block imports all the required libraries and modules used in the notebook. These include libraries for environment management, data handling, natural language processing, interaction with Couchbase, and embeddings generation. Each library serves a specific function, such as managing environment variables, handling datasets, or interacting with the Couchbase database.
import json
import logging
import os
import time
import getpass
from datetime import timedelta
from dotenv import load_dotenv
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.exceptions import (CouchbaseException,
InternalServerFailureException,
QueryIndexAlreadyExistsException,ServiceUnavailableException)
from couchbase.management.buckets import CreateBucketSettings
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 ChatOpenAI
from langchain_voyageai import VoyageAIEmbeddings
/Users/aayush.tyagi/Documents/AI/vector-search-cookbook/.venv/lib/python3.12/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
ログはスクリプトの進行状況を追跡し、エラーや警告をキャプチャするように設定されています。これはデバッグと実行フローの理解にとって極めて重要です。ログ出力にはタイムスタンプ、ログレベル(例:INFO、ERROR)、スクリプト内で何が起こっているかを説明するメッセージが含まれます。.
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s',force=True)
# Set the logging from the httpx library to CRITICAL to avoid excessive logging
logging.getLogger('httpx').setLevel(logging.CRITICAL)
In this section, we prompt the user to input essential configuration settings needed for integrating Couchbase with Cohere’s API. 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.
スクリプトはまた、必要なすべての入力が提供されていることを検証し、重要な情報が欠落している場合はエラーを発生させます。このアプローチにより、機密情報をハードコーディングすることなく統合が安全かつ正しく構成されるようになります。これにより、コードの全体的なセキュリティとメンテナンス性が向上します。.
load_dotenv()
VOYAGE_API_KEY = os.getenv('VOYAGE_API_KEY') or getpass.getpass('Enter your VoyageAI API key: ')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') or getpass.getpass('Enter your OpenAI API key: ')
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_voyage): ') or 'vector_search_voyage'
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: voyage): ') or 'voyage'
CACHE_COLLECTION = os.getenv('CACHE_COLLECTION') or input('Enter your cache collection name (default: cache): ') or 'cache'
# Verifying that essential environment variables are set
if not VOYAGE_API_KEY:
raise ValueError("VOYAGE_API_KEY is required.")
if not OPENAI_API_KEY:
raise ValueError("OPENAI_API_KEY is required.")
The script attempts to establish a connection to the Couchbase database using the credentials retrieved from the environment variables. Couchbase is a NoSQL database known for its flexibility, scalability, and support for various data models, including document-based storage. The connection is authenticated using a username and password, and the script waits until the connection is fully established before proceeding.
試す:
auth = PasswordAuthenticator(CB_USERNAME, CB_PASSWORD)
options = ClusterOptions(auth)
cluster = クラスタ(CB_HOST, オプション)
cluster.wait_until_ready(timedelta(seconds=5))
logging.info("Couchbaseに接続されました")
例外として e を除きます:
ConnectionErrorを発生させる (f"Couchbaseへの接続に失敗しました: {str(e}")")
2025-02-24 01:02:11,426 - INFO - Successfully connected to Couchbase
Couchbaseでのコレクションのセットアップ
setup_collection() 関数は、Couchbase で階層的なデータ構造の作成と設定を処理します:
-
バケットの作成:
- 指定されたバケットが存在するかどうかをチェックし、存在しない場合は作成します。
- RAM割り当て(1024MB)、レプリケーション(無効)などのバケットプロパティを設定します
- 注:Capellaではバケットを作成できません。
-
スコープ管理:
- リクエストされたスコープがバケット内に存在するかどうかを確認します。
- 必要に応じて新しいスコープを作成します(デフォルトの「_default」スコープの場合は除きます)。
-
コレクションのセットアップ:
- 範囲内でコレクションが存在するかどうかをチェックする
- 存在しない場合コレクションを作成する
- コレクションの準備が完了するまで2秒待つ
追加のタスク:
- Creates primary index on collection for query performance
- 既存のドキュメントをすべてクリアして、状態をクリアにします
- 包括的なエラー処理とログの記録を実装
この関数は設定するために 2 回呼び出されます:
- ベクトル埋め込み用のメインコレクション
- 結果の保存用のキャッシュコレクション
def setup_collection(cluster, bucket_name, scope_name, collection_name):
try:
# Check if bucket exists, create if it doesn't
try:
bucket = cluster.bucket(bucket_name)
logging.info(f"Bucket '{bucket_name}' exists.")
except Exception as e:
logging.info(f"Bucket '{bucket_name}' does not exist. Creating it...")
bucket_settings = CreateBucketSettings(
name=bucket_name,
bucket_type='couchbase',
ram_quota_mb=1024,
flush_enabled=True,
num_replicas=0
)
cluster.buckets().create_bucket(bucket_settings)
time.sleep(2) # Wait for bucket creation to complete and become available
bucket = cluster.bucket(bucket_name)
logging.info(f"Bucket '{bucket_name}' created successfully.")
bucket_manager = bucket.collections()
# Check if scope exists, create if it doesn't
scopes = bucket_manager.get_all_scopes()
scope_exists = any(scope.name == scope_name for scope in scopes)
if not scope_exists and scope_name != "_default":
logging.info(f"Scope '{scope_name}' does not exist. Creating it...")
bucket_manager.create_scope(scope_name)
logging.info(f"Scope '{scope_name}' created successfully.")
# 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.")
# Wait for collection to be ready
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)
2025-02-24 01:02:12,840 - INFO - Bucket 'vector-search-testing' exists.
2025-02-24 01:02:15,328 - INFO - Collection 'voyage' already exists. Skipping creation.
2025-02-24 01:02:18,539 - INFO - Primary index present or created successfully.
2025-02-24 01:02:21,013 - INFO - All documents cleared from the collection.
2025-02-24 01:02:21,014 - INFO - Bucket 'vector-search-testing' exists.
2025-02-24 01:02:23,506 - INFO - Collection 'cache' already exists. Skipping creation.
2025-02-24 01:02:26,647 - INFO - Primary index present or created successfully.
2025-02-24 01:02:26,913 - INFO - All documents cleared from the collection.
<couchbase.collection.Collection at 0x7ac2ee5d6c30>
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.
This Voyage vector search index configuration requires specific default settings to function properly. This tutorial uses the bucket named vector-search-testing with the scope shared and collection voyage. The configuration is set up for vectors with exactly 1536 dimensions, using dot product similarity and optimized for recall. If you want to use a different bucket, scope, or collection, you will need to modify the index configuration accordingly.
For more information on creating a vector search index, please follow the 説明書.
# 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 = '/path_to_your_index_file/voyage_index.json' # Local setup: specify your file path here
# # Version for Google Colab
# def load_index_definition_colab():
# 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)
# return index_definition
# except Exception as e:
# raise ValueError(f"Error loading index definition from {index_definition_path}: {str(e)}")
# Version for Local Environment
def load_index_definition_local(index_definition_path):
try:
with open(index_definition_path, 'r') as file:
index_definition = json.load(file)
return index_definition
except Exception as e:
raise ValueError(f"Error loading index definition from {index_definition_path}: {str(e)}")
# Usage
# Uncomment the appropriate line based on your environment
# index_definition = load_index_definition_colab()
index_definition = load_index_definition_local('voyage_index.json')
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 ServiceUnavailableException:
raise RuntimeError("Search service is not available. Please ensure the Search service is enabled in your Couchbase cluster.")
except InternalServerFailureException as e:
logging.error(f"Internal server error: {str(e)}")
raise
2025-04-21 13:43:33,489 - INFO - Index 'vector_search_voyage' found
2025-04-21 13:43:33,505 - INFO - Index 'vector_search_voyage' already exists. Skipping creation/update.
Embeddings are created using the Voyage API. Embeddings are vectors (arrays of numbers) that represent the meaning of text in a high-dimensional space. These embeddings are crucial for tasks like semantic search, where the goal is to find text that is semantically similar to a query. The script uses a pre-trained model provided by Voyage to generate embeddings for the text in the dataset.
try:
embeddings = VoyageAIEmbeddings(voyage_api_key=VOYAGE_API_KEY,model="voyage-large-2")
logging.info("Successfully created VoyageAIEmbeddings")
except Exception as e:
raise ValueError(f"Error creating VoyageAIEmbeddings: {str(e)}")
2025-02-24 01:02:29,797 - INFO - Successfully created VoyageAIEmbeddings
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)}")
2025-02-24 01:02:34,123 - INFO - Successfully created vector store
検索エンジンを構築するには、検索するためのデータが必要です。我々は RealTimeData の BBC News データセットを使用しています。このデータセットには現実世界のニュース記事が含まれています。このデータセットには BBC が取り扱う様々なトピックや時代を網羅したニュース記事が含まれています。データセットのロードは重要なステップです。なぜなら、このデータセットは検索エンジンの作業に不可欠な素材を提供してくれるからです。ニュース記事の質の高さと多様性は、検索エンジンのテストと改良に最適で、実世界のニュースコンテンツを効果的に処理できることを保証します。.
BBC Newsのデータセットを使用することで、本物のニュース記事を扱うことができ、効果的に関連するニュースコンテンツを処理して取得できる検索エンジンを構築およびテストすることができます。このデータセットは、Hugging Faceのデータセットライブラリを使用して読み込みられ、具体的には「RealTimeData/bbc_news_alltime」データセットを「2024-12」バージョンで読み込みます。.
試す:
news_dataset = load_dataset(
「RealTimeData/bbc_news_alltime」、分割「train」で2024年12月20日時点のデータ
)
print(f"BBCニュースデータセットを読み込みました。長さ {len(news_dataset)} の行があります")
logging.info(f"BBCニュースデータセットを {len(news_dataset)} 行で正常に読み込んだ")
例外として e を除きます:
ValueErrorを発生させる(「BBCニュースデータセットの読み込みエラー: {str(e)}」)
2025-02-24 01:02:39,306 - INFO - Successfully loaded the BBC News dataset with 2687 rows.
Loaded the BBC News dataset with 2687 rows
データの整理
我々は、ニュース記事の内容をRAGシステムに使用します。.
このデータセットには重複するレコードがいくつか含まれています。この重複したレコードを削除することで、RAGシステムの検索段階で重複した結果を回避します。.
news_articles = news_dataset["content"]
unique_articles = set()
ニュース記事の記事の項について:
記事の場合:
unique_articles.add(article)
unique_news_articles = list(unique_articles)
print(f"データベースには {len(unique_news_articles)} の記事があります")
弊社データベースには1749件のユニークな記事があります。.
ベクトルストアへのデータの保存
大量の記事を効率的に処理するため、一度に複数の記事を処理します。このバッチ処理アプローチにより、メモリ使用量を管理し、インジェストプロセスをより適切に制御できます。.
まず、トークン制限の問題を避けるために、文字数が50,000文字を超える記事はすべて除外します。その後、ベクターストアのadd_textsメソッドを使用して、フィルタリングされた記事をベクターデータベースに追加します。batch_sizeパラメータは、各イテレーションで処理される記事数を制御します。.
このアプローチにはいくつかの利点があります:
- メモリ効率:より小さなバッチでの処理によりメモリの過負荷を防ぐ
- Error Handling: If an error occurs, only the current batch is affected
- 進捗の追跡:取り込みの進捗をより簡単に監視および追跡できます
- リソース管理:CPUおよびネットワークリソースの利用率のより良い制御
We use a conservative batch size of 25 to ensure reliable operation. The optimal batch size depends on many factors including:
- 挿入されているドキュメントのサイズ
- 利用可能なシステムリソース
- ネットワークの状況
- 同時作業の負荷
調整前に、具体的な作業負荷に基づいてパフォーマンスを測定することを検討してください。.
batch_size = 25
# Automatic Batch Processing
articles = [article for article in unique_news_articles if article and len(article) <= 50000]
try:
vector_store.add_texts(
texts=articles,
batch_size=batch_size
)
logging.info("Document ingestion completed successfully.")
except Exception as e:
raise ValueError(f"Failed to save documents to vector store: {str(e)}")
2025-02-24 01:39:56,883 - INFO - Document ingestion completed successfully.
A cache is set up using Couchbase to store intermediate results and frequently accessed data. Caching is important for improving performance, as it reduces the need to repeatedly calculate or retrieve the same data. The cache is linked to a specific collection in Couchbase, and it is used later in the script to store the results of language model queries.
試す:
cache = CouchbaseCache(
cluster=cluster,
bucket_name=CB_BUCKET_NAME、
scope_name=SCOPE_NAME,
collection_name=CACHE_COLLECTION、
)
logging.info("キャッシュが正常に作成されました")
set_llm_cache(cache)
例外として e を除きます:
ValueErrorを発生させる(f"キャッシュの作成に失敗しました: {str(e)}")
2025-02-24 01:39:59,753 - INFO - Successfully created cache
The script initializes a Cohere language model (LLM) that will be used for generating responses to queries. LLMs are powerful tools for natural language understanding and generation, capable of producing human-like text based on input prompts. The model is configured with specific parameters, such as the temperature, which controls the randomness of its outputs.
try:
llm = ChatOpenAI(
openai_api_key=OPENAI_API_KEY,
model="gpt-4o-2024-08-06",
temperature=0
)
logging.info(f"Successfully created OpenAI LLM with model gpt-4o-2024-08-06")
except Exception as e:
raise ValueError(f"Error creating OpenAI LLM: {str(e)}")
2025-02-24 01:39:59,846 - INFO - Successfully created OpenAI LLM with model gpt-4o-2024-08-06
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.
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 was manchester city manager pep guardiola's reaction to the team's current form?"
try:
# Perform the semantic search
start_time = time.time()
search_results = vector_store.similarity_search_with_score(query, k=10)
search_elapsed_time = time.time() - start_time
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):")
print("-" * 80) # Add separator line
for doc, score in search_results:
print(f"Score: {score:.4f}, Text: {doc.page_content}")
print("-" * 80) # Add separator between results
except CouchbaseException as e:
raise RuntimeError(f"Error performing semantic search: {str(e)}")
except Exception as e:
raise RuntimeError(f"Unexpected error: {str(e)}")
2025-02-24 01:40:02,318 - INFO - Semantic search completed in 2.46 seconds
Semantic Search Results (completed in 2.46 seconds):
--------------------------------------------------------------------------------
Score: 0.7965, Text: 'Self-doubt, errors & big changes' - inside the crisis at Man City
Pep Guardiola has not been through a moment like this in his managerial career. Manchester City have lost nine matches in their past 12 - as many defeats as they had suffered in their previous 106 fixtures. At the end of October, City were still unbeaten at the top of the Premier League and favourites to win a fifth successive title. Now they are seventh, 12 points behind leaders Liverpool having played a game more. It has been an incredible fall from grace and left people trying to work out what has happened - and whether Guardiola can make it right. After discussing the situation with those who know him best, I have taken a closer look at the future - both short and long term - and how the current crisis at Man City is going to be solved.
Pep Guardiola's Man City have lost nine of their past 12 matches
Guardiola has also been giving it a lot of thought. He has not been sleeping very well, as he has said, and has not been himself at times when talking to the media. He has been talking to a lot of people about what is going on as he tries to work out the reasons for City's demise. Some reasons he knows, others he still doesn't. What people perhaps do not realise is Guardiola hugely doubts himself and always has. He will be thinking "I'm not going to be able to get us out of this" and needs the support of people close to him to push away those insecurities - and he has that. He is protected by his people who are very aware, like he is, that there are a lot of people that want City to fail. It has been a turbulent time for Guardiola. Remember those marks he had on his head after the 3-3 draw with Feyenoord in the Champions League? He always scratches his head, it is a gesture of nervousness. Normally nothing happens but on that day one of his nails was far too sharp so, after talking to the players in the changing room where he scratched his head because of his usual agitated gesturing, he went to the news conference. His right-hand man Manel Estiarte sent him photos in a message saying "what have you got on your head?", but by the time Guardiola returned to the coaching room there was hardly anything there again. He started that day with a cover on his nose after the same thing happened at the training ground the day before. Guardiola was having a footballing debate with Kyle Walker about positional stuff and marked his nose with that same nail. There was also that remarkable news conference after the Manchester derby when he said "I don't know what to do". That is partly true and partly not true. Ignore the fact Guardiola suggested he was "not good enough". He actually meant he was not good enough to resolve the situation with the group of players he has available and with all the other current difficulties. There are obviously logical explanations for the crisis and the first one has been talked about many times - the absence of injured midfielder Rodri. You know the game Jenga? When you take the wrong piece out, the whole tower collapses. That is what has happened here. It is normal for teams to have an over-reliance on one player if he is the best in the world in his position. And you cannot calculate the consequences of an injury that rules someone like Rodri out for the season. City are a team, like many modern ones, in which the holding midfielder is a key element to the construction. So, when you take Rodri out, it is difficult to hold it together. There were Plan Bs - John Stones, Manuel Akanji, even Nathan Ake - but injuries struck. The big injury list has been out of the ordinary and the busy calendar has also played a part in compounding the issues. However, one factor even Guardiola cannot explain is the big uncharacteristic errors in almost every game from international players. Why did Matheus Nunes make that challenge to give away the penalty against Manchester United? Jack Grealish is sent on at the end to keep the ball and cannot do that. There are errors from Walker and other defenders. These are some of the best players in the world. Of course the players' mindset is important, and confidence is diminishing. Wrong decisions get taken so there is almost panic on the pitch instead of calm. There are also players badly out of form who are having to play because of injuries. Walker is now unable to hide behind his pace, I'm not sure Kevin de Bruyne is ever getting back to the level he used to be at, Bernardo Silva and Ilkay Gundogan do not have time to rest, Grealish is not playing at his best. Some of these players were only meant to be playing one game a week but, because of injuries, have played 12 games in 40 days. It all has a domino effect. One consequence is that Erling Haaland isn't getting the service to score. But the Norwegian still remains City's top-scorer with 13. Defender Josko Gvardiol is next on the list with just four. The way their form has been analysed inside the City camp is there have only been three games where they deserved to lose (Liverpool, Bournemouth and Aston Villa). But of course it is time to change the dynamic.
Guardiola has never protected his players so much. He has not criticised them and is not going to do so. They have won everything with him. Instead of doing more with them, he has tried doing less. He has sometimes given them more days off to clear their heads, so they can reset - two days this week for instance. Perhaps the time to change a team is when you are winning, but no-one was suggesting Man City were about to collapse when they were top and unbeaten after nine league games. Some people have asked how bad it has to get before City make a decision on Guardiola. The answer is that there is no decision to be made. Maybe if this was Real Madrid, Barcelona or Juventus, the pressure from outside would be massive and the argument would be made that Guardiola has to go. At City he has won the lot, so how can anyone say he is failing? Yes, this is a crisis. But given all their problems, City's renewed target is finishing in the top four. That is what is in all their heads now. The idea is to recover their essence by improving defensive concepts that are not there and re-establishing the intensity they are known for. Guardiola is planning to use the next two years of his contract, which is expected to be his last as a club manager, to prepare a new Manchester City. When he was at the end of his four years at Barcelona, he asked two managers what to do when you feel people are not responding to your instructions. Do you go or do the players go? Sir Alex Ferguson and Rafael Benitez both told him that the players need to go. Guardiola did not listen because of his emotional attachment to his players back then and he decided to leave the Camp Nou because he felt the cycle was over. He will still protect his players now but there is not the same emotional attachment - so it is the players who are going to leave this time. It is likely City will look to replace five or six regular starters. Guardiola knows it is the end of an era and the start of a new one. Changes will not be immediate and the majority of the work will be done in the summer. But they are open to any opportunities in January - and a holding midfielder is one thing they need. In the summer City might want to get Spain's Martin Zubimendi from Real Sociedad and they know 60m euros (£50m) will get him. He said no to Liverpool last summer even though everything was agreed, but he now wants to move on and the Premier League is the target. Even if they do not get Zubimendi, that is the calibre of footballer they are after. A new Manchester City is on its way - with changes driven by Guardiola, incoming sporting director Hugo Viana and the football department.
--------------------------------------------------------------------------------
Score: 0.7948, Text: Manchester City boss Pep Guardiola has won 18 trophies since he arrived at the club in 2016
Manchester City boss Pep Guardiola says he is "fine" despite admitting his sleep and diet are being affected by the worst run of results in his entire managerial career. In an interview with former Italy international Luca Toni for Amazon Prime Sport before Wednesday's Champions League defeat by Juventus, Guardiola touched on the personal impact City's sudden downturn in form has had. Guardiola said his state of mind was "ugly", that his sleep was "worse" and he was eating lighter as his digestion had suffered. City go into Sunday's derby against Manchester United at Etihad Stadium having won just one of their past 10 games. The Juventus loss means there is a chance they may not even secure a play-off spot in the Champions League. Asked to elaborate on his comments to Toni, Guardiola said: "I'm fine. "In our jobs we always want to do our best or the best as possible. When that doesn't happen you are more uncomfortable than when the situation is going well, always that happened. "In good moments I am happier but when I get to the next game I am still concerned about what I have to do. There is no human being that makes an activity and it doesn't matter how they do." Guardiola said City have to defend better and "avoid making mistakes at both ends". To emphasise his point, Guardiola referred back to the third game of City's current run, against a Sporting side managed by Ruben Amorim, who will be in the United dugout at the weekend. City dominated the first half in Lisbon, led thanks to Phil Foden's early effort and looked to be cruising. Instead, they conceded three times in 11 minutes either side of half-time as Sporting eventually ran out 4-1 winners. "I would like to play the game like we played in Lisbon on Sunday, believe me," said Guardiola, who is facing the prospect of only having three fit defenders for the derby as Nathan Ake and Manuel Akanji try to overcome injury concerns. If there is solace for City, it comes from the knowledge United are not exactly flying. Their comeback Europa League victory against Viktoria Plzen on Thursday was their third win of Amorim's short reign so far but only one of those successes has come in the Premier League, where United have lost their past two games against Arsenal and Nottingham Forest. Nevertheless, Guardiola can see improvements already on the red side of the city. "It's already there," he said. "You see all the patterns, the movements, the runners and the pace. He will do a good job at United, I'm pretty sure of that."
Guardiola says skipper Kyle Walker has been offered support by the club after the City defender highlighted the racial abuse he had received on social media in the wake of the Juventus trip. "It's unacceptable," he said. "Not because it's Kyle - for any human being. "Unfortunately it happens many times in the real world. It is not necessary to say he has the support of the entire club. It is completely unacceptable and we give our support to him."
--------------------------------------------------------------------------------
Score: 0.7755, Text: Pep Guardiola has said Manchester City will be his final managerial job in club football before he "maybe" coaches a national team.
The former Barcelona and Bayern Munich boss has won 15 major trophies since taking charge of City in 2016.
The 53-year-old Spaniard was approached in the summer about the possibility of becoming England manager, but last month signed a two-year contract extension with City until 2027.
... (output truncated for brevity)
Couchbase と LangChain はシームレスに統合され、RAG (Retrieval-Augmented Generation) チェーンを作成できます。これにより、文脈的に関連したレスポンスの生成プロセスが強化されます。この設定では、Couchbase がベクターストアとして機能し、ドキュメントのエンベディングが保存されます。クエリが実行されると、LangChain はクエリのエンベディングと保存されたドキュメントエンベディングを比較して、最も関連性の高いドキュメントをCouchbaseから取得します。これらの文脈情報を提供するドキュメントは、LangChain 内の生成言語モデルに渡されます。.
取得したドキュメントのコンテキストを搭載した言語モデルは、情報に基づいた正確なレスポンスを生成します。この統合により、RAG チェーンはCouchbaseの効率的なストレージと検索機能を利用できます。一方、LangChainは取得したドキュメントのコンテキストに基づいてレスポンスの生成を行います。これらを組み合わせることで、検索と生成の両方の強みを組み合わせ、非常に関連性があり正確な回答を提供できる強力なシステムが構築されます。.
try:
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")
except Exception as e:
raise ValueError(f"Error creating LLM chains: {str(e)}")
2025-02-24 01:40:02,392 - INFO - Successfully created RAG chain
try:
# Get RAG response
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")
except InternalServerFailureException as e:
if "query request rejected" in str(e):
print("Error: Search request was rejected due to rate limiting. Please try again later.")
else:
print(f"Internal server error occurred: {str(e)}")
except Exception as e:
print(f"Unexpected error occurred: {str(e)}")
RAG Response: Pep Guardiola has expressed concern about Manchester City's current form, describing his state of mind as "ugly" and admitting that his sleep and diet have been affected. He acknowledged the team's poor run of results and emphasized the need to defend better and avoid mistakes. Despite the challenges, Guardiola remains calm and focused on finding solutions, expressing trust in his players and a determination to return to form. He has not criticized his players publicly and has instead offered them support, giving them more days off to reset. Guardiola is planning for the future, acknowledging the end of an era and the need for changes in the team.
RAG response generated in 7.56 seconds
Couchbaseは、RAG(Retrieval-Augmented Generation)のレスポンスに対するキャッシュメカニズムとして、特定のクエリに対する事前計算済みの結果を保存および取得することで効果的に使用できます。このアプローチは、特に繰り返しまたは類似したクエリに対応する場合、システムの効率性と速度を向上させます。クエリが最初に処理されるとき、RAG チェーンは関連するドキュメントを検索し、言語モデルを使用してレスポンスを生成し、そのレスポンスをCouchbaseに保存します。このクエリはキーとして機能します。.
同じクエリの次回のリクエストでは、システムがまずCouchbaseをチェックします。キャッシュされたレスポンスが見つかった場合は、RAGプロセス全体を再実行する必要がなくなって、直接Couchbaseから取得されます。これによりレスポンスタイムが大幅に短縮されます。これは、ドキュメントの検索とレスポンス生成という計算に時間がかかるステップを省くためです。このセットアップにおけるCouchbaseの役割は、これらのレスポンスをキャッシュするための高速でスケーラブルなストレージソリューションを提供することであり、頻繁に寄せられる問い合わせをより迅速かつ効率的に回答できるよう保証します。.
try:
queries = [
"What happened in the match between Fullham and Liverpool?",
"What was manchester city manager pep guardiola's reaction to the team's current form?", # Repeated query
"What happened in the match between Fullham and Liverpool?", # Repeated query
]
for i, query in enumerate(queries, 1):
print(f"nQuery {i}: {query}")
start_time = time.time()
response = rag_chain.invoke(query)
elapsed_time = time.time() - start_time
print(f"Response: {response}")
print(f"Time taken: {elapsed_time:.2f} seconds")
except InternalServerFailureException as e:
if "query request rejected" in str(e):
print("Error: Search request was rejected due to rate limiting. Please try again later.")
else:
print(f"Internal server error occurred: {str(e)}")
except Exception as e:
print(f"Unexpected error occurred: {str(e)}")
Query 1: What happened in the match between Fullham and Liverpool?
Response: In the match between Fulham and Liverpool, the game ended in a 2-2 draw. Liverpool played the majority of the match with ten men after Andy Robertson received a red card in the 17th minute. Despite being a player down, Liverpool managed to equalize twice, with Diogo Jota scoring an 86th-minute equalizer. The performance was praised as impressive, with Liverpool maintaining more than 60% possession and leading in several attacking metrics.
Time taken: 6.54 seconds
Query 2: What was manchester city manager pep guardiola's reaction to the team's current form?
Response: Pep Guardiola has expressed concern about Manchester City's current form, describing his state of mind as "ugly" and admitting that his sleep and diet have been affected. He acknowledged the team's poor run of results and emphasized the need to defend better and avoid mistakes. Despite the challenges, Guardiola remains calm and focused on finding solutions, expressing trust in his players and a determination to return to form. He has not criticized his players publicly and has instead offered them support, giving them more days off to reset. Guardiola is planning for the future, acknowledging the end of an era and the need for changes in the team.
Time taken: 1.98 seconds
Query 3: What happened in the match between Fullham and Liverpool?
Response: In the match between Fulham and Liverpool, the game ended in a 2-2 draw. Liverpool played the majority of the match with ten men after Andy Robertson received a red card in the 17th minute. Despite being a player down, Liverpool managed to equalize twice, with Diogo Jota scoring an 86th-minute equalizer. The performance was praised as impressive, with Liverpool maintaining more than 60% possession and leading in several attacking metrics.
Time taken: 1.85 seconds
結論
By following these steps, you’ll have a fully functional semantic search engine that leverages the strengths of Couchbase and Voyage. This guide is designed not just to show you how to build the system, but also to explain why each step is necessary, giving you a deeper understanding of the principles behind semantic search and how to implement it effectively. Whether you’re a newcomer to software development or an experienced developer looking to expand your skills, this guide will provide you with the knowledge and tools you need to create a powerful, AI-driven search engine.