-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generic model creation and mistral support started
- Loading branch information
1 parent
86d2042
commit cecbf7e
Showing
8 changed files
with
119 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .base import Model | ||
from .models import get_model | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
import re | ||
|
||
from transformers import PreTrainedTokenizerFast | ||
|
||
from libertai_agents.interfaces import ToolCallFunction | ||
from libertai_agents.models.base import Model | ||
|
||
|
||
class HermesModel(Model): | ||
tokenizer: PreTrainedTokenizerFast | ||
vm_url: str | ||
|
||
def __init__(self, model_id: str, vm_url: str): | ||
super().__init__(model_id, vm_url) | ||
|
||
@staticmethod | ||
def extract_tool_calls_from_response(response: str) -> list[ToolCallFunction]: | ||
tool_calls = re.findall("<tool_call>\s*(.*)\s*</tool_call>", response) | ||
return [ToolCallFunction(**json.loads(call)) for call in tool_calls] | ||
|
||
def generate_tool_call_id(self) -> str | None: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
import re | ||
|
||
from transformers import PreTrainedTokenizerFast | ||
|
||
from libertai_agents.interfaces import ToolCallFunction | ||
from libertai_agents.models.base import Model | ||
|
||
|
||
class MistralModel(Model): | ||
tokenizer: PreTrainedTokenizerFast | ||
vm_url: str | ||
|
||
def __init__(self, model_id: str, vm_url: str): | ||
super().__init__(model_id, vm_url) | ||
|
||
@staticmethod | ||
def extract_tool_calls_from_response(response: str) -> list[ToolCallFunction]: | ||
tool_calls = re.findall("<tool_call>\s*(.*)\s*</tool_call>", response) | ||
return [ToolCallFunction(**json.loads(call)) for call in tool_calls] | ||
|
||
def generate_tool_call_id(self) -> str | None: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import typing | ||
|
||
from huggingface_hub import login | ||
Check failure on line 3 in libertai_agents/models/models.py
|
||
from pydantic import BaseModel | ||
|
||
from libertai_agents.models.base import Model | ||
from libertai_agents.models.hermes import HermesModel | ||
from libertai_agents.models.mistral import MistralModel | ||
|
||
|
||
class ModelConfiguration(BaseModel): | ||
vm_url: str | ||
constructor: typing.Type[Model] | ||
|
||
|
||
ModelId = typing.Literal[ | ||
"NousResearch/Hermes-2-Pro-Llama-3-8B", | ||
"NousResearch/Hermes-3-Llama-3.1-8B", | ||
"mistralai/Mistral-Nemo-Instruct-2407" | ||
] | ||
MODEL_IDS: list[ModelId] = list(typing.get_args(ModelId)) | ||
|
||
MODELS_CONFIG: dict[ModelId, ModelConfiguration] = { | ||
"NousResearch/Hermes-2-Pro-Llama-3-8B": ModelConfiguration( | ||
vm_url="https://curated.aleph.cloud/vm/84df52ac4466d121ef3bb409bb14f315de7be4ce600e8948d71df6485aa5bcc3/completion", | ||
constructor=HermesModel), | ||
"NousResearch/Hermes-3-Llama-3.1-8B": ModelConfiguration(vm_url="http://localhost:8080/completion", | ||
constructor=HermesModel), | ||
"mistralai/Mistral-Nemo-Instruct-2407": ModelConfiguration(vm_url="http://localhost:8080/completion", | ||
constructor=MistralModel) | ||
} | ||
|
||
|
||
def get_model(model_id: ModelId, hf_token: str | None = None) -> Model: | ||
model_configuration = MODELS_CONFIG.get(model_id) | ||
|
||
if model_configuration is None: | ||
raise ValueError(f'model_id must be one of {MODEL_IDS}') | ||
|
||
if hf_token is not None: | ||
login(hf_token) | ||
|
||
return model_configuration.constructor(model_id=model_id, **model_configuration.model_dump(exclude={'constructor'})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters