forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
"""Chain that runs python code.
|
|
|
|
Heavily borrowed from https://replit.com/@amasad/gptpy?v=1#main.py
|
|
"""
|
|
import sys
|
|
from io import StringIO
|
|
from typing import Dict, List
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from langchain.chains.base import Chain
|
|
|
|
|
|
class PythonChain(Chain, BaseModel):
|
|
"""Chain to run python code."""
|
|
|
|
input_key: str = "code"
|
|
output_key: str = "output"
|
|
|
|
@property
|
|
def input_keys(self) -> List[str]:
|
|
"""Expect input key."""
|
|
return [self.input_key]
|
|
|
|
@property
|
|
def output_keys(self) -> List[str]:
|
|
"""Return output key."""
|
|
return [self.output_key]
|
|
|
|
def _run(self, inputs: Dict[str, str]) -> Dict[str, str]:
|
|
old_stdout = sys.stdout
|
|
sys.stdout = mystdout = StringIO()
|
|
exec(inputs[self.input_key])
|
|
sys.stdout = old_stdout
|
|
output = mystdout.getvalue()
|
|
return {self.output_key: output}
|
|
|
|
def run(self, code: str) -> str:
|
|
"""More user-friendly interface for interfacing with python."""
|
|
return self({self.input_key: code})[self.output_key]
|