diff --git a/alembic/versions/186dc125c8c4_set_null_on_delete_audit.py b/alembic/versions/186dc125c8c4_set_null_on_delete_audit.py deleted file mode 100644 index f48e906e..00000000 --- a/alembic/versions/186dc125c8c4_set_null_on_delete_audit.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Set null on delete audit - -Revision ID: 186dc125c8c4 -Revises: 9aa759c05b19 -Create Date: 2024-02-26 15:43:49.759556 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = '186dc125c8c4' -down_revision: Union[str, None] = '9aa759c05b19' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint('audit_user_id_fkey', 'audit', type_='foreignkey') - op.create_foreign_key(None, 'audit', 'users', ['user_id'], ['id'], ondelete='SET NULL') - # op.create_unique_constraint('unique_user_role', 'user_roles', ['user_id', 'role_id', 'company_id']) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - # op.drop_constraint('unique_user_role', 'user_roles', type_='unique') - op.drop_constraint(None, 'audit', type_='foreignkey') - op.create_foreign_key('audit_user_id_fkey', 'audit', 'users', ['user_id'], ['id']) - # ### end Alembic commands ### diff --git a/alembic/versions/9aa759c05b19_create_audit_model.py b/alembic/versions/9aa759c05b19_create_audit_model.py deleted file mode 100644 index b48d89f6..00000000 --- a/alembic/versions/9aa759c05b19_create_audit_model.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Create audit model - -Revision ID: 9aa759c05b19 -Revises: -Create Date: 2024-02-25 10:03:28.092131 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = '9aa759c05b19' -down_revision: Union[str, None] = None -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('audit', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('timestamp', sa.DateTime(), nullable=False), - sa.Column('user_id', sa.Integer(), nullable=True), - sa.Column('model', sa.String(), nullable=False), - sa.Column('action', sa.String(), nullable=False), - sa.Column('details', postgresql.JSONB(astext_type=sa.Text()), nullable=True), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_audit_id'), 'audit', ['id'], unique=False) - # op.create_unique_constraint('unique_user_role', 'user_roles', ['user_id', 'role_id', 'company_id']) - op.add_column('users', sa.Column('password_created', sa.DateTime(), nullable=True)) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('users', 'password_created') - # op.drop_constraint('unique_user_role', 'user_roles', type_='unique') - op.drop_index(op.f('ix_audit_id'), table_name='audit') - op.drop_table('audit') - # ### end Alembic commands ### diff --git a/alembic/versions/9ae2f4e97436_create_department_and_audit_models.py b/alembic/versions/9ae2f4e97436_create_department_and_audit_models.py new file mode 100644 index 00000000..d73395f3 --- /dev/null +++ b/alembic/versions/9ae2f4e97436_create_department_and_audit_models.py @@ -0,0 +1,67 @@ +"""Create department and audit models + +Revision ID: 9ae2f4e97436 +Revises: +Create Date: 2024-02-28 14:53:19.144973 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = '9ae2f4e97436' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('departments', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.Column('total_users', sa.Integer(), nullable=True), + sa.Column('total_documents', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_departments_id'), 'departments', ['id'], unique=False) + op.create_index(op.f('ix_departments_name'), 'departments', ['name'], unique=True) + op.create_table('audit', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('timestamp', sa.DateTime(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.Column('model', sa.String(), nullable=False), + sa.Column('action', sa.String(), nullable=False), + sa.Column('details', postgresql.JSONB(astext_type=sa.Text()), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_audit_id'), 'audit', ['id'], unique=False) + op.add_column('document', sa.Column('department_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'document', 'departments', ['department_id'], ['id']) + # op.create_unique_constraint('unique_user_role', 'user_roles', ['user_id', 'role_id', 'company_id']) + op.add_column('users', sa.Column('password_created', sa.DateTime(), nullable=True)) + op.add_column('users', sa.Column('department_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'users', 'departments', ['department_id'], ['id']) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'users', type_='foreignkey') + op.drop_column('users', 'department_id') + op.drop_column('users', 'password_created') + # op.drop_constraint('unique_user_role', 'user_roles', type_='unique') + op.drop_constraint(None, 'document', type_='foreignkey') + op.drop_column('document', 'department_id') + op.drop_index(op.f('ix_audit_id'), table_name='audit') + op.drop_table('audit') + op.drop_index(op.f('ix_departments_name'), table_name='departments') + op.drop_index(op.f('ix_departments_id'), table_name='departments') + op.drop_table('departments') + # ### end Alembic commands ### diff --git a/poetry.lock b/poetry.lock index c6dfe5b2..5d880768 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2438,12 +2438,12 @@ pyasn1 = ">=0.4.6" [[package]] name = "llama-cpp-python" -version = "0.2.43" +version = "0.2.53" description = "Python bindings for the llama.cpp library" optional = false python-versions = ">=3.8" files = [ - {file = "llama_cpp_python-0.2.43.tar.gz", hash = "sha256:fb3fd97622f7c1e373b28de1147fcdcc6a203705e6cc6376074225cf4f94711b"}, + {file = "llama_cpp_python-0.2.53.tar.gz", hash = "sha256:f7ff8eda538ca6c80521a8bbf80d3ef4527ecb28f6d08fa9b3bb1f0cfc3b684e"}, ] [package.dependencies] @@ -7238,4 +7238,4 @@ chroma = ["chromadb"] [metadata] lock-version = "2.0" python-versions = ">=3.11,<3.12" -content-hash = "281365e7beb25015eb71d4531d959724c7e108c6af0990b064483ef70e510749" +content-hash = "9e14793e879c9de04c91618b1a54fc93933d10345e69195d6d436dc9492cbc03" diff --git a/private_gpt/launcher.py b/private_gpt/launcher.py index 4f819785..cc2bb03a 100644 --- a/private_gpt/launcher.py +++ b/private_gpt/launcher.py @@ -42,19 +42,13 @@ def create_app(root_injector: Injector) -> FastAPI: app.add_middleware( CORSMiddleware, allow_credentials=True, - allow_origins=["http://localhost:80/", "http://10.1.101.125:80", "http://quickgpt.gibl.com.np:80", "http://127.0.0.1", - "http://10.1.101.125", "http://quickgpt.gibl.com.np", "http://localhost:8001", "http://192.168.1.93", "http://192.168.1.93:88", + allow_origins=["http://localhost:3000/", "http://10.1.101.125:80", "http://quickgpt.gibl.com.np:80", "http://127.0.0.1", + "http://10.1.101.125", "http://quickgpt.gibl.com.np", "http://localhost:8000", "http://192.168.1.93", "http://192.168.1.93:88", "http://192.168.1.98", "http://192.168.1.98:5173", "http://localhost:3000","https://globaldocquery.gibl.com.np/", "http://127.0.0.1/", "http://localhost/", - "http://localhost:80", "http://192.168.1.131:80/", "http://192.168.1.131"], + "http://localhost:80", "http://192.168.1.131", 'http://192.168.1.131:3000'], allow_methods=["DELETE", "GET", "POST", "PUT", "OPTIONS", "PATCH"], allow_headers=["*"], ) - # if settings.ui.enabled: - # logger.debug("Importing the UI module") - # from private_gpt.ui.admin_ui import PrivateAdminGptUi - # admin_ui = root_injector.get(PrivateAdminGptUi) - # admin_ui.mount_in_admin_app(app, '/admin') - return app \ No newline at end of file diff --git a/private_gpt/server/chat/chat_router.py b/private_gpt/server/chat/chat_router.py index 50232619..8c55473c 100644 --- a/private_gpt/server/chat/chat_router.py +++ b/private_gpt/server/chat/chat_router.py @@ -55,7 +55,7 @@ class ChatBody(BaseModel): responses={200: {"model": OpenAICompletion}}, tags=["Contextual Completions"], ) -def chat_completion( +async def chat_completion( request: Request, body: ChatBody ) -> OpenAICompletion | StreamingResponse: """Given a list of messages comprising a conversation, return a response. diff --git a/private_gpt/server/completions/completions_router.py b/private_gpt/server/completions/completions_router.py index 17fa76e5..f5b4ff7e 100644 --- a/private_gpt/server/completions/completions_router.py +++ b/private_gpt/server/completions/completions_router.py @@ -100,7 +100,7 @@ class CompletionsBody(BaseModel): responses={200: {"model": OpenAICompletion}}, tags=["Contextual Completions"], ) -def prompt_completion( +async def prompt_completion( request: Request, body: CompletionsBody, db: Session = Depends(deps.get_db), @@ -148,4 +148,4 @@ def prompt_completion( include_sources=body.include_sources, context_filter=body.context_filter, ) - return chat_completion(request, chat_body) + return await chat_completion(request, chat_body) diff --git a/private_gpt/users/api/v1/routers/auth.py b/private_gpt/users/api/v1/routers/auth.py index f5b7ee10..b876299f 100644 --- a/private_gpt/users/api/v1/routers/auth.py +++ b/private_gpt/users/api/v1/routers/auth.py @@ -19,7 +19,7 @@ logger = logging.getLogger(__name__) LDAP_SERVER = settings.LDAP_SERVER # LDAP_ENABLE = settings.LDAP_ENABLE -LDAP_ENABLE = True +LDAP_ENABLE = False router = APIRouter(prefix="/auth", tags=["auth"]) @@ -113,12 +113,12 @@ def login_access_token( log_audit: models.Audit = Depends(deps.get_audit_logger), db: Session = Depends(deps.get_db), form_data: OAuth2PasswordRequestForm = Depends(), - active_subscription: models.Subscription = Depends(deps.get_active_subscription) + # active_subscription: models.Subscription = Depends(deps.get_active_subscription) ) -> Any: """ OAuth2 compatible token login, get an access token for future requests """ - def ad_auth(LDAP_ENABLE): + def ad_auth(): if LDAP_ENABLE: existing_user = crud.user.get_by_email(db, email=form_data.username) @@ -133,23 +133,25 @@ def login_access_token( depart = crud.department.get_by_department_name(db, name=department) if depart: - ad_user_register(db=db, email=form_data.username, fullname=username, password=form_data.password, department_id=depart.id) + user = ad_user_register(db=db, email=form_data.username, fullname=username, password=form_data.password, department_id=depart.id) else: department_in = schemas.DepartmentCreate(name=department) new_department = crud.department.create(db, obj_in=department_in) - ad_user_register(db=db, email=form_data.username, fullname=username, password=form_data.password, department_id=new_department.id) - return True - return False + user = ad_user_register(db=db, email=form_data.username, fullname=username, password=form_data.password, department_id=new_department.id) + return user + return None - if not (ad_auth(LDAP_ENABLE)): - raise HTTPException( - status_code=403, - detail="Invalid Credentials!!!", + if LDAP_ENABLE: + user = ad_auth() + if not user: + raise HTTPException( + status_code=403, + detail="Invalid Credentials!!!", + ) + else: + user = crud.user.authenticate( + db, email=form_data.username, password=form_data.password ) - - user = crud.user.authenticate( - db, email=form_data.username, password=form_data.password - ) if not user: raise HTTPException( status_code=400, detail="Incorrect email or password"