- Notifications
You must be signed in to change notification settings - Fork497
A RAG app to ask questions about rows in a database table. Deployable on Azure Container Apps with PostgreSQL Flexible Server.
License
Azure-Samples/rag-postgres-openai-python
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This project creates a web-based chat application with an API backend that can use OpenAI chat models to answer questions about the rows in a PostgreSQL database table. The frontend is built with React and FluentUI, while the backend is written with Python and FastAPI.
This project is designed for deployment to Azure usingthe Azure Developer CLI, hosting the app on Azure Container Apps, the database in Azure PostgreSQL Flexible Server, and the models in Azure OpenAI.
This project provides the following features:
- Hybrid search on the PostgreSQL database table, usingthe pgvector extension for the vector search plusfull text search, combining the results using RRF (Reciprocal Rank Fusion).
- OpenAI function calling to optionally convert user queries into query filter conditions, such as turning "Climbing gear cheaper than $30?" into "WHERE price < 30".
- Conversion of user queries into vectors using the OpenAI embedding API.
The deployed app uses a user-assigned managed identity to authenticate to Azure services, and stores logs in Log Analytics.
You have a few options for getting started with this template.The quickest way to get started is GitHub Codespaces, since it will setup all the tools for you, but you can alsoset it up locally.
You can run this template virtually by using GitHub Codespaces. The button will open a web-based VS Code instance in your browser:
Open the template (this may take several minutes):
Open a terminal window
Continue with thedeployment steps
A related option is VS Code Dev Containers, which will open the project in your local VS Code using theDev Containers extension:
Start Docker Desktop (install it if not already installed)
Open the project:
In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.
Continue with thedeployment steps
Make sure the following tools are installed:
Download the project code:
azd init -t rag-postgres-openai-python
Open the project folder
Install required Python packages and backend application:
pip install -r requirements-dev.txtpip install -e src/backend
Continue with thedeployment steps
Once you've opened the project inCodespaces,Dev Containers, orlocally, you can deploy it to Azure.
Sign in to your Azure account:
azd auth login
For GitHub Codespaces users, if the previous command fails, try:
azd auth login --use-device-code
Create a new azd environment:
azd env new
This will create a folder under
.azure/
in your project to store the configuration for this deployment. You may have multiple azd environments if desired.(Optional) If you would like to customize the deployment touse existing Azure resources, you can set the values now.
Provision the resources and deploy the code:
azd up
You will be asked to select two locations, first a region for most of the resources (Container Apps, PostgreSQL), then a region specifically for the Azure OpenAI models. This project uses the gpt-4o-mini and text-embedding-3-large models which may not be available in all Azure regions. Check forup-to-date region availability and select a region accordingly.
Since the local app uses OpenAI models, you should first deploy it for the optimal experience.
Copy
.env.sample
into a.env
file.To use Azure OpenAI, set
OPENAI_CHAT_HOST
andOPENAI_EMBED_HOST
to "azure". Then fill in the values ofAZURE_OPENAI_ENDPOINT
andAZURE_OPENAI_CHAT_DEPLOYMENT
based on the deployed values. You can display the values using this command:azd env get-values
To use OpenAI.com OpenAI, set
OPENAI_CHAT_HOST
andOPENAI_EMBED_HOST
to "openai". Then fill in the value forOPENAICOM_KEY
.To use Ollama, set
OPENAI_CHAT_HOST
to "ollama". Then update the values forOLLAMA_ENDPOINT
andOLLAMA_CHAT_MODEL
to match your local setup and model. We recommend using "llama3.1" for the chat model, since it has support for function calling, and "nomic-embed-text" for the embedding model, since the sample data has already been embedded with this model. If you cannot use function calling, then turn off "Advanced flow" in the Developer Settings. If you cannot use the embedding model, then turn off vector search in the Developer Settings.
Run these commands to install the web app as a local package (named
fastapi_app
), set up the local database, and seed it with test data:python -m pip install -r src/backend/requirements.txtpython -m pip install -e src/backendpython ./src/backend/fastapi_app/setup_postgres_database.pypython ./src/backend/fastapi_app/setup_postgres_seeddata.py
Build the frontend:
cd src/frontendnpm installnpm run buildcd ../../
There must be an initial build of static assets before running the backend, since the backend serves static files from the
src/static
directory.Run the FastAPI backend (with hot reloading). This should be run from the root of the project:
python -m uvicorn fastapi_app:create_app --factory --reload
Or you can run "Backend" in the VS Code Run & Debug menu.
Run the frontend (with hot reloading):
cd src/frontendnpm run dev
Or you can run "Frontend" or "Frontend & Backend" in the VS Code Run & Debug menu.
Open the browser at
http://localhost:5173/
and you will see the frontend.
Pricing may vary per region and usage. Exact costs cannot be estimated.You may try theAzure pricing calculator for the resources below:
- Azure Container Apps: Pay-as-you-go tier. Costs based on vCPU and memory used.Pricing
- Azure OpenAI: Standard tier, GPT and Ada models. Pricing per 1K tokens used, and at least 1K tokens are used per question.Pricing
- Azure PostgreSQL Flexible Server: Burstable Tier with 1 CPU core, 32GB storage. Pricing is hourly.Pricing
- Azure Monitor: Pay-as-you-go tier. Costs based on data ingested.Pricing
This template usesManaged Identity for authenticating to the Azure services used (Azure OpenAI, Azure PostgreSQL Flexible Server).
Additionally, we have added aGitHub Action that scans the infrastructure-as-code files and generates a report containing any detected issues. To ensure continued best practices in your own repository, we recommend that anyone creating solutions based on our templates ensure that theGithub secret scanning setting is enabled.
Further documentation is available in thedocs/
folder:
- Understanding the RAG flow
- Customizing the data
- Deploying with existing resources
- Using Entra auth with PostgreSQL tools
- Monitoring with Azure Monitor
- Load testing
- Quality evaluation
- Safety evaluation
Please post in the issue tracker with any questions or issues.
About
A RAG app to ask questions about rows in a database table. Deployable on Azure Container Apps with PostgreSQL Flexible Server.
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.