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

fix: Fixes schema validation of guideline extraction pipeline #45

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/app/services/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -316,8 +317,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,
Expand All @@ -327,15 +327,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(
Expand All @@ -353,8 +351,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):
Expand All @@ -365,8 +362,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)
Expand All @@ -388,8 +384,7 @@ def generate_examples_for_instruction(
parameters=EXAMPLE_SCHEMA,
),
json.dumps({"instruction": instruction, "language": language}),
timeout,
user_id,
**kwargs,
)
)

Expand Down
Loading