This repository contains example code for demonstrating REST API integration and vector manipulation within a SQL Server context. Note: This is development-level code and is not intended for production use. It is meant to showcase how to implement these features in SQL Server using SQL CLR assemblies.
REST API Integration:
myRestEndpoint.cs
: Provides a SQL CLR stored procedure to invoke REST APIs from SQL Server. Supports:
Vector Type and Operations:
myVector.cs
: Implements a SQL CLR User-Defined Type (UDT) for vectors, allowing:
myRestEndpoint.cs
:
myRestEndpoint.csproj
:
myVector.cs
:
myVector.csproj
:
myVector.sln
:
dbo.
prefix when invoking vector functions.REST API Calls:
Deploy the myRestEndpoint
assembly to your SQL Server and use the sp_invoke_external_rest_endpoint2
stored procedure to call REST APIs directly from SQL Server.
DECLARE @response NVARCHAR(MAX);
DECLARE @chat_completion NVARCHAR(MAX);
EXEC dbo.sp_invoke_external_rest_endpoint2 @url = 'https://xxxxx.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2023-05-15', @method = 'POST', @payload = N'{"messages": [{"role": "system", "content": "Your system message"}, {"role": "user", "content": "Your user prompt"}], "temperature": 0.1, "max_tokens": 1000}', @headers = '{"Content-Type":"application/json", "api-key":"zzzzzz"}', @response = @response OUTPUT;
SET @chat_completion = CAST(JSON_VALUE(@response, '$.choices[0].message.content') AS NVARCHAR(MAX)); SELECT @chat_completion AS Content;
### Example for Azure OpenAI Embeddings:
```sql
DECLARE @response NVARCHAR(MAX);
DECLARE @vector NVARCHAR(MAX);
EXEC dbo.sp_invoke_external_rest_endpoint2
@url = 'https://xxxxxx.openai.azure.com/openai/deployments/gpt-4o-mini/embeddings?api-version=2023-05-15',
@method = 'POST',
@payload = N'{"input": "Your input text"}',
@headers = '{"Content-Type":"application/json", "api-key":"zzzzzz"}',
@response = @response OUTPUT;
WITH ParsedEmbedding AS (
SELECT value
FROM OPENJSON(@response, '$.data[0].embedding')
)
SELECT @vector = STRING_AGG(value, ',')
FROM ParsedEmbedding;
SELECT @vector AS Embedding;
Local Embeddings Calculation: Use the sp_invoke_ollama_model stored procedure to calculate embeddings locally with a compatible endpoint.
DECLARE @response NVARCHAR(MAX);
DECLARE @vector VECTOR;
EXEC dbo.sp_invoke_ollama_model
@endpoint = 'http://localhost:11434/api/embeddings',
@model = 'all-minilm',
@prompt = 'The sky is blue because of Rayleigh scattering',
@response = @response OUTPUT;
WITH ParsedEmbedding AS (
SELECT value
FROM OPENJSON(@response, '$.embedding')
)
SELECT @vector = CAST(STRING_AGG(value, ',') AS VECTOR)
FROM ParsedEmbedding;
SELECT
@vector AS value,
@vector.Size() AS size,
CAST(@vector AS NVARCHAR(MAX)) AS text;
Vector
UDT to your SQL Server.VECTOR_DISTANCE
function to calculate distances between vectors.DECLARE @v1 Vector = '[1.0, 2.0, 3.0]';
DECLARE @v2 Vector = '[4.0, 5.0, 6.0]';
SELECT dbo.VectorDistance('euclidean', @v1, @v2) AS Distance;
This repository is for educational and development purposes only. It is not designed for use in production environments. Use it at your own risk.
This project is licensed under the MIT License. See the LICENSE
file for details.