Skip to content

Commit

Permalink
Intense cleanup of the API, rename of components and streamlining con…
Browse files Browse the repository at this point in the history
…cept.
  • Loading branch information
Getty committed Jan 12, 2025
1 parent 3babc0f commit 1fc30e6
Show file tree
Hide file tree
Showing 22 changed files with 440 additions and 275 deletions.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[project]
name = "tackleberry"
version = "0.1.0.dev1"
version = "0.1.0"
description = "Tackleberry (or TB) is helping you tackle the access to AI"
authors = [
{ name = "Torsten Raudßus", email = "[email protected]" },
]
dependencies = [
"pydantic>=2.0.0",
"instructor>=1.7.0",
"typing-extensions>=4.0.0",
"pyyaml>=6.0.0",
]
Expand All @@ -27,7 +28,9 @@ dev = [
"ollama",
"transformers",
"groq",
"instructor[groq]",
"anthropic",
"instructor[anthropic]",
]

[build-system]
Expand Down
2 changes: 1 addition & 1 deletion tackleberry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
try:
__version__ = version("tackleberry")
except ImportError:
__version__ = "0.0.1.dev0"
__version__ = "0.0.1.dev1"
42 changes: 42 additions & 0 deletions tackleberry/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Union

from .context import TBContext
from .model import TBModel

from pydantic import BaseModel

class TBChat:
count = 0

def __init__(self,
model_name_or_model: Union[str, TBModel],
context: TBContext = None,
system_prompt: str = None,
struct: BaseModel = None,
name: str = None,
**kwargs,
):
TBChat.count += 1
self.name = name or f'TBChat-{TBChat.count}'
if isinstance(model_name_or_model, TBModel):
self.model = model_name_or_model
else:
from . import TB
self.model = TB.model(model_name_or_model)
self.struct = struct
self.context = context if context is not None else TBContext()
if system_prompt is not None:
self.context.add_system(system_prompt)
self.model_name = self.model.name
self.runtime = self.model.runtime

def get_messages(self):
return self.runtime.get_messages_from_context(self.context)

def query(self,
query_or_context: Union[str, TBContext],
struct: BaseModel = None,
**kwargs,
):
context = query_or_context if isinstance(query_or_context, TBContext) else self.context.copy_with_query(query_or_context)
return self.runtime.chat_context(self, context, struct=struct, **kwargs)
37 changes: 20 additions & 17 deletions tackleberry/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import copy

class TBMessage:

"""Base class for message"""
def __init__(self, content: str, role: str):
self.role = role
self.content = content
Expand All @@ -29,7 +29,7 @@ def __init__(self, user_message: str, role: Optional[str] = None):
super().__init__(user_message, role if role is not None else "user")

class TBContext:

"""Combined messages as context"""
def __init__(self, system_prompt: Optional[str] = None, user_query: Optional[str] = None):
self.messages = []
self.query = user_query
Expand All @@ -44,8 +44,11 @@ def add_system(self, system_prompt: str):
self.messages.append(TBMessageSystem(system_prompt))
return self

def add_assistant(self, assistant_context: str):
self.messages.append(TBMessageAssistant(assistant_context))
def has_system(self):
return any(isinstance(message, TBMessageSystem) for message in self.messages)

def add_assistant(self, assistant_message: str):
self.messages.append(TBMessageAssistant(assistant_message))
return self

def add_user(self, user_message: str):
Expand All @@ -56,39 +59,39 @@ def add_query(self, user_query: str):
self.query = user_query
return self

def spawn(self):
def copy(self):
return copy.deepcopy(self)

def spawn_with(self, message: Union[TBMessage, 'TBContext']):
clone = self.spawn()
def copy_with(self, message: Union[TBMessage, 'TBContext']):
clone = self.copy()
if isinstance(message, TBMessage):
clone.add(message)
elif isinstance(message, TBContext):
for cmessage in message.messages:
clone.add(cmessage)
return clone

def spawn_with_system(self, system_prompt: str):
return self.spawn_with(TBMessageSystem(system_prompt))
def copy_with_system(self, system_prompt: str):
return self.copy_with(TBMessageSystem(system_prompt))

def spawn_with_assistant(self, assistant_context: str):
return self.spawn_with(TBMessageAssistant(assistant_context))
def copy_with_assistant(self, assistant_context: str):
return self.copy_with(TBMessageAssistant(assistant_context))

def spawn_with_user(self, user_message: str):
return self.spawn_with(TBMessageUser(user_message))
def copy_with_user(self, user_message: str):
return self.copy_with(TBMessageUser(user_message))

def spawn_with_query(self, user_query: str):
return self.spawn().add_query(user_query)
def copy_with_query(self, user_query: str):
return self.copy().add_query(user_query)

def all_messages(self):
def get_messages(self):
messages = self.messages
if not self.query is None:
messages.append(TBMessageUser(self.query))
return messages

def to_messages(self):
message_list = []
for message in self.all_messages():
for message in self.get_messages():
message_list.append({
"content": message.content,
"role": message.role,
Expand Down
31 changes: 0 additions & 31 deletions tackleberry/engine/anthropic.py

This file was deleted.

28 changes: 0 additions & 28 deletions tackleberry/engine/groq.py

This file was deleted.

28 changes: 0 additions & 28 deletions tackleberry/engine/openai.py

This file was deleted.

44 changes: 22 additions & 22 deletions tackleberry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from importlib import import_module

from .registry import TBRegistry
from .engine import TBEngine
from .runtime import TBRuntime
from .context import TBContext

class TBMain:
Expand All @@ -16,7 +16,7 @@ def __init__(self,
TBMain.count += 1
self.name = name or f'TB-{TBMain.count}'
self.registry = registry if registry else TBMain.registry
self.engines = {}
self.runtimes = {}

def __str__(self):
return f"TBMain instance {self.name}"
Expand All @@ -32,41 +32,41 @@ def model(self,
):
model_parts = model.split('/')
if len(model_parts) > 1:
engine_class = model_parts.pop(0)
runtime_class = model_parts.pop(0)
model = '/'.join(model_parts)
else:
engine_class = self.registry.get_engine_by_model(model)
if engine_class is None:
raise Exception(f"Can't find engine for model '{model}'")
engine = self.engine(engine_class, **kwargs)
if engine is None:
raise Exception(f"Can't find engine for engine class '{engine_class}'")
return engine.model(model)
runtime_class = self.registry.get_runtime_by_model(model)
if runtime_class is None:
raise Exception(f"Can't find runtime for model '{model}'")
runtime = self.runtime(runtime_class)
if runtime is None:
raise Exception(f"Can't find runtime for runtime class '{runtime_class}'")
return runtime.model(model, **kwargs)

def chat(self,
model: str,
**kwargs,
):
return self.model(model).chat(**kwargs)

def engine(self,
engine_class: str,
def runtime(self,
runtime_class: str,
**kwargs,
):
if engine_class in self.engines:
return self.engines[engine_class]
if runtime_class in self.runtimes:
return self.runtimes[runtime_class]
try:
from importlib import import_module
from_list = [f"TBEngine{engine_class.title()}"]
mod = import_module(f".engine.{engine_class}", package=__package__)
self.engines[engine_class] = getattr(mod, from_list[0])(**kwargs)
from_list = [f"TBRuntime{runtime_class.title()}"]
mod = import_module(f".runtime.{runtime_class}", package=__package__)
self.runtimes[runtime_class] = getattr(mod, from_list[0])(**kwargs)
except ImportError:
mod = import_module(f"tackleberry.engine.{engine_class}")
self.engines[engine_class] = getattr(mod, f"TBEngine{engine_class.title()}")(**kwargs)
if isinstance(self.engines[engine_class], TBEngine):
return self.engines[engine_class]
mod = import_module(f"tackleberry.runtime.{runtime_class}")
self.runtimes[runtime_class] = getattr(mod, f"TBRuntime{runtime_class.title()}")(**kwargs)
if isinstance(self.runtimes[runtime_class], TBRuntime):
return self.runtimes[runtime_class]
else:
raise Exception(f"Can't find engine '{engine_class}'")
raise Exception(f"Can't find runtime '{runtime_class}'")

TB = TBMain()

Expand Down
12 changes: 12 additions & 0 deletions tackleberry/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .runtime import TBRuntime

class TBModel:

def __init__(self, runtime: TBRuntime, name: str, **kwargs):
self.runtime = runtime
self.name = name
self.options = kwargs

def chat(self, **kwargs):
from .chat import TBChat
return TBChat(self, **kwargs)
20 changes: 0 additions & 20 deletions tackleberry/model/__init__.py

This file was deleted.

11 changes: 0 additions & 11 deletions tackleberry/model/base.py

This file was deleted.

Loading

0 comments on commit 1fc30e6

Please sign in to comment.