Skip to content

Commit

Permalink
feat: Added more mathematical functions and made them optional.
Browse files Browse the repository at this point in the history
Removed duckduckgo_instant_search from the DuckDuckGo toolset.
  • Loading branch information
anirbanbasu committed Sep 9, 2024
1 parent b5dfc4c commit 0002ad9
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 18 deletions.
222 changes: 222 additions & 0 deletions src/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@
ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # noqa

from llama_index.core.tools.tool_spec.base import BaseToolSpec

from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec

import math

from utils import EMPTY_STRING


class DuckDuckGoFullSearchOnlyToolSpec(DuckDuckGoSearchToolSpec):
"""Modified version of DuckDuckGoSearch tool spec because we do not need the duckduckgo_instant_search."""

spec_functions = ["duckduckgo_full_search"]


class StringFunctionsToolSpec(BaseToolSpec):
"""Tool spec for some string manipulation functions."""

Expand Down Expand Up @@ -292,6 +301,20 @@ def math_cube_root(self, n: int | float) -> float:
"""
return math.cbrt(n)

def math_nth_root(self, x: int | float, n: int) -> float:
"""
MathematicalFunctions: Computes the nth root of a number.
Args:
x (int | float): The number to find the nth root of.
n (int): The root to find.
Returns:
float: The nth root of the number.
"""
# Alternatively: x ** (1 / n)
return math.pow(x, 1 / n)

def math_exponential(self, n: int | float) -> float:
"""
MathematicalFunctions: Computes the value of e raised to the power n,
Expand All @@ -318,3 +341,202 @@ def math_logarithm(self, n: int | float, base: int | float = math.e) -> float:
float: The logarithm of the number to the given base.
"""
return math.log(n, base)

def math_sine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the sine of an angle in radians.
Args:
x (int | float): The angle in radians.
Returns:
float: The sine of the angle.
"""
return math.sin(x)

def math_cosine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the cosine of an angle in radians.
Args:
x (int | float): The angle in radians.
Returns:
float: The cosine of the angle.
"""
return math.cos(x)

def math_tangent(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the tangent of an angle in radians.
Args:
x (int | float): The angle in radians.
Returns:
float: The tangent of the angle.
"""
return math.tan(x)

def math_arc_sine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse sine of a number.
Args:
x (int | float): The number to find the inverse sine of.
Returns:
float: The inverse sine of the number.
"""
return math.asin(x)

def math_arc_cosine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse cosine of a number.
Args:
x (int | float): The number to find the inverse cosine of.
Returns:
float: The inverse cosine of the number.
"""
return math.acos(x)

def math_arc_tangent(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse tangent of a number.
Args:
x (int | float): The number to find the inverse tangent of.
Returns:
float: The inverse tangent of the number.
"""
return math.atan(x)

def math_radians_to_degrees(self, x: int | float) -> float:
"""
MathematicalFunctions: Converts an angle from radians to degrees.
Args:
x (int | float): The angle in radians.
Returns:
float: The angle in degrees.
"""
return math.degrees(x)

def math_degrees_to_radians(self, x: int | float) -> float:
"""
MathematicalFunctions: Converts an angle from degrees to radians.
Args:
x (int | float): The angle in degrees.
Returns:
float: The angle in radians.
"""
return math.radians(x)

def math_distance(
self, x1: int | float, y1: int | float, x2: int | float, y2: int | float
) -> float:
"""
MathematicalFunctions: Computes the Euclidean distance between two points in a plane.
Args:
x1 (int | float): The x-coordinate of the first point.
y1 (int | float): The y-coordinate of the first point.
x2 (int | float): The x-coordinate of the second point.
y2 (int | float): The y-coordinate of the second point.
Returns:
float: The Euclidean distance between the two points.
"""
return math.dist([x1, y1], [x2, y2])

# TODO: Wrap math.hypot

def math_hyperbolic_sine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the hyperbolic sine of a number.
Args:
x (int | float): The number to find the hyperbolic sine of.
Returns:
float: The hyperbolic sine of the number.
"""
return math.sinh(x)

def math_hyperbolic_cosine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the hyperbolic cosine of a number.
Args:
x (int | float): The number to find the hyperbolic cosine of.
Returns:
float: The hyperbolic cosine of the number.
"""
return math.cosh(x)

def math_hyperbolic_tangent(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the hyperbolic tangent of a number.
Args:
x (int | float): The number to find the hyperbolic tangent of.
Returns:
float: The hyperbolic tangent of the number.
"""
return math.tanh(x)

def math_hyperbolic_arc_sine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse hyperbolic sine of a number.
Args:
x (int | float): The number to find the inverse hyperbolic sine of.
Returns:
float: The inverse hyperbolic sine of the number.
"""
return math.asinh(x)

def math_hyperbolic_arc_cosine(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse hyperbolic cosine of a number.
Args:
x (int | float): The number to find the inverse hyperbolic cosine of.
Returns:
float: The inverse hyperbolic cosine of the number.
"""
return math.acosh(x)

def math_hyperbolic_arc_tangent(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the inverse hyperbolic tangent of a number.
Args:
x (int | float): The number to find the inverse hyperbolic tangent of.
Returns:
float: The inverse hyperbolic tangent of the number.
"""
return math.atanh(x)

def math_gamma(self, x: int | float) -> float:
"""
MathematicalFunctions: Computes the gamma function of a number.
Args:
x (int | float): The number to find the gamma function of.
Returns:
float: The gamma function of the number.
"""
return math.gamma(x)
38 changes: 27 additions & 11 deletions src/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ def create_component_settings(self) -> gr.Group:

with gr.Accordion(label="Tools for agents", open=False):
gr.Markdown(
"**Note** that making too many tools available to the agents _may_ cause query performance degradation! Select only the tools specific to your task."
"**Note** that making too many tools available to the agents _is likely to_ cause query performance degradation. Therefore, select only the tools specific to your task."
)
check_arxiv = gr.Checkbox(
label=ToolNames.TOOL_NAME_ARXIV,
interactive=True,
info="Tool to allow the agents to search for research papers.",
info="Tool to allow the agents to search for research papers on arXiv.",
value=self.dqa_engine.is_toolset_present(ToolNames.TOOL_NAME_ARXIV),
)

Expand Down Expand Up @@ -367,6 +367,15 @@ def create_component_settings(self) -> gr.Group:
),
)

check_mathematical_functions = gr.Checkbox(
label=ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS,
interactive=True,
info="Tool to allow the agents to call some mathematical functions.",
value=self.dqa_engine.is_toolset_present(
ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS
),
)

gr.Markdown("**Basic tools (cannot be deselected)**")
with gr.Row(equal_height=True):
gr.Checkbox(
Expand All @@ -387,15 +396,6 @@ def create_component_settings(self) -> gr.Group:
),
)

gr.Checkbox(
label=ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS,
interactive=False,
info="Tool to allow the agents to call some mathematical functions.",
value=self.dqa_engine.is_toolset_present(
ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS
),
)

gr.Markdown("**List of available tools**")
list_of_tools = gr.List(
label="The list of tools that are available to the agents, including those that are not user-selectable.",
Expand Down Expand Up @@ -464,6 +464,22 @@ def toggle_yahoo_finance_tool(checked: bool):
self.dqa_engine.remove_toolset(ToolNames.TOOL_NAME_YAHOO_FINANCE)
return self.dqa_engine.get_descriptive_tools_dataframe()

@check_mathematical_functions.change(
api_name=False,
inputs=[check_mathematical_functions],
outputs=[list_of_tools],
)
def toggle_mathematical_functions_tool(checked: bool):
if checked:
self.dqa_engine.add_or_set_toolset(
ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS
)
else:
self.dqa_engine.remove_toolset(
ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS
)
return self.dqa_engine.get_descriptive_tools_dataframe()

@dropdown_web_search.change(
api_name=False,
inputs=[dropdown_web_search],
Expand Down
12 changes: 5 additions & 7 deletions src/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from llama_index.tools.wikipedia import WikipediaToolSpec
from llama_index.tools.tavily_research import TavilyToolSpec
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec

from llama_index.tools.yahoo_finance import YahooFinanceToolSpec
from llama_index.core.tools import FunctionTool
Expand All @@ -48,6 +47,7 @@
# from llama_index.core.agent import ReActAgent

from tools import (
DuckDuckGoFullSearchOnlyToolSpec,
StringFunctionsToolSpec,
BasicArithmeticCalculatorSpec,
MathematicalFunctionsSpec,
Expand Down Expand Up @@ -709,11 +709,9 @@ def __init__(self, llm: LLM | None = None):
# Mandatory tools
self.tools.extend(StringFunctionsToolSpec().to_tool_list())
self.tools.extend(BasicArithmeticCalculatorSpec().to_tool_list())
self.tools.extend(MathematicalFunctionsSpec().to_tool_list())

# TODO: Populate the tools based on toolset names specified in the environment variables?
self.tools.extend(ArxivToolSpec().to_tool_list())
self.tools.extend(DuckDuckGoSearchToolSpec().to_tool_list())
self.tools.extend(DuckDuckGoFullSearchOnlyToolSpec().to_tool_list())

def _are_tools_present(self, tool_names: list[str]) -> bool:
"""
Expand Down Expand Up @@ -773,7 +771,7 @@ def is_toolset_present(self, toolset_name: str) -> bool:
return self._are_tools_present(
[
tool.metadata.name
for tool in DuckDuckGoSearchToolSpec().to_tool_list()
for tool in DuckDuckGoFullSearchOnlyToolSpec().to_tool_list()
]
)
elif toolset_name == ToolNames.TOOL_NAME_STRING_FUNCTIONS:
Expand Down Expand Up @@ -843,7 +841,7 @@ def remove_toolset(self, toolset_name: str):
self._remove_tools_by_names(
[
tool.metadata.name
for tool in DuckDuckGoSearchToolSpec().to_tool_list()
for tool in DuckDuckGoFullSearchOnlyToolSpec().to_tool_list()
]
)
elif toolset_name == ToolNames.TOOL_NAME_STRING_FUNCTIONS:
Expand Down Expand Up @@ -895,7 +893,7 @@ def add_or_set_toolset(
elif toolset_name == ToolNames.TOOL_NAME_MATHEMATICAL_FUNCTIONS:
self.tools.extend(MathematicalFunctionsSpec().to_tool_list())
elif toolset_name == ToolNames.TOOL_NAME_DUCKDUCKGO:
self.tools.extend(DuckDuckGoSearchToolSpec().to_tool_list())
self.tools.extend(DuckDuckGoFullSearchOnlyToolSpec().to_tool_list())
elif toolset_name == ToolNames.TOOL_NAME_STRING_FUNCTIONS:
self.tools.extend(StringFunctionsToolSpec().to_tool_list())
elif toolset_name == ToolNames.TOOL_NAME_TAVILY:
Expand Down

0 comments on commit 0002ad9

Please sign in to comment.