erincon01 / Vector

Vector type implementation in SQL CLR - before SQL Server 2025
MIT License
0 stars 1 forks source link

Example Code for REST API Calls and Vector Calculations in SQL Server

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.

Table of Contents

What's new

Features

  1. REST API Integration:

    • myRestEndpoint.cs: Provides a SQL CLR stored procedure to invoke REST APIs from SQL Server. Supports:
      • GET and POST methods.
      • Custom headers in JSON format.
      • Payload handling for POST requests.
  2. Vector Type and Operations:

    • myVector.cs: Implements a SQL CLR User-Defined Type (UDT) for vectors, allowing:
      • Serialization and deserialization of vectors.
      • Support for multiple distance metrics (Cosine, Euclidean, Manhattan, and (dot) Negative Inner Product).
      • Usage within SQL Server for advanced calculations.

Files in this Repository

REST API Integration

Vector Operations

Requirements

Limitations

Compile

Usage

  1. 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.

    Example for Chat Completion:

    
    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;
  1. Local Embeddings Calculation: Use the sp_invoke_ollama_model stored procedure to calculate embeddings locally with a compatible endpoint.

    Example for Local Embeddings:

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;
  1. Vector Calculations:
    • Deploy the Vector UDT to your SQL Server.
    • Use the VECTOR_DISTANCE function to calculate distances between vectors.
    • Example SQL query:
      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;

Pending Work

Disclaimer

This repository is for educational and development purposes only. It is not designed for use in production environments. Use it at your own risk.

License

This project is licensed under the MIT License. See the LICENSE file for details.