mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 13:26:32 -04:00
62d0a01a0f
# Delete a useless "print"
26 lines
793 B
Python
26 lines
793 B
Python
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 = repr(e)
|
|
return output
|