TiDB Cloud, is a comprehensive Database-as-a-Service (DBaaS) solution, that provides dedicated and serverless options. TiDB Serverless is now integrating a built-in vector search into the MySQL landscape. With this enhancement, you can seamlessly develop AI applications using TiDB Serverless without the need for a new database or additional technical stacks. Create a free TiDB Serverless cluster and start using the vector search feature at pingcap.com/ai.This guide provides a detailed guide on utilizing the TiDB Vector functionality, showcasing its features and practical applications.
Setting up environments
Begin by installing the necessary packages.Semantic similarity search
TiDB supports both cosine and Euclidean distances (‘cosine’, ‘l2’), with ‘cosine’ being the default choice. The code snippet below creates a table namedTABLE_NAME
in TiDB, optimized for vector searching. Upon successful execution of this code, you will be able to view and access the TABLE_NAME
table directly within your TiDB database.
Filter with metadata
perform searches using metadata filters to retrieve a specific number of nearest-neighbor results that align with the applied filters.Supported metadata types
Each vector in the TiDB Vector Store can be paired with metadata, structured as key-value pairs within a JSON object. The keys are strings, and the values can be of the following types:- String
- Number (integer or floating point)
- Booleans (true, false)
Metadata filter syntax
The available filters include:- $or - Selects vectors that meet any one of the given conditions.
- $and - Selects vectors that meet all of the given conditions.
- $eq - Equal to
- $ne - Not equal to
- $gt - Greater than
- $gte - Greater than or equal to
- $lt - Less than
- $lte - Less than or equal to
- $in - In array
- $nin - Not in array
Using as a Retriever
In LangChain, a retriever is an interface that retrieves documents in response to an unstructured query, offering a broader functionality than a vector store. The code below demonstrates how to utilize TiDB Vector as a retriever.Advanced Use Case Scenario
Let’s look a advanced use case - a travel agent is crafting a custom travel report for clients who desire airports with specific amenities such as clean lounges and vegetarian options. The process involves:- A semantic search within airport reviews to extract airport codes meeting these amenities.
- A subsequent SQL query that joins these codes with route information, detailing airlines and destinations aligned with the clients’ preferences.
Delete
You can remove the TiDB Vector Store by using the.drop_vectorstore()
method.