Implementation:Run llama Llama index AzureOpenAIFinetuneEngine
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:
- Stores all parameters as instance attributes.
- Creates a
SyncAzureOpenAIclient using three environment variables:AZURE_OPENAI_ENDPOINT-- the Azure endpoint URLAZURE_OPENAI_API_KEY-- the API key (optional, defaults toNone)OPENAI_API_VERSION-- the API version string (defaults to"2024-02-01")
- If
start_job_idis provided, immediately retrieves the corresponding job object from the Azure API and stores it inself._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:
- Calls
finetuning_handler.save_finetuning_events(data_path)to persist the captured events as a JSONL file. - Returns a new
AzureOpenAIFinetuneEngineinstance constructed with the givenbase_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:
- Retrieves the current job via
self.get_current_job()(inherited from base class). - Validates that the job has a
fine_tuned_modelID and its status is"succeeded". RaisesValueErrorif either check fails. - 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}. - The deployment payload specifies a standard SKU with capacity 1, using the OpenAI format with version
"1". - 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:
- Retrieves the current job via
self.get_current_job(). - Constructs and returns an
AzureOpenAILLM object using the providedengineparameter (orcurrent_job.fine_tuned_modelas 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_handlerto return an Azure-typed instance.get_finetuned_modelto return anAzureOpenAILLM (which requires anengineparameter) instead of a standardOpenAILLM.
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
- Run_llama_Llama_index_MistralAIFinetuneEngine -- MistralAI fine-tuning engine implementation
- Run_llama_Llama_index_CohereRerankerFinetuneEngine -- Cohere reranker fine-tuning engine implementation