Skip to content

Commit 8d0869c

Browse files
authored
change run to use args and kwargs (langchain-ai#367)
Before, `run` was not able to be called with multiple arguments. This expands the functionality.
1 parent a7084ad commit 8d0869c

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[flake8]
22
exclude =
3+
venv
34
.venv
45
__pycache__
56
notebooks

langchain/chains/base.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,23 @@ def apply(self, input_list: List[Dict[str, Any]]) -> List[Dict[str, str]]:
119119
"""Call the chain on all inputs in the list."""
120120
return [self(inputs) for inputs in input_list]
121121

122-
def run(self, text: str) -> str:
123-
"""Run text in, text out (if applicable)."""
124-
if len(self.input_keys) != 1:
125-
raise ValueError(
126-
f"`run` not supported when there is not exactly "
127-
f"one input key, got {self.input_keys}."
128-
)
122+
def run(self, *args: str, **kwargs: str) -> str:
123+
"""Run the chain as text in, text out or multiple variables, text out."""
129124
if len(self.output_keys) != 1:
130125
raise ValueError(
131126
f"`run` not supported when there is not exactly "
132-
f"one output key, got {self.output_keys}."
127+
f"one output key. Got {self.output_keys}."
133128
)
134-
return self({self.input_keys[0]: text})[self.output_keys[0]]
129+
130+
if args and not kwargs:
131+
if len(args) != 1:
132+
raise ValueError("`run` supports only one positional argument.")
133+
return self(args[0])[self.output_keys[0]]
134+
135+
if kwargs and not args:
136+
return self(kwargs)[self.output_keys[0]]
137+
138+
raise ValueError(
139+
f"`run` supported with either positional arguments or keyword arguments"
140+
f" but not both. Got args: {args} and kwargs: {kwargs}."
141+
)

tests/unit_tests/chains/test_base.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class FakeChain(Chain, BaseModel):
1212

1313
be_correct: bool = True
1414
the_input_keys: List[str] = ["foo"]
15+
the_output_keys: List[str] = ["bar"]
1516

1617
@property
1718
def input_keys(self) -> List[str]:
@@ -21,7 +22,7 @@ def input_keys(self) -> List[str]:
2122
@property
2223
def output_keys(self) -> List[str]:
2324
"""Output key of bar."""
24-
return ["bar"]
25+
return self.the_output_keys
2526

2627
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
2728
if self.be_correct:
@@ -63,3 +64,45 @@ def test_single_input_error() -> None:
6364
chain = FakeChain(the_input_keys=["foo", "bar"])
6465
with pytest.raises(ValueError):
6566
chain("bar")
67+
68+
69+
def test_run_single_arg() -> None:
70+
"""Test run method with single arg."""
71+
chain = FakeChain()
72+
output = chain.run("bar")
73+
assert output == "baz"
74+
75+
76+
def test_run_multiple_args_error() -> None:
77+
"""Test run method with multiple args errors as expected."""
78+
chain = FakeChain()
79+
with pytest.raises(ValueError):
80+
chain.run("bar", "foo")
81+
82+
83+
def test_run_kwargs() -> None:
84+
"""Test run method with kwargs."""
85+
chain = FakeChain(the_input_keys=["foo", "bar"])
86+
output = chain.run(foo="bar", bar="foo")
87+
assert output == "baz"
88+
89+
90+
def test_run_kwargs_error() -> None:
91+
"""Test run method with kwargs errors as expected."""
92+
chain = FakeChain(the_input_keys=["foo", "bar"])
93+
with pytest.raises(ValueError):
94+
chain.run(foo="bar", baz="foo")
95+
96+
97+
def test_run_args_and_kwargs_error() -> None:
98+
"""Test run method with args and kwargs."""
99+
chain = FakeChain(the_input_keys=["foo", "bar"])
100+
with pytest.raises(ValueError):
101+
chain.run("bar", foo="bar")
102+
103+
104+
def test_multiple_output_keys_error() -> None:
105+
"""Test run with multiple output keys errors as expected."""
106+
chain = FakeChain(the_output_keys=["foo", "bar"])
107+
with pytest.raises(ValueError):
108+
chain.run("bar")

0 commit comments

Comments
 (0)