langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Improved query, print & exception handling in REPL Tool

Open svdeepak99 opened this issue 1 year ago • 3 comments

The following improvements are made to the Python REPL Tool:

  1. 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'])`

  1. 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).

  2. Made sure the exception format type is returned in all cases, when the exec() function throws an exception.

svdeepak99 avatar Apr 20 '23 08:04 svdeepak99

I corrected the lint formatting by running the "poetry run black ." command and created a new commit.

svdeepak99 avatar Apr 22 '23 23:04 svdeepak99

@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.

svdeepak99 avatar Apr 27 '23 05:04 svdeepak99

@hwchase17, is there anything blocking this merge?

svdeepak99 avatar May 03 '23 02:05 svdeepak99

Reopened this PR & merged in https://github.com/hwchase17/langchain/pull/4997

svdeepak99 avatar May 23 '23 03:05 svdeepak99