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

e2b tool - fix initialization and improve tool description #12345

Merged
merged 2 commits into from
Oct 26, 2023
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
1 change: 1 addition & 0 deletions docs/docs/integrations/tools/e2b_data_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down
26 changes: 10 additions & 16 deletions libs/langchain/langchain/tools/e2b_data_analysis/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
It should be in python format NOT markdown. \
The code should NOT be wrapped in backticks. \
All python packages including requests, matplotlib, scipy, numpy, pandas, \
etc are available. \
If you have any files outputted write them to "/home/user" directory \
path."""
etc are available. Create and display chart using `plt.show()`."""


def _unparse(tree: ast.AST) -> str:
Expand Down Expand Up @@ -99,7 +97,7 @@ class E2BDataAnalysisTool(BaseTool):
name = "e2b_data_analysis"
args_schema: Type[BaseModel] = E2BDataAnalysisToolArguments
session: Any
_uploaded_files: List[UploadedFile] = Field(default_factory=list)
uploaded_files: List[UploadedFile] = Field(default_factory=list)

def __init__(
self,
Expand Down Expand Up @@ -130,23 +128,21 @@ def __init__(
on_exit=on_exit,
on_artifact=on_artifact,
)
super().__init__(session=session, **kwargs)
self.description = (
base_description + "\n\n" + self.uploaded_files_description
).strip()
super().__init__(session=session, description=base_description, **kwargs)
self.uploaded_files = []

def close(self) -> None:
"""Close the cloud sandbox."""
self._uploaded_files = []
self.uploaded_files = []
self.session.close()

@property
def uploaded_files_description(self) -> str:
if len(self._uploaded_files) == 0:
if len(self.uploaded_files) == 0:
return ""
lines = ["The following files available in the sandbox:"]

for f in self._uploaded_files:
for f in self.uploaded_files:
if f.description == "":
lines.append(f"- path: `{f.remote_path}`")
else:
Expand Down Expand Up @@ -210,16 +206,14 @@ def upload_file(self, file: IO, description: str) -> UploadedFile:
remote_path=remote_path,
description=description,
)
self._uploaded_files.append(f)
self.uploaded_files.append(f)
return f

def remove_uploaded_file(self, uploaded_file: UploadedFile) -> None:
"""Remove uploaded file from the sandbox."""
self.session.filesystem.remove(uploaded_file.remote_path)
self._uploaded_files = [
f
for f in self._uploaded_files
if f.remote_path != uploaded_file.remote_path
self.uploaded_files = [
f for f in self.uploaded_files if f.remote_path != uploaded_file.remote_path
]

def as_tool(self) -> Tool:
Expand Down