Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
refactor: Refactors args
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Nov 5, 2023
1 parent 0510428 commit 5f3b86b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/app/services/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -121,15 +125,19 @@ 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")
elif mode == ExecutionMode.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
]
Expand All @@ -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)

Check warning on line 162 in src/app/services/openai.py

View check run for this annotation

Codecov / codecov/patch

src/app/services/openai.py#L162

Added line #L162 was not covered by tests
# 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,
Expand Down

0 comments on commit 5f3b86b

Please sign in to comment.