SQLServerCentral Article

How to connect your SQL Server 2025 semantic search for free

,

Introduction

In previous articles, we learned how to use AI-powered semantic search in SQL Server 2025. To summarize, we use AI to create a smarter search in our database. It is similar to Google Search, but it allows us to perform AI-powered semantic searches directly from T-SQL. Previously, we used the LIKE operator in T-SQL or Full-Text search to retrieve information from tables. However, with the help of the AI, we can do a smarter semantic search using AI.

In the previous article, we used SQL Server, an OpenAI REST API. In SQL Server, we created an external model to connect to the OpenAI REST API.

Now, instead of paying for API tokens, we will use Ollama to run a local AI model and perform semantic searches without per-token API charges. The following diagram explains the new structure.

Requirements

  1. First of all, you will need to have SQL Server installed.
  2. Secondly, SSMS installed.
  3. Thirdly, Ollama

Overview

To configure our own REST API and use it with SQL Server, we will need the following:

  1. Install the nomic-embed-text model in Ollama.
  2. Enable the SQL Server configuration options to support REST endpoints
  3. Configure an HTTPS certificate in the REST API
  4. Install Caddy to have an HTTPS Certificate (we used Caddy, but you can use any option from section 3).
  5. Create the external Ollama model in SQL Server.
  6. Test the AI SQL Server 2025 semantic search.

Install the nomic-embed-text AI model in Ollama for semantic search

Nomic AI developed a nice embedding model. The main objective is to convert text to vectors. The AI uses vectors to find relationships between words. For more information about vectors in AI, refer to our Vectors in SQL Server 2025 article to understand how the AI works.

To install this model, in the command line, run this:

ollama pull   nomic-embed-text

This will install the AI model in Ollama.

Enable the SQL Server configuration options to support REST endpoints

In SSMS, connect to your SQL Server. To verify that the option is enabled, use this query to check if the value is 1 (enabled) or 0 disabled. Verify the run_value.

EXEC sp_configure 'external rest endpoint enabled'

If it is disabled, use the following commands to enable it.

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'external rest endpoint enabled', 1;
RECONFIGURE;
GO

These commands will enable you to connect to the REST API in Ollama. If you do not enable the REST endpoint, you will have the following error message:

Msg 31643, Level 16, State 2, Line 17: ' ai_generate_embeddings' is disabled on this instance of SQL Server. Use sp_configure 'external rest endpoint enabled' to enable it.

Configure an HTTPS certificate in REST API for semantic search

For security reasons, to create an external model in SQL Server, you need a secure HTTPS certificate. This will provide a secure connection.

The REST API that connects to Ollama and our embedded model uses a simple HTTP connection by default.

To create an HTTPS certificate, there are several options. Here are some:

  • Caddy is a simple and modern web server created in Go. You can easily configure HTTPS there.
  • You could use OpenSSL to create your certificate and then NGIX as the web server.
  • Traefik is another option. It is particularly well suited to Docker environments. It is an open-source application proxy.
  • HAProxy is an advanced option for high traffic and load balancing. It is ideal for distributed environments.

In this example, we will use Caddy, but you can use any other option.

Installing Caddy to have an HTTPS Certificate server for semantic search

In this option, we will use Caddy, but you can use any other option.

To configure Caddy, follow these steps:

  1. First, download the file and install Caddy.
  2. In the Caddy folder, configure the Caddyfile with something like this:
https://192.168.1.8:11435 {
tls internal
reverse_proxy localhost:11434
}

Where 192.168.1.8 is the IP of the local machine (in this case, everything was installed on the same Windows desktop). The protocol used is HTTPS. TLS internal means that we will use TLS certificates using an internal certification authority. The reverse proxy forwards requests to Ollama on port 11434 and exposes the service over HTTPS on port 11435.

Also, you need to import the root.crt file in Windows. Usually, you can find the certificate here: PPDATACaddy\pki\authorities\local

In addition, we need to import the Caddy certificate in Windows. For more information about importing certificates, refer to this link. This step certifies the Caddy certificate in SQL Server, confirming that the IP is safe.

Finally, restart the SQL Server service to refresh the certificates.

Create the external Ollama model in SQL Server

In this step, we will create an external model in T-SQL. The external model will allow us to connect to the embedded model used to convert our search words into vectors, and with that information, we will use AI to create smarter queries.

First, we need a master key to create the external model. The following example shows how to create a master key using T-SQL if you do not have one.

CREATE MASTER KEY ENCRYPTION BY PASSWORD= 'MysecurEP4$w0rd123!'
GO

This key is used to protect the credentials and to connect SQL Server with Ollama using Caddy. Use a secure password of your preference.

Secondly, we will create a database-scoped credential.

CREATE DATABASE SCOPED CREDENTIAL [https://192.168.1.8:11435/]
WITH IDENTITY = 'HTTPEndpointHeaders',
SECRET = '{}';

The scoped credential registers a credential inside the database. We provide the IP of the endpoint and the port ([https://192.168.1.8:11435/]). We also indicate that the identity uses credentials based on HTTP headers (IDENTITY = 'HTTPEndpointHeaders'). Finally, the secret is empty ('{}') because no API key or header is required in this case.

Thirdly, we will create the external model named OllamaNomicEmbed.

CREATE EXTERNAL MODEL OllamaNomicEmbed
WITH (
LOCATION = 'https://192.168.1.8:11435/v1/embeddings',
API_FORMAT = 'OpenAI',
MODEL_TYPE = EMBEDDINGS,
MODEL = 'nomic-embed-text',
CREDENTIAL = [https://192.168.1.8:11435/]
);

This statement registers the AI model that SQL Server will use for the embeddings.

Here is an explanation of the parameters:

  • First, we have the location that includes the HTTPS address.
  • Secondly, we have the API format used. We usually work with the OpenAI format. We are using the OpenAI format, but not any OpenAI model.
  • Thirdly, the model_type embeddings means that it is a model used to convert text into vectors.
  • Model is the name of the model used. In this case, we are using the nomic embed text model to convert text into vectors.
  • Finally, CREDENTIAL specifies the database-scoped credential associated with the endpoint

To grant permissions to other users, use these commands:

GRANT EXECUTE ON EXTERNAL MODEL::OllamaNomicEmbed
TO [YourUser];
GO

Test the AI SQL Server 2025 semantic search

For this test, we create a table with some movies.

CREATE TABLE MovieDemoEN (
Id              INT            IDENTITY (1, 1) PRIMARY KEY,
Title           NVARCHAR (100),
Synopsis        NVARCHAR (MAX),
VectorEmbedding VECTOR(768)
);

We add data to the table:

INSERT  INTO MovieDemoEN (Title, Synopsis, VectorEmbedding)

VALUES                  ('The Matrix', 'A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.', AI_GENERATE_EMBEDDINGS('A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.' USE MODEL OllamaNomicEmbed)),
('Inception', 'A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.', AI_GENERATE_EMBEDDINGS('A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.' USE MODEL OllamaNomicEmbed)),
('Interstellar', 'A team of explorers travel through a wormhole in space in an attempt to ensure humanity’s survival.', AI_GENERATE_EMBEDDINGS('A team of explorers travel through a wormhole in space in an attempt to ensure humanity survival.' USE MODEL OllamaNomicEmbed)),
('Star Wars: A New Hope', 'The Imperial Forces hold galaxy hostage, but a rebellious farm boy joins a galactic alliance to destroy the space station.', AI_GENERATE_EMBEDDINGS('The Imperial Forces hold galaxy hostage, but a rebellious farm boy joins a galactic alliance to destroy the space station.' USE MODEL OllamaNomicEmbed)),
('Avatar', 'A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.', AI_GENERATE_EMBEDDINGS('A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.' USE MODEL OllamaNomicEmbed)),
('Back to the Future', 'Marty McFly, a 17-year-old high school student, is accidentally sent thirty years into the past in a time-traveling DeLorean invented by his close friend.', AI_GENERATE_EMBEDDINGS('Marty McFly, a 17-year-old high school student, is accidentally sent thirty years into the past in a time-traveling DeLorean invented by his close friend.' USE MODEL OllamaNomicEmbed)),
('Gravity', 'Two astronauts work together to survive after an accident leaves them stranded in space.', AI_GENERATE_EMBEDDINGS('Two astronauts work together to survive after an accident leaves them stranded in space.' USE MODEL OllamaNomicEmbed)),
('The Lord of the Rings: The Fellowship of the Ring', 'A meek Hobbit from the Shire and eight companions set out on a journey to destroy the powerful One Ring and save Middle-earth from the Dark Lord.', AI_GENERATE_EMBEDDINGS('A meek Hobbit from the Shire and eight companions set out on a journey to destroy the powerful One Ring and save Middle-earth from the Dark Lord.' USE MODEL OllamaNomicEmbed)),
('Jurassic Park', 'During a preview tour, a theme park suffers a major power breakdown that allows its cloned dinosaur exhibits to run amok.', AI_GENERATE_EMBEDDINGS('During a preview tour, a theme park suffers a major power breakdown that allows its cloned dinosaur exhibits to run amok.' USE MODEL OllamaNomicEmbed)),
('Gladiator', 'A former Roman general sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.', AI_GENERATE_EMBEDDINGS('A former Roman general sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.' USE MODEL OllamaNomicEmbed)),
('Pan Labyrinth', 'In the falangist Spain of 1944, the bookish young stepdaughter of a sadistic army officer escapes into an eerie but captivating mythical world.', AI_GENERATE_EMBEDDINGS('In the falangist Spain of 1944, the bookish young stepdaughter of a sadistic army officer escapes into an eerie but captivating mythical world.' USE MODEL OllamaNomicEmbed)),
('Toy Story', 'A little boy toys come to life whenever he is not around, leading to rivalry and friendship dynamics between an old-fashioned cowboy and a modern space action figure.', AI_GENERATE_EMBEDDINGS('A little boy toys come to life whenever he is not around, leading to rivalry and friendship dynamics between an old-fashioned cowboy and a modern space action figure.' USE MODEL OllamaNomicEmbed)),
('The Lion King', 'Lion prince Simba and his father are targeted by his bitter uncle, who wants to ascend the throne himself.', AI_GENERATE_EMBEDDINGS('Lion prince Simba and his father are targeted by his bitter uncle, who wants to ascend the throne himself.' USE MODEL OllamaNomicEmbed)),
('Finding Nemo', 'After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home.', AI_GENERATE_EMBEDDINGS('After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home.' USE MODEL OllamaNomicEmbed)),
('Forrest Gump', 'The presidencies of Kennedy and Johnson, the Vietnam War, and other historical events unfold from the perspective of an Alabama man with an low IQ.', AI_GENERATE_EMBEDDINGS('The presidencies of Kennedy and Johnson, the Vietnam War, and other historical events unfold from the perspective of an Alabama man with an low IQ.' USE MODEL OllamaNomicEmbed)),
('Titanic', 'A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.', AI_GENERATE_EMBEDDINGS('A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.' USE MODEL OllamaNomicEmbed)),
('Pulp Fiction', 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.', AI_GENERATE_EMBEDDINGS('The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.' USE MODEL OllamaNomicEmbed)),
('Whiplash', 'A promising young drummer enrolls at a cut-throat music conservatory where his dreams of greatness are mentored by an instructor who will stop at nothing.', AI_GENERATE_EMBEDDINGS('A promising young drummer enrolls at a cut-throat music conservatory where his dreams of greatness are mentored by an instructor who will stop at nothing.' USE MODEL OllamaNomicEmbed)),
('Parasite', 'Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.', AI_GENERATE_EMBEDDINGS('Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.' USE MODEL OllamaNomicEmbed)),
('Avengers: Endgame', 'After the devastating events of Infinity War, the universe is in ruins. The Avengers assemble once more to reverse Thanos actions and restore balance.', AI_GENERATE_EMBEDDINGS('After the devastating events of Infinity War, the universe is in ruins. The Avengers assemble once more to reverse Thanos actions and restore balance.' USE MODEL OllamaNomicEmbed)),
('Mad Max: Fury Road', 'In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in search for her homeland with the help of a group of female prisoners and a drifter.', AI_GENERATE_EMBEDDINGS('In a post-apocalyptic wasteland, a woman rebels against a tyrannical ruler in search for her homeland with the help of a group of female prisoners and a drifter.' USE MODEL OllamaNomicEmbed)),
('Die Hard', 'An NYPD officer tries to save his wife and several others taken hostage by German terrorists during a Christmas party in a Los Angeles skyscraper.', AI_GENERATE_EMBEDDINGS('An NYPD officer tries to save his wife and several others taken hostage by German terrorists during a Christmas party in a Los Angeles skyscraper.' USE MODEL OllamaNomicEmbed)),
('The Exorcist', 'When a young girl is possessed by a mysterious entity, her mother seeks the help of two priests to save her.', AI_GENERATE_EMBEDDINGS('When a young girl is possessed by a mysterious entity, her mother seeks the help of two priests to save her.' USE MODEL OllamaNomicEmbed));

Finally, we test the model using the vector distance function. This function will find the closest matches using AI and vectors.

The following example will list the three movies most closely related to love and violence.

SELECT   TOP 3 Title,
Synopsis,
VECTOR_DISTANCE('cosine', VectorEmbedding, CAST (AI_GENERATE_EMBEDDINGS('love and violence' USE MODEL OllamaNomicEmbed) AS VECTOR(768))) AS SimilarityDistance
FROM     MovieDemoEN
ORDER BY SimilarityDistance ASC

GO

The result of the query is this one:

As you can see, I was looking for a movie related to love and violence. Using the VECTOR_DISTANCE function, the AI finds the related words and finds the movie whose synopsis is most similar to the query. For more information about the VECTOR_DISTANCE, read this article.

In this case, Pulp Fiction, which includes the story of love between Butch Coolidge and Fabienne. We can also see a lot of violence. You may wonder why Toy Story is here. Well, because it is about friendship (related to love) and rivalry between Buzz and Woody. Is there some violence?

Well, a little bit.

Conclusion

Now that we have configured the model, we can test semantic search without paying for external API tokens. To summarize, we need to have an HTTPS connection; we need Ollama, an embedded model installed in Ollama, and then we create credentials and the external model in SQL. Once installed, we can now have smarter queries with AI for free.

I hope you enjoy them.

 

 

 

 

 

 

 

 

 

Rate

★ ★ ★ ★ ★ ★ ★ ★ ★ ★

You rated this post out of 5. Change rating

Share

Share

Rate

★ ★ ★ ★ ★ ★ ★ ★ ★ ★

You rated this post out of 5. Change rating