$ show Azure-Samples/rag-postgres-openai-python
This repository provides a production-ready, full-stack Retrieval-Augmented Generation (RAG) chat application built with Python FastAPI and React TypeScript. It demonstrates how to use Azure OpenAI (or OpenAI.com, or Ollama) to answer questions about data stored in Azure Database for PostgreSQL with the pgvector extension. The system supports both a simple RAG flow (embedding + direct search) and an advanced flow that uses OpenAI function calling to rewrite queries and apply column filters (brand, price). It features hybrid search combining full-text search and vector similarity via Reciprocal Rank Fusion. The backend is a monolith serving API endpoints and a React frontend, designed for deployment to Azure Container Apps using the Azure Developer CLI. The codebase includes comprehensive unit tests, end-to-end tests with Playwright, evaluation scripts, load testing with locust, and safety evaluation using Azure AI Red Teaming. The architecture is cleanly separated into reusable modules, making it a solid reference architecture for building RAG applications on Azure.
The LLM identified 4 files worth highlighting for reuse:
A well-encapsulated class that performs hybrid search (vector + full-text) against a PostgreSQL table using pgvector. It builds SQL queries with optional WHERE filter clauses and uses RRF to merge results. The searcher is instantiated with configurable embedding parameters and can be reused in any project that needs semantic plus keyword search over a relational database.
An abstract base class that defines the interface for RAG chat flows. It provides common logic for processing chat parameters, preparing RAG request strings, and declares abstract methods for context preparation and answer generation. This pattern allows clean separation of simple vs advanced RAG flows and is extensible for other retrieval strategies.
Contains the builder for a search_database function tool definition and a parser that extracts the search query plus optional price/brand filters from the LLM's function call response. This module isolates the logic for converting a natural language query into structured search arguments, a common pattern in RAG systems that use tool-using agents.
Provides mock implementations for Azure credentials, enabling unit testing of Azure-dependent code without actual cloud resources. The MockAzureCredential and MockAzureCredentialExpired classes implement the TokenCredential interface and can be used to simulate authentication success or failure scenarios in any Python project using Azure Identity.