This lab guides you through developing an agent-driven, Retrieval-Augmented Generation (RAG) application that explores U.S. Case Law data. You'll learn how to combine PostgreSQL's powerful database capabilities with AI techniques to create a legal research assistant capable of providing accurate and contextually relevant answers.
- A Microsoft Agent Framework Agent that can reason over legal cases stored in PostgreSQL
- A system that utilizes vector embeddings for semantic search
- AI function tools for database search and external data retrieval
- An application enhanced with the GraphRAG pattern for improved answer quality
- Azure Database for PostgreSQL: Database platform with AI extensions
- Microsoft Agent Framework: Framework for building AI agents with function tools
- Azure OpenAI: For embeddings and LLM chat completions
- PostgreSQL Extensions: Vector and Graph capabilities (DiskANN, AGE)
- Python: For agent development and database interaction
├── LICENSE # MIT License file
├── azure.yaml # Azure Developer CLI configuration
├── requirements.txt # Python package dependencies
├── Code/
│ └── lab.ipynb # Main Jupyter notebook tutorial
├── Dataset/
│ └── cases.csv # Legal case dataset
├── Docs/
│ └── lab_manual.md # Detailed lab instructions
│ └── images/ # Architecture diagrams and screenshots
├── Scripts/
│ ├── create_graph.sql # SQL script for graph creation
│ ├── get_env.ps1 # Script to get environment variables
│ ├── get_user_token.ps1 # Script to get PostgreSQL access token
│ ├── initialize_dataset.sql # Database initialization script
│ ├── load_age.ps1 # Apache AGE installation script
│ └── write_env.ps1 # Environment variable setup script
├── infra/
│ ├── deployment_script.ps1 # Deployment script
│ ├── main.bicep # Main Azure Bicep deployment template
│ ├── main.parameters.json # Bicep parameters file
│ └── pg.bicep # PostgreSQL-specific Bicep template
└── Archive/ # Legacy scripts and configurations
- Azure subscription with access to Azure OpenAI
- Visual Studio Code with the PostgreSQL extension
- Python environment with necessary libraries:
- PostgreSQL connectivity (
psycopg,psycopg-binary,psycopg-pool) - Modeling and validation (
pydantic) - OpenAI and Agent Framework integration (
openai,agent-framework) - Notebook compatibility (
nest_asyncio,ipykernel) - Azure authentication (
azure-identity) - Environment management (
python-dotenv)
- PostgreSQL connectivity (
-
Setup Azure PostgreSQL Database:
- Database connection and configuration
- Install the
azure_aiextension - Configure Azure OpenAI connectivity
-
Using AI-driven features in PostgreSQL:
- Pattern matching queries
- Semantic vector search using embeddings
- DiskANN indexing for fast vector similarity search
-
Building the Agent Application:
- Setting up Microsoft Agent Framework
- Creating database search AI function tools
- Implementing semantic reranking
- Adding external data sources (weather API)
- Testing and improving the agent
Follow these steps to deploy a PostgreSQL Flexible Server to Azure with the pgvector extension enabled:
-
Login to your Azure account:
azd auth login
-
Create a new azd environment:
azd env new
Enter a name that will be used for the resource group. This will create a new folder in the
.azurefolder, and set it as the active environment for any calls toazdgoing forward. -
Run this command to provision all the resources:
azd provision
This will create a new resource group, and create the PostgreSQL Flexible server and Azure OpenAI resources in that resource group. It will also create a
.envfile in the root of the project, which contains the connection information for the PostgreSQL server and Azure OpenAI.
- Follow the instructions in
Docs/lab_manual.mdto setup your environment - Open
Code/lab.ipynbin Visual Studio Code to follow the step-by-step guide - Run the PowerShell scripts in the
Scripts/folder to configure your environment and database
- GraphRAG Solution for Azure Database for PostgreSQL
- Graph data in Azure Database for PostgreSQL
- PostgreSQL extension for Visual Studio Code
- Microsoft Agent Framework Documentation
This project is licensed under the MIT License - see the LICENSE file for details.
