Book a Demo Start Instantly

In the age of Generative AI (GenAI) and Large Language Models (LLMs), MySQL users have faced challenges in building AI applications such as Retrieval-Augmented Generation (RAG) apps due to the lack of native vector storage, essential vector functions, performance issues, and scalability limits. However, with TiDB Serverless, a MySQL-compatible database that includes vector storage, these challenges can be overcome. This tutorial will guide you through building a RAG app using LlamaIndex and TiDB Serverless, Which is a MySQL Compatible database and its built-in Vector Storage.


To get started, ensure you have the following:

  • A running TiDB Serverless cluster with vector search enabled
  • Python 3.8 or later
  • OpenAI API key

👉 Try the most advanced MySQL vector solution with TiDB Serverless.

Join the Waitlist


Step-by-Step Guide

1. Clone the Repository

First, clone the example repository from GitHub:

git clone
2. Set Up a Virtual Environment

Navigate to the project directory and set up a virtual environment:

cd tidb-vector-python/examples/llamaindex-tidb-vector
python3 -m venv .venv
source .venv/bin/activate
3. Install Dependencies

Install the required dependencies:

pip install -r requirements.txt
4. Set Environment Variables

Set the necessary environment variables for OpenAI and TiDB:

export OPENAI_API_KEY="your_openai_api_key"
export TIDB_HOST="your_tidb_host"
export TIDB_USERNAME="your_tidb_username"
export TIDB_PASSWORD="your_tidb_password"
5. Run the Example

To see the application in action, run the provided script:

python --url ""

This script allows you to interact with a specified URL and ask questions based on the content.

Example Code Breakdown

Here’s a detailed look at the example code:

import os
import click
from sqlalchemy import URL
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.tidbvector import TiDBVectorStore
from llama_index.readers.web import SimpleWebPageReader

# Configure the connection URL
tidb_connection_url = URL(
    query={"ssl_verify_cert": True, "ssl_verify_identity": True},

# Set up the vector store
tidbvec = TiDBVectorStore(
    vector_dimension=1536,  # The dimension is decided by the model

tidb_vec_index = VectorStoreIndex.from_vector_store(tidbvec)
storage_context = StorageContext.from_defaults(vector_store=tidbvec)
query_engine = tidb_vec_index.as_query_engine(streaming=True)

def do_prepare_data(url):
    documents = SimpleWebPageReader(html_to_text=True).load_data([url])
    tidb_vec_index.from_documents(documents, storage_context=storage_context, show_progress=True)

_default_url = ''

@click.option('--url', default=_default_url, help=f'URL you want to talk to, default={_default_url}')
def chat_with_url(url):
    while True:
        question = click.prompt("Enter your question")
        response = query_engine.query(question)

if __name__ == '__main__':

Expanding More AI Application Examples with TiDB Serverless Vector Storage

Unlocking the potential of TiDB Serverless vector storage extends beyond traditional data management, offering a gateway to innovative AI applications. Here are some concise examples:


Using TiDB Serverless with built-in vector storage support enables you to build powerful AI applications leveraging semantic and similarity search capabilities. This tutorial covered the setup of TiDB Serverless, creating tables with vector fields, and performing vector search operations. For more detailed documentation and advanced features, refer to the TiDB Vector Search Documentation.

Additional Resources

Feel free to reach out on Discord or the TiDB Support Portal for assistance. Happy coding!

👉 Build your own RAG application with LlamaIndex and MySQL-Compatible Database TiDB Serverless.

Join the Waitlist

Last updated May 21, 2024

Spin up a Serverless database with 25GiB free resources.

Start Now