From 599a71108c905f3414f61ebfc34819a7dbece491 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:19:56 +0100 Subject: [PATCH 1/3] refactor: Refactors arg passing --- src/app/services/openai.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/app/services/openai.py b/src/app/services/openai.py index 44cc79a..bddbda8 100644 --- a/src/app/services/openai.py +++ b/src/app/services/openai.py @@ -316,8 +316,7 @@ def _analyze( prompt: str, payload: Dict[str, Any], schema: ObjectSchema, - timeout: int = 20, - user_id: Union[str, None] = None, + **kwargs, ) -> Dict[str, Any]: return self._request( prompt, @@ -327,15 +326,13 @@ def _analyze( parameters=schema, ), json.dumps(payload), - timeout, - user_id, + **kwargs, ) def parse_guidelines_from_text( self, corpus: str, - timeout: int = 20, - user_id: Union[str, None] = None, + **kwargs, ) -> List[GuidelineContent]: if not isinstance(corpus, str): raise HTTPException( @@ -353,8 +350,7 @@ def parse_guidelines_from_text( parameters=PARSING_SCHEMA, ), json.dumps(corpus), - timeout, - user_id, + **kwargs, ) guidelines = [validate_model(GuidelineContent, elt) for elt in response["result"]] if any(guideline is None for guideline in guidelines): @@ -365,8 +361,7 @@ def generate_examples_for_instruction( self, instruction: str, language: str, - timeout: int = 20, - user_id: Union[str, None] = None, + **kwargs, ) -> GuidelineExample: if ( not isinstance(instruction, str) @@ -388,8 +383,7 @@ def generate_examples_for_instruction( parameters=EXAMPLE_SCHEMA, ), json.dumps({"instruction": instruction, "language": language}), - timeout, - user_id, + **kwargs, ) ) From 3084d6e26e320288022db8f5be7c252904fef28d Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:20:17 +0100 Subject: [PATCH 2/3] fix: Catches TypeError on parsing --- src/app/services/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/services/openai.py b/src/app/services/openai.py index bddbda8..11444f8 100644 --- a/src/app/services/openai.py +++ b/src/app/services/openai.py @@ -187,7 +187,7 @@ class ExecutionMode(str, Enum): def validate_model(model: Type[ModelInp], data: Dict[str, Any]) -> Union[ModelInp, None]: try: return model(**data) - except ValidationError: + except (ValidationError, TypeError): return None From 58e88d2709a1890779be867413d89bfacc1dae69 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:20:26 +0100 Subject: [PATCH 3/3] feat: Allows override of model --- src/app/services/openai.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/services/openai.py b/src/app/services/openai.py index 11444f8..9fb8daa 100644 --- a/src/app/services/openai.py +++ b/src/app/services/openai.py @@ -281,11 +281,12 @@ def _request( openai_fn: OpenAIFunction, message: str, timeout: int = 20, + model: Union[OpenAIModel, None] = None, user_id: Union[str, None] = None, ) -> Dict[str, Any]: # Prepare the request _payload = ChatCompletion( - model=self.model, + model=model or self.model, messages=[ OpenAIMessage( role=OpenAIChatRole.SYSTEM,