-
Notifications
You must be signed in to change notification settings - Fork 192
feat: add support for FastAPI in code interpreter tool #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "create-llama": patch | ||
| --- | ||
|
|
||
| Add support E2B code interpreter tool for FastAPI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
templates/components/engines/python/agent/tools/interpreter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import os | ||
| import logging | ||
| import base64 | ||
| import uuid | ||
| from pydantic import BaseModel | ||
| from typing import List, Tuple, Dict | ||
| from llama_index.core.tools import FunctionTool | ||
| from e2b_code_interpreter import CodeInterpreter | ||
| from e2b_code_interpreter.models import Logs | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class InterpreterExtraResult(BaseModel): | ||
| type: str | ||
| filename: str | ||
| url: str | ||
|
|
||
|
|
||
| class E2BToolOutput(BaseModel): | ||
| is_error: bool | ||
| logs: Logs | ||
| results: List[InterpreterExtraResult] = [] | ||
|
|
||
|
|
||
| class E2BCodeInterpreter: | ||
|
|
||
| output_dir = "tool-output" | ||
|
|
||
| def __init__(self, api_key: str, filesever_url_prefix: str): | ||
| self.api_key = api_key | ||
| self.filesever_url_prefix = filesever_url_prefix | ||
leehuwuj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def get_output_path(self, filename: str) -> str: | ||
| # if output directory doesn't exist, create it | ||
| if not os.path.exists(self.output_dir): | ||
| os.makedirs(self.output_dir, exist_ok=True) | ||
| return os.path.join(self.output_dir, filename) | ||
|
|
||
| def save_to_disk(self, base64_data: str, ext: str) -> Dict: | ||
| filename = f"{uuid.uuid4()}.{ext}" # generate a unique filename | ||
| buffer = base64.b64decode(base64_data) | ||
| output_path = self.get_output_path(filename) | ||
|
|
||
| try: | ||
| with open(output_path, "wb") as file: | ||
| file.write(buffer) | ||
| except IOError as e: | ||
| logger.error(f"Failed to write to file {output_path}: {str(e)}") | ||
| raise e | ||
leehuwuj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| logger.info(f"Saved file to {output_path}") | ||
|
|
||
| return { | ||
| "outputPath": output_path, | ||
| "filename": filename, | ||
| } | ||
|
|
||
| def get_file_url(self, filename: str) -> str: | ||
| return f"{self.filesever_url_prefix}/{self.output_dir}/{filename}" | ||
|
|
||
| def parse_result(self, result) -> List[InterpreterExtraResult]: | ||
| """ | ||
| The result could include multiple formats (e.g. png, svg, etc.) but encoded in base64 | ||
| We save each result to disk and return saved file metadata (extension, filename, url) | ||
| """ | ||
| if not result: | ||
| return [] | ||
|
|
||
| output = [] | ||
|
|
||
| try: | ||
| formats = result.formats() | ||
| base64_data_arr = [result[format] for format in formats] | ||
|
|
||
| for ext, base64_data in zip(formats, base64_data_arr): | ||
| if ext and base64_data: | ||
| result = self.save_to_disk(base64_data, ext) | ||
| filename = result["filename"] | ||
| output.append( | ||
| InterpreterExtraResult( | ||
| type=ext, filename=filename, url=self.get_file_url(filename) | ||
| ) | ||
| ) | ||
| except Exception as error: | ||
| logger.error("Error when saving data to disk", error) | ||
|
|
||
| return output | ||
|
|
||
| def interpret(self, code: str) -> E2BToolOutput: | ||
| with CodeInterpreter(api_key=self.api_key) as interpreter: | ||
| logger.info( | ||
| f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}" | ||
| ) | ||
| exec = interpreter.notebook.exec_cell(code) | ||
|
|
||
| if exec.error: | ||
| output = E2BToolOutput(is_error=True, logs=[exec.error]) | ||
| else: | ||
| if len(exec.results) == 0: | ||
| output = E2BToolOutput(is_error=False, logs=exec.logs, results=[]) | ||
| else: | ||
| results = self.parse_result(exec.results[0]) | ||
| output = E2BToolOutput( | ||
| is_error=False, logs=exec.logs, results=results | ||
| ) | ||
| return output | ||
|
|
||
|
|
||
| def code_interpret(code: str) -> Dict: | ||
marcusschiesser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Execute python code in a Jupyter notebook cell and return any result, stdout, stderr, display_data, and error. | ||
| """ | ||
| api_key = os.getenv("E2B_API_KEY") | ||
| filesever_url_prefix = os.getenv("FILESERVER_URL_PREFIX") | ||
| if not api_key: | ||
| raise ValueError( | ||
| "E2B_API_KEY key is required to run code interpreter. Get it here: https://e2b.dev/docs/getting-started/api-key" | ||
| ) | ||
| if not filesever_url_prefix: | ||
| raise ValueError( | ||
| "FILESERVER_URL_PREFIX is required to display file output from sandbox" | ||
| ) | ||
|
|
||
| interpreter = E2BCodeInterpreter( | ||
| api_key=api_key, filesever_url_prefix=filesever_url_prefix | ||
| ) | ||
| output = interpreter.interpret(code) | ||
| return output.dict() | ||
|
|
||
|
|
||
| # Specify as functions tools to be loaded by the ToolFactory | ||
| tools = [FunctionTool.from_defaults(code_interpret)] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see that python tool is generating the output dir if it doesn't exist - how about we add this to TS too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change is already applied to TS.