From f9f19824422ed34c56ee7d39bd847119b316bc6d Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Mon, 12 Jun 2023 18:05:36 -0400 Subject: [PATCH] Add logger config --- src/config.py | 37 ++++++++++++++++++++++++ src/config.yaml | 6 ++++ src/internal/__init__.py | 0 src/internal/chain.py | 0 src/internal/documents.py | 3 ++ src/main.py | 20 +++++++++++-- src/routers/documents.py | 60 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/config.py create mode 100644 src/config.yaml create mode 100644 src/internal/__init__.py create mode 100644 src/internal/chain.py create mode 100644 src/internal/documents.py create mode 100644 src/routers/documents.py diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..5df9ac2 --- /dev/null +++ b/src/config.py @@ -0,0 +1,37 @@ +import envyaml +import os +import logging +from logging import Formatter, StreamHandler + +config = envyaml.EnvYAML(os.environ.get('CONFIG_PATH', 'config.yaml')) + +def initLogger() -> None: + """ + Initializes the logger + """ + # style the logger output to look like uvicorn for root app + FORMAT = '%(asctime)-15s %(levelname)s %(message)s' + DATE_FORMAT = '%b %d %H:%M:%S' + formatter = Formatter(fmt=FORMAT, datefmt=DATE_FORMAT) + handler = StreamHandler() + handler.setFormatter(formatter) + logger = logging.getLogger() + + # remove default handler + logger.handlers = [] + logger.addHandler(handler) + + if config["app.log.level"] == 'debug': + logging.basicConfig(level=logging.DEBUG) + elif config["app.log.level"] == 'info': + logging.basicConfig(level=logging.INFO) + elif config["app.log.level"] == 'warning': + logging.basicConfig(level=logging.WARNING) + elif config["app.log.level"] == 'error': + logging.basicConfig(level=logging.ERROR) + elif config["app.log.level"] == 'critical': + logging.basicConfig(level=logging.CRITICAL) + else: + logging.basicConfig(level=logging.INFO) + logging.warning("Invalid log level. Using INFO as default") + diff --git a/src/config.yaml b/src/config.yaml new file mode 100644 index 0000000..6dd43b1 --- /dev/null +++ b/src/config.yaml @@ -0,0 +1,6 @@ +weaviate: + url: "localhost:" + +app: + log: + level: "" diff --git a/src/internal/__init__.py b/src/internal/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/internal/chain.py b/src/internal/chain.py new file mode 100644 index 0000000..e69de29 diff --git a/src/internal/documents.py b/src/internal/documents.py new file mode 100644 index 0000000..a78fb7e --- /dev/null +++ b/src/internal/documents.py @@ -0,0 +1,3 @@ + +from langchain.text_splitter import SpacyTextSplitter + diff --git a/src/main.py b/src/main.py index 73f7a4a..c6a1458 100644 --- a/src/main.py +++ b/src/main.py @@ -1,8 +1,24 @@ -from fastapi import FastAPI +from fastapi import FastAPI, status +from fastapi.responses import RedirectResponse -from routers import question +from routers import question, documents + +import logging +from config import config, initLogger + +initLogger() app = FastAPI() app.include_router(question.router) +app.include_router(documents.router) +logging.warn("Test message") + + +@app.get("/") +async def root(): + """ + Redirects to the docs page + """ + return RedirectResponse(url="/docs", status_code=status.HTTP_303_SEE_OTHER) diff --git a/src/routers/documents.py b/src/routers/documents.py new file mode 100644 index 0000000..57ef928 --- /dev/null +++ b/src/routers/documents.py @@ -0,0 +1,60 @@ +from fastapi import APIRouter +from pydantic import BaseModel +from enum import Enum + +router = APIRouter( + prefix="/documents", + tags=["documents"], + responses={404: {"description": "Not found"}}, + ) + +class Document(BaseModel): + id: int + title: str + content: str + +# Document Type enum +class DocumentType(str, Enum): + markdown = "md" + #html = "html" + #pdf = "pdf" + epub = "epub" + odt = "odt" + docx = "docx" + + +@router.get("/{document_type}") +async def read_documents(document_type: DocumentType): + """ + Get all documents + """ + pass + +@router.get("/{document_id}") +async def read_document(document_id: int): + """ + Get a specific document + """ + pass + +@router.post("/") +async def create_document(document: Document): + """ + Create a new document + """ + pass + +@router.put("/{document_id}") +async def update_document(document_id: int, document: Document): + """ + Update a document + """ + pass + +@router.delete("/{document_id}") +async def delete_document(document_id: int): + """ + Delete a document + """ + pass +