Skip to content

Commit 62c249f

Browse files
committed
[chore] improve the python app setup
1 parent 02c3880 commit 62c249f

File tree

11 files changed

+111
-10
lines changed

11 files changed

+111
-10
lines changed

py-langchain/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,27 @@ To start with the basic examples, you'll just need to add your OpenAI API key an
4141
Next, install the required packages using your preferred package manager, e.g. uv:
4242

4343
```bash
44-
uv sync --frozen
44+
uv sync
4545
```
4646

47-
Now you're ready to start and migrate the database:
47+
Now you're ready to start the database:
4848

4949
```bash
5050
# start the postgres database
5151
docker compose up -d
5252
```
5353

54+
Initialize FGA store:
55+
56+
```bash
57+
source .venv/bin/activate
58+
python -m app.core.fga_init
59+
```
60+
5461
Now you're ready to run the development server:
5562

5663
```bash
5764
source .venv/bin/activate
58-
uv pip install auth0_fastapi # install the auth0 fastapi package
5965
fastapi dev app/main.py
6066
```
6167

@@ -77,6 +83,7 @@ Finally, you can start the frontend server in another terminal:
7783

7884
```bash
7985
cd frontend
86+
cp .env.example .env # Copy the `.env.example` file to `.env`.
8087
npm install
8188
npm run dev
8289
```

py-langchain/backend/.env.example

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ AUTH0_CLIENT_ID=''
66
AUTH0_CLIENT_SECRET=''
77

88
OPENAI_API_KEY=''
9+
10+
# Database
911
DATABASE_URL="postgresql+psycopg://postgres:postgres@localhost:5432/ai_documents_db"
1012

1113
# LANGGRAPH
@@ -18,6 +20,6 @@ FGA_CLIENT_SECRET=<your-fga-store-client-secret>
1820
FGA_API_URL=https://api.xxx.fga.dev
1921
FGA_API_AUDIENCE=https://api.xxx.fga.dev/
2022

21-
# Shop API URL
22-
SHOP_API_URL="http://localhost:3001/api/shop"
23-
SHOP_API_AUDIENCE="https://api.shop-online-demo.com"
23+
# Shop API URL (Optional)
24+
# SHOP_API_URL="http://localhost:3001/api/shop"
25+
# SHOP_API_AUDIENCE="https://api.shop-online-demo.com"

py-langchain/backend/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,34 @@ To start with the basic examples, you'll just need to add your OpenAI API key an
1515
Next, install the required packages using your preferred package manager, e.g. uv:
1616

1717
```bash
18-
uv sync --frozen
18+
uv sync
19+
```
20+
21+
Now you're ready to start and migrate the database:
22+
23+
```bash
24+
# start the postgres database
25+
docker compose up -d
26+
```
27+
28+
Initialize FGA store:
29+
30+
```bash
31+
source .venv/bin/activate
32+
python -m app.core.fga_init
1933
```
2034

2135
Now you're ready to run the development server:
2236

2337
```bash
2438
source .venv/bin/activate
25-
uv pip install auth0_fastapi # install the auth0 fastapi package
2639
fastapi dev app/main.py
2740
```
41+
42+
Next, you'll need to start an in-memory LangGraph server on port 54367, to do so open a new terminal and run:
43+
44+
```bash
45+
source .venv/bin/activate
46+
uv pip install -U langgraph-api
47+
langgraph dev --port 54367 --allow-blocking
48+
```

py-langchain/backend/app/core/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Settings(BaseSettings):
3232
FGA_AUTHORIZATION_MODEL_ID: str | None = None
3333

3434
# Shop API
35-
SHOP_API_URL: str = "http://localhost:3001/api/shop"
36-
SHOP_API_AUDIENCE: str = "https://api.shop-online-demo.com"
35+
SHOP_API_URL: str = ""
36+
SHOP_API_AUDIENCE: str = ""
3737

3838
# OpenAI
3939
OPENAI_API_KEY: str
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import asyncio
2+
import json
3+
import os
4+
import openfga_sdk
5+
from openfga_sdk.client import OpenFgaClient
6+
from openfga_sdk.credentials import Credentials, CredentialConfiguration
7+
8+
from app.core.config import settings
9+
10+
11+
def build_openfga_client() -> OpenFgaClient:
12+
"""Build and return an OpenFGA client using settings configuration."""
13+
openfga_client_config = openfga_sdk.ClientConfiguration(
14+
api_url=settings.FGA_API_URL,
15+
store_id=settings.FGA_STORE_ID,
16+
credentials=Credentials(
17+
method="client_credentials",
18+
configuration=CredentialConfiguration(
19+
api_issuer=settings.FGA_API_TOKEN_ISSUER,
20+
api_audience=settings.FGA_API_AUDIENCE,
21+
client_id=settings.FGA_CLIENT_ID,
22+
client_secret=settings.FGA_CLIENT_SECRET,
23+
),
24+
),
25+
)
26+
return OpenFgaClient(openfga_client_config)
27+
28+
29+
async def main():
30+
"""
31+
Initializes the OpenFgaClient, writes an authorization model, and configures pre-defined tuples.
32+
33+
This function performs the following steps:
34+
1. Creates an instance of OpenFgaClient with the necessary configuration.
35+
2. Writes an authorization model with specified schema version and type definitions.
36+
"""
37+
38+
fga_client = build_openfga_client()
39+
40+
# Define the authorization model
41+
body_string = "{\"schema_version\":\"1.1\",\"type_definitions\":[{\"type\":\"user\"},{\"metadata\":{\"relations\":{\"can_view\":{},\"owner\":{\"directly_related_user_types\":[{\"type\":\"user\"}]},\"viewer\":{\"directly_related_user_types\":[{\"type\":\"user\"},{\"type\":\"user\",\"wildcard\":{}}]}}},\"relations\":{\"can_view\":{\"union\":{\"child\":[{\"computedUserset\":{\"relation\":\"owner\"}},{\"computedUserset\":{\"relation\":\"viewer\"}}]}},\"owner\":{\"this\":{}},\"viewer\":{\"this\":{}}},\"type\":\"doc\"}]}"
42+
# Write the authorization model
43+
model = await fga_client.write_authorization_model(json.loads(body_string))
44+
45+
print(f'NEW MODEL ID: {model.authorization_model_id}')
46+
47+
# Properly close the client session
48+
await fga_client.close()
49+
50+
51+
if __name__ == '__main__':
52+
asyncio.run(main())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
model
2+
schema 1.1
3+
4+
type user
5+
6+
type doc
7+
relations
8+
define owner: [user]
9+
define viewer: [user, user:*]
10+
define can_view: owner or viewer
File renamed without changes.

py-langchain/frontend/bun.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"axios": "^1.10.0",
1919
"class-variance-authority": "^0.7.1",
2020
"clsx": "^2.1.1",
21+
"date-fns": "^4.1.0",
2122
"lucide-react": "^0.525.0",
2223
"marked": "^16.1.1",
2324
"next-themes": "^0.4.6",
@@ -477,6 +478,8 @@
477478

478479
"csstype": ["[email protected]", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
479480

481+
"date-fns": ["[email protected]", "", {}, "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg=="],
482+
480483
"debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ=="],
481484

482485
"decamelize": ["[email protected]", "", {}, "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA=="],

ts-langchain/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ bun install # or npm install
4444
docker compose up -d
4545
# create the database schema
4646
bun db:migrate # or npm run db:migrate
47+
# initialize FGA store
48+
bun fga:init # or npm run fga:init
4749
```
4850

4951
Now you're ready to run the development server:

ts-llamaindex/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ bun install # or npm install
4545
docker compose up -d
4646
# create the database schema
4747
bun db:migrate # or npm run db:migrate
48+
# initialize FGA store
49+
bun fga:init # or npm run fga:init
4850
```
4951

5052
Now you're ready to run the development server:

0 commit comments

Comments
 (0)