Azure Database for PostgreSQL フレキシブル サーバーで azure_local_ai を使用してベクトル埋め込みを生成する (プレビュー)

前提条件

  1. メモリ最適化 VM SKU で実行されている Azure Database for PostgreSQL フレキシブル サーバー インスタンス。 Azure メモリ最適化 VM の詳細については、「Azure VM のサイズ - メモリ - Azure Virtual Machines」を参照してください

  2. 次の拡張機能を有効にします。

    1. vector

    2. azure_local_ai

Azure Database for PostgreSQL - フレキシブル サーバーで拡張機能を有効にする方法については、「Azure Database for PostgreSQL で拡張機能を有効にする方法」を参照してください。

Note

Azure Local AI プレビューを有効にすると、multilingual-e5-small モデルが Azure Database for PostgreSQL フレキシブル サーバー インスタンスにデプロイされます。 リンクされたドキュメントには、e5 チームのライセンス条項が記載されています。 その他のサードパーティ製のオープンソース モデルも、継続的にインストールできるようになる可能性があります。

azure_local_ai 拡張機能によって提供される関数

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 とローカルにデプロイされた multilingual-e5-small モデルを使用して埋め込みを作成し、ベクトルとして保存します

ローカルにデプロイされた multilingual-e5 モデルを使用して埋め込み生成をテストするために、独自の環境で使用できる例を次に示します。

--Create docs table
CREATE TABLE docs(doc_id int generated always as identity primary key, doc text not null, embedding float4[], last_update timestamptz default now());

--Insert data into the docs table
INSERT INTO docs(doc) 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 docs
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)::vector) STORED; -- TEXT string sent to local model

--View floating point entries in the doc_vector column
select doc_vector from docs;

-- Add a single record to the docs table and the vector embedding using azure_local_ai and locally deployed model will be automatically generated
INSERT INTO docs(doc) VALUES ('Semantic Search with Azure Database for PostgreSQL - Flexible Server and Azure OpenAI');

--View all doc entries and their doc_vector column
select doc, doc_vector, last_update from docs;

-- Simple array embedding
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 モデルを使用して埋め込み生成をテストするために、独自の環境で使用できる例を次に示します。

-- Update embeddings upon insertion

-- create table
create table docs(doc_id int generated always as identity primary key, doc text not null, last_update timestamptz default now(), embedding float4[] 
	GENERATED ALWAYS AS (azure_local_ai.create_embeddings('multilingual-e5-small:v1', doc)) STORED);

--Insert data into the docs table
INSERT INTO docs(doc) 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_docs as (
 select doc_id, doc, embedding
  from docs
), target_doc as (
 select azure_local_ai.create_embeddings('multilingual-e5-small:v1', 'Generate text embeddings in PostgreSQL.') embedding
)
select all_docs.doc_id, all_docs.doc , 1 - (all_docs.embedding::vector <=> target_doc.embedding::vector) as similarity
 from target_doc, all_docs
 order by similarity desc
 limit 2;

ONNX Runtime の構成

azure_local_ai.get_setting

構成オプションの現在の値を取得するために使用されます。

SELECT azure_local_ai.get_setting(key TEXT)

azure_local_ai 拡張機能では、ONNX Runtime サービス内の ONNX Runtime スレッドプールの構成パラメーターの確認がサポートされています。 現時点では、変更は許可されていません。 「ONNX Runtime のパフォーマンス チューニング」を参照してください。

引数

キー

key の有効な値は次のとおりです。

  • intra_op_parallelism: ONNX Runtime スレッドプールによる単一演算子の並列化に使用されるスレッドの合計数を設定します。 既定では、intra ops スレッドの数を可能な限り最大化することで (既定では利用可能なすべての CPU)、全体的なスループットを大幅に向上させます。
  • inter_op_parallelism: ONNX Runtime スレッドプールによって複数の演算子を並列に計算するために使用されるスレッドの合計数を設定します。 既定では、最小スレッド数 (1) に設定します。 これを増やすと、スレッド間のコンテキスト切り替えが頻繁に発生するため、多くの場合にパフォーマンスが低下します。
  • spin_control: 要求に対する ONNX Runtime スレッドプールのスピンを切り替えます。 無効にすると CPU 使用量が少なくなり、待機時間が長くなります。 既定では true (有効) に設定されています。

返り値の種類

TEXT は選択した設定の現在の値を表します。