diff --git a/.gitignore b/.gitignore index 30967a0..3aa0eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ .DS_Store + +# Local env file +.local.env diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..907ce47 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,45 @@ +# All directory paths for COPY commands are relative to the build context + +# Ensure this python version stays in sync with CI +FROM python:3.11-slim as base +WORKDIR /backend + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 +ENV POETRY_HOME="/opt/poetry" +ENV MYPYPATH="/app/src/stubs" + +# Use bash as the shell for the build +# https://github.com/docker/for-linux/issues/408#issuecomment-414748815 +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + +RUN set -eux && \ + apt-get update && \ + apt-get install -y \ + build-essential \ + # to install poetry + curl \ + # to install psycopg2 + libpq-dev python3-dev + +# https://python-poetry.org/docs/master/#installing-with-the-official-installer +RUN curl -sSL https://install.python-poetry.org | python - +ENV PATH="$POETRY_HOME/bin:$PATH" + +# install deps before copying project files so the cache is only invalidated +# when the deps change +COPY ./backend/pyproject.toml ./backend/poetry.lock . +RUN poetry config virtualenvs.create false +RUN poetry install --no-root --only main + +COPY ./backend . + +EXPOSE 8000 + +### +# development image +### +FROM base as development + +ENTRYPOINT ["bash", "./scripts/local_entry_point.sh"] \ No newline at end of file diff --git a/backend/db/models.py b/backend/db/models.py index 14fda2b..9025668 100644 --- a/backend/db/models.py +++ b/backend/db/models.py @@ -66,16 +66,6 @@ class Extractor(TimestampedModel): server_default="", comment="The name of the extractor.", ) - created_at = Column( - DateTime(timezone=True), - server_default=func.now(), - comment="Time when this extracted was originally created.", - ) - modified_at = Column( - DateTime(timezone=True), - onupdate=func.now(), - comment="Last time this was modified.", - ) schema = Column( JSONB, nullable=False, diff --git a/backend/scripts/local_entry_point.sh b/backend/scripts/local_entry_point.sh new file mode 100644 index 0000000..c4554f6 --- /dev/null +++ b/backend/scripts/local_entry_point.sh @@ -0,0 +1,10 @@ +# -e: fail on any nonzero exit status +# -u: fail if any referenced variables are not set +# -x: print commands before running them +# -o pipefail: fail if a command in a pipe has a nonzero exit code +set -euxo pipefail + +# For now just create the db if it doesn't exist +python -m scripts.run_migrations create + +uvicorn server.main:app --host 0.0.0.0 --port 8000 --reload diff --git a/backend/server/settings.py b/backend/server/settings.py index 6f01116..86dca67 100644 --- a/backend/server/settings.py +++ b/backend/server/settings.py @@ -1,5 +1,7 @@ from __future__ import annotations +import os + from langchain_openai import ChatOpenAI from sqlalchemy.engine import URL @@ -9,12 +11,12 @@ CHUNK_SIZE = int(4_096 * 0.8) MAX_CONCURRENCY = 1 -def get_postgres_url(): +def get_postgres_url() -> URL: url = URL.create( drivername="postgresql", username="langchain", password="langchain", - host="localhost", + host=os.environ.get("PG_HOST", "localhost"), database="langchain", port=5432, ) diff --git a/docker-compose.yml b/docker-compose.yml index e8ecd40..622b30d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3" +version: "3.8" name: langchain-extract services: @@ -7,24 +7,37 @@ services: # Make sure to keep in sync with CI # version if being tested on CI. image: postgres:16 + expose: + - "5432" ports: - "5432:5432" environment: POSTGRES_DB: langchain POSTGRES_USER: langchain POSTGRES_PASSWORD: langchain + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U langchain -d langchain -W langchain" ] + interval: 10s + timeout: 5s + retries: 5 volumes: - postgres_data:/var/lib/postgresql/data -# For rely on docker compose to spin up postgres -# but developer using docker -# Add backend when we actually need it -# backend: -# build: ./backend -# ports: -# - "8000:8000" -# depends_on: -# - postgres + backend: + build: + context: . + dockerfile: ./backend/Dockerfile + target: development + env_file: + - .local.env + environment: + - PG_HOST=postgres + ports: + - "8000:8000" # Backend is accessible on localhost:8100 + depends_on: + - postgres + volumes: + - ./backend:/backend volumes: postgres_data: