forked from Archives/langchain
Move PythonRepl -> langchain.utilities (#2917)
This commit is contained in:
parent
7c73e9df5d
commit
36aa7f30e4
@ -7,8 +7,8 @@ from langchain.chains.base import Chain
|
|||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.chains.llm_math.prompt import PROMPT
|
from langchain.chains.llm_math.prompt import PROMPT
|
||||||
from langchain.prompts.base import BasePromptTemplate
|
from langchain.prompts.base import BasePromptTemplate
|
||||||
from langchain.python import PythonREPL
|
|
||||||
from langchain.schema import BaseLanguageModel
|
from langchain.schema import BaseLanguageModel
|
||||||
|
from langchain.utilities import PythonREPL
|
||||||
|
|
||||||
|
|
||||||
class LLMMathChain(Chain):
|
class LLMMathChain(Chain):
|
||||||
|
@ -13,8 +13,8 @@ from langchain.chains.llm import LLMChain
|
|||||||
from langchain.chains.pal.colored_object_prompt import COLORED_OBJECT_PROMPT
|
from langchain.chains.pal.colored_object_prompt import COLORED_OBJECT_PROMPT
|
||||||
from langchain.chains.pal.math_prompt import MATH_PROMPT
|
from langchain.chains.pal.math_prompt import MATH_PROMPT
|
||||||
from langchain.prompts.base import BasePromptTemplate
|
from langchain.prompts.base import BasePromptTemplate
|
||||||
from langchain.python import PythonREPL
|
|
||||||
from langchain.schema import BaseLanguageModel
|
from langchain.schema import BaseLanguageModel
|
||||||
|
from langchain.utilities import PythonREPL
|
||||||
|
|
||||||
|
|
||||||
class PALChain(Chain):
|
class PALChain(Chain):
|
||||||
|
@ -1,26 +1,4 @@
|
|||||||
"""Mock Python REPL."""
|
"""For backwards compatibility."""
|
||||||
import sys
|
from langchain.utilities.python import PythonREPL
|
||||||
from io import StringIO
|
|
||||||
from typing import Dict, Optional
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
__all__ = ["PythonREPL"]
|
||||||
|
|
||||||
|
|
||||||
class PythonREPL(BaseModel):
|
|
||||||
"""Simulates a standalone Python REPL."""
|
|
||||||
|
|
||||||
globals: Optional[Dict] = Field(default_factory=dict, alias="_globals")
|
|
||||||
locals: Optional[Dict] = Field(default_factory=dict, alias="_locals")
|
|
||||||
|
|
||||||
def run(self, command: str) -> str:
|
|
||||||
"""Run command with own globals/locals and returns anything printed."""
|
|
||||||
old_stdout = sys.stdout
|
|
||||||
sys.stdout = mystdout = StringIO()
|
|
||||||
try:
|
|
||||||
exec(command, self.globals, self.locals)
|
|
||||||
sys.stdout = old_stdout
|
|
||||||
output = mystdout.getvalue()
|
|
||||||
except Exception as e:
|
|
||||||
sys.stdout = old_stdout
|
|
||||||
output = str(e)
|
|
||||||
return output
|
|
||||||
|
@ -7,8 +7,8 @@ from typing import Dict, Optional
|
|||||||
|
|
||||||
from pydantic import Field, root_validator
|
from pydantic import Field, root_validator
|
||||||
|
|
||||||
from langchain.python import PythonREPL
|
|
||||||
from langchain.tools.base import BaseTool
|
from langchain.tools.base import BaseTool
|
||||||
|
from langchain.utilities import PythonREPL
|
||||||
|
|
||||||
|
|
||||||
def _get_default_python_repl() -> PythonREPL:
|
def _get_default_python_repl() -> PythonREPL:
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""General utilities."""
|
"""General utilities."""
|
||||||
from langchain.python import PythonREPL
|
|
||||||
from langchain.requests import TextRequestsWrapper
|
from langchain.requests import TextRequestsWrapper
|
||||||
from langchain.utilities.apify import ApifyWrapper
|
from langchain.utilities.apify import ApifyWrapper
|
||||||
from langchain.utilities.bash import BashProcess
|
from langchain.utilities.bash import BashProcess
|
||||||
@ -7,6 +6,7 @@ from langchain.utilities.bing_search import BingSearchAPIWrapper
|
|||||||
from langchain.utilities.google_search import GoogleSearchAPIWrapper
|
from langchain.utilities.google_search import GoogleSearchAPIWrapper
|
||||||
from langchain.utilities.google_serper import GoogleSerperAPIWrapper
|
from langchain.utilities.google_serper import GoogleSerperAPIWrapper
|
||||||
from langchain.utilities.openweathermap import OpenWeatherMapAPIWrapper
|
from langchain.utilities.openweathermap import OpenWeatherMapAPIWrapper
|
||||||
|
from langchain.utilities.python import PythonREPL
|
||||||
from langchain.utilities.searx_search import SearxSearchWrapper
|
from langchain.utilities.searx_search import SearxSearchWrapper
|
||||||
from langchain.utilities.serpapi import SerpAPIWrapper
|
from langchain.utilities.serpapi import SerpAPIWrapper
|
||||||
from langchain.utilities.wikipedia import WikipediaAPIWrapper
|
from langchain.utilities.wikipedia import WikipediaAPIWrapper
|
||||||
@ -16,7 +16,6 @@ __all__ = [
|
|||||||
"ApifyWrapper",
|
"ApifyWrapper",
|
||||||
"BashProcess",
|
"BashProcess",
|
||||||
"TextRequestsWrapper",
|
"TextRequestsWrapper",
|
||||||
"PythonREPL",
|
|
||||||
"GoogleSearchAPIWrapper",
|
"GoogleSearchAPIWrapper",
|
||||||
"GoogleSerperAPIWrapper",
|
"GoogleSerperAPIWrapper",
|
||||||
"WolframAlphaAPIWrapper",
|
"WolframAlphaAPIWrapper",
|
||||||
@ -25,4 +24,5 @@ __all__ = [
|
|||||||
"BingSearchAPIWrapper",
|
"BingSearchAPIWrapper",
|
||||||
"WikipediaAPIWrapper",
|
"WikipediaAPIWrapper",
|
||||||
"OpenWeatherMapAPIWrapper",
|
"OpenWeatherMapAPIWrapper",
|
||||||
|
"PythonREPL",
|
||||||
]
|
]
|
||||||
|
25
langchain/utilities/python.py
Normal file
25
langchain/utilities/python.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import sys
|
||||||
|
from io import StringIO
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
|
class PythonREPL(BaseModel):
|
||||||
|
"""Simulates a standalone Python REPL."""
|
||||||
|
|
||||||
|
globals: Optional[Dict] = Field(default_factory=dict, alias="_globals")
|
||||||
|
locals: Optional[Dict] = Field(default_factory=dict, alias="_locals")
|
||||||
|
|
||||||
|
def run(self, command: str) -> str:
|
||||||
|
"""Run command with own globals/locals and returns anything printed."""
|
||||||
|
old_stdout = sys.stdout
|
||||||
|
sys.stdout = mystdout = StringIO()
|
||||||
|
try:
|
||||||
|
exec(command, self.globals, self.locals)
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
output = mystdout.getvalue()
|
||||||
|
except Exception as e:
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
output = str(e)
|
||||||
|
return output
|
@ -3,8 +3,8 @@ import sys
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from langchain.python import PythonREPL
|
|
||||||
from langchain.tools.python.tool import PythonAstREPLTool, PythonREPLTool
|
from langchain.tools.python.tool import PythonAstREPLTool, PythonREPLTool
|
||||||
|
from langchain.utilities import PythonREPL
|
||||||
|
|
||||||
_SAMPLE_CODE = """
|
_SAMPLE_CODE = """
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user