Skip to content

Commit

Permalink
Add rag match and reformat code (#276)
Browse files Browse the repository at this point in the history
* Add rag match and reformat code

Signed-off-by: Aisuko <[email protected]>

* fix linter

Signed-off-by: Aisuko <[email protected]>

* remove no useful test cases

Signed-off-by: Aisuko <[email protected]>

* Add teardown event for all the httpx clients

Signed-off-by: Aisuko <[email protected]>

---------

Signed-off-by: Aisuko <[email protected]>
  • Loading branch information
Aisuko authored Jul 20, 2024
1 parent d9fdf92 commit 72e510d
Show file tree
Hide file tree
Showing 45 changed files with 566 additions and 450 deletions.
21 changes: 14 additions & 7 deletions backend/src/api/routes/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
from src.repository.crud.account import AccountCRUDRepository
from src.securities.authorizations.jwt import jwt_generator, jwt_required
from src.utilities.exceptions.database import EntityDoesNotExist
from src.utilities.exceptions.http.exc_404 import http_404_exc_id_not_found_request, http_404_exc_username_not_found_request
from src.utilities.exceptions.http.exc_404 import (
http_404_exc_id_not_found_request,
http_404_exc_username_not_found_request,
)
from src.utilities.exceptions.http.exc_401 import http_exc_401_cunauthorized_request

router = fastapi.APIRouter(prefix="/accounts", tags=["accounts"])
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/verify")


@router.get(
path="",
name="accounts:read-accounts",
Expand All @@ -37,7 +41,7 @@
async def get_accounts(
token: str = fastapi.Depends(oauth2_scheme),
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
jwt_payload: dict = fastapi.Depends(jwt_required)
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> list[AccountInResponse]:
"""
Get a list of accounts
Expand Down Expand Up @@ -99,7 +103,7 @@ async def get_account(
id: int,
token: str = fastapi.Depends(oauth2_scheme),
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
jwt_payload: dict = fastapi.Depends(jwt_required)
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> AccountInResponse:
"""
Get an account by id
Expand Down Expand Up @@ -151,6 +155,7 @@ async def get_account(
),
)


@router.patch(
path="",
name="accounts:update-current-account",
Expand All @@ -161,7 +166,7 @@ async def update_account(
token: str = fastapi.Depends(oauth2_scheme),
account_update: AccountInUpdate = fastapi.Body(...),
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
jwt_payload: dict = fastapi.Depends(jwt_required)
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> AccountInResponse:
"""
update current account info
Expand Down Expand Up @@ -226,6 +231,7 @@ async def update_account(
),
)


@router.patch(
path="/{id}",
name="accounts:update-account-by-id",
Expand All @@ -237,7 +243,7 @@ async def update_account_by_admin(
token: str = fastapi.Depends(oauth2_scheme),
account_update: AccountInUpdate = fastapi.Body(...),
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
jwt_payload: dict = fastapi.Depends(jwt_required)
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> AccountInResponse:
"""
update account info by account id
Expand Down Expand Up @@ -301,9 +307,10 @@ async def update_account_by_admin(

@router.delete(path="", name="accounts:delete-account-by-id", status_code=fastapi.status.HTTP_200_OK)
async def delete_account(
id: int, account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
id: int,
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
token: str = fastapi.Depends(oauth2_scheme),
jwt_payload: dict = fastapi.Depends(jwt_required)
jwt_payload: dict = fastapi.Depends(jwt_required),
) -> dict[str, str]:
"""
Delete an account by id
Expand Down
8 changes: 2 additions & 6 deletions backend/src/api/routes/ai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import fastapi

from src.api.dependencies.repository import get_rag_repository, get_repository
from src.models.schemas.ai_model import AiModelCreate, AiModelChooseResponse, AiModelInResponse,AiModelCreateResponse
from src.models.schemas.ai_model import AiModelCreate, AiModelChooseResponse, AiModelInResponse, AiModelCreateResponse
from src.repository.crud.ai_model import AiModelCRUDRepository
from src.repository.rag.chat import RAGChatModelRepository

Expand Down Expand Up @@ -137,8 +137,4 @@ async def create_ai_model(
raise EntityDoesNotExist(f"AiModel with id `{ai_model.name}` alread exist!")

ai_model = await aimodel_repo.create_aimodel(aimodel_create=req_model)
return AiModelCreateResponse(
id=ai_model.id,
name=ai_model.name,
des=ai_model.des
)
return AiModelCreateResponse(id=ai_model.id, name=ai_model.name, des=ai_model.des)
21 changes: 12 additions & 9 deletions backend/src/api/routes/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Annotated
from src.api.dependencies.repository import get_repository
from src.models.schemas.account import AccountInCreate, AccountInLogin, AccountInResponse, AccountWithToken
from src.config.settings.const import ANONYMOUS_USER,ANONYMOUS_PASS
from src.config.settings.const import ANONYMOUS_USER, ANONYMOUS_PASS
from src.repository.crud.account import AccountCRUDRepository
from src.securities.authorizations.jwt import jwt_generator
from src.utilities.exceptions.database import EntityAlreadyExists
Expand All @@ -29,6 +29,7 @@

router = fastapi.APIRouter(prefix="/auth", tags=["authentication"])


@router.post(
"/signup",
name="auth:signup",
Expand All @@ -43,9 +44,9 @@ async def signup(
Create a new account
```bash
curl -X 'POST' 'http://127.0.0.1:8000/api/auth/signup'
-H 'accept: application/json'
-H 'Content-Type: application/json'
curl -X 'POST' 'http://127.0.0.1:8000/api/auth/signup'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-d '{"username": "aisuko", "email": "[email protected]", "password": "aisuko"}'
```
Expand All @@ -62,7 +63,6 @@ async def signup(
- **updated_at**: The update time
"""


try:
await account_repo.is_username_taken(username=account_create.username)
await account_repo.is_email_taken(email=account_create.email)
Expand Down Expand Up @@ -123,7 +123,7 @@ async def signin(

if account_login.username == ANONYMOUS_USER:
raise await http_exc_400_credentials_bad_signin_request()

try:
db_account = await account_repo.read_user_by_password_authentication(account_login=account_login)

Expand All @@ -146,14 +146,15 @@ async def signin(
),
)


@router.get(
path="/token",
name="authentication: token for anonymous user",
response_model=dict,
status_code=fastapi.status.HTTP_200_OK,
)
async def get_token(
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository))
account_repo: AccountCRUDRepository = fastapi.Depends(get_repository(repo_type=AccountCRUDRepository)),
) -> dict:
"""
Get chat history for an anonymous user
Expand All @@ -171,6 +172,7 @@ async def get_token(

return {"token": access_token}


@router.post("/verify")
async def login_for_access_token(
form_data: Annotated[fastapi.security.OAuth2PasswordRequestForm, fastapi.Depends()],
Expand All @@ -191,8 +193,9 @@ async def login_for_access_token(
- **token_type**: The token type
"""
try:
db_account= await account_repo.read_user_by_password_authentication(
account_login=AccountInLogin(username=form_data.username,password=form_data.password))
db_account = await account_repo.read_user_by_password_authentication(
account_login=AccountInLogin(username=form_data.username, password=form_data.password)
)
except Exception:
raise await http_exc_400_failed_validate_request()
access_token = jwt_generator.generate_access_token(account=db_account)
Expand Down
Loading

0 comments on commit 72e510d

Please sign in to comment.