{"id":17764,"date":"2024-06-23T08:07:46","date_gmt":"2024-06-23T15:07:46","guid":{"rendered":"https:\/\/www.pingcap.com\/?post_type=article&#038;p=17764"},"modified":"2025-08-18T05:25:43","modified_gmt":"2025-08-18T12:25:43","slug":"build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search","status":"publish","type":"article","link":"https:\/\/www.pingcap.com\/ko\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/","title":{"rendered":"Build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search &#8211; a MySQL-compatible Database"},"content":{"rendered":"<p>In the evolving landscape of conversational AI, Retrieval-Augmented Generation (RAG) has emerged as a powerful approach to enhance chatbot performance. By combining the strengths of retrieval and generation, RAG-based systems provide more accurate and contextually relevant responses. In this article, we will explore how to build a RAG-based chatbot using LlamaIndex and TiDB Vector Search, a MySQL-compatible database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_RAG\"><\/span>Why RAG?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Retrieval-Augmented Generation (RAG) leverages both retrieval mechanisms and generative models. The retrieval mechanism fetches relevant documents or data snippets in response to a query, while the generative model creates human-like responses based on the retrieved information. This combination ensures that the chatbot has access to precise data and can generate coherent and contextually appropriate responses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Components_of_Our_RAG-based_Chatbot\"><\/span>Components of Our RAG-based Chatbot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\" start=\"1\">\n<li><strong>LlamaIndex<\/strong>: A library that facilitates the creation of indices for efficient retrieval.<\/li>\n\n\n\n<li><strong>TiDB Vector Search<\/strong>: A MySQL-compatible, distributed database with advanced vector search capabilities.<\/li>\n\n\n\n<li><strong>SimpleWebPageReader<\/strong>: A tool for loading and converting web page content into text format.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Up_the_Environment\"><\/span>Setting Up the Environment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we dive into the code, ensure you have the following environment variables set up:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>TIDB_USERNAME<\/code>: Your TiDB username.<\/li>\n\n\n\n<li><code>TIDB_PASSWORD<\/code>: Your TiDB password.<\/li>\n\n\n\n<li><code>TIDB_HOST<\/code>: The host address of your TiDB instance.<\/li>\n\n\n\n<li><code>OPENAI_API_KEY<\/code>: Your OpenAI API key.<\/li>\n<\/ul>\n\n\n\n<p><strong>Prerequisites<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A running <a href=\"\/ko\/tidb-cloud-starter\/\">TiDB Cloud \uc2a4\ud0c0\ud130<\/a> cluster with vector search enabled.<\/li>\n\n\n\n<li>Python 3.8 or later.<\/li>\n\n\n\n<li>OpenAI <a href=\"https:\/\/platform.openai.com\/docs\/quickstart\">API key<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_the_Example\"><\/span>Running the Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Clone the Repository<\/h3>\n\n\n\n<p>First, clone the repository containing the example code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/pingcap\/tidb-vector-python.git\ncd tidb-vector-python\/examples\/llamaindex-tidb-vector<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Virtual Environment<\/h3>\n\n\n\n<p>Next, create and activate a virtual environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m venv .venv\nsource .venv\/bin\/activate<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install Dependencies<\/h3>\n\n\n\n<p>Install the required Python packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install -r requirements.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set Environment Variables<\/h3>\n\n\n\n<p>Set the necessary environment variables with your credentials:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export OPENAI_API_KEY=\"sk-*******\"\nexport TIDB_HOST=\"gateway01.*******.shared.aws.tidbcloud.com\"\nexport TIDB_USERNAME=\"****.root\"\nexport TIDB_PASSWORD=\"****\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Code_Walkthrough\"><\/span>Code Walkthrough<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Below is the complete code to build our RAG-based chatbot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\nimport os\n\nimport click\nfrom sqlalchemy import URL\nfrom llama_index.core import VectorStoreIndex, StorageContext\nfrom llama_index.vector_stores.tidbvector import TiDBVectorStore # type: ignore\nfrom llama_index.readers.web import SimpleWebPageReader\n\n# Define TiDB connection URL\ntidb_connection_url = URL(\n    \"mysql+pymysql\",\n    username=os.environ&#91;'TIDB_USERNAME'],\n    password=os.environ&#91;'TIDB_PASSWORD'],\n    host=os.environ&#91;'TIDB_HOST'],\n    port=4000,\n    database=\"test\",\n    query={\"ssl_verify_cert\": True, \"ssl_verify_identity\": True},\n)\n\n# Initialize TiDB Vector Store\ntidbvec = TiDBVectorStore(\n    connection_string=tidb_connection_url,\n    table_name=\"llama_index_rag_test\",\n    distance_strategy=\"cosine\",\n    vector_dimension=1536, # The dimension is decided by the model\n    drop_existing_table=False,\n)\n\n# Create VectorStoreIndex and StorageContext\ntidb_vec_index = VectorStoreIndex.from_vector_store(tidbvec)\nstorage_context = StorageContext.from_defaults(vector_store=tidbvec)\nquery_engine = tidb_vec_index.as_query_engine(streaming=True)\n\n# Function to prepare data\ndef do_prepare_data(url):\n    documents = SimpleWebPageReader(html_to_text=True).load_data(&#91;url,])\n    tidb_vec_index.from_documents(documents, storage_context=storage_context, show_progress=True)\n\n# Default URL for data loading\n_default_url = 'https:\/\/docs.pingcap.com\/tidb\/stable\/overview'\n\n@click.command()\n@click.option('--url', default=_default_url,\n              help=f'URL you want to talk to, default={_default_url}')\ndef chat_with_url(url):\n    do_prepare_data(url)\n    while True:\n        question = click.prompt(\"Enter your question\")\n        response = query_engine.query(question)\n        click.echo(response)\n\nif __name__ == '__main__':\n    chat_with_url()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\" start=\"1\">\n<li><strong>Importing Libraries<\/strong>: We start by importing the necessary libraries. <code>os<\/code> is used for environment variables, <code>click<\/code> for command-line interaction, and various modules from <code>llama_index<\/code> \uadf8\ub9ac\uace0 <code>sqlalchemy<\/code> for handling the vector store and database connection.<\/li>\n\n\n\n<li><strong>Defining TiDB Connection URL<\/strong>: We create a connection URL using the <code>URL<\/code> class from <code>sqlalchemy<\/code>. This URL includes the TiDB credentials and connection details.<\/li>\n\n\n\n<li><strong>Initializing TiDB Vector Store<\/strong>: We instantiate <code>TiDBVectorStore<\/code> with the connection string, table name, distance strategy (cosine similarity), vector dimension, and an option to drop the existing table.<\/li>\n\n\n\n<li><strong>Creating VectorStoreIndex and StorageContext<\/strong>: We create an index from the vector store and a storage context for managing data storage.<\/li>\n\n\n\n<li><strong>Data Preparation Function<\/strong>: The <code>do_prepare_data<\/code> function loads data from a given URL, converts it to text, and stores it in the vector index.<\/li>\n\n\n\n<li><strong>Command-Line Interaction<\/strong>: Using <code>click<\/code>, we define a command-line interface to allow users to specify a URL for data loading and interact with the chatbot by entering questions.<\/li>\n\n\n\n<li><strong>Main Function<\/strong>: The <code>chat_with_url<\/code> function prepares data from the specified URL and enters a loop where it prompts the user for questions and returns responses from the query engine.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Running the Chatbot<\/h3>\n\n\n\n<p>To run the chatbot, save the code to a file, for example, <code>chat_with_url.py<\/code>, and execute it in your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python chat_with_url.py<\/code><\/pre>\n\n\n\n<p>You can specify a different URL by using the <code>--url<\/code> option. The chatbot will load the data from the given URL and be ready to answer your questions based on the retrieved information.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python chat_with_url.py --help\nUsage: chat_with_url.py &#91;OPTIONS]\n\nOptions:\n  --url TEXT  URL you want to talk to,\n              default=https:\/\/docs.pingcap.com\/tidb\/stable\/overview\n  --help      Show this message and exit.\n$\n$ python chat_with_url.py\nEnter your question: tidb vs mysql\nTiDB is an open-source distributed SQL database that supports Hybrid Transactional and Analytical Processing (HTAP) workloads. It is MySQL compatible and features horizontal scalability, strong consistency, and high availability. TiDB is designed to provide users with a one-stop database solution that covers OLTP, OLAP, and HTAP services. It offers easy horizontal scaling, financial-grade high availability, real-time HTAP capabilities, cloud-native features, and compatibility with the MySQL protocol and ecosystem.\nEnter your question:<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>By integrating LlamaIndex with <a href=\"https:\/\/www.pingcap.com\/ko\/blog\/integrating-vector-search-into-tidb-for-ai-applications\/\">TiDB Vector Search<\/a>, we can build a robust RAG-based chatbot that leverages the power of both retrieval and generation. This approach ensures that our chatbot provides accurate, relevant, and contextually appropriate responses. With TiDB&#8217;s advanced vector search capabilities, the system is scalable and efficient, making it suitable for a wide range of applications.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the evolving landscape of conversational AI, Retrieval-Augmented Generation (RAG) has emerged as a powerful approach to enhance chatbot performance. By combining the strengths of retrieval and generation, RAG-based systems provide more accurate and contextually relevant responses. In this article, we will explore how to build a RAG-based chatbot using LlamaIndex and TiDB Vector Search, [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"template":"","class_list":["post-17764","article","type-article","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Build a RAG-based Chatbot with LlamaIndex &amp; TiDB Vector Search<\/title>\n<meta name=\"description\" content=\"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Build a RAG-based Chatbot with LlamaIndex &amp; TiDB Vector Search\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pingcap.com\/ko\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/\" \/>\n<meta property=\"og:site_name\" content=\"TiDB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/pingcap2015\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-18T12:25:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"714\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:label1\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data1\" content=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/\",\"url\":\"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/\",\"name\":\"Build a RAG-based Chatbot with LlamaIndex & TiDB Vector Search\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/#website\"},\"datePublished\":\"2024-06-23T15:07:46+00:00\",\"dateModified\":\"2025-08-18T12:25:43+00:00\",\"description\":\"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pingcap.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Articles\",\"item\":\"https:\/\/www.pingcap.com\/article\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search &#8211; a MySQL-compatible Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pingcap.com\/#website\",\"url\":\"https:\/\/www.pingcap.com\/\",\"name\":\"TiDB\",\"description\":\"TiDB | SQL at Scale\",\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pingcap.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pingcap.com\/#organization\",\"name\":\"PingCAP\",\"url\":\"https:\/\/www.pingcap.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"width\":811,\"height\":232,\"caption\":\"PingCAP\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/pingcap2015\",\"https:\/\/x.com\/PingCAP\",\"https:\/\/linkedin.com\/company\/pingcap\",\"https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Build a RAG-based Chatbot with LlamaIndex & TiDB Vector Search","description":"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database","robots":{"index":"noindex","follow":"follow"},"og_locale":"ko_KR","og_type":"article","og_title":"Build a RAG-based Chatbot with LlamaIndex & TiDB Vector Search","og_description":"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database","og_url":"https:\/\/www.pingcap.com\/ko\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/","og_site_name":"TiDB","article_publisher":"https:\/\/facebook.com\/pingcap2015","article_modified_time":"2025-08-18T12:25:43+00:00","og_image":[{"width":1440,"height":714,"url":"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@PingCAP","twitter_misc":{"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/","url":"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/","name":"Build a RAG-based Chatbot with LlamaIndex & TiDB Vector Search","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"datePublished":"2024-06-23T15:07:46+00:00","dateModified":"2025-08-18T12:25:43+00:00","description":"In this tutorial, we will build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search - which is a MySQL-compatible Database","breadcrumb":{"@id":"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pingcap.com\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pingcap.com\/"},{"@type":"ListItem","position":2,"name":"Articles","item":"https:\/\/www.pingcap.com\/article\/"},{"@type":"ListItem","position":3,"name":"Build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search &#8211; a MySQL-compatible Database"}]},{"@type":"WebSite","@id":"https:\/\/www.pingcap.com\/#website","url":"https:\/\/www.pingcap.com\/","name":"\ud2f0DB","description":"TiDB | SQL at Scale","publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pingcap.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/www.pingcap.com\/#organization","name":"PingCAP","url":"https:\/\/www.pingcap.com\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/","url":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","contentUrl":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","width":811,"height":232,"caption":"PingCAP"},"image":{"@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/pingcap2015","https:\/\/x.com\/PingCAP","https:\/\/linkedin.com\/company\/pingcap","https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA"]}]}},"card_markup":"        <a class=\"card-article\" href=\"https:\/\/www.pingcap.com\/ko\/article\/build-a-rag-based-chatbot-with-llamaindex-and-tidb-vector-search\/\">            <h3>Build a RAG-based Chatbot with LlamaIndex and TiDB Vector Search &#8211; a MySQL-compatible Database<\/h3>            <p>In the evolving landscape of conversational AI, Retrieval-Augmented Generation (RAG) has emerged as a powerful approach to enhance chatbot performance. By combining the strengths of retrieval and generation, RAG-based systems provide more accurate and contextually relevant responses. In this article, we will explore how to build a RAG-based chatbot using LlamaIndex and TiDB Vector Search, [&hellip;]<\/p>        <\/a>","_links":{"self":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article\/17764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/types\/article"}],"author":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/users\/8"}],"wp:attachment":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media?parent=17764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}