codekingpro/portable-devtools
114k
1"""2Tool implementations for the Riza (https://riza.io) code interpreter API.3 4Documentation: https://docs.riza.io5API keys: https://dashboard.riza.io6"""7 8from typing import Any, Optional, Type9 10from langchain_core.callbacks import (11 CallbackManagerForToolRun,12)13from langchain_core.tools import BaseTool, ToolException14from pydantic import BaseModel, Field15 16 17class ExecPythonInput(BaseModel):18 code: str = Field(description="the Python code to execute")19 20 21class ExecPython(BaseTool):22 """Riza Code tool.23 24 Setup:25 Install ``langchain-community`` and ``rizaio`` and set environment variable ``RIZA_API_KEY``.26 27 .. code-block:: bash28 29 pip install -U langchain-community rizaio30 export RIZA_API_KEY="your-api-key"31 32 Instantiation:33 .. code-block:: python34 35 from langchain_community.tools.riza.command import ExecPython36 37 tool = ExecPython()38 39 Invocation with args:40 .. code-block:: python41 42 tool.invoke("x = 5; print(x)")43 44 .. code-block:: python45 46 '5\\n'47 48 Invocation with ToolCall:49 50 .. code-block:: python51 52 tool.invoke({"args": {"code":"x = 5; print(x)"}, "id": "1", "name": tool.name, "type": "tool_call"})53 54 .. code-block:: python55 56 tool.invoke({"args": {"code":"x = 5; print(x)"}, "id": "1", "name": tool.name, "type": "tool_call"})57 58 """ # noqa: E50159 60 name: str = "riza_exec_python"61 description: str = """Execute Python code to solve problems.62 63 The Python runtime does not have filesystem access. You can use the httpx64 or requests library to make HTTP requests. Always print output to stdout."""65 args_schema: Type[BaseModel] = ExecPythonInput66 handle_tool_error: bool = True67 68 client: Any = None69 runtime_revision_id: Optional[str] = None70 71 def __init__(72 self, runtime_revision_id: Optional[str] = None, **kwargs: Any73 ) -> None:74 try:75 from rizaio import Riza76 except ImportError as e:77 raise ImportError(78 "Couldn't import the `rizaio` package. "79 "Try running `pip install rizaio`."80 ) from e81 super().__init__(**kwargs)82 self.client = Riza()83 self.runtime_revision_id = runtime_revision_id84 85 def _run(86 self, code: str, run_manager: Optional[CallbackManagerForToolRun] = None87 ) -> str:88 output = self.client.command.exec(89 runtime_revision_id=self.runtime_revision_id, language="python", code=code90 )91 if output.exit_code > 0:92 raise ToolException(93 f"Riza code execution returned a non-zero exit code. "94 f"The output captured from stderr was:\n{output.stderr}"95 )96 return output.stdout97 98 99class ExecJavaScriptInput(BaseModel):100 code: str = Field(description="the JavaScript code to execute")101 102 103class ExecJavaScript(BaseTool):104 """A tool implementation to execute JavaScript via Riza's Code Interpreter API."""105 106 name: str = "riza_exec_javascript"107 description: str = """Execute JavaScript code to solve problems.108 109 The JavaScript runtime does not have filesystem access, but can use fetch110 to make HTTP requests and does include the global JSON object. Always print111 output to stdout."""112 args_schema: Type[BaseModel] = ExecJavaScriptInput113 handle_tool_error: bool = True114 115 client: Any = None116 runtime_revision_id: Optional[str] = None117 118 def __init__(119 self, runtime_revision_id: Optional[str] = None, **kwargs: Any120 ) -> None:121 try:122 from rizaio import Riza123 except ImportError as e:124 raise ImportError(125 "Couldn't import the `rizaio` package. "126 "Try running `pip install rizaio`."127 ) from e128 super().__init__(**kwargs)129 self.client = Riza()130 self.runtime_revision_id = runtime_revision_id131 132 def _run(133 self, code: str, run_manager: Optional[CallbackManagerForToolRun] = None134 ) -> str:135 output = self.client.command.exec(136 runtime_revision_id=self.runtime_revision_id,137 language="javascript",138 code=code,139 )140 if output.exit_code > 0:141 raise ToolException(142 f"Riza code execution returned a non-zero exit code. "143 f"The output captured from stderr was:\n{output.stderr}"144 )145 return output.stdout146 