From 6774468568b38b3f71a140d616b9ac6ad3fbcc25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:38:08 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/users/tools.md | 4 ++-- .../jupyter-ai/jupyter_ai/chat_handlers/tools.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/source/users/tools.md b/docs/source/users/tools.md index 553bd1f2d..ac3f73917 100644 --- a/docs/source/users/tools.md +++ b/docs/source/users/tools.md @@ -2,7 +2,7 @@ In many situations LLMs will handle complex mathematical formulas quite well and return correct answers, but this is often not the case. Even for textual repsonses, using custom functions can constrain responses to formats and content that is more accurate and acceptable. -Jupyter AI includes a slash command `/tools` that directs the LLM to use functions from a tools library that you provide. This is a single file titled `mytools.py` which will be stored under `.jupyter/jupyter-ai/tools/`. +Jupyter AI includes a slash command `/tools` that directs the LLM to use functions from a tools library that you provide. This is a single file titled `mytools.py` which will be stored under `.jupyter/jupyter-ai/tools/`. The usage of this slash command is as follows: @@ -12,7 +12,7 @@ For example, we may try: `/tools -t mytools.py What is the sum of 1 and 2?` -Note that since the file has to be placed in `.jupyter/jupyter-ai/tools/`, only file name is needed in the command. +Note that since the file has to be placed in `.jupyter/jupyter-ai/tools/`, only file name is needed in the command. We provide an example of the tools file here, containing just three functions. Make sure to add the `@tool` decorator to each function and to import all packages that are not already installed within each function. The functions below are common financial formulas that are widely in use and you may expect that an LLM would be trained on these. While this is accurate, we will see that the LLM is unable to accurately execute the math in these formulas. diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/tools.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/tools.py index 1765ad076..c6ee64490 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/tools.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/tools.py @@ -1,11 +1,10 @@ import argparse import ast import math -import ast -from pathlib import Path # LangGraph imports for using tools import os +from pathlib import Path from typing import Dict, Literal, Type import numpy as np @@ -144,7 +143,10 @@ def get_tool_names(self, tools_file_path): for node in ast.walk(tree): if isinstance(node, ast.FunctionDef): for decorator in node.decorator_list: - if isinstance(decorator, ast.Name) and decorator.id == 'tool': + if ( + isinstance(decorator, ast.Name) + and decorator.id == "tool" + ): tools.append(node.name) return tools except FileNotFoundError as e: @@ -208,9 +210,7 @@ def call_tool(state: MessagesState): agentic_workflow.add_node("tools", tool_node) # Add edges to the graph agentic_workflow.add_edge("__start__", "agent") - agentic_workflow.add_conditional_edges( - "agent", self.conditional_continue - ) + agentic_workflow.add_conditional_edges("agent", self.conditional_continue) agentic_workflow.add_edge("tools", "agent") # Compile graph app = agentic_workflow.compile() @@ -228,7 +228,7 @@ async def process_message(self, message: HumanChatMessage): if args.tools: self.tools_file_path = os.path.join( Path.home(), ".jupyter/jupyter-ai/tools", args.tools - ) + ) query = " ".join(args.query) if not query: