Skip to content

Commit 5f709cd

Browse files
committed
Merge branch 'main' into generate
2 parents 154c4d2 + 99982fb commit 5f709cd

30 files changed

+405
-102
lines changed

.devcontainer/devcontainer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"version": "18",
1717
"nodeGypDependencies": false
1818
},
19-
"ghcr.io/azure/azure-dev/azd:latest": {},
20-
// Required for azd to package the app to ACA
21-
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
19+
"ghcr.io/azure/azure-dev/azd:latest": {
20+
"version": "1.10.1"
21+
},
2222
"ghcr.io/prulloac/devcontainer-features/ollama:1": {}
2323
},
2424
// Configure tool-specific properties.

.github/workflows/app-tests.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ name: App Tests
33
on:
44
push:
55
branches: [ main ]
6+
paths-ignore:
7+
- "**.md"
8+
- ".azdo/**"
9+
- ".devcontainer/**"
10+
- ".github/**"
611
pull_request:
712
branches: [ main ]
13+
paths-ignore:
14+
- "**.md"
15+
- ".azdo/**"
16+
- ".devcontainer/**"
17+
- ".github/**"
818
workflow_dispatch:
919

1020
permissions:

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/Azure-Samples/rag-postgres-openai-python)
44
[![Open in Dev Containers](https://img.shields.io/static/v1?style=for-the-badge&label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/azure-samples/rag-postgres-openai-python)
55

6-
This project creates a web-based chat application with an API backend that can use OpenAI chat models to answer questions about the items in a PostgreSQL database table. The frontend is built with React and FluentUI, while the backend is written with Python and FastAPI.
6+
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.
77

88
This project is designed for deployment to Azure using [the Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/), hosting the app on Azure Container Apps, the database in Azure PostgreSQL Flexible Server, and the models in Azure OpenAI.
99

1010
* [Features](#features)
1111
* [Getting started](#getting-started)
12-
* [GitHub Codespaces](#github-codespaces)
13-
* [VS Code Dev Containers](#vs-code-dev-containers)
14-
* [Local environment](#local-environment)
12+
* [GitHub Codespaces](#github-codespaces)
13+
* [VS Code Dev Containers](#vs-code-dev-containers)
14+
* [Local environment](#local-environment)
1515
* [Deployment](#deployment)
1616
* [Local development](#local-development)
1717
* [Costs](#costs)
@@ -27,7 +27,13 @@ This project provides the following features:
2727
* OpenAI function calling to optionally convert user queries into query filter conditions, such as turning "Climbing gear cheaper than $30?" into "WHERE price < 30".
2828
* Conversion of user queries into vectors using the OpenAI embedding API.
2929

30-
![Screenshot of chat app with question about climbing gear](docs/screenshot_chat.png)
30+
![Screenshot of chat app with question about climbing gear](docs/images/screenshot_chat.png)
31+
32+
## Architecture diagram
33+
34+
The deployed app uses a user-assigned managed identity to authenticate to Azure services, and stores logs in Log Analytics.
35+
36+
![Architecture diagram: Azure Container Apps, Azure Container Registry, Managed Identity, Azure OpenAI, Azure Database for PostgreSQL](docs/images/azure_architecture.png)
3137

3238
## Getting started
3339

@@ -95,7 +101,11 @@ Once you've opened the project in [Codespaces](#github-codespaces), [Dev Contain
95101
azd auth login
96102
```
97103
98-
If you have any issues with that command, you may also want to try `azd auth login --use-device-code`.
104+
For GitHub Codespaces users, if the previous command fails, try:
105+
106+
```shell
107+
azd auth login --use-device-code
108+
```
99109
100110
2. Create a new azd environment:
101111
@@ -107,7 +117,7 @@ Once you've opened the project in [Codespaces](#github-codespaces), [Dev Contain
107117
108118
3. (Optional) If you would like to customize the deployment to [use existing Azure resources](docs/deploy_existing.md), you can set the values now.
109119
110-
3. Provision the resources and deploy the code:
120+
4. Provision the resources and deploy the code:
111121
112122
```shell
113123
azd up
@@ -136,7 +146,7 @@ Since the local app uses OpenAI models, you should first deploy it for the optim
136146
1. 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:
137147
138148
```bash
139-
python3 -m pip install -e src/backend
149+
python -m pip install -e src/backend
140150
python ./src/backend/fastapi_app/setup_postgres_database.py
141151
python ./src/backend/fastapi_app/setup_postgres_seeddata.py
142152
```
@@ -155,7 +165,7 @@ Since the local app uses OpenAI models, you should first deploy it for the optim
155165
3. Run the FastAPI backend (with hot reloading). This should be run from the root of the project:
156166
157167
```shell
158-
python3 -m uvicorn fastapi_app:create_app --factory --reload
168+
python -m uvicorn fastapi_app:create_app --factory --reload
159169
```
160170
161171
Or you can run "Backend" in the VS Code Run & Debug menu.
@@ -191,7 +201,10 @@ Additionally, we have added a [GitHub Action](https://github.com/microsoft/secur
191201
192202
Further documentation is available in the `docs/` folder:
193203
204+
* [Understanding the RAG flow](docs/rag_flow.md)
205+
* [Customizing the data](docs/customize_data.md)
194206
* [Deploying with existing resources](docs/deploy_existing.md)
207+
* [Using Entra auth with PostgreSQL tools](docs/using_entra_auth.md)
195208
* [Monitoring with Azure Monitor](docs/monitoring.md)
196209
* [Load testing](docs/loadtesting.md)
197210
* [Evaluation](docs/evaluation.md)
@@ -200,5 +213,6 @@ Please post in the issue tracker with any questions or issues.
200213
201214
## Resources
202215
216+
* [RAGHack livestream: Building RAG with PostgreSQL](https://www.youtube.com/watch?v=Dk65oQjYAfo)
203217
* [RAG chat with Azure AI Search + Python](https://github.com/Azure-Samples/azure-search-openai-demo/)
204218
* [Develop Python apps that use Azure AI services](https://learn.microsoft.com/azure/developer/python/azure-ai-for-python-developers)

azure.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ name: rag-postgres-openai-python
33
metadata:
44
template: rag-postgres-openai-python@0.0.1
55
requiredVersions:
6-
azd: ">= 1.9.5"
6+
azd: ">= 1.10.0"
77
services:
88
web:
99
project: ./src/backend
1010
language: py
1111
module: web
1212
host: containerapp
13+
docker:
14+
remoteBuild: true
1315
hooks:
1416
prebuild:
1517
windows:

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ page_type: sample
1616
urlFragment: rag-postgres-openai-python
1717
---
1818

19-
This project creates a web-based chat application with an API backend that can use OpenAI chat models to answer questions about the items in a PostgreSQL database table. The frontend is built with React and FluentUI, while the backend is written with Python and FastAPI.
19+
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.
2020

2121
This project is designed for deployment to Azure using [the Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/), hosting the app on Azure Container Apps, the database in Azure PostgreSQL Flexible Server, and the models in Azure OpenAI.
2222

docs/customize_data.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# RAG on PostgreSQL: Customizing the data
2+
3+
This guide shows you how to bring in a table with a different schema than the sample table.
4+
For a full example of code changes needed, check out [this branch](https://github.com/Azure-Samples/rag-postgres-openai-python/compare/main...otherdata).
5+
6+
## Define the table schema
7+
8+
1. Update seed_data.json file with the new data
9+
2. Update the SQLAlchemy models in postgres_models.py to reflect the new schema
10+
3. Add the new table to the database:
11+
12+
```shell
13+
python src/backend/fastapi_app/setup_postgres_database.py
14+
```
15+
16+
These scripts will run on the local database by default. They will run on the production database as part of the `azd up` deployment process.
17+
18+
## Add embeddings to the seed data
19+
20+
If you don't yet have any embeddings in `seed_data.json`:
21+
22+
1. Update the references to models in update_embeddings.py
23+
2. Generate new embeddings for the seed data:
24+
25+
```shell
26+
python src/backend/fastapi_app/update_embeddings.py --in_seed_data
27+
```
28+
29+
That script will use whatever OpenAI host is defined in the `.env` file.
30+
You may want to run it twice for multiple models, once for Azure OpenAI embedding model and another for Ollama embedding model. Change `OPENAI_EMBED_HOST` between runs.
31+
32+
## Add the seed data to the database
33+
34+
Now that you have the new table schema and `seed_data.json` populated with embeddings, you can add the seed data to the database:
35+
36+
```shell
37+
python src/backend/fastapi_app/setup_postgres_seeddata.py
38+
```
39+
40+
## Update the LLM prompts
41+
42+
3. Update the question answering prompt at `src/backend/fastapi_app/prompts/answer.txt` to reflect the new domain.
43+
4. Update the function calling definition in `src/backend/fastapi_app/query_rewriter.py` to reflect the new schema and domain. Replace the `brand_filter` and `price_filter` with any filters that are relevant to your new schema.
44+
5. Update the query rewriting prompt at `src/backend/fastapi_app/prompts/query.txt` to reflect the new domain and filters.
45+
6. Update the query rewriting examples at `src/backend/fastapi_app/prompts/query_fewshots.json` to match the new domain and filters.
46+
47+
## Update the API
48+
49+
The FastAPI routes use type annotations to define the schema of the data that they accept and return, so you'll need to update the API to reflect the new schema.
50+
51+
1. Modify `ItemPublic` in `src/backend/fastapi_app/api_models.py` to reflect the new schema.
52+
2. Modify `RAGContext` if your schema uses a string ID instead of integer ID.
53+
54+
## Update the frontend
55+
56+
1. Modify the Answer component in `src/frontend/src/components/Answer/Answer.tsx` to display the desired fields from the new schema.
57+
2. Modify the examples in `/workspace/src/frontend/src/components/Example/ExampleList.tsx` to examples for the new domain.

docs/deploy_existing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Deploying with existing Azure resources
1+
# RAG on PostgreSQL: Deploying with existing Azure resources
22

33
If you already have existing Azure resources, or if you want to specify the exact name of new Azure Resource, you can do so by setting `azd` environment values.
44
You should set these values before running `azd up`. Once you've set them, return to the [deployment steps](../README.md#deployment).
55

6-
### Openai.com OpenAI account
6+
## Openai.com OpenAI account
77

88
1. Run `azd env set DEPLOY_AZURE_OPENAI false`
99
1. Run `azd env set OPENAI_CHAT_HOST openaicom`

docs/images/advanced_hybrid_flow.png

112 KB
Loading

docs/images/azure_architecture.png

75.5 KB
Loading
62.7 KB
Loading

0 commit comments

Comments
 (0)