refined tool use
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
|
|
||||||
print("running __main__.-py")
|
print("running __main__.-py")
|
||||||
|
|
||||||
from llama import main
|
from llama import main
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
@@ -8,7 +8,9 @@ import torch
|
|||||||
import time
|
import time
|
||||||
import utils
|
import utils
|
||||||
import re
|
import re
|
||||||
|
import os
|
||||||
|
|
||||||
|
torch.set_num_threads(os.cpu_count()) # Adjust this to the number of threads/cores you have
|
||||||
|
|
||||||
|
|
||||||
class Inference:
|
class Inference:
|
||||||
|
8
llama.py
8
llama.py
@@ -3,6 +3,7 @@ import random
|
|||||||
from tool_helper import tool_list, parse_and_execute_tool_call
|
from tool_helper import tool_list, parse_and_execute_tool_call
|
||||||
from tool_functions import register_dummy
|
from tool_functions import register_dummy
|
||||||
from inference import Inference, torch_reseed
|
from inference import Inference, torch_reseed
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +64,8 @@ def main():
|
|||||||
|
|
||||||
inference = Inference()
|
inference = Inference()
|
||||||
|
|
||||||
messages = [{"role": "system", "content": systemmessage + "\n" + inference.generate_tool_use_header(tool_list)}]
|
current_date_and_time = datetime.datetime.now().strftime("Current date is %Y-%m-%d and its %H:%M %p right now.")
|
||||||
|
messages = [{"role": "system", "content": systemmessage + "\n" + current_date_and_time + "\n" + inference.generate_tool_use_header(tool_list)}]
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# print an input prompt to receive text or commands
|
# print an input prompt to receive text or commands
|
||||||
@@ -72,8 +74,8 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if input_text.startswith("!"):
|
if input_text.startswith("!"):
|
||||||
# append_generate_chat("<tool_response>%s</tool_response>" % input_text[1:], role="tool")
|
append_generate_chat("<tool_response>%s</tool_response>" % input_text[1:], role="tool")
|
||||||
append_generate_chat("%s" % input_text[1:], role="tool") # depending on the chat template the tool response tags must or must not be passed. :(
|
# append_generate_chat("%s" % input_text[1:], role="tool") # depending on the chat template the tool response tags must or must not be passed. :(
|
||||||
|
|
||||||
elif input_text.startswith("/clear"):
|
elif input_text.startswith("/clear"):
|
||||||
print("clearing chat history")
|
print("clearing chat history")
|
||||||
|
@@ -58,8 +58,21 @@ def solve_multi_equation(equations, variables):
|
|||||||
|
|
||||||
# solutionpairs = [f"{variable}={value.doit()}" for variable, value in zip(variables, list(solution)[0])]
|
# solutionpairs = [f"{variable}={value.doit()}" for variable, value in zip(variables, list(solution)[0])]
|
||||||
|
|
||||||
return "solved equation system for " + ", ".join(solutionpairs[:-1]) + " and " + solutionpairs[-1]
|
# return "solved equation system for " + ", ".join(solutionpairs[:-1]) + " and " + solutionpairs[-1]
|
||||||
|
|
||||||
|
if len(equations) > 1:
|
||||||
|
leadin = "Solved equation system "
|
||||||
|
else:
|
||||||
|
leadin = "Solved equation "
|
||||||
|
return leadin + _natural_join([_pretty_equation(e) for e in equations]) + " for " + _natural_join(solutionpairs) + "."
|
||||||
|
|
||||||
|
def _natural_join(data: list[any], joiner=", ", last=" and "):
|
||||||
|
if len(data) > 1:
|
||||||
|
return joiner.join(data[:-1]) + last + data[-1]
|
||||||
|
return last.join(data)
|
||||||
|
|
||||||
|
def _pretty_equation(simpy_Eq) -> str:
|
||||||
|
return f"{simpy_Eq.lhs} = {simpy_Eq.rhs}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -45,13 +45,13 @@ def test_math_solver_2():
|
|||||||
|
|
||||||
def test_math_solver_3a():
|
def test_math_solver_3a():
|
||||||
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7 and x - y = 1 for x, y")
|
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7 and x - y = 1 for x, y")
|
||||||
assert result == "solved equation system for x=2 and y=1"
|
assert result == "Solved equation system 2*x + 3*y = 7 and x - y = 1 for x=2 and y=1."
|
||||||
|
|
||||||
def test_math_solver_3b():
|
def test_math_solver_3b():
|
||||||
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7, x - y = 1 for x and y")
|
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7, x - y = 1 for x and y")
|
||||||
assert result == "solved equation system for x=2 and y=1"
|
assert result == "Solved equation system 2*x + 3*y = 7 and x - y = 1 for x=2 and y=1."
|
||||||
|
|
||||||
def test_math_solver_4():
|
def test_math_solver_4():
|
||||||
result = tool_functions.math_evaluate("solve 2*x**3 + 3*y = 7 and x - y = 1 for x, y")
|
result = tool_functions.math_evaluate("solve 2*x**3 + 3*y = 7 and x - y = 1 for x, y")
|
||||||
assert result == "solved equation system for x=~1.421 and y=~0.421"
|
assert result == "Solved equation system 2*x**3 + 3*y = 7 and x - y = 1 for x=~1.421 and y=~0.421."
|
||||||
|
|
||||||
|
@@ -7,42 +7,37 @@ import math_interpreter
|
|||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
|
||||||
@tool
|
# @tool
|
||||||
def current_time():
|
# def current_time():
|
||||||
"""Get the current local date and time as a string."""
|
# """Get the current local date and time as a string."""
|
||||||
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
# # return datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||||
|
# return f"The current local date and time is {datetime.datetime.now().strftime('%Y-%m-%d %H:%M %p')}."
|
||||||
|
|
||||||
@tool
|
|
||||||
def random_float():
|
|
||||||
"""Generate a random float from 0..1."""
|
|
||||||
return str(random.random())
|
|
||||||
|
|
||||||
# @tool
|
# @tool
|
||||||
# def random_float(a: float=0.0, b: float=1.0):
|
# def random_float():
|
||||||
# """Generate a random float in range [a, b], including both end points. Optional pass no parameter and range 0..1 will be used.
|
# """Generate a random float in range 0 to 1."""
|
||||||
# Args:
|
# # return str(random.random())
|
||||||
# a: minimum possible value
|
# return f"The freshly generated a random number from 0..1 is: {random.random():.5f}."
|
||||||
# b: maximum possible value"""
|
|
||||||
# return str(random.randint(a, b))
|
|
||||||
|
|
||||||
|
|
||||||
@tool
|
# @tool
|
||||||
def random_int(a: int, b: int):
|
# def random_int(a: int, b: int):
|
||||||
"""Generate a random integer in range [a, b], including both end points.
|
# """Generate a random integer in the range [a, b], including both end points.
|
||||||
Args:
|
# Args:
|
||||||
a: minimum possible value
|
# a: minimum possible value (must be <= b)
|
||||||
b: maximum possible value"""
|
# b: maximum possible value (must be >= a)"""
|
||||||
return str(random.randint(a, b))
|
# # return str(random.randint(a, b))
|
||||||
|
# return f"A fresh generated random integer between {a} and {b} is {random.randint(a, b)}."
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@tool
|
@tool
|
||||||
def math_evaluate(expression: str):
|
def math_evaluate(expression: str):
|
||||||
"""evaluate and reduce a mathematical expression.
|
"""Evaluate and simplify a mathematical expression. Returns the evaluated result or a simplified version of the expression as a string.
|
||||||
Args:
|
Args:
|
||||||
expression: Reduce mathematic expression (without '=') algebraically.
|
expression: A valid arithmetic expression (e.g., '2 + 3 * 4'). The expression must not contain '='."""
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
tokens = math_lexer.tokenize(expression)
|
tokens = math_lexer.tokenize(expression)
|
||||||
parser = math_ast.Parser()
|
parser = math_ast.Parser()
|
||||||
@@ -55,11 +50,10 @@ Args:
|
|||||||
|
|
||||||
@tool
|
@tool
|
||||||
def math_solve(equations: list[str], variables: list[str]):
|
def math_solve(equations: list[str], variables: list[str]):
|
||||||
"""evaluate a mathematical equation system and solve equation systems.
|
"""Solve a system of linear or non-linear equation system. Returns the solutions as a string, or an error message if the input is invalid or unsolvable.
|
||||||
Args:
|
Args:
|
||||||
equations: list of mathematical equations containing a '='.
|
equations: A list of mathematical equations in the format 'x + y = 2'.
|
||||||
variables: list of variables to solve for. Must be lower or equal the number of given equations.
|
variables: A list of variables to solve for. The number of variables must not exceed the number of equations."""
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables)
|
expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables)
|
||||||
print(expression)
|
print(expression)
|
||||||
|
Reference in New Issue
Block a user