File size: 5,695 Bytes
c3b34bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
from math import sqrt
import logging
from llama_index.core.tools import FunctionTool
def add(a: float, b: float, context: str) -> str:
"""Add two numbers.
Args:
a (float): Addend 1
b (float): Addend 2
context (str): Context for the addition, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a + b)} for {context}"
def sub(a: float, b: float, context: str) -> str:
"""Subtract two numbers.
Args:
a (float): Minuend
b (float): Subtrahend
context (str): Context for the subtraction, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a - b)} for {context}"
def multiply(a: float, b: float, context: str) -> str:
"""Multiply two numbers.
Args:
a (float): Multiplicand
b (float): Multiplier
context (str): Context for the multiplication, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a * b)} for {context}"
def divide(a: float, b: float, context: str) -> str:
"""Divide two numbers.
Args:
a (float): Dividend
b (float): Divisor
context (str): Context for the division, this should be used to keep track of the operations.
Returns:
str: The result with the context included, or an error message if division by zero occurs.
"""
if b == 0:
return f"Error: Division by zero for {context}"
return f"Result {str(a / b)} for {context}"
def floordiv(a: float, b: float, context: str) -> str:
"""Floor divide two numbers.
Args:
a (float): Dividend
b (float): Divisor
context (str): Context for the floor division, this should be used to keep track of the operations.
Returns:
str: The result with the context included, or an error message if division by zero occurs.
"""
if b == 0:
return f"Error: Division by zero for {context}"
return f"Result {str(a // b)} for {context}"
def mod(a: float, b: float, context: str) -> str:
"""Get the modulus of two numbers.
Args:
a (float): Dividend
b (float): Divisor
context (str): Context for the modulus operation, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a % b)} for {context}"
def sqrt(a: float, context: str) -> str:
"""Get the square root of a number.
Args:
a (float): Number to find the square root of
context (str): Context for the square root operation, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a**0.5)} for {context}"
def equals(a: float, b: float, context: str) -> str:
"""Check if two numbers are equal.
Args:
a (float): First number
b (float): Second number
context (str): Context for the equality check, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a == b)} for {context}"
def less_than(a: float, b: float, context: str) -> str:
"""Check if the first number is less than the second number.
Args:
a (float): First number
b (float): Second number
context (str): Context for the less than check, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a < b)} for {context}"
def more_than(a: float, b: float, context: str) -> str:
"""Check if the first number is more than the second number.
Args:
a (float): First number
b (float): Second number
context (str): Context for the more than check, this should be used to keep track of the operations.
Returns:
str: The result with the context included.
"""
return f"Result {str(a > b)} for {context}"
def solve_math_problem(problem: str) -> str:
"""
Solve mathematical problems using GPT-4.
Args:
problem: The mathematical problem to solve
Returns:
str: Solution
"""
try:
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": "You are a mathematics expert. Solve problems step by step with clear explanations."
},
{
"role": "user",
"content": f"Please solve this mathematical problem step by step:\n\n{problem}"
}
]
)
return response.choices[0].message.content
except Exception as e:
return f"Math problem solving failed: {str(e)}"
def get_math_tools():
"""
Get the math tools.
"""
return [
FunctionTool.from_defaults(fn=sum),
FunctionTool.from_defaults(fn=sub),
FunctionTool.from_defaults(fn=add),
FunctionTool.from_defaults(fn=sqrt),
FunctionTool.from_defaults(fn=floordiv),
FunctionTool.from_defaults(fn=mod),
FunctionTool.from_defaults(fn=multiply),
FunctionTool.from_defaults(fn=equals),
FunctionTool.from_defaults(fn=less_than),
FunctionTool.from_defaults(fn=more_than),
]
|