SQLServerCentral Article

Building a Simple SQL/AI Environment

,

Al is everywhere....almost.

In my day-to-day role as a DBA, I don't really have the opportunity or need to use AI in any meaningful way. I don't need it to write code for me, or explain how SQL Server works. My AI use is limited to the summaries returned in my Google search results.

But I am curious. How does it work, what can I do with it, can I build my own lab?

GPT for All

I wanted to build a ChatGPT style AI interface that I could play with. My main requirements were it had to run locally and I had to be able to use it with t-sql. My search led me to an open source project named GPT4All (https://www.nomic.ai/gpt4all).

GPT4All runs locally and can be configured to keep all data local. It also has an API that can be leveraged in T-SQL code.

After getting GPT4All installed and adding an AI model to use (in the example code below I am using the Phi-3 Mini Instruct model, adjust to whatever model you’ve installed), key settings are: Enable Datalake and Enable Local API Server:

Leaving Enable Datalake unchecked will keep all of your interactions with the model local. Enabling the Local API Server will allow you to interact with the model outside of the GPT4All environment.

There are many options for what model you use, how it is configured, and how it behaves. There is even a "Local Docs" option for having your AI interactions reference documents of various types and provide source references to any info used. The application is pretty straightforward to figure out but refer to the GPT4All official documentation for functionality, configuration and use instructions. As the project matures, I'm sure functionality will be expanded.

On my local SQL Server Developer Edition instance I created a stored procedure to handle my interaction with the GPT4All API:

CREATE PROCEDURE [dbo].[AskAI] @Question VARCHAR(8000)
AS
BEGIN
    DECLARE @psCommand VARCHAR(8000);
    DECLARE @response VARCHAR(8000);

    SET @Question = REPLACE(@Question, '"', '\"');
    SET @psCommand
        = 'powershell -Command "$response = Invoke-WebRequest -URI http://localhost:4891/v1/chat/completions -Method POST -ContentType application/json -Body ''{\"model\":\"Phi-3 Mini Instruct\",\"messages\":[{\"role\":\"user\",\"content\":\"'
          + @Question
          + '\"}],\"max_tokens\":2048,\"temperature\":0.25}''; $response.Content | Out-File -FilePath ''C:\Utils\MSSQL\2022\GPT\phi3_response.json'' -Encoding UTF8"';

    EXEC xp_cmdshell @psCommand;

    SELECT @response = BulkColumn
    FROM
        OPENROWSET(BULK 'C:\Utils\MSSQL\2022\GPT\phi3_response.json', SINGLE_CLOB)
        AS Contents;

    SELECT @Question;
    SELECT JSON_VALUE(@response, '$.choices[0].message.content') AS AssistantReply;
END;

Passing a question or request into the stored proc will produce an Al generated answer:

For now, this is a toy. I’m exploring how the different models behave with different configurations and looking to see how varied the responses are. Getting a feel for what each is good at.

I have started thinking about a few experiments I want to try. Some may be absolute failures, but those are the best learning experiences. If you build anything interesting with this (or any AI), post in the comments. I’d be curious to hear what you come up with.

 

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating