Skip to content

Commit 05944b3

Browse files
authored
Merge pull request #35917 from AV25242/main
managed identity, references added
2 parents 1b06c48 + 0d128d3 commit 05944b3

File tree

4 files changed

+329
-144
lines changed

4 files changed

+329
-144
lines changed

docs/t-sql/functions/ai-generate-chunks-transact-sql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ This example uses `AI_GENERATE_EMBEDDINGS` with `AI_GENERATE_CHUNKS` to create e
174174
```sql
175175
INSERT INTO my_embeddings (chunked_text, vector_embeddings)
176176
SELECT c.chunk,
177-
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAiModel)
177+
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAIModel)
178178
FROM table_with_text AS t
179179
CROSS APPLY
180180
AI_GENERATE_CHUNKS (SOURCE = t.text_to_chunk, CHUNK_TYPE = FIXED, CHUNK_SIZE = 100) AS c;

docs/t-sql/functions/ai-generate-embeddings-transact-sql.md

Lines changed: 171 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: The AI_GENERATE_EMBEDDINGS function creates vector arrays for data.
44
author: jettermctedder
55
ms.author: bspendolini
66
ms.reviewer: randolphwest
7-
ms.date: 11/17/2025
7+
ms.date: 12/04/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -132,7 +132,11 @@ SELECT id,
132132
large_text,
133133
AI_GENERATE_EMBEDDINGS(c.chunk_text USE MODEL MyAzureOpenAIModel)
134134
FROM myTable
135-
CROSS APPLY AI_GENERATE_CHUNKS (SOURCE = large_text, CHUNK_TYPE = FIXED, CHUNK_SIZE = 10) AS c;
135+
CROSS APPLY AI_GENERATE_CHUNKS (
136+
SOURCE = large_text,
137+
CHUNK_TYPE = FIXED,
138+
CHUNK_SIZE = 10
139+
) AS c;
136140
```
137141

138142
### C. Create embeddings with a table update
@@ -170,9 +174,11 @@ SELECT id,
170174
FROM myTable;
171175
```
172176

173-
### F. A full example with chunking, AI_GENERATE_EMBEDDINGS, and model creation
177+
### F. A full example with model creation, chunking and embedding generation
174178

175-
The following example demonstrates an end-to-end process for making your data AI-ready:
179+
### [Azure OpenAI API Key](#tab/request-headers)
180+
181+
The following example demonstrates an end-to-end process for making your data AI-ready using Azure OpenAI API Key:
176182

177183
1. Use [CREATE EXTERNAL MODEL](../statements/create-external-model-transact-sql.md) to register and make your embedding model accessible.
178184

@@ -185,37 +191,50 @@ The following example demonstrates an end-to-end process for making your data AI
185191
> [!NOTE]
186192
> Replace `<password>` with a valid password.
187193
194+
Enable the external REST endpoint invocation on the database server:
195+
188196
```sql
189-
-- Enable the external REST endpoint invocation on the database server
190197
EXECUTE sp_configure 'external rest endpoint enabled', 1;
191198
RECONFIGURE WITH OVERRIDE;
192199
GO
200+
```
201+
202+
Create a database master key:
193203

194-
-- Create a master key for the database
204+
```sql
195205
IF NOT EXISTS (SELECT *
196206
FROM sys.symmetric_keys
197207
WHERE [name] = '##MS_DatabaseMasterKey##')
198208
BEGIN
199209
CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'<password>';
200210
END
201211
GO
212+
```
213+
214+
Create access credentials to Azure OpenAI using a key:
202215

203-
-- Create access credentials to Azure OpenAI using a key:
204-
CREATE DATABASE SCOPED CREDENTIAL [https://my-azure-openai-endpoint.openai.azure.com/]
216+
```sql
217+
CREATE DATABASE SCOPED CREDENTIAL [https://my-azure-openai-endpoint.cognitiveservices.azure.com/]
205218
WITH IDENTITY = 'HTTPEndpointHeaders', secret = '{"api-key":"YOUR_AZURE_OPENAI_KEY"}';
206219
GO
220+
```
207221

208-
-- Create an external model to call the Azure OpenAI embeddings REST endpoint
222+
Create an external model to call the Azure OpenAI embeddings REST endpoint:
223+
224+
```sql
209225
CREATE EXTERNAL MODEL MyAzureOpenAIModel
210226
WITH (
211-
LOCATION = 'https://my-azure-openai-endpoint.openai.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-05-15',
227+
LOCATION = 'https://my-azure-openai-endpoint.cognitiveservices.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-05-15',
212228
API_FORMAT = 'Azure OpenAI',
213229
MODEL_TYPE = EMBEDDINGS,
214230
MODEL = 'text-embedding-ada-002',
215-
CREDENTIAL = [https://my-azure-openai-endpoint.openai.azure.com/]
231+
CREDENTIAL = [https://my-azure-openai-endpoint.cognitiveservices.azure.com/]
216232
);
233+
```
234+
235+
Create a table with text to chunk and insert data:
217236

218-
-- Create a table with text to chunk and insert data
237+
```sql
219238
CREATE TABLE textchunk
220239
(
221240
text_id INT IDENTITY (1, 1) PRIMARY KEY,
@@ -224,30 +243,158 @@ CREATE TABLE textchunk
224243
GO
225244

226245
INSERT INTO textchunk (text_to_chunk)
227-
VALUES
228-
('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.'),
229-
('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')
246+
VALUES ('All day long we seemed to dawdle through a land 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.'),
247+
('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')
230248
GO
249+
```
250+
251+
Create a new table to hold the chunked text and vector embeddings:
231252

232-
-- Create a new table to hold the chunked text and vector embeddings
253+
```sql
233254
CREATE TABLE text_embeddings
234255
(
235256
embeddings_id INT IDENTITY (1, 1) PRIMARY KEY,
236257
chunked_text NVARCHAR (MAX),
237258
vector_embeddings VECTOR(1536)
238259
);
260+
```
239261

240-
-- Insert the chunked text and vector embeddings into the text_embeddings table using AI_GENERATE_CHUNKS and AI_GENERATE_EMBEDDINGS
262+
Insert the chunked text and vector embeddings into the text_embeddings table using `AI_GENERATE_CHUNKS` and `AI_GENERATE_EMBEDDINGS`:
263+
264+
```sql
241265
INSERT INTO text_embeddings (chunked_text, vector_embeddings)
242-
SELECT c.chunk, AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAIModel)
243-
FROM textchunk t
244-
CROSS APPLY
245-
AI_GENERATE_CHUNKS(source = t.text_to_chunk, chunk_type = FIXED, chunk_size = 100) c;
266+
SELECT c.chunk,
267+
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAIModel)
268+
FROM textchunk AS t
269+
CROSS APPLY AI_GENERATE_CHUNKS (
270+
SOURCE = t.text_to_chunk,
271+
CHUNK_TYPE = FIXED,
272+
CHUNK_SIZE = 100
273+
) AS c;
274+
```
275+
276+
View the results
277+
278+
```sql
279+
SELECT *
280+
FROM text_embeddings;
281+
```
282+
283+
### [Managed Identity](#tab/managed-identity)
284+
285+
The following example demonstrates an end-to-end process for making your data AI-ready using Managed Identity:
246286

247-
-- View the results
248-
SELECT * FROM text_embeddings;
287+
1. Enable [Managed Identity](../statements/create-external-model-transact-sql.md#managed-identity). This option applies to SQL Server Azure Arc / Azure Virtual Machine (VM) based deployments.
288+
289+
1. Use [CREATE EXTERNAL MODEL with Azure OpenAI using Managed Identity](../statements/create-external-model-transact-sql.md#create-an-external-model-with-azure-openai-using-managed-identity) to register and make your embedding model accessible using Managed Identity.
290+
291+
1. Split the dataset into smaller chunks with [AI_GENERATE_CHUNKS](ai-generate-chunks-transact-sql.md), so the data fits within the model's context window and improves retrieval accuracy.
292+
293+
1. Generate embeddings using `AI_GENERATE_EMBEDDINGS`.
294+
295+
1. Insert the results into a table with a [vector data type](../data-types/vector-data-type.md).
296+
297+
> [!NOTE]
298+
> Replace `<password>` with a [valid password](../statements/create-master-key-transact-sql.md#password-password).
299+
300+
Enable managed identity for authentication:
301+
302+
```sql
303+
EXECUTE sp_configure 'allow server scoped db credentials', 1;
304+
RECONFIGURE WITH OVERRIDE;
249305
```
250306

307+
Enable the external REST endpoint invocation on the database server:
308+
309+
```sql
310+
EXECUTE sp_configure 'external rest endpoint enabled', 1;
311+
RECONFIGURE WITH OVERRIDE;
312+
GO
313+
```
314+
315+
Create a database master key:
316+
317+
```sql
318+
IF NOT EXISTS (SELECT *
319+
FROM sys.symmetric_keys
320+
WHERE [name] = '##MS_DatabaseMasterKey##')
321+
BEGIN
322+
CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'<password>';
323+
END
324+
GO
325+
```
326+
327+
Create access credentials to Azure OpenAI using a key:
328+
329+
```sql
330+
CREATE DATABASE SCOPED CREDENTIAL [https://my-azure-openai-endpoint.cognitiveservices.azure.com/]
331+
WITH IDENTITY = 'Managed Identity', secret = '{"resourceid":"https://cognitiveservices.azure.com"}';
332+
GO
333+
```
334+
335+
Create an external model to call the Azure OpenAI embeddings REST endpoint:
336+
337+
```sql
338+
CREATE EXTERNAL MODEL MyAzureOpenAIModel
339+
WITH (
340+
LOCATION = 'https://my-azure-openai-endpoint.cognitiveservices.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-05-15',
341+
API_FORMAT = 'Azure OpenAI',
342+
MODEL_TYPE = EMBEDDINGS,
343+
MODEL = 'text-embedding-ada-002',
344+
CREDENTIAL = [https://my-azure-openai-endpoint.cognitiveservices.azure.com/]
345+
);
346+
```
347+
348+
Create a table with text to chunk and insert data:
349+
350+
```sql
351+
CREATE TABLE textchunk
352+
(
353+
text_id INT IDENTITY (1, 1) PRIMARY KEY,
354+
text_to_chunk NVARCHAR (MAX)
355+
);
356+
GO
357+
358+
INSERT INTO textchunk (text_to_chunk)
359+
VALUES ('All day long we seemed to dawdle through a land 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.'),
360+
('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')
361+
GO
362+
```
363+
364+
Create a new table to hold the chunked text and vector embeddings:
365+
366+
```sql
367+
CREATE TABLE text_embeddings
368+
(
369+
embeddings_id INT IDENTITY (1, 1) PRIMARY KEY,
370+
chunked_text NVARCHAR (MAX),
371+
vector_embeddings VECTOR(1536)
372+
);
373+
```
374+
375+
Insert the chunked text and vector embeddings into the text_embeddings table using `AI_GENERATE_CHUNKS` and `AI_GENERATE_EMBEDDINGS`:
376+
377+
```sql
378+
INSERT INTO text_embeddings (chunked_text, vector_embeddings)
379+
SELECT c.chunk,
380+
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyAzureOpenAIModel)
381+
FROM textchunk AS t
382+
CROSS APPLY AI_GENERATE_CHUNKS (
383+
SOURCE = t.text_to_chunk,
384+
CHUNK_TYPE = FIXED,
385+
CHUNK_SIZE = 100
386+
) AS c;
387+
```
388+
389+
View the results:
390+
391+
```sql
392+
SELECT *
393+
FROM text_embeddings;
394+
```
395+
396+
---
397+
251398
## Related content
252399

253400
- [CREATE EXTERNAL MODEL (Transact-SQL)](../statements/create-external-model-transact-sql.md)
@@ -256,3 +403,4 @@ SELECT * FROM text_embeddings;
256403
- [AI_GENERATE_CHUNKS (Transact-SQL)](ai-generate-chunks-transact-sql.md)
257404
- [sp_invoke_external_rest_endpoint](../../relational-databases/system-stored-procedures/sp-invoke-external-rest-endpoint-transact-sql.md)
258405
- [Create and deploy an Azure OpenAI in Azure AI Foundry Models resource](/azure/ai-services/openai/how-to/create-resource)
406+
- [Connect your SQL Server to Azure Arc](../../sql-server/azure-arc/connect.md#connect-your-sql-server-to-azure-arc)

0 commit comments

Comments
 (0)