How to implement Edge SQL Vector Search
Vector Search is an Azion Edge SQL feature that enables customers to implement semantic search engines. While traditional search models aim to find exact matches, such as keyword matches, vector search models use specialized algorithms to identify similar items based on their mathematical representations, or vector embeddings.
Go to Vector Search ReferenceAs an example for implementation, this guide will cover setting up the vector search logic in a TypeScript application, with a database using the Langchain library with OpenAI and the Azion SQL API.
Requirements
- Generate an Azion personal token.
- Generate an OpenAI API Key.
- Install Azion CLI.
- Create a typescript application.
- As in the example, you can use the Azion CLI to create a Simple Typescript Router application.
- Set up Edge SQL.
- Install the Azion Libraries.
Setting up the main.ts file
The first stage is setting up the main.ts
file, the entry point for your implementation. It establishes the connection to the Azion API and OpenAI, configures the environment using credentials, and contains the main logic for performing and testing vector-based search queries.
- In the
.env
file, add your Azion personal token and OpenAI API Key as variables. - Set up the
main.ts
file, using the following structure as an example:
In summary, this code creates a TypeScript function called vectorSearch
, which implements a semantic search feature in Azion Edge SQL:
- Database setup: it connects to a database (
vectorDatabase
), and creates adocuments
table withcontent
and an embedding column, indexing the embeddings for faster vector searches. - Embedding model: it uses the
text-embedding-3-small
model to generate vector embeddings for each document. - Insert data: it inserts sample text data with embeddings into the database.
- Query execution: searches for the most relevant documents related to a given query, using similarity to rank the results.
- Error handling: each major operation has an error handling configuration, ensuring that a JSON error message is returned if an error occurs.
Run and test vector search
Now, it’s time to run your application and test the vector search implementation. To do so:
- Create your application locally using the
azion build
command. - Start a local development server for the application with the
azion dev
command. - With the application running, query Azion Edge SQL to create the database in the platform.
- Send your query to the localhost with the value.
- For example:
What is the capital of Brazil?
- For example:
- The server should interpret the query and return an appropriate response.
- In this case, the server returns a vector representation of the query and you’ll receive a list of the documents most similar to the query.
- If searching in the Edge SQL shell, you can find the information inserted into the database table of the
vectorDatabase
database.
Done! You’ve implemented a vector search in your application. Now you can refine and adapt it to attend better to your use case.