Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index EmbeddingFinetuneEngine Get Model

From Leeroopedia

Overview

The get_finetuned_model method is provided by both SentenceTransformersFinetuneEngine and EmbeddingAdapterFinetuneEngine. It loads the finetuned model from disk and returns it as a BaseEmbedding instance ready for use in LlamaIndex retrieval pipelines. Each engine uses a different loading strategy appropriate to its finetuning approach.

Source Locations

SentenceTransformersFinetuneEngine.get_finetuned_model

Property Value
File llama-index-finetuning/llama_index/finetuning/embeddings/sentence_transformer.py
Lines 106-109
Class SentenceTransformersFinetuneEngine
Method get_finetuned_model(**model_kwargs) -> BaseEmbedding

EmbeddingAdapterFinetuneEngine.get_finetuned_model

Property Value
File llama-index-finetuning/llama_index/finetuning/embeddings/adapter.py
Lines 174-178
Class EmbeddingAdapterFinetuneEngine
Method get_finetuned_model(**model_kwargs) -> BaseEmbedding

Method Signatures

Sentence Transformers Implementation

def get_finetuned_model(self, **model_kwargs: Any) -> BaseEmbedding:
    """Gets finetuned model."""
    embed_model_str = "local:" + self.model_output_path
    return resolve_embed_model(embed_model_str)

Adapter Implementation

def get_finetuned_model(self, **model_kwargs: Any) -> BaseEmbedding:
    """Get finetuned model."""
    return AdapterEmbeddingModel(
        self.embed_model, self._model_output_path, **model_kwargs
    )

Parameters

Parameter Type Description
**model_kwargs Any Additional keyword arguments passed to the model loader. For Sentence Transformers, these are not used in the current implementation. For adapters, they are forwarded to AdapterEmbeddingModel.

Return Value

Both implementations return a BaseEmbedding instance:

  • Sentence Transformers -- Returns the resolved embedding model loaded from the local path via resolve_embed_model("local:{path}")
  • Adapter -- Returns an AdapterEmbeddingModel that wraps the base embedding model with the trained adapter weights

Internal Behavior

Sentence Transformers Path

  1. Constructs the model identifier string: "local:" + self.model_output_path
  2. Passes it to resolve_embed_model() from llama_index.core.embeddings.utils
  3. resolve_embed_model detects the "local:" prefix, strips it, and loads the Sentence Transformer model from the local directory
  4. The loaded model is wrapped in a LlamaIndex-compatible BaseEmbedding subclass
  5. The returned object can be used directly with Settings.embed_model, VectorStoreIndex, or any other LlamaIndex component expecting an embedding model

Adapter Path

  1. Creates an AdapterEmbeddingModel with the original base embedding model and the adapter output path
  2. The AdapterEmbeddingModel loads the trained adapter weights from self._model_output_path
  3. At inference time, text is first embedded by self.embed_model (the base model), then the embedding is transformed by the adapter layer
  4. The result is returned as a standard BaseEmbedding interface

Usage Example

from llama_index.finetuning import (
    SentenceTransformersFinetuneEngine,
    EmbeddingQAFinetuneDataset,
)

# After finetuning
train_dataset = EmbeddingQAFinetuneDataset.from_json("train_dataset.json")
finetune_engine = SentenceTransformersFinetuneEngine(
    dataset=train_dataset,
    model_id="BAAI/bge-small-en",
    model_output_path="finetuned_model",
)
finetune_engine.finetune()

# Load the finetuned model
embed_model = finetune_engine.get_finetuned_model()

# Use it for embedding
embedding = embed_model.get_text_embedding("sample text")
query_embedding = embed_model.get_query_embedding("sample query")

Adapter Example

from llama_index.finetuning import EmbeddingAdapterFinetuneEngine
from llama_index.embeddings.openai import OpenAIEmbedding

base_embed = OpenAIEmbedding()
adapter_engine = EmbeddingAdapterFinetuneEngine(
    dataset=train_dataset,
    embed_model=base_embed,
    model_output_path="adapter_output",
)
adapter_engine.finetune()

# Load adapter model (wraps base + adapter)
ft_embed = adapter_engine.get_finetuned_model()

Dependencies

  • llama_index.core.embeddings.utils.resolve_embed_model -- Resolves model identifier strings to BaseEmbedding instances (Sentence Transformers path)
  • llama_index.embeddings.adapter.AdapterEmbeddingModel -- Composite model wrapping base embedding + adapter (adapter path)
  • llama_index.core.base.embeddings.base.BaseEmbedding -- Return type interface

Knowledge Sources

LlamaIndex Finetuning Source LlamaIndex Embedding Finetuning Guide

Metadata

Machine Learning Embeddings Finetuning Model Serialization LlamaIndex

Principle:Run_llama_Llama_index_Finetuned_Embedding_Loading

2026-02-11 00:00 GMT

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment