mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 21:33:31 -04:00
Move PythonRepl -> langchain.utilities (#2917)
This commit is contained in:
+3
-25
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user