Skip to content

Commit 043cdc6

Browse files
committed
Small refactoring and adding up TBModelChat
1 parent b5927f2 commit 043cdc6

15 files changed

+121
-41
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1-
[![Tackleberry Factory](https://raw.githubusercontent.com/Getty/tackleberry/main/tackleberry.jpg)](https://github.com/Getty/tackleberry)
1+
[![Tackleberry](https://raw.githubusercontent.com/Getty/tackleberry/main/tackleberry.jpg)](https://github.com/Getty/tackleberry)
22

33
# Tackleberry
4+
5+
6+
7+
# Install
8+
9+
## Using PIP
10+
11+
### Stable Version with PIP
12+
13+
Install from `PyPi`
14+
15+
```console
16+
pip install --upgrade tackleberry
17+
```
18+

tackleberry/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from importlib.metadata import version
2-
from .__main__ import TB
2+
from .main import TB
33

44
__all__ = ['TB']
55

tackleberry/context.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ def __init__(self, content: str, role: str):
99
self.content = content
1010

1111
class TBMessageSystem(TBMessage):
12-
12+
"""A system message"""
1313
def __init__(self, system_prompt: str):
1414
super().__init__(system_prompt, role="system")
1515

16-
class TBMessageAssistant(TBMessage):
16+
class TBSystemPromptError(Exception):
17+
"""Error to throw if system prompt is not possible in the way as used."""
18+
def __init__(self, message):
19+
super().__init__(message)
1720

21+
class TBMessageAssistant(TBMessage):
22+
"""An AI assistant message"""
1823
def __init__(self, assistant_context: str):
1924
super().__init__(assistant_context, role="assistant")
2025

2126
class TBMessageUser(TBMessage):
22-
27+
"""User message"""
2328
def __init__(self, user_message: str, role: Optional[str] = None):
2429
super().__init__(user_message, role if role is not None else "user")
2530

tackleberry/engine/__init__.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
from typing import Any, Dict, Optional
1+
import os
2+
import importlib
23

3-
class TBEngine:
4+
# Special case: Explicitly import TBEngine from base.py
5+
from .base import TBEngine
46

5-
def __init__(self):
6-
pass
7+
# Automatically detect all Python files in the current directory
8+
current_dir = os.path.dirname(__file__)
9+
module_files = [
10+
f for f in os.listdir(current_dir)
11+
if f.endswith(".py") and f not in ("__init__.py", "base.py")
12+
]
713

8-
def model(self, model: str):
9-
from ..model import TBModel
10-
return TBModel(self, model)
14+
# Create __all__ with the module names and the special case
15+
__all__ = ["TBEngine"] + [os.path.splitext(f)[0] for f in module_files]
16+
17+
# Dynamically import the modules and add them to the global namespace
18+
for module_name in __all__[1:]: # Skip "TBEngine" as it's already imported
19+
module = importlib.import_module(f".{module_name}", package=__name__)
20+
globals()[module_name] = module

tackleberry/engine/anthropic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Any, Union, Dict, List, Optional
21
import os
32

4-
from . import TBEngine
3+
from .base import TBEngine
54

65
class TBEngineAnthropic(TBEngine):
76
default_max_tokens = 256

tackleberry/engine/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class TBEngine:
2+
3+
def __init__(self):
4+
pass
5+
6+
def model(self, model: str):
7+
from ..model import TBModel
8+
return TBModel(self, model)
9+
10+
def chat(self, model: str):
11+
from ..model import TBModelChat
12+
return TBModelChat(self, model)

tackleberry/engine/groq.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Any, Union, Dict, List, Optional
21
import os
32

4-
from . import TBEngine
3+
from .base import TBEngine
54

65
class TBEngineGroq(TBEngine):
76

tackleberry/engine/hf.py

-15
This file was deleted.

tackleberry/engine/ollama.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Any, Union, Dict, List, Optional
21
import os
32
from urllib.parse import urlparse
43
import base64
54

6-
from . import TBEngine
5+
from .base import TBEngine
76

87
class TBEngineOllama(TBEngine):
98

tackleberry/engine/openai.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Any, Union, Dict, List, Optional
21
import os
32

4-
from . import TBEngine
3+
from .base import TBEngine
54

65
class TBEngineOpenai(TBEngine):
76

tackleberry/engine/trf.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .base import TBEngine
2+
3+
class TBEngineTrf(TBEngine):
4+
5+
def __init__(self,
6+
hf_token: str = None,
7+
**kwargs,
8+
):
9+
self.hf_token = hf_token
10+
11+
def __str__(self):
12+
return f"TB Engine HuggingFace transformers {hex(id(self))}"
File renamed without changes.

tackleberry/model/__init__.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
from typing import Any, Dict, Optional
1+
import os
2+
import importlib
23

3-
from ..engine import TBEngine
4+
# Special case: Explicitly import TBModel from base.py
5+
from .base import TBModel
46

5-
class TBModel:
7+
# Automatically detect all Python files in the current directory
8+
current_dir = os.path.dirname(__file__)
9+
module_files = [
10+
f for f in os.listdir(current_dir)
11+
if f.endswith(".py") and f not in ("__init__.py", "base.py")
12+
]
613

7-
def __init__(self, engine: TBEngine, model: str):
8-
self.engine = engine
9-
self.model = model
14+
# Create __all__ with the module names and the special case
15+
__all__ = ["TBModel"] + [os.path.splitext(f)[0] for f in module_files]
16+
17+
# Dynamically import the modules and add them to the global namespace
18+
for module_name in __all__[1:]: # Skip "TBModel" as it's already imported
19+
module = importlib.import_module(f".{module_name}", package=__name__)
20+
globals()[module_name] = module

tackleberry/model/base.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Any, Dict, Optional
2+
3+
from ..engine import TBEngine
4+
5+
class TBModel:
6+
7+
def __init__(self, engine: TBEngine, name: str):
8+
self.engine = engine
9+
self.name = name

tackleberry/model/chat.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import Union
2+
3+
from .base import TBModel
4+
from ..engine import TBEngine
5+
from ..context import TBContext, TBSystemPromptError
6+
7+
class TBModelChat(TBModel):
8+
9+
def __init__(self,
10+
engine: TBEngine,
11+
model_name_or_model: Union[str|TBModel],
12+
system_prompt: str = None,
13+
context: TBContext = None,
14+
**kwargs,
15+
):
16+
self.engine = engine
17+
if context is not None and system_prompt is not None:
18+
raise TBSystemPromptError("A TBModelChat can't handle system_prompt and context at once.")
19+
self.system_prompt = system_prompt
20+
self.context = context
21+
if isinstance(model_name_or_model, TBModel):
22+
self.model = model_name_or_model
23+
else:
24+
self.model = self.engine.model(model_name_or_model)
25+
self.name = model_name_or_model.name

0 commit comments

Comments
 (0)