mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-04 02:03:32 +00:00 
			
		
		
		
	Move PythonRepl -> langchain.utilities (#2917)
This commit is contained in:
		@@ -7,8 +7,8 @@ from langchain.chains.base import Chain
 | 
			
		||||
from langchain.chains.llm import LLMChain
 | 
			
		||||
from langchain.chains.llm_math.prompt import PROMPT
 | 
			
		||||
from langchain.prompts.base import BasePromptTemplate
 | 
			
		||||
from langchain.python import PythonREPL
 | 
			
		||||
from langchain.schema import BaseLanguageModel
 | 
			
		||||
from langchain.utilities import PythonREPL
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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.math_prompt import MATH_PROMPT
 | 
			
		||||
from langchain.prompts.base import BasePromptTemplate
 | 
			
		||||
from langchain.python import PythonREPL
 | 
			
		||||
from langchain.schema import BaseLanguageModel
 | 
			
		||||
from langchain.utilities import PythonREPL
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PALChain(Chain):
 | 
			
		||||
 
 | 
			
		||||
@@ -1,26 +1,4 @@
 | 
			
		||||
"""Mock Python REPL."""
 | 
			
		||||
import sys
 | 
			
		||||
from io import StringIO
 | 
			
		||||
from typing import Dict, Optional
 | 
			
		||||
"""For backwards compatibility."""
 | 
			
		||||
from langchain.utilities.python import PythonREPL
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
__all__ = ["PythonREPL"]
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,8 @@ from typing import Dict, Optional
 | 
			
		||||
 | 
			
		||||
from pydantic import Field, root_validator
 | 
			
		||||
 | 
			
		||||
from langchain.python import PythonREPL
 | 
			
		||||
from langchain.tools.base import BaseTool
 | 
			
		||||
from langchain.utilities import PythonREPL
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _get_default_python_repl() -> PythonREPL:
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
"""General utilities."""
 | 
			
		||||
from langchain.python import PythonREPL
 | 
			
		||||
from langchain.requests import TextRequestsWrapper
 | 
			
		||||
from langchain.utilities.apify import ApifyWrapper
 | 
			
		||||
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_serper import GoogleSerperAPIWrapper
 | 
			
		||||
from langchain.utilities.openweathermap import OpenWeatherMapAPIWrapper
 | 
			
		||||
from langchain.utilities.python import PythonREPL
 | 
			
		||||
from langchain.utilities.searx_search import SearxSearchWrapper
 | 
			
		||||
from langchain.utilities.serpapi import SerpAPIWrapper
 | 
			
		||||
from langchain.utilities.wikipedia import WikipediaAPIWrapper
 | 
			
		||||
@@ -16,7 +16,6 @@ __all__ = [
 | 
			
		||||
    "ApifyWrapper",
 | 
			
		||||
    "BashProcess",
 | 
			
		||||
    "TextRequestsWrapper",
 | 
			
		||||
    "PythonREPL",
 | 
			
		||||
    "GoogleSearchAPIWrapper",
 | 
			
		||||
    "GoogleSerperAPIWrapper",
 | 
			
		||||
    "WolframAlphaAPIWrapper",
 | 
			
		||||
@@ -25,4 +24,5 @@ __all__ = [
 | 
			
		||||
    "BingSearchAPIWrapper",
 | 
			
		||||
    "WikipediaAPIWrapper",
 | 
			
		||||
    "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
 | 
			
		||||
 | 
			
		||||
from langchain.python import PythonREPL
 | 
			
		||||
from langchain.tools.python.tool import PythonAstREPLTool, PythonREPLTool
 | 
			
		||||
from langchain.utilities import PythonREPL
 | 
			
		||||
 | 
			
		||||
_SAMPLE_CODE = """
 | 
			
		||||
```
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user