Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
homanp committed Sep 26, 2023
1 parent 2c7ad6a commit 1426c7f
Show file tree
Hide file tree
Showing 18 changed files with 3,557 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.git
.DS_Store
.gitignore
.dockerignore

# PyCharm specific folder

.idea

# Environments

.env
.venv
env/
venv/
ENV/

# UI
ui/
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_URL=
DATABASE_MIGRATION_URL=
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
exclude =
venv
.venv
__pycache__
notebooks
# Recommend matching the black line length (default 88),
# rather than using the flake8 default of 79:
max-line-length = 88
extend-ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
E501,
38 changes: 38 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: lint

on:
push:
branches: [main]
pull_request:

env:
POETRY_VERSION: "1.4.2"

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
steps:
- uses: actions/checkout@v3
- name: Install poetry
run: |
pipx install poetry==$POETRY_VERSION
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: poetry
- name: Install dependencies
run: |
cd libs/superagent
poetry install
- name: Analysing the code with our lint
run: |
cd libs/superagent
make lint
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__
*.pyc
.env
.env*.local
.venv
superenv/
.DS_Store
venv/
/.vscode
/.codesandbox
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM python:3.11 AS builder
# Use the python latest image

RUN pip install poetry

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

# Copy only dependency files for layer caching
COPY pyproject.toml poetry.lock ./

# Install the required packages of the application into .venv
RUN poetry install --no-root && rm -rf $POETRY_CACHE_DIR

FROM python:3.11 AS runtime

ENV PATH="/app/.venv/bin:$PATH"
ENV PORT="8080"

COPY --from=builder /app/.venv /app/.venv

COPY . ./

RUN prisma generate

# Bind the port and refer to the app.py app
CMD exec gunicorn --bind :$PORT --workers 2 --timeout 0 --worker-class uvicorn.workers.UvicornWorker --threads 8 lib.main:app
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
format:
poetry run black .
poetry run ruff --select I --fix .
poetry run vulture .

PYTHON_FILES=.
lint: PYTHON_FILES=.
lint_diff: PYTHON_FILES=$(shell git diff --name-only --diff-filter=d master | grep -E '\.py$$')

lint lint_diff:
poetry run black $(PYTHON_FILES) --check
poetry run ruff .
poetry run vulture .
Empty file added lib/__init__.py
Empty file.
Empty file added lib/api/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions lib/api/ingest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import APIRouter

router = APIRouter()


@router.post(
"/ingest",
name="ingest",
description="Ingest data",
)
async def ingest():
"""Endpoint for ingesting data"""
return {"success": True, "data": None}
13 changes: 13 additions & 0 deletions lib/api/invoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import APIRouter

router = APIRouter()


@router.post(
"/invoke",
name="invoke",
description="Invoke model",
)
async def invoke():
"""Endpoint for invoking model"""
return {"success": True, "data": None}
69 changes: 69 additions & 0 deletions lib/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import logging
import time

import colorlog
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware

from lib.routers import router
from lib.utils.prisma import prisma

# Create a color formatter
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(levelname)s: %(message)s",
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
},
secondary_log_colors={},
style="%",
) # Create a console handler and set the formatter
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers=[console_handler],
force=True,
)

app = FastAPI(
title="Superagent",
docs_url="/",
description="Build, manage and deploy LLM-powered Agents",
version="0.1.0",
)

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
print(f"Total request time: {process_time} secs")
return response


@app.on_event("startup")
async def startup():
await prisma.connect()


@app.on_event("shutdown")
async def shutdown():
await prisma.disconnect()


app.include_router(router)
10 changes: 10 additions & 0 deletions lib/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generator client {
provider = "prisma-client-py"
interface = "asyncio"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("DATABASE_SHADOW_URL")
}
9 changes: 9 additions & 0 deletions lib/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi import APIRouter

from lib.api import ingest, invoke

router = APIRouter()
api_prefix = "/api/v1"

router.include_router(ingest.router, tags=["Ingest"], prefix=api_prefix)
router.include_router(invoke.router, tags=["Invoke"], prefix=api_prefix)
3 changes: 3 additions & 0 deletions lib/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from prisma import Prisma

prisma = Prisma()
Empty file added lib/utils/prisma.py
Empty file.
Loading

0 comments on commit 1426c7f

Please sign in to comment.