Skip to content

Commit

Permalink
only return valid elements in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Sep 12, 2024
1 parent f90eded commit 7a3e9af
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions oproof/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ def serialize_output(text: str, responses: List[Dict[str, Any]], include_prompts
result = {
"original_text": text,
"responses": [
{
"prompt": response.get("prompt", ""),
"response": response.get("response", ""),
"is_valid": response.get("is_valid", False),
"domain": response.get("domain", "unknown"),
"context": response.get("context", "unknown"),
"reason": response.get("reason", "No reason provided"),
"raw_response": response.get("raw_response", "") if include_prompts else None
}
Serializer._serialize_response(response, include_prompts)
for response in responses
]
}
return result

@staticmethod
def _serialize_response(response: Dict[str, Any], include_prompts: bool) -> Dict[str, Any]:
serialized_response = {
"prompt": response.get("prompt", ""),
"response": response.get("response", ""),
"is_valid": response.get("is_valid", False),
"domain": response.get("domain", "unknown"),
"context": response.get("context", "unknown")
}

if response.get("reason") is not None:
serialized_response["reason"] = response.get("reason")

if include_prompts and response.get("raw_response") is not None:
serialized_response["raw_response"] = response.get("raw_response")

return serialized_response

0 comments on commit 7a3e9af

Please sign in to comment.