langchain/libs/experimental/tests/unit_tests
Liu Xiaodong 3b473d10f2
experimental: clean python repl input(experimental:Added code for PythonREPL) (#20930)
Update python.py(experimental:Added code for PythonREPL)

Added code for PythonREPL, defining a static method 'sanitize_input'
that takes the string 'query' as input and returns a sanitizing string.
The purpose of this method is to remove unwanted characters from the
input string, Specifically:

1. Delete the whitespace at the beginning and end of the string (' \s').
2. Remove the quotation marks (`` ` ``) at the beginning and end of the
string.
3. Remove the keyword "python" at the beginning of the string (case
insensitive) because the user may have typed it.

This method uses regular expressions (regex) to implement sanitizing.

It all started with this code:
from langchain.agents import Tool
from langchain_experimental.utilities import PythonREPL

python_repl = PythonREPL()
repl_tool = Tool(
    name="python_repl",
description="Remove redundant formatting marks at the beginning and end
of source code from input.Use a Python shell to execute python commands.
If you want to see the output of a value, you should print it out with
`print(...)`.",
    func=python_repl.run,
)

When I call the agent to write a piece of code for me and execute it
with the defined code, I must get an error: SyntaxError('invalid
syntax', ('<string>', 1, 1,'In', 1, 2))

After checking, I found that pythonREPL has less formatting of input
code than the soon-to-be deprecated pythonREPL tool, so I added this
step to it, so that no matter what code I ask the agent to write for me,
it can be executed smoothly and get the output result.
I have tried modifying the prompt words to solve this problem before,
but it did not work, and by adding a simple format check, the problem is
well resolved.
<img width="1271" alt="image"
src="https://github.com/langchain-ai/langchain/assets/164149097/c49a685f-d246-4b11-b655-fd952fc2f04c">

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
2024-05-01 05:19:09 +00:00
..
agents experimental[patch]: fix zero-shot pandas agent (#17442) 2024-02-12 21:58:35 -08:00
chat_models community[patch]: update use of deprecated llm methods (#20393) 2024-04-12 17:28:23 -04:00
python infra: add print rule to ruff (#16221) 2024-02-09 16:13:30 -08:00
rl_chain experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
__init__.py black formatting 2023-09-11 13:33:43 -04:00
conftest.py Add conftest file to langchain experimental (#9886) 2023-08-28 17:52:16 -07:00
fake_llm.py langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
test_bash.py add LLMBashChain to experimental (#11305) 2023-10-02 16:00:14 -04:00
test_data_anonymizer.py langchain-experimental: Add allow_list support in experimental/data_anonymizer (#11597) 2023-10-11 14:50:41 -07:00
test_imports.py infra: Fix test filesystem paths incompatible with windows (#14388) 2023-12-21 13:45:42 -08:00
test_llm_bash.py infra: add print rule to ruff (#16221) 2024-02-09 16:13:30 -08:00
test_llm_symbolic_math.py Add SymbolicMathChain to experiment in preparation for deprecation (#11129) 2023-10-05 13:54:43 -04:00
test_logical_fallacy.py adding new chain for logical fallacy removal from model output in chain (#9887) 2023-09-03 15:44:27 -07:00
test_mock.py Harrison/move experimental (#8084) 2023-07-21 10:36:28 -07:00
test_pal.py remove code (#8425) 2023-07-28 13:19:44 -07:00
test_python.py experimental: clean python repl input(experimental:Added code for PythonREPL) (#20930) 2024-05-01 05:19:09 +00:00
test_reversible_data_anonymizer.py langchain-experimental: Add allow_list support in experimental/data_anonymizer (#11597) 2023-10-11 14:50:41 -07:00
test_smartllm.py experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
test_sql.py experimental[patch]: PromptTemplate import fix (#19617) 2024-03-26 17:03:13 -07:00
test_tot.py core[patch], langchain[patch]: fix required deps (#14373) 2023-12-07 14:24:58 -08:00