自動埋め込みの例
この例では、 自動埋め込み機能をpytidbクライアントで使用する方法を示します。
pytidbクライアントを使用して TiDB に接続します。- 自動埋め込み用に構成された VectorField を使用してテーブルを定義します。
- プレーンテキスト データを挿入します。埋め込みはバックグラウンドで自動的に入力されます。
- 自然言語クエリを使用してベクトル検索を実行します。埋め込みは透過的に生成されます。
前提条件
始める前に、次のものがあることを確認してください。
- Python (>=3.10) : パイソン 3.10 以降のバージョンをインストールします。
- TiDB Cloud Starter クラスター: TiDB Cloudに無料の TiDB クラスターを作成できます。
実行方法
ステップ1. pytidbリポジトリのクローンを作成する
git clone https://github.com/pingcap/pytidb.git
cd pytidb/examples/auto_embedding/
ステップ2. 必要なパッケージをインストールする
python -m venv .venv
source .venv/bin/activate
pip install -r reqs.txt
ステップ3. 環境変数を設定する
- TiDB Cloudコンソールでクラスターページに移動し、ターゲット クラスターの名前をクリックして概要ページに移動します。
- 右上隅の「接続」をクリックします。接続パラメータがリストされた接続ダイアログが表示されます。
- 次のように接続パラメータに応じて環境変数を設定します。
cat > .env <<EOF
TIDB_HOST={gateway-region}.prod.aws.tidbcloud.com
TIDB_PORT=4000
TIDB_USERNAME={prefix}.root
TIDB_PASSWORD={password}
TIDB_DATABASE=test
# Using TiDB Cloud Free embedding model by default, which does not require setting up any API key
EMBEDDING_PROVIDER=tidbcloud_free
EOF
ステップ4.デモを実行する
python main.py
期待される出力:
=== Define embedding function ===
Embedding function (model id: tidbcloud_free/amazon/titan-embed-text-v2) defined
=== Define table schema ===
Table created
=== Truncate table ===
Table truncated
=== Insert sample data ===
Inserted 3 chunks
=== Perform vector search ===
id: 1, text: TiDB is a distributed database that supports OLTP, OLAP, HTAP and AI workloads., distance: 0.30373281240458805
id: 2, text: PyTiDB is a Python library for developers to connect to TiDB., distance: 0.422506501973434
id: 3, text: LlamaIndex is a Python library for building AI-powered applications., distance: 0.5267239638442787
関連リソース
- ソースコード: GitHubでビュー