Azure Database for PostgreSQL - フレキシブル サーバーで Azure OpenAI を使用してベクトル埋め込みを生成する

適用対象: Azure Database for PostgreSQL - フレキシブル サーバー

Azure OpenAI 埋め込みを簡単に呼び出して、入力のベクトル表現を取得し、そのベクトル表現をベクトル類似性検索や機械学習モデルで使用できます。

前提条件

  1. azure_ai 拡張機能を有効にして構成します
  2. OpenAI アカウントを作成して、Azure OpenAI Service へのアクセスを要求します。
  3. 目的のサブスクリプションで Azure OpenAI へのアクセスを許可します。
  4. Azure OpenAI リソースを作成し、モデルをデプロイするためのアクセス許可を付与します。
  5. Azure OpenAI サービス リソースとモデルを作成およびデプロイし、埋め込みモデル text-embedding-ada-002 などをデプロイします。 埋め込みを作成するために必要なデプロイ名をコピーします。

OpenAI エンドポイントとキーを設定する

Azure OpenAI リソースの [リソース管理]>[キーとエンドポイント] で、Azure OpenAI リソースのエンドポイントとキーを見つけることができます。 モデル デプロイを起動するには、このエンドポイントといずれかのキーを使用して azure_ai 拡張機能を有効にします。

select azure_ai.set_setting('azure_openai.endpoint', 'https://<endpoint>.openai.azure.com'); 
select azure_ai.set_setting('azure_openai.subscription_key', '<API Key>'); 

azure_openai.create_embeddings

Azure OpenAI API を呼び出し、その入力に対して指定されたデプロイを使用して埋め込みを作成します。

azure_openai.create_embeddings(deployment_name text, input text, timeout_ms integer DEFAULT 3600000, throw_on_error boolean DEFAULT true, max_attempts integer DEFAULT 1, retry_delay_ms integer DEFAULT 1000)
azure_openai.create_embeddings(deployment_name text, input text[], batch_size integer DEFAULT 100, timeout_ms integer DEFAULT 3600000, throw_on_error boolean DEFAULT true, max_attempts integer DEFAULT 1, retry_delay_ms integer DEFAULT 1000)

引数

deployment_name

text: モデルを含む Azure OpenAI Studio でのデプロイの名前。

input

text または text[] は、使用される関数のオーバーロードに応じて、埋め込みが作成される 1 つのテキストまたはテキストの配列。

dimensions

integer DEFAULT NULL: 結果として出力される埋め込みに必要なディメンションの数。 text-embedding-3 以降のモデルでのみサポートされます。 azure_ai 拡張機能のバージョン 1.1.0 以降で使用できます

batch_size

integer DEFAULT 100 は、一度に処理するレコードの数 (パラメーター input がタイプ text[] の関数のオーバーロードでのみ使用可能 )。

timeout_ms

integer DEFAULT 3600000 操作停止後のタイムアウト時間 (ミリ秒単位)。

throw_on_error

boolean DEFAULT true エラーが発生すると、関数から例外がスローされ、ラップしていたトランザクションがロールバックされます。

max_attempts

integer DEFAULT 1 は、再試行可能なエラーで失敗した場合に、拡張機能が Azure OpenAI の埋め込み作成を再試行する回数。

retry_delay_ms

integer DEFAULT 1000 は、再試行可能なエラーで失敗した場合に、拡張機能が埋め込み作成のために Azure OpenAI エンドポイントを再度呼び出す前に待機する時間 (ミリ秒)。

返り値の種類

real[] または TABLE(embedding real[]) は、選択されたデプロイによって処理されるときに、使用される関数のオーバーロードに応じて、入力テキストのベクトル表現を含む単一の要素または単一列のテーブル。

OpenAI を使用して埋め込みを作成し、ベクトル データ型に格納する

-- Create tables and populate data
DROP TABLE IF EXISTS conference_session_embeddings;
DROP TABLE IF EXISTS conference_sessions;

CREATE TABLE conference_sessions(
  session_id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  title text,
  session_abstract text,
  duration_minutes integer,
  publish_date timestamp
);

-- Create a table to store embeddings with a vector column.
CREATE TABLE conference_session_embeddings(
  session_id integer NOT NULL REFERENCES conference_sessions(session_id),
  session_embedding vector(1536)
);

-- Insert a row into the sessions table
INSERT INTO conference_sessions
    (title,session_abstract,duration_minutes,publish_date) 
VALUES
    ('Gen AI with Azure Database for PostgreSQL flexible server'
    ,'Learn about building intelligent applications with azure_ai extension and pg_vector' 
    , 60, current_timestamp)
    ,('Deep Dive: PostgreSQL database storage engine internals'
    ,' We will dig deep into storage internals'
    , 30, current_timestamp)
    ;

-- Get an embedding for the Session Abstract
SELECT
     pg_typeof(azure_openai.create_embeddings('text-embedding-ada-002', c.session_abstract)) as embedding_data_type
    ,azure_openai.create_embeddings('text-embedding-ada-002', c.session_abstract)
  FROM
    conference_sessions c LIMIT 10;

-- Insert embeddings 
INSERT INTO conference_session_embeddings
    (session_id, session_embedding)
SELECT
    c.session_id, (azure_openai.create_embeddings('text-embedding-ada-002', c.session_abstract))
FROM
    conference_sessions as c  
LEFT OUTER JOIN
    conference_session_embeddings e ON e.session_id = c.session_id
WHERE
    e.session_id IS NULL;

-- Create a HNSW index
CREATE INDEX ON conference_session_embeddings USING hnsw (session_embedding vector_ip_ops);


-- Retrieve top similarity match
SELECT
    c.*
FROM
    conference_session_embeddings e
INNER JOIN
    conference_sessions c ON c.session_id = e.session_id
ORDER BY
    e.session_embedding <#> azure_openai.create_embeddings('text-embedding-ada-002', 'Session to learn about building chatbots')::vector
LIMIT 1;

次のステップ