Browse Source

python package restructuring

master
Florin Tobler 5 months ago
parent
commit
7224111a0b
  1. 1
      .gitignore
  2. 2
      .vscode/launch.json
  3. 0
      chatbug/__init__.py
  4. 3
      chatbug/__main__.py
  5. 4
      chatbug/download_model.py
  6. 0
      chatbug/file_append.py
  7. 6
      chatbug/generation_loop.py
  8. 0
      chatbug/gpt2.py
  9. 4
      chatbug/inference.py
  10. 4
      chatbug/inference_profile_experiement.py
  11. 19
      chatbug/llama.py
  12. 3
      chatbug/matheval/__init__.py
  13. 5
      chatbug/matheval/ast.py
  14. 3
      chatbug/matheval/interpreter.py
  15. 0
      chatbug/matheval/lexer.py
  16. 2
      chatbug/model_selection.py
  17. 0
      chatbug/modelconfig.py
  18. 0
      chatbug/nvidia_smi.py
  19. 22
      chatbug/tool_functions.py
  20. 2
      chatbug/tool_helper.py
  21. 0
      chatbug/utils.py
  22. 22
      setup.py
  23. 1
      tests/__init__.py
  24. 30
      tests/test_inference.py
  25. 4
      tests/test_tool_function_decorator.py
  26. 12
      tests/test_tool_functions.py
  27. 16
      tests/test_tool_parse_exec.py

1
.gitignore

@ -2,3 +2,4 @@
*.prof *.prof
__pycache__ __pycache__
*.venv *.venv
*.egg-info

2
.vscode/launch.json

@ -15,7 +15,7 @@
"name": "PyDebug: __main__.py", "name": "PyDebug: __main__.py",
"type": "debugpy", "type": "debugpy",
"request": "launch", "request": "launch",
"program": "__main__.py", "program": "chatbug/__main__.py",
"console": "integratedTerminal" "console": "integratedTerminal"
} }
] ]

0
chatbug/__init__.py

3
__main__.py → chatbug/__main__.py

@ -1,6 +1,7 @@
print("running __main__.-py") print("running __main__.-py")
from llama import main from chatbug.llama import main
if __name__ == "__main__": if __name__ == "__main__":
main() main()

4
download_model.py → chatbug/download_model.py

@ -1,7 +1,7 @@
from inference import Inference from chatbug.inference import Inference
from modelconfig import Modelconfig from chatbug.modelconfig import Modelconfig
def main(): def main():

0
file_append.py → chatbug/file_append.py

6
generation_loop.py → chatbug/generation_loop.py

@ -1,9 +1,9 @@
import time import time
import json import json
import random import random
from tool_helper import tool_list, parse_and_execute_tool_call from chatbug.tool_helper import tool_list, parse_and_execute_tool_call
from inference import Inference, torch_reseed from chatbug.inference import Inference, torch_reseed
from file_append import check_append_file from chatbug.file_append import check_append_file

0
gpt2.py → chatbug/gpt2.py

4
inference.py → chatbug/inference.py

@ -14,10 +14,10 @@ from transformers.cache_utils import (
) )
import torch import torch
import time import time
import utils
import re import re
import os import os
from modelconfig import Modelconfig import chatbug.utils as utils
from chatbug.modelconfig import Modelconfig
torch.set_num_threads(os.cpu_count()) # Adjust this to the number of threads/cores you have torch.set_num_threads(os.cpu_count()) # Adjust this to the number of threads/cores you have

4
inference_profile_test.py → chatbug/inference_profile_experiement.py

@ -1,9 +1,9 @@
from inference import Inference
from modelconfig import Modelconfig
import time import time
import nvidia_smi import nvidia_smi
import torch import torch
import gc import gc
from chatbug.inference import Inference
from chatbug.modelconfig import Modelconfig
def empty_cuda(): def empty_cuda():

19
llama.py → chatbug/llama.py

@ -1,10 +1,11 @@
from tool_helper import tool_list
from tool_functions import register_dummy
from inference import Inference
import datetime import datetime
import model_selection from chatbug.tool_helper import tool_list
from generation_loop import Terminal, msg from chatbug.tool_functions import register_dummy
from chatbug.inference import Inference
from chatbug.generation_loop import Terminal, msg
from chatbug import model_selection
register_dummy() register_dummy()
@ -35,9 +36,11 @@ def initialize_config(inference: Inference) -> Terminal:
return terminal return terminal
def main():
if __name__ == "__main__":
inference = Inference(model_selection.get_model()) inference = Inference(model_selection.get_model())
terminal = initialize_config(inference) terminal = initialize_config(inference)
terminal.join() terminal.join()
if __name__ == "__main__":
main()

3
chatbug/matheval/__init__.py

@ -0,0 +1,3 @@
from chatbug.matheval import ast
from chatbug.matheval import interpreter
from chatbug.matheval import lexer

5
math_ast.py → chatbug/matheval/ast.py

@ -1,6 +1,5 @@
from chatbug.matheval import lexer
import math_lexer as lexer from chatbug.matheval.lexer import Token
from math_lexer import Token
class Statement: class Statement:

3
math_interpreter.py → chatbug/matheval/interpreter.py

@ -1,10 +1,11 @@
import math_ast as ast
from sympy.parsing.sympy_parser import parse_expr from sympy.parsing.sympy_parser import parse_expr
from sympy.core.numbers import Integer, One, Zero from sympy.core.numbers import Integer, One, Zero
from sympy import symbols, Eq, solveset, linsolve, nonlinsolve from sympy import symbols, Eq, solveset, linsolve, nonlinsolve
from sympy.core.symbol import Symbol from sympy.core.symbol import Symbol
from chatbug.matheval import ast
def interpret(statement: ast.Statement) -> str: def interpret(statement: ast.Statement) -> str:

0
math_lexer.py → chatbug/matheval/lexer.py

2
model_selection.py → chatbug/model_selection.py

@ -1,5 +1,5 @@
from modelconfig import Modelconfig from chatbug.modelconfig import Modelconfig

0
modelconfig.py → chatbug/modelconfig.py

0
nvidia_smi.py → chatbug/nvidia_smi.py

22
tool_functions.py → chatbug/tool_functions.py

@ -1,10 +1,10 @@
import random import random
import datetime import datetime
from tool_helper import tool from chatbug.tool_helper import tool
import math_lexer import chatbug.matheval as matheval
import math_ast # from chatbug.matheval import interpreter, lexer
import math_interpreter # from chatbug.matheval.ast import Parser
import utils import chatbug.utils as utils
# @tool # @tool
@ -39,10 +39,10 @@ def math_evaluate(expression: str):
Args: Args:
expression: A valid arithmetic expression (e.g., '2 + 3 * 4'). The expression must not contain '='.""" expression: A valid arithmetic expression (e.g., '2 + 3 * 4'). The expression must not contain '='."""
try: try:
tokens = math_lexer.tokenize(expression) tokens = matheval.lexer.tokenize(expression)
parser = math_ast.Parser() parser = matheval.ast.Parser()
ast = parser.parse(tokens) ast = parser.parse(tokens)
return math_interpreter.interpret(ast) return matheval.interpreter.interpret(ast)
except Exception as e: except Exception as e:
utils.print_error("Tool call evaluation failed. - " + str(e)) utils.print_error("Tool call evaluation failed. - " + str(e))
return "Tool call evaluation failed." return "Tool call evaluation failed."
@ -58,10 +58,10 @@ Args:
expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables) expression = "solve " + " and ".join(equations) + " for " + " and ".join(variables)
print(expression) print(expression)
tokens = math_lexer.tokenize(expression) tokens = lexer.tokenize(expression)
parser = math_ast.Parser() parser = ast.Parser()
ast = parser.parse(tokens) ast = parser.parse(tokens)
return math_interpreter.interpret(ast) return interpreter.interpret(ast)
except Exception as e: except Exception as e:
utils.print_error("Tool call evaluation failed. - " + str(e)) utils.print_error("Tool call evaluation failed. - " + str(e))
return "Tool call evaluation failed." return "Tool call evaluation failed."

2
tool_helper.py → chatbug/tool_helper.py

@ -2,7 +2,7 @@
from typing import Callable, List, Optional from typing import Callable, List, Optional
import json import json
import re import re
import utils import chatbug.utils as utils
tool_list = [] tool_list = []

0
utils.py → chatbug/utils.py

22
setup.py

@ -0,0 +1,22 @@
from setuptools import setup, find_packages
setup(
name='chatbug',
version='0.1.0',
description='A conversational AI chatbot',
author='Florin Tobler',
author_email='florin.tobler@hotmail.com',
packages=find_packages(exclude=["tests"]),
install_requires=[
'transformers',
'accelerate',
'bitsandbytes',
'pytest',
'pywebview',
],
# entry_points={
# 'console_scripts': [
# 'chatbug=chatbug.app:main',
# ],
# },
)

1
tests/__init__.py

@ -1 +0,0 @@
# empty

30
tests/test_inference.py

@ -1,32 +1,20 @@
import pytest import pytest
import tests.helper as helper from tests import helper
inference = None inference = None
InferenceClass = None
Tensor = None Tensor = None
def prepare(): def prepare():
if InferenceClass == None:
test_import_inference_module_librarys()
if inference == None:
test_instantiate_inference_instance()
def test_import_inference_module_librarys():
import inference
import torch
global InferenceClass
global Tensor
InferenceClass = inference.Inference
Tensor = torch.Tensor
def test_instantiate_inference_instance():
if InferenceClass == None:
test_import_inference_module_librarys()
global inference global inference
inference = InferenceClass() global Tensor
if inference == None:
from torch import Tensor as _Tensor
from chatbug.inference import Inference
from chatbug.model_selection import get_model
inference = Inference(get_model())
Tensor = _Tensor
def test_tool_header_generation(): def test_tool_header_generation():

4
tests/test_tool_function_decorator.py

@ -1,6 +1,6 @@
import pytest import pytest
import tool_helper import chatbug.tool_helper as tool_helper
import tests.helper as helper from tests import helper

12
tests/test_tool_functions.py

@ -1,6 +1,6 @@
import pytest import pytest
import tool_functions import chatbug.tool_functions as tool_functions
from tests import helper
def test_math_evaluate_1(): def test_math_evaluate_1():
@ -28,6 +28,13 @@ def test_math_evaluate_5():
result = tool_functions.math_evaluate("sin(pi/2) + cos(0)") result = tool_functions.math_evaluate("sin(pi/2) + cos(0)")
assert result == "sin(pi/2) + cos(0) = 2" assert result == "sin(pi/2) + cos(0) = 2"
def test_math_evaluate_solve_a():
result = tool_functions.math_evaluate("solve 240=x*r+x*r^2+x*r^3+s and r=1.618 and s=5 for x, r, s")
assert result == "Solved equation system 240 = r**3*x + r**2*x + r*x + s, r = 1.61800000000000 and s = 5 for x=27.7393327937747=~27.739, r=1.61800000000000=~1.618 and s=5.00000000000000=~5.000."
def test_math_evaluate_solve_b():
result = tool_functions.math_evaluate("solve 250=x+x*r+s and r=1.618 and s=0 for x, r, s")
assert result == "Solved equation system 250 = r*x + s + x, r = 1.61800000000000 and s = 0 for x=95.4927425515661=~95.493, r=1.61800000000000=~1.618 and s=0."
@ -54,4 +61,3 @@ def test_math_solver_3b():
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 2*x**3 + 3*y = 7 and x - y = 1 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."

16
tests/test_tool_parse_exec.py

@ -1,7 +1,8 @@
import pytest import pytest
import tool_helper from chatbug import tool_helper
from unittest import mock from unittest import mock
import tests.helper as helper from tests import helper
import re
@ -40,34 +41,34 @@ def test_match_and_extract_matching3_with_newline():
def test_string_malformed_faulty(): def test_string_malformed_faulty():
with mock.patch("utils.print_error") as print_error_mock: with mock.patch("chatbug.utils.print_error") as print_error_mock:
result = tool_helper._execute_tool_call_str("{json_content}", []) result = tool_helper._execute_tool_call_str("{json_content}", [])
assert result == None assert result == None
print_error_mock.assert_called_once() # this will check if the mocked function on the context was called. print_error_mock.assert_called_once() # this will check if the mocked function on the context was called.
def test_tool_call_json_1(): def test_tool_call_json_1():
with mock.patch("utils.print_error") as print_error_mock: with mock.patch("chatbug.utils.print_error") as print_error_mock:
result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": 1, "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2]) result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": 1, "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2])
assert result == "result_1_zwei" assert result == "result_1_zwei"
assert print_error_mock.call_count == 0 assert print_error_mock.call_count == 0
def test_tool_call_json_2(): def test_tool_call_json_2():
with mock.patch("utils.print_error") as print_error_mock: with mock.patch("chatbug.utils.print_error") as print_error_mock:
result = tool_helper._execute_tool_call_json({"name": "tool_dummy2", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2]) result = tool_helper._execute_tool_call_json({"name": "tool_dummy2", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2])
assert result == "SOME_TEXT" assert result == "SOME_TEXT"
assert print_error_mock.call_count == 0 assert print_error_mock.call_count == 0
def test_tool_call_json_non_existing_call_check(): def test_tool_call_json_non_existing_call_check():
with mock.patch("utils.print_error") as print_error_mock: with mock.patch("chatbug.utils.print_error") as print_error_mock:
result = tool_helper._execute_tool_call_json({"name": "tool_dummy_which_is_not_existing", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2]) result = tool_helper._execute_tool_call_json({"name": "tool_dummy_which_is_not_existing", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2])
assert result == None assert result == None
assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called. assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called.
def test_tool_call_json_wrong_arguments_check(): def test_tool_call_json_wrong_arguments_check():
with mock.patch("utils.print_error") as print_error_mock: with mock.patch("chatbug.utils.print_error") as print_error_mock:
result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": "must_be_an_int_but_is_string", "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2]) result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": "must_be_an_int_but_is_string", "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2])
assert result == None assert result == None
assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called. assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called.
@ -75,7 +76,6 @@ def test_tool_call_json_wrong_arguments_check():
def test_regex_multiline(): def test_regex_multiline():
import re
pattern = r"<start>(.*)</end>" pattern = r"<start>(.*)</end>"
# The text to search (spanning multiple lines) # The text to search (spanning multiple lines)

Loading…
Cancel
Save