RAG Example
This example demonstrates how to use PyTiDB to build a minimal RAG application.
- Use Ollama to deploy local embedding model and LLM model
- Use Streamlit to build a Web UI for the RAG application
- Use PyTiDB to build a minimal RAG application
RAG application built with PyTiDB
Prerequisites
- Python 3.10+
- A TiDB Cloud Serverless cluster: Create a free cluster here: tidbcloud.com ↗️
- Ollama: You can install it from Ollama ↗️
How to run
Step 1: Prepare the inference API
Pull the embedding and LLM model via ollama CLI:
Test the /embed
and /generate
endpoints to make sure they are running:
curl http://localhost:11434/api/embed -d '{
"model": "mxbai-embed-large",
"input": "Llamas are members of the camelid family"
}'
curl http://localhost:11434/api/generate -d '{
"model": "gemma3:4b",
"prompt": "Hello, Who are you?"
}'
Step 2: Clone the repository to local
Step 3: Install the required packages and setup environment
Step 4: Set up environment to connect to database
Go to TiDB Cloud console and get the connection parameters, then set up the environment variable like this:
cat > .env <<EOF
TIDB_HOST={gateway-region}.prod.aws.tidbcloud.com
TIDB_PORT=4000
TIDB_USERNAME={prefix}.root
TIDB_PASSWORD={password}
TIDB_DATABASE=test
EOF
Step 5: Run the Streamlit app
Step 6: Open the browser and visit http://localhost:8501
Troubleshooting
502 Bad Gateway
Error
Try to disable the global proxy settings.
Related Resources
- Source Code: View on GitHub
-
Category: Ai-Apps
-
Description: Build a RAG application that combines document retrieval with language generation.