Python SDKによる組み込みseekdbの体験
この例では、Linux環境を例に、pyseekdb(OceanBaseが提供するPythonクライアント)を使用して組み込みseekdbをすばやく体験する方法を説明します。
Linuxシステム以外にも、macOSおよびWindowsシステムでpyseekdbを使用できますが、現在はサーバーモードのseekdbのみサポートしています。macOSおよびWindowsシステムでの使用方法については、pyseekdb クイックスタートを参照してください。
この例では、以下の操作を実行します:
- pyseekdbと組み込みseekdbをデプロイします。
- seekdbに接続し、データベースを作成します。
- データベースに接続し、Embedding Functionsを持つcollectionを作成します。
- documentsを使用してデータを追加します(ベクトルは自動生成されます)。
- 混合検索を実行します(ベクトルは自動生成されます)し、クエリ結果を出力します。
- 環境をクリーンアップします。
背景
pyseekdb
pyseekdbは、OceanBaseが提供するPythonクライアントです。単一のAPIセットに基づき、3種類のデータベース接続モードを実装しており、組み込みモードのseekdb、サーバーモードのseekdb、およびOceanBaseデータベースへの接続をサポートします。
このクライアントをインストールすると、組み込みモードのseekdbも同時にインストールされるため、組み込みseekdbに直接接続してデータベースの作成などの操作を実行できます。または、既にデプロイ済みのサーバーモードのseekdbまたはOceanBaseデータベースにリモート接続することも選択できます。
seekdbのデプロイメントモード
seekdbは、迅速なプロトタイプ開発から大量ユーザーのサポートまで、あらゆるアプリケーションニーズに対応する、多様で柔軟なデプロイメントモードを提供します。
-
組み込みモード
seekdbは軽量ライブラリとしてアプリケーションに組み込まれ、pipワンクリックでのインストールをサポートします。個人学習や迅速なプロトタイプ開発に適しており、さまざまなエンドデバイス上で効率的に動作します。
-
サーバーモード
テストおよび本番環境でのデプロイメントモードとして推奨されています。軽量で使いやすく、安定した高効率なサービス提供に適しています。
サーバーモードの使用方法については、SQLを通じてサーバーモードのseekdbを体験するを参照してください。
ステップ1:pyseekdbをインストールし、組み込みseekdbをデプロイする
前提条件
環境が以下の要件を満たしていることを確認してください:
- オペレーティングシステム: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に接続し、hybrid_search_testという名前のデータベースを作成します。
-
Admin Clientの詳細については、Admin Clientを参照してください。
-
データベース作成の詳細については、create_database - データベースの作成を参照してください。
import pyseekdb
# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("hybrid_search_test")
ステップ3:データベースに接続し、Embedding Functionsを含むcollectionを作成する
Client を使用して hybrid_search_test データベースに接続し、collectionを作成します。
- Clientの詳細については、Clientを参照してください。
- collectionの作成方法の詳細については、create_collection - Collectionの作成を参照してください。
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Create collection
collection = client.create_collection(
name="hybrid_search_demo"
)
ステップ4:データの挿入
add メソッドを使用して、コレクションにデータを挿入します。
データの挿入の詳細については、add - データの挿入を参照してください。
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# get collection
collection = client.get_collection("hybrid_search_demo")
# Define documents
documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections"
]
# Define metadatas
metadatas = [
{"category": "AI", "topic": "machine learning", "year": 2023, "popularity": 95},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 88},
{"category": "AI", "topic": "deep learning", "year": 2024, "popularity": 92},
{"category": "Data Science", "topic": "data analysis", "year": 2023, "popularity": 85},
{"category": "AI", "topic": "nlp", "year": 2024, "popularity": 90},
{"category": "AI", "topic": "computer vision", "year": 2023, "popularity": 87},
{"category": "AI", "topic": "reinforcement learning", "year": 2024, "popularity": 89},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 91},
{"category": "AI", "topic": "general ai", "year": 2023, "popularity": 93},
{"category": "AI", "topic": "neural networks", "year": 2024, "popularity": 94}
]
ids = [f"doc_{i+1}" for i in range(len(documents))]
# Insert data
collection.add(ids=ids, documents=documents, metadatas=metadatas)
ステップ5:ハイブリッド検索を実行し、クエリ結果を出力する
hybrid_search メソッドを使用してハイブリッド検索クエリを実行し、クエリ結果を出力します。
ハイブリッド検索の詳細については、hybrid_search - ハイブリッド検索を参照してください。
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# get collection
collection = client.get_collection("hybrid_search_demo")
# Perform hybrid search
hybrid_result = collection.hybrid_search(
query={"where_document": {"$contains": "machine learning"}, "n_results": 10},
knn={"query_texts": ["AI research"], "n_results": 10},
rank={"rrf": {}},
n_results=5
)
# Print results
print("\nhybrid_search() Results:")
print(f" ids: {hybrid_result ['ids'][0]}")
print(f" Document: {hybrid_result ['documents'][0]}")
ステップ6:環境をクリーンアップ
上記のサンプルデータベースとコレクションが不要になった場合は、delete_collection メソッドでコレクションを削除し、delete_database メソッドでデータベースを削除できます。
-
コレクションの削除の詳細については、delete_collection - コレクションの削除を参照してください。
-
データベースの削除の詳細については、delete_database - データベースの削除を参照してください。
import pyseekdb
# Create embedded client
admin = pyseekdb.AdminClient(path="./seekdb.db")
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Delete collection
client.delete_collection("hybrid_search_demo")
print(f"\nDeleted collection")
# Delete database
admin.delete_database("hybrid_search_test")
print(f"\nDeleted database")
全体の例
import pyseekdb
#==================== Create Database ====================
# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("hybrid_search_test")
# ==================== Create Collection ====================
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Create collection
collection = client.create_collection(
name="hybrid_search_demo"
)
# ==================== Add Data to Collection ====================
# Define documents
documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections"
]
# Define metadatas
metadatas = [
{"category": "AI", "topic": "machine learning", "year": 2023, "popularity": 95},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 88},
{"category": "AI", "topic": "deep learning", "year": 2024, "popularity": 92},
{"category": "Data Science", "topic": "data analysis", "year": 2023, "popularity": 85},
{"category": "AI", "topic": "nlp", "year": 2024, "popularity": 90},
{"category": "AI", "topic": "computer vision", "year": 2023, "popularity": 87},
{"category": "AI", "topic": "reinforcement learning", "year": 2024, "popularity": 89},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 91},
{"category": "AI", "topic": "general ai", "year": 2023, "popularity": 93},
{"category": "AI", "topic": "neural networks", "year": 2024, "popularity": 94}
]
ids = [f"doc_{i+1}" for i in range(len(documents))]
# Insert data
collection.add(ids=ids, documents=documents, metadatas=metadatas)
# ==================== Perform Hybrid Search ====================
# Perform hybrid search
hybrid_result = collection.hybrid_search(
query={"where_document": {"$contains": "machine learning"}, "n_results": 10},
knn={"query_texts": ["AI research"], "n_results": 10},
rank={"rrf": {}},
n_results=5
)
# ==================== Print Query Results ====================
# Print results
print("\nhybrid_search() Results:")
print(f" ids: {hybrid_result ['ids'][0]}")
print(f" Document: {hybrid_result ['documents'][0]}")
# ==================== Cleanup ====================
# Delete collection
client.delete_collection("hybrid_search_demo")
print(f"\nDeleted collection")
# Delete database
admin.delete_database("hybrid_search_test")
print(f"\nDeleted database")
詳細を見る
-
pyseekdbの詳細な紹介と使用方法については、pyseekdbを参照してください。
-
pyseekdbのその他の使用例については、以下を参照してください:
-
Complete example:現在のpyseekdbがサポートする全機能を実演します。
-
Hybrid search example:seekdbのハイブリッド検索の使用方法を実演します。
-
Hybrid search example:seekdbのハイブリッド検索の使用方法を実演し、ベクトル検索の機能と比較します。
-
-
Python SDK以外にも、seekdbはSQLを通じた操作をサポートしています。SQLの使用方法については、SQLでサーバーモードのseekdbをすぐに体験するを参照してください。