| 56 | |
| 57 | |
| 58 | class CodeInterpreterSession: |
| 59 | def __init__( |
| 60 | self, |
| 61 | llm: Optional[BaseLanguageModel] = None, |
| 62 | additional_tools: list[BaseTool] = [], |
| 63 | callbacks: Callbacks = None, |
| 64 | **kwargs: Any, |
| 65 | ) -> None: |
| 66 | _handle_deprecated_kwargs(kwargs) |
| 67 | self.codebox = CodeBox(requirements=settings.CUSTOM_PACKAGES) |
| 68 | self.verbose = kwargs.get("verbose", settings.DEBUG) |
| 69 | self.tools: list[BaseTool] = self._tools(additional_tools) |
| 70 | self.llm: BaseLanguageModel = llm or self._choose_llm() |
| 71 | self.callbacks = callbacks |
| 72 | self.agent_executor: Optional[AgentExecutor] = None |
| 73 | self.input_files: list[File] = [] |
| 74 | self.output_files: list[File] = [] |
| 75 | self.code_log: list[tuple[str, str]] = [] |
| 76 | |
| 77 | @classmethod |
| 78 | def from_id(cls, session_id: UUID, **kwargs: Any) -> "CodeInterpreterSession": |
| 79 | session = cls(**kwargs) |
| 80 | session.codebox = CodeBox.from_id(session_id) |
| 81 | session.agent_executor = session._agent_executor() |
| 82 | return session |
| 83 | |
| 84 | @property |
| 85 | def session_id(self) -> Optional[UUID]: |
| 86 | return self.codebox.session_id |
| 87 | |
| 88 | def start(self) -> SessionStatus: |
| 89 | status = SessionStatus.from_codebox_status(self.codebox.start()) |
| 90 | self.agent_executor = self._agent_executor() |
| 91 | self.codebox.run( |
| 92 | f"!pip install -q {' '.join(settings.CUSTOM_PACKAGES)}", |
| 93 | ) |
| 94 | return status |
| 95 | |
| 96 | async def astart(self) -> SessionStatus: |
| 97 | status = SessionStatus.from_codebox_status(await self.codebox.astart()) |
| 98 | self.agent_executor = self._agent_executor() |
| 99 | await self.codebox.arun( |
| 100 | f"!pip install -q {' '.join(settings.CUSTOM_PACKAGES)}", |
| 101 | ) |
| 102 | return status |
| 103 | |
| 104 | def _tools(self, additional_tools: list[BaseTool]) -> list[BaseTool]: |
| 105 | return additional_tools + [ |
| 106 | StructuredTool( |
| 107 | name="python", |
| 108 | description="Input a string of code to a ipython interpreter. " |
| 109 | "Write the entire code in a single string. This string can " |
| 110 | "be really long, so you can use the `;` character to split lines. " |
| 111 | "Start your code on the same line as the opening quote. " |
| 112 | "Do not start your code with a line break. " |
| 113 | "For example, do 'import numpy', not '\\nimport numpy'." |
| 114 | "Variables are preserved between runs. " |
| 115 | + ( |
no outgoing calls