Skip to content

Commit bc41dbd

Browse files
authored
Merge pull request Azure-Samples#16 from Azure-Samples/readme
Add more API routes
2 parents 95d0e73 + 37901c5 commit bc41dbd

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

.devcontainer/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
- postgres-data:/var/lib/postgresql/data
2525
environment:
2626
POSTGRES_DB: postgres
27-
POSTGRES_USER: postgres
27+
POSTGRES_USER: admin
2828
POSTGRES_PASSWORD: postgres
2929

3030
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Use these values to connect to the local database from within the devcontainer
22
POSTGRES_HOST=localhost
3-
POSTGRES_USERNAME=postgres
3+
POSTGRES_USERNAME=admin
44
POSTGRES_PASSWORD=postgres
55
POSTGRES_DATABASE=postgres
66
POSTGRES_SSL=disable

.github/workflows/app-tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
postgres:
2020
image: pgvector/pgvector:pg16
2121
env:
22+
POSTGRES_USER: admin
2223
POSTGRES_PASSWORD: postgres
2324
ports:
2425
- 5432:5432

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"driver": "PostgreSQL",
88
"name": "local",
99
"database": "postgres",
10-
"username": "postgres",
10+
"username": "admin",
1111
"password": "postgres"
1212
},
1313
{

src/fastapi_app/api_routes.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
import fastapi
2+
from sqlalchemy import select
3+
from sqlalchemy.ext.asyncio import async_sessionmaker
24

3-
from .api_models import ChatRequest
4-
from .globals import global_storage
5-
from .postgres_searcher import PostgresSearcher
6-
from .rag_advanced import AdvancedRAGChat
7-
from .rag_simple import SimpleRAGChat
5+
from fastapi_app.api_models import ChatRequest
6+
from fastapi_app.globals import global_storage
7+
from fastapi_app.postgres_models import Item
8+
from fastapi_app.postgres_searcher import PostgresSearcher
9+
from fastapi_app.rag_advanced import AdvancedRAGChat
10+
from fastapi_app.rag_simple import SimpleRAGChat
811

912
router = fastapi.APIRouter()
1013

1114

15+
@router.get("/items/{id}")
16+
async def item_handler(id: int):
17+
"""A simple API to get an item by ID."""
18+
async_session_maker = async_sessionmaker(global_storage.engine, expire_on_commit=False)
19+
async with async_session_maker() as session:
20+
item = (await session.scalars(select(Item).where(Item.id == id))).first()
21+
return item.to_dict()
22+
23+
24+
@router.get("/similar")
25+
async def similar_handler(id: int, n: int = 5):
26+
"""A similarity API to find items similar to items with given ID."""
27+
async_session_maker = async_sessionmaker(global_storage.engine, expire_on_commit=False)
28+
async with async_session_maker() as session:
29+
item = (await session.scalars(select(Item).where(Item.id == id))).first()
30+
closest = await session.execute(
31+
select(Item, Item.embedding.l2_distance(item.embedding))
32+
.filter(Item.id != id)
33+
.order_by(Item.embedding.l2_distance(item.embedding))
34+
.limit(n)
35+
)
36+
return [item.to_dict() | {"distance": round(distance, 2)} for item, distance in closest]
37+
38+
1239
@router.post("/chat")
1340
async def chat_handler(chat_request: ChatRequest):
1441
messages = [message.model_dump() for message in chat_request.messages]

0 commit comments

Comments
 (0)