mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-08-12 11:40:44 +08:00
ran pre-commit and fixed issues
This commit is contained in:
+1
-2
@@ -4,7 +4,6 @@
|
||||
|
||||
Please edit `alembic.ini` and specify your database uri in the parameter `sqlalchemy.url`.
|
||||
|
||||
|
||||
## REST Server Configuration
|
||||
|
||||
Please either use environment variables or create a `.env` file in the backend root directory (in which this readme file is located) to specify the `DATABASE_URI`.
|
||||
@@ -15,4 +14,4 @@ Example contents of a `.env` file for the backend:
|
||||
DATABASE_URI="postgresql://<username>:<password>@<host>/<database_name>"
|
||||
BACKEND_CORS_ORIGINS=["http://localhost", "http://localhost:4200", "http://localhost:3000", "http://localhost:8080", "https://localhost", "https://localhost:4200", "https://localhost:3000", "https://localhost:8080", "http://dev.ocgpt.laion.ai", "https://stag.ocgpt.laion.ai", "https://ocgpt.laion.ai"]
|
||||
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1 +1 @@
|
||||
Generic single-database configuration.
|
||||
Generic single-database configuration.
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
@@ -64,9 +63,7 @@ def run_migrations_online() -> None:
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
)
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""first revision
|
||||
|
||||
Revision ID: 23e5fea252dd
|
||||
Revises:
|
||||
Revises:
|
||||
Create Date: 2022-12-12 12:47:28.801354
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '23e5fea252dd'
|
||||
revision = "23e5fea252dd"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
@@ -23,25 +23,29 @@ def upgrade() -> None:
|
||||
sa.Column("name", sa.String(200), nullable=False),
|
||||
sa.Column("service_admin_email", sa.String(128), nullable=True),
|
||||
sa.Column("api_key", sa.String(300), nullable=False),
|
||||
sa.Column("can_append", sa.Boolean, nullable=False, server_default='true'),
|
||||
sa.Column("can_write", sa.Boolean, nullable=False, server_default='false'),
|
||||
sa.Column("can_delete", sa.Boolean, nullable=False, server_default='false'),
|
||||
sa.Column("can_read", sa.Boolean, nullable=False, server_default='true'),
|
||||
sa.Column("can_append", sa.Boolean, nullable=False, server_default="true"),
|
||||
sa.Column("can_write", sa.Boolean, nullable=False, server_default="false"),
|
||||
sa.Column("can_delete", sa.Boolean, nullable=False, server_default="false"),
|
||||
sa.Column("can_read", sa.Boolean, nullable=False, server_default="true"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_service_client_api_key"), "service_client", ["api_key"], unique=True)
|
||||
|
||||
|
||||
op.create_table(
|
||||
"labeler",
|
||||
sa.Column("id", sa.Integer, sa.Identity()),
|
||||
sa.Column("display_name", sa.String(96), nullable=False),
|
||||
sa.Column("discord_username", sa.String(96), nullable=True),
|
||||
sa.Column("created_date", sa.DateTime, nullable=False, server_default=sa.func.current_timestamp()),
|
||||
sa.Column("is_enabled", sa.Boolean, nullable=False, server_default='true'),
|
||||
sa.Column("notes", sa.String(10*1024), nullable=True),
|
||||
sa.Column(
|
||||
"created_date",
|
||||
sa.DateTime,
|
||||
nullable=False,
|
||||
server_default=sa.func.current_timestamp(),
|
||||
),
|
||||
sa.Column("is_enabled", sa.Boolean, nullable=False, server_default="true"),
|
||||
sa.Column("notes", sa.String(10 * 1024), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("discord_username")
|
||||
sa.UniqueConstraint("discord_username"),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
@@ -51,8 +55,16 @@ def upgrade() -> None:
|
||||
sa.Column("prompt", sa.Text, nullable=False),
|
||||
sa.Column("response", sa.Text, nullable=True),
|
||||
sa.Column("lang", sa.String(32), nullable=True),
|
||||
sa.Column("created_date", sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()),
|
||||
sa.ForeignKeyConstraint(["labeler_id"], ["labeler.id"],),
|
||||
sa.Column(
|
||||
"created_date",
|
||||
sa.DateTime(),
|
||||
nullable=False,
|
||||
server_default=sa.func.current_timestamp(),
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["labeler_id"],
|
||||
["labeler.id"],
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("prompt_labeler_id"), "prompt", ["labeler_id"], unique=False)
|
||||
|
||||
+15
-9
@@ -1,10 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Generator
|
||||
from sqlmodel import Session
|
||||
from fastapi import Security, HTTPException
|
||||
from fastapi.security.api_key import APIKeyQuery, APIKeyHeader, APIKey
|
||||
|
||||
from app.database import engine
|
||||
from app.models import ServiceClient
|
||||
|
||||
from fastapi import HTTPException, Security
|
||||
from fastapi.security.api_key import APIKey, APIKeyHeader, APIKeyQuery
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
@@ -28,16 +29,21 @@ async def get_api_key(
|
||||
|
||||
|
||||
def api_auth(
|
||||
api_key: APIKey, db: Session, create: bool = False, read: bool = True, update: bool = False, delete: bool = False
|
||||
api_key: APIKey,
|
||||
db: Session,
|
||||
create: bool = False,
|
||||
read: bool = True,
|
||||
update: bool = False,
|
||||
delete: bool = False,
|
||||
) -> ServiceClient:
|
||||
if api_key is not None:
|
||||
api_client = db.query(ServiceClient).filter(ServiceClient.api_key == api_key).first()
|
||||
if api_client is not None:
|
||||
if (
|
||||
(create == False or api_client.can_append)
|
||||
and (read == False or api_client.can_read)
|
||||
and (update == False or api_client.can_write)
|
||||
and (delete == False or api_client.can_delete)
|
||||
(create is False or api_client.can_append)
|
||||
and (read is False or api_client.can_read)
|
||||
and (update is False or api_client.can_write)
|
||||
and (delete is False or api_client.can_delete)
|
||||
):
|
||||
return api_client
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
from app.api.v1 import labelers, prompts
|
||||
from fastapi import APIRouter
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(labelers.router, prefix="/labelers", tags=["labelers"])
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST
|
||||
|
||||
from app import crud, schemas
|
||||
from app.api import deps
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED
|
||||
|
||||
from app import crud, schemas
|
||||
from app.api import deps
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -50,7 +49,7 @@ def create_prompt(
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Invalid labeler user name")
|
||||
if not labeler.is_enabled:
|
||||
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Labeler disabled")
|
||||
|
||||
|
||||
item_in.labeler_id = labeler.id
|
||||
item_in.discord_username = None
|
||||
item = crud.prompt.create(db=db, obj_in=item_in)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from pydantic import AnyHttpUrl, BaseSettings, PostgresDsn, validator
|
||||
|
||||
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .crud_labeler import labeler
|
||||
from .crud_prompt import prompt
|
||||
from .crud_prompt import prompt
|
||||
|
||||
__all__ = ["labeler", "prompt"]
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Session, SQLModel
|
||||
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=SQLModel)
|
||||
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
||||
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
||||
@@ -25,9 +25,7 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
||||
return db.query(self.model).filter(self.model.id == id).first()
|
||||
|
||||
def get_multi(
|
||||
self, db: Session, *, begin_id: int = 0, limit: int = 100
|
||||
) -> List[ModelType]:
|
||||
def get_multi(self, db: Session, *, begin_id: int = 0, limit: int = 100) -> List[ModelType]:
|
||||
return db.query(self.model).filter(self.model.id >= begin_id).limit(limit).all()
|
||||
|
||||
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
||||
@@ -38,13 +36,7 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update(
|
||||
self,
|
||||
db: Session,
|
||||
*,
|
||||
db_obj: ModelType,
|
||||
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
||||
) -> ModelType:
|
||||
def update(self, db: Session, *, db_obj: ModelType, obj_in: Union[UpdateSchemaType, Dict[str, Any]]) -> ModelType:
|
||||
obj_data = jsonable_encoder(db_obj)
|
||||
if isinstance(obj_in, dict):
|
||||
update_data = obj_in
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Optional
|
||||
|
||||
from app.crud.base import CRUDBase
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from app.crud.base import CRUDBase
|
||||
from app.models.prompt import Prompt
|
||||
from app.schemas.prompt import PromptCreate
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from sqlmodel import create_engine
|
||||
# -*- coding: utf-8 -*-
|
||||
from app.config import settings
|
||||
from sqlmodel import create_engine
|
||||
|
||||
engine = create_engine(settings.DATABASE_URI)
|
||||
|
||||
+11
-12
@@ -1,16 +1,15 @@
|
||||
import dataclasses
|
||||
from datetime import datetime
|
||||
import json
|
||||
from typing import Optional
|
||||
# -*- coding: utf-8 -*-
|
||||
# flake8: noqa
|
||||
import argparse
|
||||
import dataclasses
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
from app.config import settings
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import app.api.deps
|
||||
|
||||
from app.config import settings
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
||||
|
||||
def main():
|
||||
@@ -24,14 +23,14 @@ def main():
|
||||
app.api.deps.engine = engine
|
||||
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
with Session(engine) as session:
|
||||
# create a test serivice
|
||||
#sc1 = ServiceClient(name='blub', api_key='1234')
|
||||
#session.add(sc1)
|
||||
|
||||
session.commit()
|
||||
"""
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+4
-6
@@ -1,12 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from app.api.v1.api import api_router
|
||||
from app.config import settings
|
||||
from fastapi import FastAPI
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.api.v1.api import api_router
|
||||
from app.config import settings
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
||||
)
|
||||
app = FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json")
|
||||
|
||||
# Set all CORS enabled origins
|
||||
if settings.BACKEND_CORS_ORIGINS:
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
from .service_client import ServiceClient
|
||||
# -*- coding: utf-8 -*-
|
||||
from .labeler import Labeler
|
||||
from .prompt import Prompt
|
||||
from .service_client import ServiceClient
|
||||
|
||||
__all__ = ["Labeler", "Prompt", "ServiceClient"]
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlmodel import Field, SQLModel
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Labeler(SQLModel, table=True):
|
||||
@@ -9,6 +11,9 @@ class Labeler(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
display_name: str
|
||||
discord_username: str
|
||||
created_date: Optional[datetime] = Field(sa_column=sa.Column(sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()), nullable=False)
|
||||
created_date: Optional[datetime] = Field(
|
||||
sa_column=sa.Column(sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()),
|
||||
nullable=False,
|
||||
)
|
||||
is_enabled: bool
|
||||
notes: str
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlmodel import Field, SQLModel
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Prompt(SQLModel, table=True):
|
||||
@@ -11,5 +13,7 @@ class Prompt(SQLModel, table=True):
|
||||
prompt: str
|
||||
response: Optional[str]
|
||||
lang: Optional[str]
|
||||
created_date: Optional[datetime] = Field(sa_column=sa.Column(sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()), nullable=False)
|
||||
|
||||
created_date: Optional[datetime] = Field(
|
||||
sa_column=sa.Column(sa.DateTime(), nullable=False, server_default=sa.func.current_timestamp()),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from sqlmodel import Field, SQLModel
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import Optional
|
||||
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
class ServiceClient(SQLModel, table=True):
|
||||
__tablename__ = "service_client"
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from .labeler import Labeler, LabelerCreate, LabelerUpdate
|
||||
from .prompt import Prompt, PromptCreate
|
||||
|
||||
__all__ = ["Labeler", "LabelerCreate", "LabelerUpdate", "Prompt", "PromptCreate"]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from typing import Optional
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from typing import Optional
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
alembic==1.8.1
|
||||
fastapi==0.88.0
|
||||
psycopg2-binary==2.9.5
|
||||
pydantic==1.9.1
|
||||
python-dotenv==0.21.0
|
||||
SQLAlchemy==1.4.41
|
||||
sqlmodel==0.0.8
|
||||
starlette==0.22.0
|
||||
uvicorn==0.20.0
|
||||
psycopg2-binary==2.9.5
|
||||
alembic==1.8.1
|
||||
python-dotenv==0.21.0
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
uvicorn app.main:app --reload
|
||||
uvicorn app.main:app --reload
|
||||
|
||||
Reference in New Issue
Block a user