Skip to content

Commit

Permalink
Build the frontend with the docker container, make runnable after ins…
Browse files Browse the repository at this point in the history
…tall (#37)
  • Loading branch information
kerinin authored Jan 30, 2024
1 parent 2422ced commit 5efe21d
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

# Include (don't ignore) the application code
!dewy
!frontend
!./pyproject.toml
!./poetry.lock

# Re-ignore pycache within `dewy`.
**/__pycache__

# Include (don't ignore) the migrations.
!migrations/*.sql
!migrations/*.sql
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

######
# 2. Create the actual image.
# 2. Compile the frontend
FROM node:20.9.0-alpine as frontend-stage
WORKDIR /app
COPY ./frontend/package.json ./package.json
RUN npm install --silent

COPY ./frontend/ ./
RUN npm run build

######
# 3. Create the actual image.
FROM python:3.11
WORKDIR /code

Expand All @@ -19,6 +29,7 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Finally, copy in the application code.
COPY ./dewy /code/dewy
COPY --from=frontend-stage /app/dist /code/dewy/frontend/dist

COPY ./migrations/0001_schema.sql /code/migrations/0001_schema.sql

Expand Down
38 changes: 24 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,33 @@ Dewy handles all of the parts shown in brown, letting you focus on your applicat

To get a local copy up and running follow these steps.

1. (Optional) Start a `pgvector` instance to persist your data
```sh
docker run -d \
-p 5432:5432 \
-e POSTGRES_DB=dewydb \
-e POSTGRES_USER=dewydbuser \
-e POSTGRES_PASSWORD=dewydbpwd \
-e POSTGRES_HOST_AUTH_METHOD=trust \
ankane/pgvector
```
1. Install Dewy
```
pip install dewy
```
1. Fire up Dewy
```sh
// Configure your OpenAI key (optional - local models will be used if not provided)
export OPENAI_API_KEY=...
// Configure your pgvector endpoint (this assumes you installed with Docker as shown previously)
export DB=postgresql://dewydbuser:dewydbpwd@localhost/dewydb
// Run the docker container
<<<<<<< Updated upstream
docker run -d dewy-kb
dewy
=======
docker run -d dewykb/dewy

>>>>>>> Stashed changes
// Go to the management console to start creating resources!
open localhost:3001
open http://localhost/admin
```
1. Install the API client library
```sh
Expand Down Expand Up @@ -136,15 +148,13 @@ Don't forget to give the project a star! Thanks again!
OPENAI_API_KEY=...
EOF
```
1. Run the Dewy service
1. Build the frontend
```sh
poetry run uvicorn dewy.main:app --host 0.0.0.0 --port 8000
cd frontend && npm install && npm run build
```
1. Run the admin frontend (optional)
1. Run the Dewy service
```sh
cd frontend
npm install
npm run dev
poetry run dewy
```
### Practices
Expand All @@ -167,4 +177,4 @@ If you're in a `poetry shell`, you can omit the `poetry run`:
Distributed under the Apache 2 License. See `LICENSE.txt` for more information.
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<p align="right">(<a href="#readme-top">back to top</a>)</p>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion dewy/collections/router.py → dewy/collection/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import APIRouter, Path
from loguru import logger

from dewy.collections.models import Collection, CollectionCreate
from dewy.collection.models import Collection, CollectionCreate
from dewy.common.collection_embeddings import get_dimensions
from dewy.common.db import PgConnectionDep

Expand Down
4 changes: 2 additions & 2 deletions dewy/common/collection_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from llama_index.schema import TextNode
from loguru import logger

from dewy.chunks.models import TextResult
from dewy.collections.models import DistanceMetric
from dewy.chunk.models import TextResult
from dewy.collection.models import DistanceMetric
from dewy.config import settings

from .extract import extract
Expand Down
3 changes: 3 additions & 0 deletions dewy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Config(BaseSettings):
env_file_encoding="utf-8",
)

SERVE_ADMIN_UI: bool = True
"""If true, serve the admin UI."""

DB: PostgresDsn
"""The Postgres database to connect to."""

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion dewy/documents/router.py → dewy/document/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dewy.common.collection_embeddings import CollectionEmbeddings
from dewy.common.db import PgConnectionDep, PgPoolDep
from dewy.documents.models import Document
from dewy.document.models import Document

from .models import AddDocumentRequest

Expand Down
19 changes: 18 additions & 1 deletion dewy/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import contextlib
from typing import AsyncIterator, TypedDict
from pathlib import Path
import os

import uvicorn
import asyncpg
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from loguru import logger

from dewy.common import db
Expand All @@ -15,6 +19,10 @@
class State(TypedDict):
pg_pool: asyncpg.Pool

# Resolve paths, independent of PWD
current_file_path = Path(__file__).resolve()
react_build_path = current_file_path.parent.parent / 'frontend' / 'dist'
migrations_path = current_file_path.parent.parent / 'migrations'

@contextlib.asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
Expand All @@ -25,7 +33,7 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
async with db.create_pool(settings.DB.unicode_string()) as pg_pool:
if settings.APPLY_MIGRATIONS:
async with pg_pool.acquire() as conn:
await apply_migrations(conn, migration_dir="migrations")
await apply_migrations(conn, migration_dir=migrations_path)

logger.info("Created database connection")
state = {
Expand Down Expand Up @@ -54,3 +62,12 @@ async def healthcheck() -> dict[str, str]:


app.include_router(api_router)

if settings.SERVE_ADMIN_UI and os.path.isdir(react_build_path):
logger.info("Running admin UI at http://localhost/admin")
# Serve static files from the React app build directory
app.mount("/admin", StaticFiles(directory=str(react_build_path), html=True), name="static")

# Function for running Dewy as a script
def run(*args):
uvicorn.run("dewy.main:app", host="0.0.0.0", port=80)
6 changes: 3 additions & 3 deletions dewy/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from fastapi import APIRouter

from dewy.chunks.router import router as chunks_router
from dewy.collections.router import router as collections_router
from dewy.documents.router import router as documents_router
from dewy.chunk.router import router as chunks_router
from dewy.collection.router import router as collections_router
from dewy.document.router import router as documents_router

api_router = APIRouter(prefix="/api")

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ volumes:
llama-cache:

networks:
kb-network:
kb-network:
2 changes: 1 addition & 1 deletion frontend/src/Chunk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ export const ChunkList = () => (
<ChunkListView />
<Pagination />
</ListBase>
);
);
2 changes: 1 addition & 1 deletion frontend/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ export const DocumentEdit = () => (
<TextInput source="url"/>
</SimpleForm>
</Edit>
);
);
2 changes: 1 addition & 1 deletion frontend/src/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import data from "./data.json";

export const fakeDataProvider = fakeRestDataProvider(data, true);

const apiUrl = 'http://localhost:8000';
const apiUrl = 'http://localhost';
const httpClient = fetchUtils.fetchJson;


Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ description = "Knowledge base service."
authors = ["Ben Chambers <ben.chambers@datastax.com>"]
readme = "README.md"
license = "Apache-2.0"
include = [
{ path = "migrations" },
{ path = "frontend/dist" },
]

[tool.poetry.scripts]
dewy = "dewy.main:run"

[tool.poetry.dependencies]
python = "^3.11"
Expand Down

0 comments on commit 5efe21d

Please sign in to comment.