fix:Introduce _is_python_code
check into _extract_code
#1140
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
_extract_code
and_is_python_code
Motivation:
Using a dataframe with a large number of columns, the
LLM()._execute_code
method would fail when asking how many rows there are in the dataframe. The failure would be because the LLM wasn't using triple back ticks to surround the code.If the LLM produces correct python code, but for some reason does not
preface it with ```, the
LLM()._extract_code
method fails withNoCodeFoundError.
In this change we first check if the separator is included in the response and we take only the part that is in-between. Then we clean the code from
python/py
statements.Then we check if it's valid code. This check should pass otherwise executing the code will fail, and it's better to raise an error earlier than later.
With this change, if the LLM produced perfect code without the ``` separator, it would pass and return the code, whereas previously it would fail.