-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from allegro/response-json-extraction
Added automatic json extraction from the response
- Loading branch information
Showing
15 changed files
with
249 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import re | ||
import typing | ||
|
||
from langchain.output_parsers import PydanticOutputParser | ||
from langchain.schema import OutputParserException | ||
|
||
from allms.domain.response import ResponseData, ResponseParsingOutput | ||
|
||
|
||
class ResponseParser: | ||
def __init__(self, parser: PydanticOutputParser) -> None: | ||
self._json_pattern = re.compile(r"{.*?}", re.DOTALL) | ||
self._parser = parser | ||
|
||
def _clean_extracted_json(self, extracted_json: str) -> str: | ||
json_without_newlines = extracted_json.replace("\\n", "") | ||
json_without_backslashes = json_without_newlines.replace("\\", "") | ||
|
||
return json_without_backslashes | ||
|
||
def _extract_json_from_response(self, model_response_data: ResponseData) -> str: | ||
search_results = self._json_pattern.findall(model_response_data.response) | ||
|
||
if len(search_results) == 0: | ||
return model_response_data.response | ||
|
||
return self._clean_extracted_json(search_results[0]) | ||
|
||
def _parse_response( | ||
self, | ||
model_response_data: ResponseData | ||
) -> ResponseParsingOutput: | ||
raw_response = self._extract_json_from_response(model_response_data) | ||
|
||
try: | ||
return ResponseParsingOutput( | ||
response=self._parser.parse(raw_response), | ||
error_message=None | ||
) | ||
except OutputParserException as output_parser_exception: | ||
return ResponseParsingOutput( | ||
response=None, | ||
error_message=f""" | ||
An OutputParserException has occurred for the model response: {raw_response} | ||
The exception message: {output_parser_exception} | ||
""" | ||
) | ||
|
||
def parse_model_output( | ||
self, | ||
model_responses_data: typing.List[ResponseData] | ||
) -> typing.List[ResponseData]: | ||
parsed_responses = [] | ||
|
||
for model_response_data in model_responses_data: | ||
if not model_response_data.error: | ||
response_with_error = self._parse_response(model_response_data) | ||
|
||
parsed_responses.append(ResponseData( | ||
input_data=model_response_data.input_data, | ||
response=response_with_error.response, | ||
error=response_with_error.error_message, | ||
number_of_prompt_tokens=model_response_data.number_of_prompt_tokens, | ||
number_of_generated_tokens=model_response_data.number_of_generated_tokens | ||
|
||
)) | ||
else: | ||
parsed_responses.append(model_response_data) | ||
|
||
return parsed_responses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "allms" | ||
version = "1.0.1" | ||
version = "1.0.2" | ||
description = "" | ||
authors = ["Allegro Opensource <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -17,6 +17,7 @@ langchain = "^0.0.351" | |
aioresponses = "^0.7.6" | ||
tiktoken = "^0.6.0" | ||
openai = "^0.27.8" | ||
pytest-mock = "^3.14.0" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^7.4.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.