💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Create model registry, closes #126
davidmezzetti3dfcb522caf6
diff --git a/src/python/txtai/models/__init__.py b/src/python/txtai/models/__init__.pyindex 9d73f17c..cc099292 100644--- a/src/python/txtai/models/__init__.py+++ b/src/python/txtai/models/__init__.py@@ -5,3 +5,4 @@ Models importsfrom .models import Modelsfrom .onnx import OnnxModelfrom .pooling import MeanPooling, Pooling+from .registry import Registrydiff --git a/src/python/txtai/models/onnx.py b/src/python/txtai/models/onnx.pyindex 57d81e86..ef2d1b94 100644--- a/src/python/txtai/models/onnx.py+++ b/src/python/txtai/models/onnx.py@@ -2,8 +2,6 @@ONNX module"""-from collections import namedtuple-# Conditional importtry:from onnxruntime import InferenceSession, SessionOptions@@ -17,15 +15,11 @@ import torchfrom transformers import AutoConfigfrom transformers.configuration_utils import PretrainedConfig-from transformers.models.auto.modeling_auto import (- MODEL_MAPPING,- MODEL_FOR_QUESTION_ANSWERING_MAPPING,- MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,-)-from transformers.models.auto.tokenization_auto import TOKENIZER_MAPPINGfrom transformers.modeling_outputs import SequenceClassifierOutputfrom transformers.modeling_utils import PreTrainedModel+from .registry import Registry+# pylint: disable=W0223class OnnxModel(PreTrainedModel):"""@@ -51,33 +45,7 @@ class OnnxModel(PreTrainedModel):self.model = InferenceSession(model, SessionOptions())# Add references for this class to supported AutoModel classes- name = self.__class__.__name__- if name not in MODEL_MAPPING:- for mapping in [MODEL_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING]:- self.autoadd(mapping, name, self.__class__)-- # Add references for this class to support pipeline AutoTokenizers- if type(self.config) not in TOKENIZER_MAPPING:- self.autoadd(TOKENIZER_MAPPING, type(self.config), type(self.config).__name__)-- def autoadd(self, mapping, key, value):- """- Adds OnnxModel to auto model configuration to fully support pipelines.-- Args:- mapping: auto model mapping- key: key to add- value: value to add- """-- # pylint: disable=W0212- Params = namedtuple("Params", ["config", "model"])- params = Params(key, value)-- mapping._modules[key] = params- mapping._config_mapping[key] = "config"- mapping._reverse_config_mapping[value] = key- mapping._model_mapping[key] = "model"+ Registry.register(self)def forward(self, **inputs):"""diff --git a/src/python/txtai/models/registry.py b/src/python/txtai/models/registry.pynew file mode 100644index 00000000..490edb2f--- /dev/null+++ b/src/python/txtai/models/registry.py@@ -0,0 +1,57 @@+"""+Registry module+"""++from collections import namedtuple++from transformers.models.auto.modeling_auto import (+ MODEL_MAPPING,+ MODEL_FOR_QUESTION_ANSWERING_MAPPING,+ MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,+)+from transformers.models.auto.tokenization_auto import TOKENIZER_MAPPING+++class Registry:+ """+ Methods to register models and fully support pipelines.+ """++ @staticmethod+ def register(model):+ """+ Registers a model with auto model and tokenizer configuration to fully support pipelines.++ Args:+ model: model to register+ """++ # Add references for this class to supported AutoModel classes+ name = model.__class__.__name__+ if name not in MODEL_MAPPING:+ for mapping in [MODEL_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING]:+ Registry.autoadd(mapping, name, model.__class__)++ # Add references for this class to support pipeline AutoTokenizers+ if hasattr(model, "config") and type(model.config) not in TOKENIZER_MAPPING:+ Registry.autoadd(TOKENIZER_MAPPING, type(model.config), type(model.config).__name__)++ @staticmethod+ def autoadd(mapping, key, value):+ """+ Adds auto model configuration to fully support pipelines.++ Args:+ mapping: auto model mapping+ key: key to add+ value: value to add+ """++ # pylint: disable=W0212+ Params = namedtuple("Params", ["config", "model"])+ params = Params(key, value)++ mapping._modules[key] = params+ mapping._config_mapping[key] = "config"+ mapping._reverse_config_mapping[value] = key+ mapping._model_mapping[key] = "model"