langchain
langchain copied to clipboard
Improved query, print & exception handling in REPL Tool
The following improvements are made to the Python REPL Tool:
- Improved the sanitization of query (using regex), by removing python command (since gpt-3.5-turbo sometimes assumes python console as a terminal, and runs python command first which causes error). Also sometimes 1 line python codes contain single backticks. (i) Eg of python in query:
Entering new LLMMathChain chain... 1291.25 * 6
```python
print(1291.25 * 6)
```
(ii) Eg of 1 line query with single backtick:
Thought: I have the number of unique Account IDs, but I need to count the number of active accounts Action: python_repl_ast
Action Input: `len(df[df['Status'] == 'Active'])`
-
Improved the capturing of print by redirecting stdout to output of Python REPL Tool (without cluttering the actual stdout). Also when the return of eval() function is None, then returning stdout (in case code contains a print or info statement).
-
Made sure the exception format type is returned in all cases, when the exec() function throws an exception.
I corrected the lint formatting by running the "poetry run black ." command and created a new commit.
@hwchase17 I have added 7 new test cases as you asked. I have also rerun the lining commands one by one and fixed the formatting issues. This is my first time adding test cases, so it took a while. If any linting issues persist (none were detected when run locally by me), I request you to fix it if possible.
@hwchase17, is there anything blocking this merge?
Reopened this PR & merged in https://github.com/hwchase17/langchain/pull/4997