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 AzureOpenAIFinetuneEngine

From Leeroopedia
Revision as of 11:47, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Run_llama_Llama_index_AzureOpenAIFinetuneEngine.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Overview

The AzureOpenAIFinetuneEngine class provides a fine-tuning engine for Azure OpenAI models. It extends the base OpenAIFinetuneEngine to support Azure-specific authentication, model deployment, and retrieval workflows. This module resides in the llama-index-finetuning package under the azure_openai submodule.

Source file: llama-index-finetuning/llama_index/finetuning/azure_openai/base.py (129 lines)

Dependencies

Dependency Purpose
logging Standard Python logging
json Serialization of deployment payloads
os Reading environment variables for Azure credentials
requests HTTP calls to the Azure Management REST API for model deployment
openai.AzureOpenAI (as SyncAzureOpenAI) Synchronous Azure OpenAI SDK client for file uploads and fine-tuning job management
llama_index.core.llms.llm.LLM Return type interface for fine-tuned models
llama_index.finetuning.callbacks.finetuning_handler.OpenAIFineTuningHandler Handler that captures fine-tuning events from LLM interactions
llama_index.finetuning.OpenAIFinetuneEngine Base class providing shared fine-tuning logic (file upload, job creation, job polling)
llama_index.llms.azure_openai.AzureOpenAI LlamaIndex Azure OpenAI LLM wrapper returned by get_finetuned_model

Class: AzureOpenAIFinetuneEngine

Inherits from: OpenAIFinetuneEngine

Constructor

def __init__(
    self,
    base_model: str,
    data_path: str,
    verbose: bool = False,
    start_job_id: Optional[str] = None,
    validate_json: bool = True,
) -> None
Parameter Type Default Description
base_model str required The Azure OpenAI model identifier to fine-tune (e.g., "gpt-4o-mini")
data_path str required Path to the JSONL training data file
verbose bool False Enable verbose logging output
start_job_id Optional[str] None If provided, resumes tracking of an existing fine-tuning job by its ID
validate_json bool True Whether to validate the JSON training data before uploading

Initialization behavior:

  1. Stores all parameters as instance attributes.
  2. Creates a SyncAzureOpenAI client using three environment variables:
    • AZURE_OPENAI_ENDPOINT -- the Azure endpoint URL
    • AZURE_OPENAI_API_KEY -- the API key (optional, defaults to None)
    • OPENAI_API_VERSION -- the API version string (defaults to "2024-02-01")
  3. If start_job_id is provided, immediately retrieves the corresponding job object from the Azure API and stores it in self._start_job.

Class Method: from_finetuning_handler

@classmethod
def from_finetuning_handler(
    cls,
    finetuning_handler: OpenAIFineTuningHandler,
    base_model: str,
    data_path: str,
    **kwargs: Any,
) -> "AzureOpenAIFinetuneEngine"

Factory method that initializes the engine from an OpenAIFineTuningHandler. This is used to fine-tune an Azure OpenAI model based on events captured during LLM interactions (e.g., fine-tuning gpt-4o-mini on top of gpt-4o outputs).

Workflow:

  1. Calls finetuning_handler.save_finetuning_events(data_path) to persist the captured events as a JSONL file.
  2. Returns a new AzureOpenAIFinetuneEngine instance constructed with the given base_model, data_path, and any additional keyword arguments.

Method: deploy_finetuned_model

def deploy_finetuned_model(
    self,
    token: str,
    subscription_id: str,
    resource_group: str,
    resource_name: str,
    model_deployment_name: Optional[str] = None,
) -> LLM

Deploys the fine-tuned model to Azure using the Azure Management REST API.

Parameter Type Description
token str Azure AD bearer token for authentication
subscription_id str Azure subscription ID for the OpenAI resource
resource_group str Azure resource group name
resource_name str Azure OpenAI resource name
model_deployment_name Optional[str] Custom deployment name; defaults to the fine-tuned model name

Workflow:

  1. Retrieves the current job via self.get_current_job() (inherited from base class).
  2. Validates that the job has a fine_tuned_model ID and its status is "succeeded". Raises ValueError if either check fails.
  3. Constructs a PUT request to the Azure Management endpoint at https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.CognitiveServices/accounts/{resource_name}/deployments/{model_deployment_name}.
  4. The deployment payload specifies a standard SKU with capacity 1, using the OpenAI format with version "1".
  5. Returns the JSON response from the Azure REST API.

Note: The return type annotation indicates LLM, but the method actually returns the raw JSON response dictionary from the deployment API call.

Method: get_finetuned_model

def get_finetuned_model(self, engine: str, **model_kwargs: Any) -> LLM

Returns a LlamaIndex AzureOpenAI LLM instance configured to use the fine-tuned model.

Parameter Type Description
engine str The deployment name corresponding to the fine-tuned model. If falsy, falls back to the fine-tuned model name from the job.
**model_kwargs Any Additional keyword arguments passed to the AzureOpenAI constructor

Workflow:

  1. Retrieves the current job via self.get_current_job().
  2. Constructs and returns an AzureOpenAI LLM object using the provided engine parameter (or current_job.fine_tuned_model as fallback).

Environment Variables

Variable Required Default Purpose
AZURE_OPENAI_ENDPOINT Yes none Azure OpenAI service endpoint URL
AZURE_OPENAI_API_KEY No None API key for Azure OpenAI authentication
OPENAI_API_VERSION No "2024-02-01" Azure OpenAI API version string

Relationship to Parent Class

AzureOpenAIFinetuneEngine extends OpenAIFinetuneEngine and overrides:

  • The constructor to use the Azure-specific SDK client (SyncAzureOpenAI) instead of the standard OpenAI client.
  • from_finetuning_handler to return an Azure-typed instance.
  • get_finetuned_model to return an AzureOpenAI LLM (which requires an engine parameter) instead of a standard OpenAI LLM.

It also introduces a new method, deploy_finetuned_model, that handles Azure-specific model deployment via the Azure Management REST API, which is not present in the parent class.

See Also

Page Connections

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