Skip to content

Commit

Permalink
test: Initial changes introducing tests (#12)
Browse files Browse the repository at this point in the history
This test currently doesn't pass since we currently require Redis.

Checking it in to get the other changes in while I work on removing
the redis dependency.

- Rename `app` to `dewy`
- Introduce `tests`
  • Loading branch information
bjchambers authored Jan 24, 2024
1 parent e8c7e3d commit cd8deb9
Show file tree
Hide file tree
Showing 30 changed files with 295 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
**

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

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

# Include (don't ignore) the migrations.
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Finally, copy in the application code.
COPY ./app /code/app
COPY ./dewy /code/dewy

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

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "dewy.main:app", "--host", "0.0.0.0", "--port", "80"]
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ Notebook `example_notebook.ipynb` uses the REST API directly.

Some skeleton code based on best practices from https://github.com/zhanymkanov/fastapi-best-practices.

In `poetry shell` or using `poetry run ${command}`:
The following commands run tests and apply linting.
If you're in a `poetry shell`, you can omit the `poetry run`:

* Linting (and formatting): `ruff check --fix`
* Formatting: `ruff format`
* Type Checking: `mypy app`
* Running tests: `poetry run pytest`
* Linting (and formatting): `poetry run ruff check --fix`
* Formatting: `poetry run ruff format`
* Type Checking: `poetry run mypy app`
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/chunks/router.py → dewy/chunks/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from llama_index.schema import NodeWithScore
from loguru import logger

from app.ingest.store import StoreDep
from dewy.ingest.store import StoreDep

from .models import ImageChunk, RetrieveRequest, RetrieveResponse, TextChunk

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions app/collections/router.py → dewy/collections/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from fastapi import APIRouter, Path

from app.collections.models import Collection, CollectionCreate
from app.common.db import PgConnectionDep
from dewy.collections.models import Collection, CollectionCreate
from dewy.common.db import PgConnectionDep

router = APIRouter(prefix="/collections")

Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 7 additions & 9 deletions app/config.py → dewy/config.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
from typing import Any, Optional

from fastapi.routing import APIRoute
from pydantic import PostgresDsn, RedisDsn, ValidationInfo, field_validator
from pydantic import ConfigDict, PostgresDsn, RedisDsn, ValidationInfo, field_validator
from pydantic_core import Url
from pydantic_settings import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict

from app.constants import Environment
from dewy.constants import Environment

# See https://github.com/zhanymkanov/fastapi-best-practices#10-use-pydantics-basesettings-for-configs


class Config(BaseSettings):
"""Application configuration, parsed from environment variables."""

class Config:
"""Configurations for the pydantic BaseSettings."""

env_file = ".env"
env_file_encoding = "utf-8"
model_config = SettingsConfigDict(
env_file = ".env",
env_file_encoding = "utf-8",
)

DB: PostgresDsn
"""The Postgres database to connect to."""
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions app/documents/router.py → dewy/documents/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from fastapi import APIRouter, BackgroundTasks, Body, HTTPException, Path, status, Query
from loguru import logger

from app.common.db import PgConnectionDep, PgPoolDep
from app.documents.models import Document
from app.ingest.extract import extract
from app.ingest.extract.source import ExtractSource
from app.ingest.store import Store, StoreDep
from dewy.common.db import PgConnectionDep, PgPoolDep
from dewy.documents.models import Document
from dewy.ingest.extract import extract
from dewy.ingest.extract.source import ExtractSource
from dewy.ingest.store import Store, StoreDep

from .models import CreateRequest

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/ingest/store.py → dewy/ingest/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from llama_index.vector_stores import RedisVectorStore
from loguru import logger

from app.config import settings
from dewy.config import settings

DEFAULT_OPENAI_EMBEDDING_MODEL: str = "text-embedding-ada-002"
DEFAULT_HF_EMBEDDING_MODEL: str = "BAAI/bge-small-en"
Expand Down
12 changes: 8 additions & 4 deletions app/main.py → dewy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

import asyncpg
from fastapi import FastAPI
from loguru import logger

from app.common import db
from app.config import app_configs, settings
from app.ingest.store import Store
from app.routes import api_router
from dewy.common import db
from dewy.config import app_configs, settings
from dewy.ingest.store import Store
from dewy.routes import api_router


class State(TypedDict):
store: Store
pg_pool: asyncpg.Pool



@contextlib.asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
"""Function creating instances used during the lifespan of the service."""
Expand All @@ -23,6 +25,7 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
# for simple migration scripts.
async with db.create_pool(settings.DB.unicode_string()) as pg_pool:
if settings.APPLY_MIGRATIONS:
logger.info("Applying migrations")
async with pg_pool.acquire() as conn:
with open("migrations/0001_schema.sql") as schema_file:
schema = schema_file.read()
Expand All @@ -33,6 +36,7 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[State]:
"pg_pool": pg_pool,
}

logger.info("Created store and db")
yield state


Expand Down
6 changes: 3 additions & 3 deletions app/routes.py → dewy/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from fastapi import APIRouter

from app.chunks.router import router as chunks_router
from app.collections.router import router as collections_router
from app.documents.router import router as documents_router
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

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 @@ -2,7 +2,7 @@ version: '3.7'

services:
dewy:
image: app_image
image: dewy
environment:
ENVIRONMENT: LOCAL
REDIS: "redis://default:testing123@redis:6379"
Expand Down
Loading

0 comments on commit cd8deb9

Please sign in to comment.