Azure Database for PostgreSQL フレキシブル サーバーでローカルにデプロイされた LLM を使用してベクトル埋め込みを生成する (プレビュー)
前提条件
メモリ最適化 VM SKU で実行されている Azure Database for PostgreSQL フレキシブル サーバー インスタンス。 Azure メモリ最適化 VM の詳細については、「Azure VM のサイズ - メモリ - Azure Virtual Machines」を参照してください
vector 拡張機能を有効にすることをお勧めします。これを利用するとテキスト埋め込みをデータベースに格納して効率的にインデックスを作成できるからです。
拡張機能を有効にする
azure_local_ai を Azure Database for PostgreSQL フレキシブル サーバーのインスタンスに対して有効にするには、事前に azure_local_ai
拡張機能を許可リストに載せる必要があります (「拡張機能を許可する」の説明を参照してください)。
重要
データベースで言語モデルをホストするには、大きなメモリ占有領域が必要です。 この要件をサポートするために、azure_local_ai
がサポートされるのは 4 仮想コア以上のメモリ最適化 Azure VM SKU 上のみとなります。
使用される VM がこの最小要件を満たしていない場合は、azure_local_ai
拡張機能は azure.extensions
サーバー パラメーターに対して許可される値の一覧に表示されません。
この拡張機能が許可リストに載せられた状態になったら、拡張機能を作成する手順の説明に従って、この拡張機能を使用したいデータベースのそれぞれにインストールできます。
Note
Azure Local AI を有効にすると、multilingual-e5-small モデルが Azure Database for PostgreSQL フレキシブル サーバー インスタンスにデプロイされます。 リンクされたドキュメントには、e5 チームのライセンス条項が記載されています。 その他のサードパーティ製のオープンソース モデルも、継続的にインストールできるようになる可能性があります。
拡張機能 azure_local_ai
をインストールすると azure_local_ai
というスキーマが作成され、この中にテーブルや関数など、この拡張機能の機能の実装と公開に必要な SQL 関連オブジェクトが格納されます。
重要
vector 拡張機能を有効にすることをお勧めします。テキスト埋め込みを PostgreSQL データベースに格納するには、これが必要になるからです。
拡張機能によって提供される関数
azure_local_ai
拡張機能によって一連の関数が提供されます。 これらの関数を使用すると、テキスト データからベクトル埋め込みを作成できるため、生成 AI アプリケーションを簡単に開発できます。 この拡張機能には、埋め込みの作成、設定の取得などの関数が用意されています。 これらの関数を使用すると、PostgreSQL 境界の外でホストされている AI 埋め込みモデルに対する追加のリモート API 呼び出しを行う必要がなくなり、開発プロセスを簡略化し、待機時間を短縮できます。
[スキーマ] | 名前 | 結果のデータ型 | 引数のデータ型 |
---|---|---|---|
azure_local_ai | create_embeddings | TABLE(embedding real[]) | model_uri text, inputs text[], batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | create_embeddings | real[] | model_uri text, input text, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | get_setting | jsonb | keys text[] DEFAULT ARRAY[]::text[], timeout_ms integer DEFAULT 3600000 |
azure_local_ai | get_setting | text | key text, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | model_metadata | jsonb | model_uri text |
これらの関数を表示するには、次の psql メタコマンドを使用します。
\df azure_local_ai.*
azure_local_ai.create_embeddings
azure_local_ai
拡張機能を使用すると、スカラーとバッチの両方の形式の埋め込みの作成と更新を、ローカルにデプロイされた LLM を呼び出して行うことができます。
azure_local_ai.create_embeddings(model_uri text, input text, batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000);
azure_local_ai.create_embeddings(model_uri text, array[inputs [text]], batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000);
引数
model_uri
埋め込みを作成するために呼び出されるテキスト埋め込みモデルの text
名。
input
text
または text[]
は、使用される関数のオーバーロードに応じて、埋め込みが作成される 1 つのテキストまたはテキストの配列。
batch_size
bigint DEFAULT 128
は、一度に処理するレコードの数 (パラメーター input
がタイプ text[]
の関数のオーバーロードでのみ使用可能 )。
timeout_ms
integer DEFAULT 3600000
操作停止後のタイムアウト時間 (ミリ秒単位)。
azure_local_ai.get_setting
構成オプションの現在の値を取得するために使用されます。
SELECT azure_local_ai.get_setting(key TEXT)
azure_local_ai
では、ONNX Runtime サービス内の ONNX Runtime スレッドプールの構成パラメーターの確認がサポートされます。 変更は、現時点では許可されていません。 「ONNX Runtime のパフォーマンス チューニング」を参照してください。
引数
キー
有効な値は次のとおりです。
intra_op_parallelism
: ONNX Runtime スレッドプールによる単一演算子の並列化に使用されるスレッドの合計数を設定します。 既定では、intra ops スレッドの数を可能な限り最大化することで (既定では利用可能なすべての CPU)、全体的なスループットを大幅に向上させます。inter_op_parallelism
: ONNX Runtime スレッドプールによって複数の演算子を並列に計算するために使用されるスレッドの合計数を設定します。 既定では、最小スレッド数 (1) に設定します。 これを増やすと、スレッド間のコンテキスト切り替えが頻繁に発生するため、多くの場合にパフォーマンスが低下します。spin_control
: 要求に対する ONNX Runtime スレッドプールのスピンを切り替えます。 無効にすると CPU 使用量が少なくなり、待機時間が長くなります。 既定では true (有効) に設定されています。
返り値の種類
TEXT
は選択した設定の現在の値を表します。
例
埋め込みを既存のテキストから作成する
次に示す例をご自身の環境で使用すると、ローカルにデプロイされた multilingual-e5 モデルを使用する埋め込み生成を試すことができます。
-- Create documents table
CREATE TABLE documents(doc_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, doc_contents TEXT NOT NULL, last_update TIMESTAMPTZ DEFAULT now());
--Insert data into the docs table
INSERT INTO documents(doc_contents) VALUES
('Create in-database embeddings with azure_local_ai extension.'),
('Enable RAG patterns with in-database embeddings and vectors on Azure Database for PostgreSQL - Flexible server.'),
('Generate vector embeddings in PostgreSQL with azure_local_ai extension.'),
('Generate text embeddings in PostgreSQL for retrieval augmented generation (RAG) patterns with azure_local_ai extension and locally deployed LLM.'),
('Use vector indexes and Azure OpenAI embeddings in PostgreSQL for retrieval augmented generation.');
-- Add a vector column and generate vector embeddings from locally deployed model
ALTER TABLE documents
ADD COLUMN doc_vector vector(384) -- multilingual-e5 embeddings are 384 dimensions
GENERATED ALWAYS AS (azure_local_ai.create_embeddings('multilingual-e5-small:v1', doc_contents)::vector) STORED; -- TEXT string sent to local model
--View floating point entries in the doc_vector column
SELECT doc_vector FROM documents;
-- Add a single record to the documents table and the vector embedding using azure_local_ai and locally deployed model will be automatically generated
INSERT INTO documents(doc_contents) VALUES
('Semantic Search with Azure Database for PostgreSQL - Flexible Server and Azure OpenAI');
--View all document entries, their contents, embeddings and last time the row was updated
SELECT doc_contents, doc_vector, last_update FROM documents;
-- The following command leverages the overload of azure_local_ai.create_embeddings function which accepts and array of TEXT
-- and produces a table for which each row contains the embedding of one element in the input array
SELECT azure_local_ai.create_embeddings('multilingual-e5-small:v1', array['Recommendation System with Azure Database for PostgreSQL - Flexible Server and Azure OpenAI.', 'Generative AI with Azure Database for PostgreSQL - Flexible Server.']);
新しいテキストの挿入時に埋め込みを生成する
次に示す例をご自身の環境で使用すると、ローカルにデプロイされた multilingual-e5 モデルを使用する埋め込み生成を試すことができます。
-- Create documents table
CREATE TABLE documents(doc_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, doc_contents TEXT NOT NULL, last_update TIMESTAMPTZ DEFAULT now(), doc_vector vector(384) GENERATED ALWAYS AS (azure_local_ai.create_embeddings('multilingual-e5-small:v1', doc_contents)::vector) STORED);
-- Insert data into the documents table
INSERT INTO documents(doc_contents) VALUES
('Create in-database embeddings with azure_local_ai extension.'),
('Enable RAG patterns with in-database embeddings and vectors on Azure Database for PostgreSQL - Flexible server.'),
('Generate vector embeddings in PostgreSQL with azure_local_ai extension.'),
('Generate text embeddings in PostgreSQL for retrieval augmented generation (RAG) patterns with azure_local_ai extension and locally deployed LLM.'),
('Use vector indexes and Azure OpenAI embeddings in PostgreSQL for retrieval augmented generation.');
-- Query embedding text, list results by descending similarity score
WITH all_documents AS (
SELECT doc_id, doc_contents, doc_vector FROM documents
),
target_documents AS (
SELECT azure_local_ai.create_embeddings('multilingual-e5-small:v1', 'Generate text embeddings in PostgreSQL.') doc_vector
)
SELECT all_documents.doc_id, all_docs.doc_contents , 1 - (all_documents.doc_vector::vector <=> target_documents.doc_vector::vector) AS similarity
FROM target_documents, all_documents
ORDER BY similarity DESC
LIMIT 2;
関連するコンテンツ
- Azure Database for PostgreSQL フレキシブル サーバーと Azure Cognitive Services を統合する。
- Azure Database for PostgreSQL と Azure Machine Learning サービスを統合する。
- Azure Database for PostgreSQL - フレキシブル サーバーで Azure OpenAI を使用してベクトル埋め込みを生成する。
- Azure Database for PostgreSQL - フレキシブル サーバーの Azure AI 拡張機能。
- Azure Database for PostgreSQL - フレキシブル サーバーを使用した生成 AI 。
- Azure Database for PostgreSQL - フレキシブル サーバーと Azure OpenAI を使用したレコメンデーション システム。
- Azure Database for PostgreSQL - フレキシブル サーバーと Azure OpenAI を使用したセマンティック検索。
- Azure Database for PostgreSQL - フレキシブル サーバーで pgvector を有効にして使用する。