メインコンテンツまでスキップ

OAuthトークンでVector Searchを使う

このノートブックでは、一連検索SDKまたは新しいOAuthブラウザを使用して HTTP を使用して、一連検索エンドポイントを呼び出す方法を示します。 どちらの場合も、本番運用ワークロードで推奨されているように、ネットワークに最適化されたパスが使用されます。

トークンの作成とエンドポイントの呼び出しを行うHTTP呼び出しは、任意のプログラミング言語で実装できます。本番運用アプリケーションの場合は、オフラインを 60 分ごとに更新する必要があることに注意してください。 トークンの有効期限切れによるエラーを防ぐため、Databricksは60分未満の間隔でトークンを更新することを推奨しています。

設定

Python
%pip install databricks-sdk
%pip install databricks-vectorsearch
dbutils.library.restartPython()
Python
import requests
import json
import random
from databricks.sdk import WorkspaceClient
import logging

設定定数

サービスプリンシパルの作成方法の詳細については、 Databricksドキュメントを参照してください。

Python
# Define the secret ID and secret for the service principal
CLIENT_ID = dbutils.secrets.get(scope="scope", key="service_principal_client_id")
CLIENT_SECRET = dbutils.secrets.get(scope="scope", key="service_principal_client_secret")

# You can get it by clicking the copy button next to the index name
INDEX_NAME="UC_CATALOG_INDEX_NAME"
ENDPOINT_NAME="ENDPOINT_NAME"

workspace_url = f"https://{spark.conf.get('spark.databricks.workspaceUrl')}"

Pythonクライアントを使用したクエリベクトル検索

詳細については、 APIドキュメントを参照してください。

Python
from databricks.vector_search.client import VectorSearchClient

vsc_dp = VectorSearchClient(
service_principal_client_id=CLIENT_ID,
service_principal_client_secret=CLIENT_SECRET,
workspace_url=workspace_url)

index = vsc_dp.get_index(endpoint_name=ENDPOINT_NAME, index_name=INDEX_NAME)
index.similarity_search(["text"], query_vector=[0]*2560, num_results=5, debug_level=0)

HTTPリクエストを使用したベクトル検索を呼び出す

このセクションでは、HTTP を使用してベクトル検索インデックスを呼び出す方法を示します。この方法は、お好みのツールや言語で実装できます。

API呼び出しを使用してOAuthクラウドを生成する

ネットワーク最適化パスを使用してエンドポイントを呼び出すには、 OAuthが必要です。 以下のコードはトークンを作成します。

Python
url = f"{workspace_url}/oidc/v1/token"
deets = json.dumps([
{
"type": "unity_catalog_permission",
"securable_type": "table",
"securable_object_name": INDEX_NAME,
"operation": ("ReadVectorIndex"),
},
])
payload = { 'grant_type': 'client_credentials', 'scope': 'all-apis', 'authorization_details': deets}

response = requests.post(
url=url,
auth=(CLIENT_ID, CLIENT_SECRET),
headers={"Content-Type": "application/x-www-form-urlencoded"},
data=payload,
)
if response.status_code != 200:
logging.error(f"OAuth token request failed: {response.status_code} - {response.text}")
response.raise_for_status()

token_data = response.json()
access_token = token_data.get("access_token")
if not access_token:
raise ValueError("Failed to get access token")

Pythonクライアントを使用したクエリベクトル検索

JavaScriptなどのツールを使用してベクトル検索を実行するには、クエリを作成してベクトル検索エンドポイントに送信します。

Python
index = vsc_dp.get_index(endpoint_name=ENDPOINT_NAME, index_name=INDEX_NAME)
index_url = index.index_url
print(index_url)

headers = {"Authorization": f"Bearer {access_token}"}
payload = {
"query_vector": [0]*2560,
"num_results": 5,
"columns": ["text"]}

response = requests.post(index_url+"/query", headers=headers, data=json.dumps(payload))
print(response.status_code)
print(json.dumps(response.json(), indent=2))

サンプルノートブック

OAuthバンクで「成功検索」を使用する

ノートブックを新しいタブで開く