Skip to content

Add more API routes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: postgres

# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
Expand Down
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Use these values to connect to the local database from within the devcontainer
POSTGRES_HOST=localhost
POSTGRES_USERNAME=postgres
POSTGRES_USERNAME=admin
POSTGRES_PASSWORD=postgres
POSTGRES_DATABASE=postgres
POSTGRES_SSL=disable
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/app-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
postgres:
image: pgvector/pgvector:pg16
env:
POSTGRES_USER: admin
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"driver": "PostgreSQL",
"name": "local",
"database": "postgres",
"username": "postgres",
"username": "admin",
"password": "postgres"
},
{
Expand Down
37 changes: 32 additions & 5 deletions src/fastapi_app/api_routes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import fastapi
from sqlalchemy import select
from sqlalchemy.ext.asyncio import async_sessionmaker

from .api_models import ChatRequest
from .globals import global_storage
from .postgres_searcher import PostgresSearcher
from .rag_advanced import AdvancedRAGChat
from .rag_simple import SimpleRAGChat
from fastapi_app.api_models import ChatRequest
from fastapi_app.globals import global_storage
from fastapi_app.postgres_models import Item
from fastapi_app.postgres_searcher import PostgresSearcher
from fastapi_app.rag_advanced import AdvancedRAGChat
from fastapi_app.rag_simple import SimpleRAGChat

router = fastapi.APIRouter()


@router.get("/items/{id}")
async def item_handler(id: int):
"""A simple API to get an item by ID."""
async_session_maker = async_sessionmaker(global_storage.engine, expire_on_commit=False)
async with async_session_maker() as session:
item = (await session.scalars(select(Item).where(Item.id == id))).first()
return item.to_dict()


@router.get("/similar")
async def similar_handler(id: int, n: int = 5):
"""A similarity API to find items similar to items with given ID."""
async_session_maker = async_sessionmaker(global_storage.engine, expire_on_commit=False)
async with async_session_maker() as session:
item = (await session.scalars(select(Item).where(Item.id == id))).first()
closest = await session.execute(
select(Item, Item.embedding.l2_distance(item.embedding))
.filter(Item.id != id)
.order_by(Item.embedding.l2_distance(item.embedding))
.limit(n)
)
return [item.to_dict() | {"distance": round(distance, 2)} for item, distance in closest]


@router.post("/chat")
async def chat_handler(chat_request: ChatRequest):
messages = [message.model_dump() for message in chat_request.messages]
Expand Down