Skip to content

Commit

Permalink
rest non json response must be a list
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Martin <jemartin@nvidia.com>
  • Loading branch information
jmartin-tech committed May 22, 2024
1 parent 1e90b32 commit 2aa8211
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions garak/generators/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def _call_model(self, prompt: str, generations_this_call: int = 1):
raise ConnectionError(error_msg)

if not self.response_json:
return str(resp.text)
return [str(resp.text)]

response_object = json.loads(resp.content)

Expand All @@ -301,7 +301,7 @@ def _call_model(self, prompt: str, generations_this_call: int = 1):
len(self.response_json_field) > 0
), "response_json_field needs to be complete if response_json is true; ValueError should have been raised in constructor"
if self.response_json_field[0] != "$":
response = response_object[self.response_json_field]
response = [response_object[self.response_json_field]]
else:
field_path_expr = jsonpath_ng.parse(self.response_json_field)
responses = field_path_expr.find(response_object)
Expand All @@ -318,7 +318,7 @@ def _call_model(self, prompt: str, generations_this_call: int = 1):
"RestGenerator JSONPath in response_json_field yielded nothing. Response content: %s"
% repr(resp.content)
)
return None
return [None]

return response

Expand Down
4 changes: 2 additions & 2 deletions tests/generators/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_plaintext_rest(requests_mock):
)
generator = RestGenerator()
output = generator._call_model("sup REST")
assert output == DEFAULT_TEXT_RESPONSE
assert output == [DEFAULT_TEXT_RESPONSE]


@pytest.mark.usefixtures("set_rest_config")
Expand All @@ -55,7 +55,7 @@ def test_json_rest_top_level(requests_mock):
print(generator.response_json)
print(generator.response_json_field)
output = generator._call_model("Who is Enabran Tain's son?")
assert output == DEFAULT_TEXT_RESPONSE
assert output == [DEFAULT_TEXT_RESPONSE]


@pytest.mark.usefixtures("set_rest_config")
Expand Down

0 comments on commit 2aa8211

Please sign in to comment.