Skip to content

Commit 576609e

Browse files
authored
Update PAL to allow passing local and global context to PythonREPL (#774)
Passing additional variables to the python environment can be useful for example if you want to generate code to analyze a dataset. I also added a tracker for the executed code - `code_history`.
1 parent 3f952eb commit 576609e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

langchain/chains/pal/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
from __future__ import annotations
66

7-
from typing import Any, Dict, List
7+
from typing import Any, Dict, List, Optional
88

99
from pydantic import BaseModel, Extra
1010

@@ -24,6 +24,8 @@ class PALChain(Chain, BaseModel):
2424
prompt: BasePromptTemplate
2525
stop: str = "\n\n"
2626
get_answer_expr: str = "print(solution())"
27+
python_globals: Optional[Dict[str, Any]] = None
28+
python_locals: Optional[Dict[str, Any]] = None
2729
output_key: str = "result" #: :meta private:
2830

2931
class Config:
@@ -54,7 +56,7 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
5456
self.callback_manager.on_text(
5557
code, color="green", end="\n", verbose=self.verbose
5658
)
57-
repl = PythonREPL()
59+
repl = PythonREPL(_globals=self.python_globals, _locals=self.python_locals)
5860
res = repl.run(code + f"\n{self.get_answer_expr}")
5961
return {self.output_key: res.strip()}
6062

0 commit comments

Comments
 (0)