適用対象: SQL Server 2025 (17.x) プレビュー
注
AI_GENERATE_CHUNKS
SQL Server 2025 では現在 プレビュー段階です。
AI_GENERATE_CHUNKS
は、型、サイズ、ソース式に基づいて "チャンク" またはテキストのフラグメントを作成するテーブル値関数です。
互換性レベル 170
AI_GENERATE_CHUNKS
では、互換性レベルが 170 以上である必要があります。 レベルが 170 未満の場合、データベース エンジンは AI_GENERATE_CHUNKS
関数を見つけることができません。
データベースの互換性レベルを変更するには、「データベースの 互換性レベルを表示または変更する」を参照してください。
構文
AI_GENERATE_CHUNKS (source = text_expression
, chunk_type = FIXED
[ [ , ] chunk_size = numeric_expression ]
[ [ , ] overlap = numeric_expression ]
[ [ , ] enable_chunk_set_id = numeric_expression]
)
論争
ソース
任意の文字型の 式 ( 例: nvarchar、 varchar、 nchar、 char)。
chunk_type
テキスト/ドキュメントをチャンクする型またはメソッドに名前を付ける文字列リテラル。 NULL
または列の値にすることはできません。
このリリースで受け入れられる値:
FIXED
chunk_size
chunk_type
がFIXED
場合、このパラメーターは、tinyint、smallint、int、または bigint 型の変数、リテラル、またはスカラー式として指定された各チャンクの文字/ワード カウント サイズを設定します。
chunk_size は、 NULL
、負、またはゼロ (0
) にすることはできません。
オーバーラップ
overlap パラメーターは、現在のチャンクに含める前のテキストの割合を決定します。 この割合は、 chunk_size
パラメーターに適用され、文字数でサイズが計算されます。
オーバーラップ値は、tinyint、smallint、int、または bigint 型の変数、リテラル、またはスカラー式として指定できます。 0 (0
) から 50 までの整数でなければならず、NULL または負の値にすることはできません。 既定値は 0 (0
) です。
enable_chunk_set_id
出力列を有効または無効にするフラグとして機能する int 式またはchunk_set_id
式。同じソースに属するチャンクをグループ化するのに役立つ数値を返す列。 値 1 を指定すると、列が有効になります。
enable_chunk_set_id省略、NULL、または値が 0 の場合、chunk_set_id
列は無効になり、返されません。
戻り値の型
AI_GENERATE_CHUNKS
は、次の列を含むテーブルを返します。
列名 | データの種類 | 説明 |
---|---|---|
chunk |
ソース式のデータ型と同じ | ソース式からチャンクされたテキストが返されました。 |
chunk_order |
int | 各チャンクが処理された順序に関連する順序付けられた番号のシーケンスは、 1 以降、 1 ずつ増加します。 |
chunk_offset |
int | チャンク 処理の開始に関連するソース データ/ドキュメントのチャンクの位置。 |
chunk_length |
int | 返されるテキスト チャンクの文字長。 |
chunk_set_id |
int | ソース式、ドキュメント、または行のすべてのチャンクをグループ化する ID を含む 省略可能な列 。 1 つのトランザクションで複数のドキュメントまたは行がチャンクされている場合、それぞれ異なる chunk_set_id が与えられます。 可視性は、 enable_chunk_set_id パラメーターによって制御されます。 |
戻り値の例
次のパラメーターを使用して、 AI_GENERATE_CHUNKS
の戻り値の結果の例を次に示します。
FIXED
のチャンクの種類。チャンク サイズは 50 文字です。
'chunk_set_id' が有効になっています。
チャンク テキスト:
All day long we seemed to dawdle through a country which was full of beauty of every kind. Sometimes we saw little towns or castles on the top of steep hills such as we see in old missals; sometimes we ran by rivers and streams which seemed from the wide stony margin on each side of them to be subject to great floods.
チャンク | chunk_order | chunk_offset | chunk_length | chunk_set_id |
---|---|---|---|---|
All day long we seemed to dawdle through a country |
1 | 1 | 50 | 1 |
which was full of beauty of every kind. Sometimes |
2 | 51 | 50 | 1 |
we saw little towns or castles on the top of stee |
3 | 101 | 50 | 1 |
p hills such as we see in old missals; sometimes w |
4 | 151 | 50 | 1 |
e ran by rivers and streams which seemed from the |
5 | 201 | 50 | 1 |
wide stony margin on each side of them to be subje |
6 | 251 | 50 | 1 |
ct to great floods. |
7 | 301 | 19 | 1 |
注釈
AI_GENERATE_CHUNKS
は、複数の行があるテーブルで使用できます。 チャンク サイズとチャンクされるテキストの量に応じて、結果セットは、 chunk_set_id
列で新しい列またはドキュメントを開始するタイミングを示します。 次の例では、最初の行のテキストのチャンクが完了し、2 番目の行に移動すると、 chunk_set_id
が変更されます。
chunk_order
とchunk_offset
の値も、新しい開始点を示すようにリセットされます。
CREATE TABLE textchunk (text_id INT IDENTITY(1,1) PRIMARY KEY, text_to_chunk nvarchar(max));
GO
INSERT INTO textchunk (text_to_chunk)
VALUES
('All day long we seemed to dawdle through a country which was full of beauty of every kind. Sometimes we saw little towns or castles on the top of steep hills such as we see in old missals; sometimes we ran by rivers and streams which seemed from the wide stony margin on each side of them to be subject to great floods.'),
('My Friend, Welcome to the Carpathians. I am anxiously expecting you. Sleep well to-night. At three to-morrow the diligence will start for Bukovina; a place on it is kept for you. At the Borgo Pass my carriage will await you and will bring you to me. I trust that your journey from London has been a happy one, and that you will enjoy your stay in my beautiful land. Your friend, DRACULA')
GO
SELECT c.*
FROM textchunk t
CROSS APPLY
AI_GENERATE_CHUNKS(source = text_to_chunk, chunk_type = FIXED, chunk_size = 50, enable_chunk_set_id = 1) c
チャンク | chunk_order | chunk_offset | chunk_length | chunk_set_id |
---|---|---|---|---|
All day long we seemed to dawdle through a country |
1 | 1 | 50 | 1 |
which was full of beauty of every kind. Sometimes |
2 | 51 | 50 | 1 |
we saw little towns or castles on the top of stee |
3 | 101 | 50 | 1 |
p hills such as we see in old missals; sometimes w |
4 | 151 | 50 | 1 |
e ran by rivers and streams which seemed from the |
5 | 201 | 50 | 1 |
wide stony margin on each side of them to be subje |
6 | 251 | 50 | 1 |
ct to great floods. |
7 | 301 | 19 | 1 |
My Friend, Welcome to the Carpathians. I am anxi |
1 | 1 | 50 | 2 |
ously expecting you. Sleep well to-night. At three |
2 | 51 | 50 | 2 |
to-morrow the diligence will start for Bukovina; |
3 | 101 | 50 | 2 |
a place on it is kept for you. At the Borgo Pass m |
4 | 151 | 50 | 2 |
y carriage will await you and will bring you to me |
5 | 201 | 50 | 2 |
. I trust that your journey from London has been a |
6 | 251 | 50 | 2 |
happy one, and that you will enjoy your stay in m |
7 | 301 | 50 | 2 |
y beautiful land. Your friend, DRACULA |
8 | 3:51 | 三十八 | 2 |
例示
A。 固定型とサイズが 100 文字のテキスト列をチャンクする
次の例では、 AI_GENERATE_CHUNKS
を使用してテキスト列をチャンクします。
chunk_type
のFIXED
と 100 文字のchunk_size
を使用します。
SELECT
c.chunk
FROM
docs_table t
CROSS APPLY
AI_GENERATE_CHUNKS(source = text_column, chunk_type = FIXED, chunk_size = 100) c
B. テキスト列を重ねてチャンクする
次の例では、 AI_GENERATE_CHUNKS
を使用して、重複を使用してテキスト列をチャンクします。 FIXED のchunk_type、100 文字のchunk_size、10% の重複が使用されます。
SELECT
c.chunk
FROM
docs_table t
CROSS APPLY
AI_GENERATE_CHUNKS(source = text_column, chunk_type = FIXED, chunk_size = 100, overlap = 10) c
C. AI_GENERATE_CHUNKSでAI_GENERATE_EMBEDDINGSを使用する
この例では、 AI_GENERATE_EMBEDDINGS
と AI_GENERATE_CHUNKS
を使用してテキスト チャンクから埋め込みを作成し、返されたベクター配列を AI モデル推論エンドポイントからテーブルに挿入します。
INSERT INTO
my_embeddings (chunked_text, vector_embeddings)
SELECT
c.chunk,
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAiModel)
FROM
table_with_text t
CROSS APPLY
AI_GENERATE_CHUNKS(source = t.text_to_chunk, chunk_type = FIXED, chunk_size = 100) c