mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-29 14:37:21 +00:00
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`.
This commit is contained in:
parent
3f952eb597
commit
576609e665
@ -4,7 +4,7 @@ As in https://arxiv.org/pdf/2211.10435.pdf.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from pydantic import BaseModel, Extra
|
from pydantic import BaseModel, Extra
|
||||||
|
|
||||||
@ -24,6 +24,8 @@ class PALChain(Chain, BaseModel):
|
|||||||
prompt: BasePromptTemplate
|
prompt: BasePromptTemplate
|
||||||
stop: str = "\n\n"
|
stop: str = "\n\n"
|
||||||
get_answer_expr: str = "print(solution())"
|
get_answer_expr: str = "print(solution())"
|
||||||
|
python_globals: Optional[Dict[str, Any]] = None
|
||||||
|
python_locals: Optional[Dict[str, Any]] = None
|
||||||
output_key: str = "result" #: :meta private:
|
output_key: str = "result" #: :meta private:
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
@ -54,7 +56,7 @@ class PALChain(Chain, BaseModel):
|
|||||||
self.callback_manager.on_text(
|
self.callback_manager.on_text(
|
||||||
code, color="green", end="\n", verbose=self.verbose
|
code, color="green", end="\n", verbose=self.verbose
|
||||||
)
|
)
|
||||||
repl = PythonREPL()
|
repl = PythonREPL(_globals=self.python_globals, _locals=self.python_locals)
|
||||||
res = repl.run(code + f"\n{self.get_answer_expr}")
|
res = repl.run(code + f"\n{self.get_answer_expr}")
|
||||||
return {self.output_key: res.strip()}
|
return {self.output_key: res.strip()}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user