Skip to content

Commit 0b46786

Browse files
committed
Added tools
1 parent 50394d2 commit 0b46786

File tree

7 files changed

+1819
-4
lines changed

7 files changed

+1819
-4
lines changed

jupyter_ai_jupyternaut/jupyternaut/jupyternaut.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
JupyternautSystemPromptArgs,
1616
)
1717

18+
from .toolkits.code_execution import toolkit as exec_toolkit
19+
from .toolkits.filesystem import toolkit as fs_toolkit
20+
from .toolkits.notebook import toolkit as nb_toolkit
21+
22+
1823
MEMORY_STORE_PATH = os.path.join(jupyter_data_dir(), "jupyter_ai", "memory.sqlite")
1924

2025

@@ -40,11 +45,23 @@ async def get_memory_store(self):
4045
conn = await aiosqlite.connect(MEMORY_STORE_PATH, check_same_thread=False)
4146
self._memory_store = AsyncSqliteSaver(conn)
4247
return self._memory_store
48+
49+
def get_tools(self):
50+
tools = []
51+
tools += nb_toolkit
52+
tools += fs_toolkit
4353

44-
async def _create_agent(self, model_id: str, model_args, system_prompt: str):
54+
return tools
55+
56+
async def get_agent(self, model_id: str, model_args, system_prompt: str):
4557
model = ChatLiteLLM(**model_args, model_id=model_id, streaming=True)
4658
memory_store = await self.get_memory_store()
47-
return create_agent(model, system_prompt=system_prompt, checkpointer=memory_store)
59+
return create_agent(
60+
model,
61+
system_prompt=system_prompt,
62+
checkpointer=memory_store,
63+
tools=self.get_tools()
64+
)
4865

4966
async def process_message(self, message: Message) -> None:
5067
if not hasattr(self, "config_manager"):
@@ -62,7 +79,7 @@ async def process_message(self, message: Message) -> None:
6279
model_id = self.config_manager.chat_model
6380
model_args = self.config_manager.chat_model_args
6481
system_prompt = self.get_system_prompt(model_id=model_id, message=message)
65-
agent = await self._create_agent(
82+
agent = await self.get_agent(
6683
model_id=model_id,
6784
model_args=model_args,
6885
system_prompt=system_prompt

jupyter_ai_jupyternaut/jupyternaut/toolkits/__init__.py

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Tools that provide code execution features"""
2+
3+
import asyncio
4+
import shlex
5+
from typing import Optional
6+
7+
8+
async def bash(command: str, timeout: Optional[int] = None) -> str:
9+
"""Executes a bash command and returns the result
10+
11+
Args:
12+
command: The bash command to execute
13+
timeout: Optional timeout in seconds
14+
15+
Returns:
16+
The command output (stdout and stderr combined)
17+
"""
18+
19+
proc = await asyncio.create_subprocess_exec(
20+
*shlex.split(command),
21+
stdout=asyncio.subprocess.PIPE,
22+
stderr=asyncio.subprocess.PIPE,
23+
)
24+
25+
try:
26+
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout)
27+
output = stdout.decode("utf-8")
28+
error = stderr.decode("utf-8")
29+
30+
if proc.returncode != 0:
31+
if error:
32+
return f"Error: {error}"
33+
return f"Command failed with exit code {proc.returncode}"
34+
35+
return output if output else "Command executed successfully with no output."
36+
except asyncio.TimeoutError:
37+
proc.kill()
38+
return f"Command timed out after {timeout} seconds"
39+
40+
41+
toolkit = [bash]

0 commit comments

Comments
 (0)