Learner Models

AutoLearner

class ontolearner.base.learner.AutoLearner(**kwargs: Any)[source]

Bases: ABC

Abstract base class for ontology learning models.

This class defines the standard interface for all learning models in OntoLearner, including retrieval-based, LLM-based, and hybrid approaches. All concrete learner implementations must inherit from this class and implement the required methods.

Initialize the learner with optional configuration parameters.

Parameters:

**kwargs – Arbitrary keyword arguments for learner configuration. Specific parameters depend on the concrete implementation.

fit(train_data: Any, task: str, ontologizer: bool = True)

Train the learner on the provided training data for a specific task.

This method adapts the learner to the training data, which may involve indexing examples for retrieval, fine-tuning models, or preparing task-specific components.

Parameters:
  • train_data – Training data containing ontological information. Format depends on the specific task and implementation.

  • task – The ontology learning task to train for. Supported tasks: - “term-typing”: Predict semantic types for terms - “taxonomy-discovery”: Identify hierarchical relationships - “non-taxonomy-discovery”: Identify non-hierarchical relationships - “text2onto” : Extract ontology terms and their semantic types from documents

Raises:

NotImplementedError – If not implemented by concrete class.

fit_predict(train_data: Any, eval_data: Any, task: str) Any

Train the learner and make predictions in a single step.

This convenience method combines the fit and predict operations, which is useful for workflows that don’t require separate training and prediction phases.

Parameters:
  • train_data – Training data for the learner.

  • eval_data – Evaluation data to make predictions on.

  • task – The ontology learning task to perform.

Returns:

Predictions from the predict method.

load(**kwargs: Any)

Load pre-trained models, embeddings, or other required components.

This method should initialize all necessary components for the learner, such as loading pre-trained language models, embedding models, or other resources required for the specific learning approach.

Parameters:

**kwargs – Configuration parameters for loading components. Common parameters include model_id, token, device, etc.

Raises:

NotImplementedError – If not implemented by concrete class.

predict(eval_data: Any, task: str, ontologizer: bool = True) Any

Make predictions on evaluation data for a specific task.

This method applies the trained learner to new data and returns predictions in a format appropriate for the specified task.

Parameters:
  • eval_data – Evaluation data to make predictions on. Format depends on the specific task and implementation.

  • task – The ontology learning task to perform predictions for. Must match the task used during training.

Returns:

  • term-typing: List of predicted types for each term

  • taxonomy-discovery: Boolean predictions for relationships

  • non-taxonomy-discovery: Predicted relation types

  • text2onto : Extract ontology terms and their semantic types from documents

Return type:

Predictions in a format appropriate for the task

Raises:

NotImplementedError – If not implemented by concrete class.

tasks_data_former(data: Any, task: str, test: bool = False) List[str | Dict[str, str]]
tasks_ground_truth_former(data: Any, task: str) List[Dict[str, str]]

AutoLLM

class ontolearner.base.learner.AutoLLM(label_mapper: Any, device: str = 'cpu', token: str = '', max_length: int = 512)[source]

Bases: ABC

Abstract base class for Large Language Model components.

This class defines the interface for LLM components used in ontology learning. It provides a standardized way to load and interact with various language models from different providers (Hugging Face, OpenAI, etc.).

model

The loaded language model instance.

tokenizer

The tokenizer associated with the model.

Initialize the LLM component.

Sets up the basic structure with model and tokenizer attributes that will be populated when load() is called.

generate(inputs: List[str], max_new_tokens: int = 50) List[str]

Generate text responses for the given input prompts.

This method processes a batch of input prompts and generates corresponding text responses using the loaded language model. It uses optimized settings for consistent, high-quality generation suitable for ontology learning tasks.

Generation Settings: - Temperature: 0.1 (low randomness for consistent outputs) - Padding: Automatic handling for batch processing - Device placement: Automatic GPU/CPU selection

Parameters:
  • inputs – List of input prompts to generate responses for. Each prompt should be a complete, well-formatted string.

  • max_new_tokens – Maximum number of new tokens to generate per input. Shorter values encourage concise responses.

Returns:

List of generated text responses, one for each input prompt. Responses include the original input plus generated continuation.

load(model_id: str) None

Load a language model and its associated tokenizer.

This method should initialize the model and tokenizer from the specified model identifier, handling authentication, device placement, and other configuration as needed.

Parameters:
  • model_id – Identifier for the model to load (e.g., Hugging Face model ID).

  • **kwargs – Additional configuration parameters such as: - token: Authentication token for gated models - device: Target device for model placement - torch_dtype: Data type for model weights

Raises:

NotImplementedError – If not implemented by concrete class.

AutoRetriever

class ontolearner.base.learner.AutoRetriever[source]

Bases: ABC

Abstract base class for retrieval components.

This class defines the interface for retrieval components used in ontology learning. Retrievers are responsible for finding semantically similar examples from training data to provide context for language models or to make direct predictions.

Initialize the retriever component.

Sets up the basic structure with a model attribute that will be populated when load() is called.

index(inputs: List[str])

Index the provided inputs for efficient retrieval.

This method processes and indexes the training examples to enable fast similarity search during retrieval operations.

Parameters:

inputs – List of examples to index. Format depends on the specific retrieval implementation and task requirements.

Raises:

NotImplementedError – If not implemented by concrete class.

load(model_id: str) None

Load a retrieval/embedding model.

This method should initialize the embedding model from the specified model identifier, preparing it for encoding and similarity computation.

Parameters:
  • model_id – Identifier for the model to load (e.g., sentence-transformers model ID).

  • **kwargs – Additional configuration parameters such as: - device: Target device for model placement - cache_folder: Directory for caching model files

Raises:

NotImplementedError – If not implemented by concrete class.

retrieve(query: List[str], top_k: int = 5, batch_size: int = -1) List[List[str]]

Retrieve the top-k most similar examples for each query in a list of queries.

Parameters:
  • query – List of query examples.

  • top_k – Number of most similar examples to retrieve per query.

Returns:

A list of lists, where each sublist contains the top-k most similar examples for the corresponding query.

AutoPrompt

class ontolearner.base.learner.AutoPrompt(prompt_template: str)[source]

Bases: ABC

Abstract base class for prompt formatting components.

This class defines the interface for prompt templates used in ontology learning tasks. Prompts are responsible for formatting input data and context into appropriate text prompts for language models.

prompt_template

The template string used for formatting prompts.

Initialize the prompt component with a template.

Parameters:

prompt_template – Template string with placeholders for dynamic content. Should use Python string formatting syntax (e.g., {term}, {context}).

format(**kwargs: Any) str

Format the prompt template with the provided arguments.

This method takes the template and fills in the placeholders with the provided keyword arguments to create a complete prompt.

Parameters:

**kwargs – Keyword arguments to fill template placeholders. Common arguments include: - term: Term to predict types for - context: Retrieved examples or additional context - parent/child: Concepts for taxonomy discovery - head/tail: Entities for relation discovery

Returns:

Formatted prompt string ready for language model input.

Raises:

NotImplementedError – If not implemented by concrete class.

StandardizedPrompting

class ontolearner.learner.prompt.StandardizedPrompting(task: str | None = None)[source]

Bases: AutoPrompt

Initialize the prompt component with a template.

Parameters:

prompt_template – Template string with placeholders for dynamic content. Should use Python string formatting syntax (e.g., {term}, {context}).

LabelMapper

class ontolearner.learner.label_mapper.LabelMapper(classifier: Any = LogisticRegression(), ngram_range: Tuple = (1, 1), label_dict: Dict[str, List[str]] | None = None, analyzer: str = 'word', iterator_no: int = 1000)[source]

Bases: object

LabelMapper subclass using a TF-IDF vectorizer and a classifier for label prediction.

Initializes the TFIDFLabelMapper with a specified classifier and TF-IDF configuration.

Parameters:
  • classifier (Any) – Classifier object (e.g., LogisticRegression, SVC).

  • ngram_range (Tuple) – Range of n-grams for the TF-IDF vectorizer.

  • label_dict (Dict[str, List[str]]) – Dictionary mapping each label to a list of candidate phrases.

  • analyzer (str) – Specifies whether to analyze at the ‘word’ or ‘char’ level.

  • iterator_no (int) – Number of iterations to replicate training data.

fit()

Fits the TF-IDF pipeline on the training data.

predict(X: List[str]) List[str]

Predicts labels for the given input using the TF-IDF pipeline.

Parameters:

X (List[str]) – List of input texts to classify.

Returns:

Predicted labels.

Return type:

List[str]

validate_predicts(preds: List[str])

Validates if predictions are among valid labels.

Parameters:

preds (List[str]) – List of predicted labels.

AutoLLMLearner

class ontolearner.learner.llm.AutoLLMLearner(prompting, label_mapper, llm: ~ontolearner.base.learner.AutoLLM = <class 'ontolearner.base.learner.AutoLLM'>, token: str = '', max_new_tokens: int = 5, batch_size: int = 10, device='cpu')[source]

Bases: AutoLearner

Initialize the learner with optional configuration parameters.

Parameters:

**kwargs – Arbitrary keyword arguments for learner configuration. Specific parameters depend on the concrete implementation.

load(model_id: str = 'mistralai/Mistral-7B-Instruct-v0.1', **kwargs: Any)

Load pre-trained models, embeddings, or other required components.

This method should initialize all necessary components for the learner, such as loading pre-trained language models, embedding models, or other resources required for the specific learning approach.

Parameters:

**kwargs – Configuration parameters for loading components. Common parameters include model_id, token, device, etc.

Raises:

NotImplementedError – If not implemented by concrete class.

AutoRetrieverLearner

class ontolearner.learner.retriever.AutoRetrieverLearner(base_retriever: ~typing.Any = <ontolearner.base.learner.AutoRetriever object>, top_k: int = 5, batch_size: int = -1)[source]

Bases: AutoLearner

Initialize the learner with optional configuration parameters.

Parameters:

**kwargs – Arbitrary keyword arguments for learner configuration. Specific parameters depend on the concrete implementation.

load(model_id: str = 'sentence-transformers/all-MiniLM-L6-v2')

Load pre-trained models, embeddings, or other required components.

This method should initialize all necessary components for the learner, such as loading pre-trained language models, embedding models, or other resources required for the specific learning approach.

Parameters:

**kwargs – Configuration parameters for loading components. Common parameters include model_id, token, device, etc.

Raises:

NotImplementedError – If not implemented by concrete class.

AutoRAGLearner

class ontolearner.learner.rag.AutoRAGLearner(retriever: Any, llm: Any)[source]

Bases: AutoLearner

Initialize the learner with optional configuration parameters.

Parameters:

**kwargs – Arbitrary keyword arguments for learner configuration. Specific parameters depend on the concrete implementation.

load(retriever_id: str = 'sentence-transformers/all-MiniLM-L6-v2', llm_id: str = 'mistralai/Mistral-7B-Instruct-v0.1')

Load pre-trained models, embeddings, or other required components.

This method should initialize all necessary components for the learner, such as loading pre-trained language models, embedding models, or other resources required for the specific learning approach.

Parameters:

**kwargs – Configuration parameters for loading components. Common parameters include model_id, token, device, etc.

Raises:

NotImplementedError – If not implemented by concrete class.

AutoRetriever

class ontolearner.base.learner.AutoRetriever[source]

Bases: ABC

Abstract base class for retrieval components.

This class defines the interface for retrieval components used in ontology learning. Retrievers are responsible for finding semantically similar examples from training data to provide context for language models or to make direct predictions.

Initialize the retriever component.

Sets up the basic structure with a model attribute that will be populated when load() is called.

index(inputs: List[str])

Index the provided inputs for efficient retrieval.

This method processes and indexes the training examples to enable fast similarity search during retrieval operations.

Parameters:

inputs – List of examples to index. Format depends on the specific retrieval implementation and task requirements.

Raises:

NotImplementedError – If not implemented by concrete class.

load(model_id: str) None

Load a retrieval/embedding model.

This method should initialize the embedding model from the specified model identifier, preparing it for encoding and similarity computation.

Parameters:
  • model_id – Identifier for the model to load (e.g., sentence-transformers model ID).

  • **kwargs – Additional configuration parameters such as: - device: Target device for model placement - cache_folder: Directory for caching model files

Raises:

NotImplementedError – If not implemented by concrete class.

retrieve(query: List[str], top_k: int = 5, batch_size: int = -1) List[List[str]]

Retrieve the top-k most similar examples for each query in a list of queries.

Parameters:
  • query – List of query examples.

  • top_k – Number of most similar examples to retrieve per query.

Returns:

A list of lists, where each sublist contains the top-k most similar examples for the corresponding query.