langchain/libs/experimental/langchain_experimental
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 community[patch], experimental[patch]: support tool-calling sql and p… (#20639) 2024-04-21 15:43:09 -07:00
autonomous_agents experimental[patch]: return from HuggingGPT task executor task.run() exception (#20219) 2024-04-25 20:16:39 +00:00
chat_models langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
comprehend_moderation langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
cpal experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
data_anonymizer experimental[patch]: update module doc strings (#19539) 2024-03-26 10:38:10 -04:00
fallacy_removal experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
generative_agents patch: deprecate (a)get_relevant_documents (#20477) 2024-04-22 11:14:53 -04:00
graph_transformers experimental[patch]: Add support for non-function calling LLMs in llm graph transformers (#21014) 2024-05-01 01:16:07 -04:00
llm_bash experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
llm_symbolic_math experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
llms experimental[minor]: Add bind_tools and with_structured_output functions to OllamaFunctions (#20881) 2024-04-29 14:13:33 +00:00
open_clip experimental[patch]: update module doc strings (#19539) 2024-03-26 10:38:10 -04:00
openai_assistant Move OAI assistants to langchain and add callbacks (#13236) 2023-11-13 17:42:07 -08:00
pal_chain experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
plan_and_execute experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
prompt_injection_identifier experimental[minor]: upgrade the prompt injection model (#20783) 2024-04-23 10:23:39 -04:00
prompts experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
pydantic_v1 poetry lock the experimental package. (#9478) 2023-08-22 14:09:35 -04:00
recommenders experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
retrievers langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
rl_chain experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
smart_llm experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
sql experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
synthetic_data experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
tabular_synthetic_data experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
tools langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
tot experimental[patch]: prompts import fix (#20534) 2024-04-18 16:09:11 -04:00
utilities experimental: clean python repl input(experimental:Added code for PythonREPL) (#20930) 2024-05-01 05:19:09 +00:00
video_captioning langchain: callbacks imports fix (#20348) 2024-04-12 20:13:14 +00:00
__init__.py Add version to langchain_experimental (#11613) 2023-10-10 11:17:41 -04:00
py.typed Add py.typed file to langchain-experimental. (#9557) 2023-08-21 15:37:16 -04:00
text_splitter.py experimental[patch]: Makes regex customizable in text_splitter.py (SemanticChunker class) (#20485) 2024-04-25 00:32:40 +00:00