From a8840fa77ef777e27b75609c49e38e985b6de139 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:18:19 +0100 Subject: [PATCH] fix: Fixes the route input schemas for repos & guidelines (#25) * fix: Fixes schemas * fix: Fix router doc * refactor: Refactors args --- src/app/api/api_v1/router.py | 2 +- src/app/schemas/guidelines.py | 5 +---- src/app/schemas/repos.py | 1 - src/app/services/openai.py | 20 ++++++++++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app/api/api_v1/router.py b/src/app/api/api_v1/router.py index 6ee93d6..176c153 100644 --- a/src/app/api/api_v1/router.py +++ b/src/app/api/api_v1/router.py @@ -9,7 +9,7 @@ api_router = APIRouter() api_router.include_router(login.router, prefix="/login", tags=["login"]) -api_router.include_router(users.router, prefix="/users", tags=["access"]) +api_router.include_router(users.router, prefix="/users", tags=["users"]) api_router.include_router(repos.router, prefix="/repos", tags=["repos"]) api_router.include_router(guidelines.router, prefix="/guidelines", tags=["guidelines"]) api_router.include_router(compute.router, prefix="/compute", tags=["compute"]) diff --git a/src/app/schemas/guidelines.py b/src/app/schemas/guidelines.py index 5360cf6..f859557 100644 --- a/src/app/schemas/guidelines.py +++ b/src/app/schemas/guidelines.py @@ -7,8 +7,6 @@ from pydantic import BaseModel, Field -from app.schemas.base import _CreatedAt - __all__ = ["GuidelineCreate", "GuidelineEdit", "ContentUpdate", "OrderUpdate"] @@ -17,9 +15,8 @@ class GuidelineEdit(BaseModel): details: str = Field(..., min_length=6, max_length=1000) -class GuidelineCreate(_CreatedAt, GuidelineEdit): +class GuidelineCreate(GuidelineEdit): repo_id: int = Field(..., gt=0) - updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) order: int = Field(..., ge=0, nullable=False) diff --git a/src/app/schemas/repos.py b/src/app/schemas/repos.py index 4d6f90a..d3c6b07 100644 --- a/src/app/schemas/repos.py +++ b/src/app/schemas/repos.py @@ -16,7 +16,6 @@ class RepoCreate(_Id): owner_id: int = Field(..., gt=0) full_name: str = Field(..., example="frgfm/torch-cam") - installed_at: datetime = Field(default_factory=datetime.utcnow, nullable=False) class RepoCreation(RepoCreate): diff --git a/src/app/services/openai.py b/src/app/services/openai.py index d05706c..b5fa6ec 100644 --- a/src/app/services/openai.py +++ b/src/app/services/openai.py @@ -108,7 +108,11 @@ def __init__( ) def analyze_multi( - self, code: str, guidelines: List[Guideline], timeout: int = 10, mode: ExecutionMode = ExecutionMode.SINGLE + self, + code: str, + guidelines: List[Guideline], + mode: ExecutionMode = ExecutionMode.SINGLE, + **kwargs: Any, ) -> List[ComplianceResult]: # Check args before sending a request if len(code) == 0 or len(guidelines) == 0 or any(len(guideline.details) == 0 for guideline in guidelines): @@ -121,7 +125,7 @@ def analyze_multi( MULTI_PROMPT, {"code": code, "guidelines": [guideline.details for guideline in guidelines]}, MULTI_SCHEMA, - timeout, + **kwargs, )["result"] if len(parsed_response) != len(guidelines): raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Invalid model response") @@ -129,7 +133,11 @@ def analyze_multi( with ThreadPoolExecutor() as executor: tasks = [ executor.submit( - self._analyze, MONO_PROMPT, {"code": code, "guideline": guideline.details}, MONO_SCHEMA, timeout + self._analyze, + MONO_PROMPT, + {"code": code, "guideline": guideline.details}, + MONO_SCHEMA, + **kwargs, ) for guideline in guidelines ] @@ -145,17 +153,17 @@ def analyze_multi( for guideline, res in zip(guidelines, parsed_response) ] - def analyze_mono(self, code: str, guideline: Guideline, timeout: int = 10) -> ComplianceResult: + def analyze_mono(self, code: str, guideline: Guideline, **kwargs: Any) -> ComplianceResult: # Check args before sending a request if len(code) == 0 or len(guideline.details) == 0: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="No code or guideline provided for analysis." ) - res = self._analyze(MONO_PROMPT, {"code": code, "guideline": guideline.details}, MONO_SCHEMA, timeout) + res = self._analyze(MONO_PROMPT, {"code": code, "guideline": guideline.details}, MONO_SCHEMA, **kwargs) # Return with pydantic validation return ComplianceResult(guideline_id=guideline.id, **res) - def _analyze(self, prompt: str, payload: Dict[str, Any], schema: ObjectSchema, timeout: int = 10) -> Dict[str, Any]: + def _analyze(self, prompt: str, payload: Dict[str, Any], schema: ObjectSchema, timeout: int = 20) -> Dict[str, Any]: # Prepare the request _payload = ChatCompletion( model=self.model,