Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python chain #6

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions langchain/chains/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LLMChain(Chain, BaseModel):

prompt: Prompt
llm: LLM
return_key: str = "text"
output_key: str = "text"

class Config:
"""Configuration for this pydantic object."""
Expand All @@ -29,7 +29,7 @@ def input_keys(self) -> List[str]:
@property
def output_keys(self) -> List[str]:
"""Will always return text key."""
return [self.return_key]
return [self.output_key]

def _run(self, inputs: Dict[str, Any]) -> Dict[str, str]:
selected_inputs = {k: inputs[k] for k in self.prompt.input_variables}
Expand All @@ -39,8 +39,8 @@ def _run(self, inputs: Dict[str, Any]) -> Dict[str, str]:
if "stop" in inputs:
kwargs["stop"] = inputs["stop"]
response = self.llm(prompt, **kwargs)
return {self.return_key: response}
return {self.output_key: response}

def predict(self, **kwargs: Any) -> str:
"""More user-friendly interface for interacting with LLMs."""
return self(kwargs)[self.return_key]
return self(kwargs)[self.output_key]
37 changes: 37 additions & 0 deletions langchain/chains/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Chain that runs python code."""
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 in `code` key."""
return [self.input_key]

@property
def output_keys(self) -> List[str]:
"""Return output in `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]
2 changes: 1 addition & 1 deletion tests/unit_tests/chains/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def fake_llm_chain() -> LLMChain:
"""Fake LLM chain for testing purposes."""
prompt = Prompt(input_variables=["bar"], template="This is a {bar}:")
return LLMChain(prompt=prompt, llm=FakeLLM(), return_key="text1")
return LLMChain(prompt=prompt, llm=FakeLLM(), output_key="text1")


def test_missing_inputs(fake_llm_chain: LLMChain) -> None:
Expand Down
15 changes: 15 additions & 0 deletions tests/unit_tests/chains/test_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Test python chain."""

from langchain.chains.python import PythonChain


def test_functionality() -> None:
"""Test correct functionality."""
chain = PythonChain(input_key="code1", output_key="output1")
code = "print(1 + 1)"
output = chain({"code1": code})
assert output == {"code1": code, "output1": "2\n"}

# Test with the more user-friendly interface.
simple_output = chain.run(code)
assert simple_output == "2\n"