メインコンテンツまでスキップ
バージョン: V1.2.0

SDKを使用したベクトル検索のクイックスタート

この例では、Linux環境を例に、pyseekdb(OceanBaseが提供するPythonクライアント)またはseekdb-js(OceanBaseが提供するNode.jsクライアント)の組み込みseekdbを使用して、ベクトル検索を手軽に試す方法を説明します。

ヒント

Linuxシステム以外にも、macOSおよびWindowsシステムでpyseekdbとseekdb-jsの使用をサポートしています。ただし、現在Windowsシステムではサーバーモードのseekdbのみがサポートされています。macOSおよびWindowsシステムでの使用方法については、pyseekdb クイックスタートおよびseekdb-js クイックスタートを参照してください。

この例では、以下の操作を実行します:

  1. pyseekdb / seekdb-jsと組み込みseekdbをデプロイします。
  2. seekdbに接続し、データベースを作成します。
  3. データベースに接続し、Embedding Functionsを含むcollectionを作成します。
  4. documentsを使用してデータを追加します(vectorsは自動生成されます)。
  5. ベクトル検索を実行します(vectorsは自動生成されます)およびクエリ結果を出力します。
  6. 環境をクリーンアップします。

背景​

pyseekdb​

pyseekdbは、OceanBaseが提供するPythonクライアントです。一連のAPIをベースに実装されており、3種類のデータベース接続モードを提供し、組み込みモードのseekdb、サーバーモードのseekdb、およびOceanBaseデータベースへの接続をサポートしています。

このクライアントをインストールすると、組み込みモードのseekdbも同時にインストールされるため、組み込み型のseekdbに直接接続してデータベースの作成などの操作を実行できます。または、既にデプロイ済みのサーバーモードのseekdbやOceanBaseデータベースにリモート接続することも選択できます。

seekdb-js​

seekdb-jsは、OceanBaseが提供するNode.jsクライアントです。一連のAPIをベースに実装されており、3種類のデータベース接続モードを提供し、組み込みモードのseekdb、サーバーモードのseekdb、およびOceanBaseデータベースへの接続をサポートしています。

このクライアントをインストールすると、組み込みモードのseekdbも同時にインストールされるため、組み込み型のseekdbに直接接続してデータベースの作成などの操作を実行できます。または、既にデプロイ済みのサーバーモードのseekdbやOceanBaseデータベースにリモート接続することも選択できます。

seekdbのデプロイメントモード​

seekdbは、迅速なプロトタイピングから大量ユーザーのサポートまで、あらゆるアプリケーションニーズに対応する柔軟なデプロイメントモードを提供しています。

  • 埋め込みモード

    seekdbは軽量ライブラリとしてアプリケーションに埋め込まれ、ワンクリックでインストールできます。個人学習や迅速なプロトタイピングに適しており、多様なエンドデバイスで効率的に動作します。

  • サーバーモード

    テストおよび本番環境でのデプロイメントモードとして推奨されており、軽量で使いやすく、安定した高効率なサービス提供に適しています。

    サーバーモードの使用方法については、SQLを使用したサーバーモードseekdbのクイック体験を参照してください。

ステップ1:pyseekdbをインストールし、埋め込みseekdbをデプロイ​

前提条件​

お使いの環境が以下の要件を満たしていることを確認してください:

  • OS:Linux (glibc >= 2.28)
  • Pythonバージョン:Python 3.11以降
  • システムアーキテクチャ:x86_64, aarch64

インストール​

pipを使用してインストールします。デフォルトのPythonバージョンとプラットフォームが自動的に検出されます。

pip install -U pyseekdb

pipのバージョンが古い場合は、インストール前にpipをアップグレードしてください。

pip install --upgrade pip

pyseekdbをインストールすると同時に、埋め込みモードのseekdbもインストールされます。これにより、埋め込みseekdbに直接接続してデータベースの作成などの操作を実行できます。

ステップ2:seekdbに接続し、データベースを作成​

Admin Clientを使用してseekdbに接続し、query_testという名前のデータベースを作成します。

ヒント
import pyseekdb

# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("query_test")

ステップ3:データベースに接続し、Embedding Functionsを持つcollectionを作成​

Clientを使用してquery_testデータベースに接続し、collectionを作成します。

ヒント
import pyseekdb

# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="query_test")
# Create collection
collection = client.create_collection(
name="query_demo"
)

ステップ4:データを挿入​

addメソッドを使用してcollectionにデータを挿入します。

ヒント

データ挿入の詳細については、add - データの挿入を参照してください。

import pyseekdb

# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="query_test")
# get collection
collection = client.get_collection("query_demo")

# Define documents
documents = [
"Machine learning is a subset of artificial intelligence",
"Python is a popular programming language",
"Vector databases enable semantic search",
"Neural networks are inspired by the human brain",
"Natural language processing helps computers understand text"
]

ids = ["id1", "id2", "id3", "id4", "id5"]

# Add data with documents only - embeddings will be auto-generated by embedding function
collection.add(
ids=ids,
documents=documents, # embeddings will be automatically generated
metadatas=[
{"category": "AI", "index": 0},
{"category": "Programming", "index": 1},
{"category": "Database", "index": 2},
{"category": "AI", "index": 3},
{"category": "NLP", "index": 4}
]
)

ステップ5:ベクトル検索を実行し、クエリ結果を出力​

queryメソッドを使用してベクトル検索クエリを実行し、クエリ結果を出力します。

ヒント

ベクトル検索の詳細については、query - ベクトル検索を参照してください。

import pyseekdb

# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="query_test")
# get collection
collection = client.get_collection("query_demo")

# Perform query
query_text = "artificial intelligence and machine learning"

results = collection.query(
query_texts=query_text, # Query text - will be embedded automatically
n_results=3 # Return top 3 most similar documents
)

print(f"\nQuery: '{query_text}'")
print(f"Query results: {len(results['ids'][0])} items found")

for i in range(len(results['ids'][0])):
print(f"\nResult {i+1}:")
print(f" ID: {results['ids'][0][i]}")
print(f" Distance: {results['distances'][0][i]:.4f}")
if results.get('documents'):
print(f" Document: {results['documents'][0][i]}")
if results.get('metadatas'):
print(f" Metadata: {results['metadatas'][0][i]}")

ステップ6:環境をクリーンアップ​

上記のサンプルデータベースとcollectionが不要になった場合は、delete_collectionメソッドでcollectionを削除し、delete_databaseメソッドでデータベースを削除できます。

ヒント
import pyseekdb

# Create embedded client
admin = pyseekdb.AdminClient(path="./seekdb.db")
client = pyseekdb.Client(path="./seekdb.db", database="query_test")

# Delete collection
client.delete_collection("query_demo")
print(f"\nDeleted collection")

# Delete database
admin.delete_database("query_test")
print(f"\nDeleted database")

全体の例​

import pyseekdb

#==================== Create Database ====================
# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("query_test")

# ==================== Create Collection ====================
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="query_test")
# Create collection
collection = client.create_collection(
name="query_demo"
)

# ==================== Add Data to Collection ====================
# Define documents
documents = [
"Machine learning is a subset of artificial intelligence",
"Python is a popular programming language",
"Vector databases enable semantic search",
"Neural networks are inspired by the human brain",
"Natural language processing helps computers understand text"
]

ids = ["id1", "id2", "id3", "id4", "id5"]

# Add data with documents only - embeddings will be auto-generated by embedding function
collection.add(
ids=ids,
documents=documents, # embeddings will be automatically generated
metadatas=[
{"category": "AI", "index": 0},
{"category": "Programming", "index": 1},
{"category": "Database", "index": 2},
{"category": "AI", "index": 3},
{"category": "NLP", "index": 4}
]
)

# ==================== Perform Query ====================
# Perform Query
query_text = "artificial intelligence and machine learning"

results = collection.query(
query_texts=query_text, # Query text - will be embedded automatically
n_results=3 # Return top 3 most similar documents
)

print(f"\nQuery: '{query_text}'")
print(f"Query results: {len(results['ids'][0])} items found")

for i in range(len(results['ids'][0])):
print(f"\nResult {i+1}:")
print(f" ID: {results['ids'][0][i]}")
print(f" Distance: {results['distances'][0][i]:.4f}")
if results.get('documents'):
print(f" Document: {results['documents'][0][i]}")
if results.get('metadatas'):
print(f" Metadata: {results['metadatas'][0][i]}")
# ==================== Cleanup ====================
# Delete collection
client.delete_collection("query_demo")
print(f"\nDeleted collection")

# Delete database
admin.delete_database("query_test")
print(f"\nDeleted database")

詳細を見る​