templates

This commit is contained in:
Adam Wilson
2025-08-16 16:00:35 -06:00
parent 11028c6b4e
commit 82c987404b
22 changed files with 318 additions and 81 deletions
@@ -6,12 +6,12 @@ from src.text_generation.common.model_id import ModelId
class AppleOpenELMFoundationModel(BaseFoundationModel):
"""Apple OpenELM 270M implementation"""
"""apple/OpenELM-3B-Instruct implementation"""
def __init__(self, config: AppleOpenELMConfig = AppleOpenELMConfig()):
self.config = config
super().__init__()
self.MODEL_ID = ModelId.APPLE_OPENELM_270M_INSTRUCT.value
self.MODEL_ID = ModelId.APPLE_OPENELM_3B_INSTRUCT.value
def _load_model(self) -> None:
self.tokenizer = AutoTokenizer.from_pretrained(
@@ -1,5 +1,5 @@
from transformers import pipeline
from langchain.llms import HuggingFacePipeline
from langchain_huggingface import HuggingFacePipeline
from typing import Dict, Any, List
from abc import ABC, abstractmethod
@@ -4,7 +4,7 @@ from src.text_generation.adapters.foundation_models.base.base_model_config impor
@dataclass
class MetaTinyLlamaConfig(BaseModelConfig):
"""TinyLlama-specific configuration"""
class MetaLlamaConfig(BaseModelConfig):
"""meta-llama/Llama-3.2-3B-Instruct configuration"""
use_flash_attention: bool = False
rope_scaling: Optional[Dict[str, Any]] = None
@@ -5,10 +5,10 @@ from langchain_huggingface import HuggingFacePipeline
from src.text_generation.adapters.foundation_models.apple_openelm_foundation_model import AppleOpenELMFoundationModel
from src.text_generation.adapters.foundation_models.base.base_foundation_model import BaseFoundationModel
from src.text_generation.adapters.foundation_models.base.base_model_config import BaseModelConfig
from src.text_generation.adapters.foundation_models.meta_tinyllama_foundation_model import MetaTinyLlamaFoundationModel
from src.text_generation.adapters.foundation_models.meta_llama_foundation_model import MetaLlamaFoundationModel
from src.text_generation.adapters.foundation_models.microsoft_phi3_foundation_model import MicrosoftPhi3FoundationModel
from src.text_generation.adapters.foundation_models.pipelines.apple_openelm_pipeline import AppleOpenELMPipeline
from src.text_generation.adapters.foundation_models.pipelines.meta_tinyllama_pipeline import MetaTinyLlamaPipeline
from src.text_generation.adapters.foundation_models.pipelines.meta_llama_pipeline import MetaLlamaPipeline
from src.text_generation.adapters.foundation_models.pipelines.microsoft_phi3mini_pipeline import MicrosoftPhi3MiniPipeline
from src.text_generation.common.model_id import ModelId
@@ -22,8 +22,8 @@ class FoundationModelFactory:
config = BaseModelConfig()
model_map = {
ModelId.APPLE_OPENELM_270M_INSTRUCT.value: AppleOpenELMFoundationModel,
ModelId.META_TINYLLAMA_1_1B_CHAT.value: MetaTinyLlamaFoundationModel,
ModelId.APPLE_OPENELM_3B_INSTRUCT.value: AppleOpenELMFoundationModel,
ModelId.META_LLAMA_3_2_3B_INSTRUCT.value: MetaLlamaFoundationModel,
ModelId.MICROSOFT_PHI_3_MINI4K_INSTRUCT.value: MicrosoftPhi3FoundationModel
}
@@ -37,8 +37,8 @@ class FoundationModelFactory:
"""Factory function to create the appropriate pipeline based on model name"""
pipeline_classes = {
ModelId.APPLE_OPENELM_270M_INSTRUCT.value: AppleOpenELMPipeline,
ModelId.META_TINYLLAMA_1_1B_CHAT.value: MetaTinyLlamaPipeline,
ModelId.APPLE_OPENELM_3B_INSTRUCT.value: AppleOpenELMPipeline,
ModelId.META_LLAMA_3_2_3B_INSTRUCT.value: MetaLlamaPipeline,
ModelId.MICROSOFT_PHI_3_MINI4K_INSTRUCT.value: MicrosoftPhi3MiniPipeline
}
@@ -3,7 +3,7 @@ from typing import Optional
from src.text_generation.adapters.foundation_models.apple_openelm_foundation_model import AppleOpenELMFoundationModel
from src.text_generation.adapters.foundation_models.base.base_foundation_model import BaseFoundationModel
from src.text_generation.adapters.foundation_models.base.base_model_config import BaseModelConfig
from src.text_generation.adapters.foundation_models.meta_tinyllama_foundation_model import MetaTinyLlamaFoundationModel
from src.text_generation.adapters.foundation_models.meta_llama_foundation_model import MetaLlamaFoundationModel
from src.text_generation.adapters.foundation_models.microsoft_phi3_foundation_model import MicrosoftPhi3FoundationModel
from src.text_generation.common.model_id import ModelId
@@ -17,8 +17,8 @@ class FoundationModelFactory:
config = BaseModelConfig()
model_map = {
ModelId.APPLE_OPENELM_270M_INSTRUCT.value: AppleOpenELMFoundationModel,
ModelId.META_TINYLLAMA_1_1B_CHAT.value: MetaTinyLlamaFoundationModel,
ModelId.APPLE_OPENELM_3B_INSTRUCT.value: AppleOpenELMFoundationModel,
ModelId.META_LLAMA_3_2_3B_INSTRUCT.value: MetaLlamaFoundationModel,
ModelId.MICROSOFT_PHI_3_MINI4K_INSTRUCT.value: MicrosoftPhi3FoundationModel
}
@@ -1,4 +1,4 @@
from src.text_generation.adapters.foundation_models.config.meta_tinyllama_config import MetaTinyLlamaConfig
from src.text_generation.adapters.foundation_models.config.meta_llama_config import MetaLlamaConfig
from src.text_generation.adapters.foundation_models.base.base_foundation_model import BaseFoundationModel
@@ -8,13 +8,13 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
from src.text_generation.common.model_id import ModelId
class MetaTinyLlamaFoundationModel(BaseFoundationModel):
"""Meta TinyLlama 1.1B implementation"""
class MetaLlamaFoundationModel(BaseFoundationModel):
"""meta-llama/Llama-3.2-3B-Instruct implementation"""
def __init__(self, config: MetaTinyLlamaConfig = MetaTinyLlamaConfig()):
def __init__(self, config: MetaLlamaConfig = MetaLlamaConfig()):
self.config = config
super().__init__()
self.MODEL_ID = ModelId.META_TINYLLAMA_1_1B_CHAT.value
self.MODEL_ID = ModelId.META_LLAMA_3_2_3B_INSTRUCT.value
def _load_model(self) -> None:
self.tokenizer = AutoTokenizer.from_pretrained(
@@ -4,11 +4,11 @@ from src.text_generation.adapters.foundation_models.base.base_model_pipeline imp
from typing import Any, Dict, List
class MetaTinyLlamaPipeline(BaseModelPipeline):
class MetaLlamaPipeline(BaseModelPipeline):
def get_model_specific_config(self) -> Dict[str, Any]:
return {
"max_new_tokens": 512,
# TinyLlama might need slightly different settings
# meta-llama/Llama-3.2-3B-Instruct might need slightly different settings
"top_p": 0.9, # Add nucleus sampling for better diversity
}
@@ -21,5 +21,5 @@ class PromptTemplateRepository(AbstractPromptTemplateRepository):
return None
def add(self, id: str, prompt_template: PromptTemplate) -> None:
if self.get(id) == None:
prompt_template.save(self._create_path_from_id(id))
print(f'Saving template: {id}')
prompt_template.save(self._create_path_from_id(id))
+17 -20
View File
@@ -6,16 +6,13 @@ class Constants:
PHI_3_USER_START_TOKEN = "<|user|>"
# OpenELM tokens
OPENELM_SYSTEM_START_TOKEN = "[INST]"
OPENELM_USER_START_TOKEN = "[INST]"
OPENELM_ASSISTANT_TOKEN = "[/INST]"
OPENELM_END_TOKEN = "</s>"
# TinyLlama tokens
TINYLLAMA_SYSTEM_START_TOKEN = "<|system|>"
TINYLLAMA_USER_START_TOKEN = "<|user|>"
TINYLLAMA_ASSISTANT_TOKEN = "<|assistant|>"
TINYLLAMA_END_TOKEN = "</s>"
# meta-llama/Llama-3.2-3B-Instruct tokens
LLAMA_SYSTEM_START_TOKEN = "### SYSTEM:\n"
LLAMA_USER_START_TOKEN = "### HUMAN:\n"
LLAMA_ASSISTANT_TOKEN = "### ASSISTANT:\n"
LLAMA_END_TOKEN = ""
INPUT_VARIABLE_TOKEN = "input"
ALT_COMPLETION_TEXT = "I can't answer that."
@@ -28,16 +25,16 @@ class Constants:
PHI_3_MINI_4K_INSTRUCT__04_FEW_SHOT_RAG_PLUS_COT = "phi-3-mini-4k-instruct.04-few-shot-rag-plus-cot"
PHI_3_MINI_4K_INSTRUCT__05_REFLEXION = "phi-3-mini-4k-instruct.05-reflexion"
# OpenELM templates
OPENELM_270M_INSTRUCT__01_BASIC = "openelm-270m-instruct.01-basic"
OPENELM_270M_INSTRUCT__02_ZERO_SHOT_CHAIN_OF_THOUGHT = "openelm-270m-instruct.02-zero-shot-cot"
OPENELM_270M_INSTRUCT__03_FEW_SHOT_EXAMPLES = "openelm-270m-instruct.03-few-shot"
OPENELM_270M_INSTRUCT__04_FEW_SHOT_RAG_PLUS_COT = "openelm-270m-instruct.04-few-shot-rag-plus-cot"
OPENELM_270M_INSTRUCT__05_REFLEXION = "openelm-270m-instruct.05-reflexion"
# OpenELM templates: apple/OpenELM-3B-Instruct
OPENELM_3B_INSTRUCT__01_BASIC = "openelm-3b-instruct.01-basic"
OPENELM_3B_INSTRUCT__02_ZERO_SHOT_CHAIN_OF_THOUGHT = "openelm-3b-instruct.02-zero-shot-cot"
OPENELM_3B_INSTRUCT__03_FEW_SHOT_EXAMPLES = "openelm-3b-instruct.03-few-shot"
OPENELM_3B_INSTRUCT__04_FEW_SHOT_RAG_PLUS_COT = "openelm-3b-instruct.04-few-shot-rag-plus-cot"
OPENELM_3B_INSTRUCT__05_REFLEXION = "openelm-3b-instruct.05-reflexion"
# TinyLlama templates
TINYLLAMA_1_1B_CHAT__01_BASIC = "tinyllama-1.1b-chat.01-basic"
TINYLLAMA_1_1B_CHAT__02_ZERO_SHOT_CHAIN_OF_THOUGHT = "tinyllama-1.1b-chat.02-zero-shot-cot"
TINYLLAMA_1_1B_CHAT__03_FEW_SHOT_EXAMPLES = "tinyllama-1.1b-chat.03-few-shot"
TINYLLAMA_1_1B_CHAT__04_FEW_SHOT_RAG_PLUS_COT = "tinyllama-1.1b-chat.04-few-shot-rag-plus-cot"
TINYLLAMA_1_1B_CHAT__05_REFLEXION = "tinyllama-1.1b-chat.05-reflexion"
# meta-llama/Llama-3.2-3B-Instruct templates
LLAMA_1_1B_CHAT__01_BASIC = "llama-3.2-3b-instruct.01-basic"
LLAMA_1_1B_CHAT__02_ZERO_SHOT_CHAIN_OF_THOUGHT = "llama-3.2-3b-instruct.02-zero-shot-cot"
LLAMA_1_1B_CHAT__03_FEW_SHOT_EXAMPLES = "llama-3.2-3b-instruct.03-few-shot"
LLAMA_1_1B_CHAT__04_FEW_SHOT_RAG_PLUS_COT = "llama-3.2-3b-instruct.04-few-shot-rag-plus-cot"
LLAMA_1_1B_CHAT__05_REFLEXION = "llama-3.2-3b-instruct.05-reflexion"
+2 -2
View File
@@ -2,6 +2,6 @@ from enum import Enum
class ModelId(Enum):
APPLE_OPENELM_270M_INSTRUCT = "apple/openelm-270m-instruct"
META_TINYLLAMA_1_1B_CHAT = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
APPLE_OPENELM_3B_INSTRUCT = "apple/OpenELM-3B-Instruct"
META_LLAMA_3_2_3B_INSTRUCT = "meta-llama/Llama-3.2-3B-Instruct"
MICROSOFT_PHI_3_MINI4K_INSTRUCT = "microsoft/Phi-3-mini-4k-instruct-onnx"