TerminalTool
The TerminalTool
class is a subclass of Tool
. It is used to run commands on
the terminal of your choice in a subprocess with a timeout.
Parameters
command
(str
): The command to run on the terminal.executable
(str
): The location of the executable to run the command with. Defaults to/bin/sh
.timeout
(int
): The maximum amount of time (in seconds) to wait for the command to finish. Defaults to 60.
Returns
The return value is of the type: ToolReturn
(Learn more about:
ToolReturn) with the following output and
exit_code:
output
(str): The output of the command.exit_code
(int): 0 if success, 1 if failure.
Usage
Running it directly
You can run the tool directly like so:
import asyncio
from embedia.tools import TerminalTool
terminal = TerminalTool()
resp = asyncio.run(terminal(command='ls -l'))
Running the above code prints the following on the terminal:
[time: 2023-10-02T04:49:43.831003+00:00] [id: 140578759471072] [event: Tool Start]
Tool: TerminalTool
Args: ()
Kwargs: {'command': 'ls -l'}
[time: 2023-10-02T04:49:43.885275+00:00] [id: 140578759471072] [event: Tool End]
Tool: TerminalTool
ExitCode: 0
Output:
total 36
-rw------- 1 runner runner 119 Oct 2 04:49 main.py
-rw------- 1 runner runner 21751 Sep 20 21:38 poetry.lock
-rw------- 1 runner runner 571 Oct 1 23:25 pyproject.toml
-rw------- 1 runner runner 29 Sep 14 14:36 replit.nix
Using with ToolUserAgent
Let us use the ToolUserAgent
to write some code and run it using the
TerminalTool
(Learn more about:
ToolUserAgent)
import asyncio
import os
import openai
import tiktoken
from embedia import ChatLLM, Tokenizer
from embedia.agents import ToolUserAgent
from embedia.tools import TerminalTool
class OpenAITokenizer(Tokenizer):
def __init__(self):
super().__init__()
async def _tokenize(self, text):
return tiktoken.encoding_for_model("gpt-3.5-turbo").encode(text)
class OpenAIChatLLM(ChatLLM):
def __init__(self):
super().__init__(tokenizer=OpenAITokenizer())
openai.api_key = os.environ['OPENAI_API_KEY']
async def _reply(self, prompt):
completion = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo",
temperature=0.1,
messages=[{
'role': msg.role,
'content': msg.content
} for msg in self.chat_history],
)
return completion.choices[0].message.content
if __name__ == '__main__':
tool_user_agent = ToolUserAgent(chatllm=OpenAIChatLLM(), tools=[TerminalTool()])
resp = asyncio.run(tool_user_agent("Which files are present in the current directory?"))
Running the above code prints the following on the terminal:
[time: 2023-10-02T05:02:06.906685+00:00] [id: 139886831884880] [event: Tool Start]
Tool: ToolUserAgent
Args: ('Which files are present in the current directory?',)
Kwargs: {}
[time: 2023-10-02T05:02:06.906859+00:00] [id: 139886831884880] [event: Agent Start]
Main Question:
Which files are present in the current directory?
[time: 2023-10-02T05:02:11.982358+00:00] [id: 139886832029600] [event: ChatLLM Init]
system (81 tokens):
You're an expert in choosing the function arguments based on the user's question. The question, the function description, the list of parameters and their descriptions will be provided to you. Reply with all arguments in the following json format:
{
<parameter name>: <argument value>,
<parameter name>: <argument value>,
<parameter name>: <argument value>,
}
Do not reply with anything else
[time: 2023-10-02T05:02:11.986493+00:00] [id: 139886832029600] [event: ChatLLM Start]
user (71 tokens):
Question: Which files are present in the current directory?
Function: Run the provided commands in the shell
Parameters:
command: The terminal command to be run (type: str)
executable: The executable to run the command with (type: str). Defaults to /bin/sh
timeout: Timeout in seconds (type: int). Defaults to 60.
[time: 2023-10-02T05:02:13.407947+00:00] [id: 139886832029600] [event: ChatLLM End]
assistant (24 tokens):
{
"command": "ls",
"executable": "/bin/sh",
"timeout": 60
}
Tool: ToolUserAgent
Details: {'tool': 'TerminalTool', 'args': {'command': 'ls', 'executable': '/bin/sh', 'timeout': 60}} Confirm (y/n): y
[time: 2023-10-02T05:02:17.972209+00:00] [id: 139886845738672] [event: Tool Start]
Tool: TerminalTool
Args: ()
Kwargs: {'command': 'ls', 'executable': '/bin/sh', 'timeout': 60}
[time: 2023-10-02T05:02:17.989184+00:00] [id: 139886845738672] [event: Tool End]
Tool: TerminalTool
ExitCode: 0
Output:
main.py
poetry.lock
pyproject.toml
replit.nix
[time: 2023-10-02T05:02:18.001906+00:00] [id: 139886831884880] [event: Agent Step]
Question: Which files are present in the current directory?
ToolChoice: TerminalTool
ToolArgs: {'command': 'ls', 'executable': '/bin/sh', 'timeout': 60}
ToolExitCode: 0
ToolOutput: main.py
poetry.lock
pyproject.toml
replit.nix
[time: 2023-10-02T05:02:18.012810+00:00] [id: 139886832029312] [event: ChatLLM Init]
system (113 tokens):
You're an expert in deciding what next question should be asked (if any) to reach the final answer. Your question will be acted upon and its result will be provided to you. This will repeat until we reach the final answer. The main question, actions taken till now and their results will be provided to you.
If we've reached the final answer, reply with the answer in the following format:
Final Answer: <final answer>
If not, reply with the next question in the following format:
Question: <next question>
Do not reply with anything else
[time: 2023-10-02T05:02:18.016647+00:00] [id: 139886832029312] [event: ChatLLM Start]
user (42 tokens):
Main question: Which files are present in the current directory?
Question: Which files are present in the current directory?
Output: main.py
poetry.lock
pyproject.toml
replit.nix
[time: 2023-10-02T05:02:19.099117+00:00] [id: 139886832029312] [event: ChatLLM End]
assistant (27 tokens):
Final Answer: The files present in the current directory are: main.py, poetry.lock, pyproject.toml, replit.nix
[time: 2023-10-02T05:02:19.099758+00:00] [id: 139886831884880] [event: Agent End]
Final Answer:
The files present in the current directory are: main.py, poetry.lock, pyproject.toml, replit.nix
[time: 2023-10-02T05:02:19.100218+00:00] [id: 139886831884880] [event: Tool End]
Tool: ToolUserAgent
ExitCode: 0
Output:
The files present in the current directory are: main.py, poetry.lock, pyproject.toml, replit.nix