Bug fix for ad login and updated async functionality on chat completion

This commit is contained in:
Saurab-Shrestha 2024-02-28 17:28:39 +05:45
parent 46c4a3122b
commit 78d1f0ab52
8 changed files with 93 additions and 109 deletions

View File

@ -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 ###

View File

@ -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 ###

View File

@ -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 ###

6
poetry.lock generated
View File

@ -2438,12 +2438,12 @@ pyasn1 = ">=0.4.6"
[[package]] [[package]]
name = "llama-cpp-python" name = "llama-cpp-python"
version = "0.2.43" version = "0.2.53"
description = "Python bindings for the llama.cpp library" description = "Python bindings for the llama.cpp library"
optional = false optional = false
python-versions = ">=3.8" python-versions = ">=3.8"
files = [ 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] [package.dependencies]
@ -7238,4 +7238,4 @@ chroma = ["chromadb"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">=3.11,<3.12" python-versions = ">=3.11,<3.12"
content-hash = "281365e7beb25015eb71d4531d959724c7e108c6af0990b064483ef70e510749" content-hash = "9e14793e879c9de04c91618b1a54fc93933d10345e69195d6d436dc9492cbc03"

View File

@ -42,19 +42,13 @@ def create_app(root_injector: Injector) -> FastAPI:
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_credentials=True, 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", 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:8001", "http://192.168.1.93", "http://192.168.1.93:88", "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://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_methods=["DELETE", "GET", "POST", "PUT", "OPTIONS", "PATCH"],
allow_headers=["*"], 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 return app

View File

@ -55,7 +55,7 @@ class ChatBody(BaseModel):
responses={200: {"model": OpenAICompletion}}, responses={200: {"model": OpenAICompletion}},
tags=["Contextual Completions"], tags=["Contextual Completions"],
) )
def chat_completion( async def chat_completion(
request: Request, body: ChatBody request: Request, body: ChatBody
) -> OpenAICompletion | StreamingResponse: ) -> OpenAICompletion | StreamingResponse:
"""Given a list of messages comprising a conversation, return a response. """Given a list of messages comprising a conversation, return a response.

View File

@ -100,7 +100,7 @@ class CompletionsBody(BaseModel):
responses={200: {"model": OpenAICompletion}}, responses={200: {"model": OpenAICompletion}},
tags=["Contextual Completions"], tags=["Contextual Completions"],
) )
def prompt_completion( async def prompt_completion(
request: Request, request: Request,
body: CompletionsBody, body: CompletionsBody,
db: Session = Depends(deps.get_db), db: Session = Depends(deps.get_db),
@ -148,4 +148,4 @@ def prompt_completion(
include_sources=body.include_sources, include_sources=body.include_sources,
context_filter=body.context_filter, context_filter=body.context_filter,
) )
return chat_completion(request, chat_body) return await chat_completion(request, chat_body)

View File

@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
LDAP_SERVER = settings.LDAP_SERVER LDAP_SERVER = settings.LDAP_SERVER
# LDAP_ENABLE = settings.LDAP_ENABLE # LDAP_ENABLE = settings.LDAP_ENABLE
LDAP_ENABLE = True LDAP_ENABLE = False
router = APIRouter(prefix="/auth", tags=["auth"]) router = APIRouter(prefix="/auth", tags=["auth"])
@ -113,12 +113,12 @@ def login_access_token(
log_audit: models.Audit = Depends(deps.get_audit_logger), log_audit: models.Audit = Depends(deps.get_audit_logger),
db: Session = Depends(deps.get_db), db: Session = Depends(deps.get_db),
form_data: OAuth2PasswordRequestForm = Depends(), form_data: OAuth2PasswordRequestForm = Depends(),
active_subscription: models.Subscription = Depends(deps.get_active_subscription) # active_subscription: models.Subscription = Depends(deps.get_active_subscription)
) -> Any: ) -> Any:
""" """
OAuth2 compatible token login, get an access token for future requests OAuth2 compatible token login, get an access token for future requests
""" """
def ad_auth(LDAP_ENABLE): def ad_auth():
if LDAP_ENABLE: if LDAP_ENABLE:
existing_user = crud.user.get_by_email(db, email=form_data.username) existing_user = crud.user.get_by_email(db, email=form_data.username)
@ -133,20 +133,22 @@ def login_access_token(
depart = crud.department.get_by_department_name(db, name=department) depart = crud.department.get_by_department_name(db, name=department)
if depart: 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: else:
department_in = schemas.DepartmentCreate(name=department) department_in = schemas.DepartmentCreate(name=department)
new_department = crud.department.create(db, obj_in=department_in) 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) user = ad_user_register(db=db, email=form_data.username, fullname=username, password=form_data.password, department_id=new_department.id)
return True return user
return False return None
if not (ad_auth(LDAP_ENABLE)): if LDAP_ENABLE:
user = ad_auth()
if not user:
raise HTTPException( raise HTTPException(
status_code=403, status_code=403,
detail="Invalid Credentials!!!", detail="Invalid Credentials!!!",
) )
else:
user = crud.user.authenticate( user = crud.user.authenticate(
db, email=form_data.username, password=form_data.password db, email=form_data.username, password=form_data.password
) )