🔍 Code Extractor

class AzureOpenAIChatLLM

Maturity: 50

Adapter class for interacting with Azure OpenAI's Chat Completions API, providing a simplified interface for generating chat responses using Azure-hosted OpenAI models.

File:
/tf/active/vicechatdev/docchat/llm_factory.py
Lines:
111 - 168
Complexity:
moderate

Purpose

This class serves as an adapter/wrapper around the Azure OpenAI Chat Completions API. It handles authentication, configuration, and provides a simple invoke() method to send prompts and receive responses. The class manages Azure-specific configuration including deployment names, endpoints, and API keys, and returns responses in a standardized LLMMessage format. It's designed to be used as part of a larger system that needs to interact with Azure OpenAI services.

Source Code

class AzureOpenAIChatLLM:
    """Adapter using Azure OpenAI Chat Completions API."""

    def __init__(self, deployment_name: str, api_key: Optional[str] = None, 
                 azure_endpoint: Optional[str] = None, temperature: float = 0.0, max_tokens: int = 4096):
        from openai import AzureOpenAI  # lazy import
        
        # Get Azure config from environment if not provided
        api_key = api_key or config.AZURE_OPENAI_API_KEY
        azure_endpoint = azure_endpoint or config.AZURE_OPENAI_ENDPOINT
        
        if not api_key or not azure_endpoint:
            raise ValueError("Azure OpenAI API key and endpoint must be configured")
        
        # Ensure the endpoint has the correct format
        if azure_endpoint.endswith('/'):
            azure_endpoint = azure_endpoint.rstrip('/')
        
        logger.info(f"Azure OpenAI endpoint: {azure_endpoint}")
        logger.info(f"Azure API key length: {len(api_key)}")
        
        self.client = AzureOpenAI(
            api_key=api_key,
            api_version="2024-08-01-preview",
            azure_endpoint=azure_endpoint
        )
        self.deployment_name = deployment_name
        self.temperature = temperature
        self.max_tokens = max_tokens
        
        logger.info(f"Azure OpenAI client initialized with base URL: {self.client._base_url}")
        logger.info(f"Deployment name: {deployment_name}")

    @property
    def model_name(self) -> str:
        return f"azure-{self.deployment_name}"

    def invoke(self, prompt: str) -> LLMMessage:
        logger.info(f"Azure OpenAI invoke with deployment: {self.deployment_name}")
        
        try:
            params = {
                "model": self.deployment_name,  # Azure uses deployment name
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": self.max_tokens,
                "temperature": self.temperature
            }
            
            logger.info(f"Azure OpenAI request params: model={self.deployment_name}, max_tokens={self.max_tokens}, temperature={self.temperature}")
            
            resp = self.client.chat.completions.create(**params)
            content = resp.choices[0].message.content or ''
            return LLMMessage(content=content.strip())
        except Exception as e:
            logger.error(f"Azure OpenAI error: {e}")
            raise
        content = resp.choices[0].message.content or ''
        return LLMMessage(content=content.strip())

Parameters

Name Type Default Kind
bases - -

Parameter Details

deployment_name: The name of the Azure OpenAI deployment to use. This is the deployment identifier configured in your Azure OpenAI resource, not the model name itself. Required parameter.

api_key: Azure OpenAI API key for authentication. If not provided, will attempt to read from config.AZURE_OPENAI_API_KEY. Must be a valid Azure OpenAI API key string.

azure_endpoint: The Azure OpenAI endpoint URL (e.g., 'https://your-resource.openai.azure.com'). If not provided, will attempt to read from config.AZURE_OPENAI_ENDPOINT. Trailing slashes are automatically removed.

temperature: Controls randomness in the model's output. Range 0.0 to 2.0, where 0.0 is deterministic and higher values increase randomness. Default is 0.0 for consistent outputs.

max_tokens: Maximum number of tokens to generate in the completion. Default is 4096. Controls the length of the response.

Return Value

The __init__ method returns an instance of AzureOpenAIChatLLM. The invoke() method returns an LLMMessage object containing the generated response content as a string. The model_name property returns a string in the format 'azure-{deployment_name}'.

Class Interface

Methods

__init__(self, deployment_name: str, api_key: Optional[str] = None, azure_endpoint: Optional[str] = None, temperature: float = 0.0, max_tokens: int = 4096)

Purpose: Initializes the Azure OpenAI client with authentication credentials and configuration parameters

Parameters:

  • deployment_name: Azure OpenAI deployment name to use for API calls
  • api_key: Optional Azure OpenAI API key, falls back to config.AZURE_OPENAI_API_KEY if not provided
  • azure_endpoint: Optional Azure endpoint URL, falls back to config.AZURE_OPENAI_ENDPOINT if not provided
  • temperature: Sampling temperature for response generation (0.0-2.0), default 0.0
  • max_tokens: Maximum tokens in generated response, default 4096

Returns: None (constructor)

model_name(self) -> str property

Purpose: Returns a formatted string representing the model name with 'azure-' prefix

Returns: String in format 'azure-{deployment_name}'

invoke(self, prompt: str) -> LLMMessage

Purpose: Sends a prompt to the Azure OpenAI API and returns the generated response

Parameters:

  • prompt: The user prompt/question to send to the model

Returns: LLMMessage object containing the model's response content as a stripped string

Attributes

Name Type Description Scope
client AzureOpenAI The Azure OpenAI client instance used to make API calls instance
deployment_name str The Azure OpenAI deployment name used for API requests instance
temperature float The temperature parameter controlling response randomness instance
max_tokens int Maximum number of tokens to generate in responses instance

Dependencies

  • openai
  • logging
  • dataclasses
  • typing
  • config

Required Imports

from dataclasses import dataclass
from typing import Optional
import logging
import config

Conditional/Optional Imports

These imports are only needed under specific conditions:

from openai import AzureOpenAI

Condition: imported lazily inside __init__ method when the class is instantiated

Required (conditional)

Usage Example

# Ensure config module has Azure credentials
# config.AZURE_OPENAI_API_KEY = 'your-api-key'
# config.AZURE_OPENAI_ENDPOINT = 'https://your-resource.openai.azure.com'

from your_module import AzureOpenAIChatLLM

# Instantiate with deployment name
llm = AzureOpenAIChatLLM(
    deployment_name='gpt-4',
    temperature=0.7,
    max_tokens=2000
)

# Or provide credentials explicitly
llm = AzureOpenAIChatLLM(
    deployment_name='gpt-4',
    api_key='your-azure-api-key',
    azure_endpoint='https://your-resource.openai.azure.com',
    temperature=0.0,
    max_tokens=4096
)

# Get the model name
print(llm.model_name)  # Output: 'azure-gpt-4'

# Invoke the model with a prompt
response = llm.invoke('What is the capital of France?')
print(response.content)  # Access the response content

Best Practices

  • Always ensure Azure OpenAI credentials are properly configured before instantiation, either via config module or constructor parameters
  • Handle exceptions when calling invoke() as network errors or API issues may occur
  • The class performs lazy import of AzureOpenAI, so the openai package must be installed but won't be imported until instantiation
  • Use temperature=0.0 for deterministic outputs, higher values for more creative responses
  • The deployment_name must match an actual deployment in your Azure OpenAI resource
  • Monitor max_tokens to control response length and API costs
  • The class logs extensively, ensure logging is configured appropriately for debugging
  • Endpoint URLs should not have trailing slashes (automatically handled by the class)
  • The class is stateless after initialization - safe to reuse for multiple invoke() calls
  • API version is hardcoded to '2024-08-01-preview' - may need updates for newer API versions

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class OpenAIChatLLM 78.3% similar

    Adapter class for interacting with OpenAI's Chat Completions API, supporting both GPT-4 and GPT-5 model families with automatic parameter adjustment based on model type.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
  • class LLMClient_v1 73.9% similar

    A client class for interacting with Large Language Models (LLMs), specifically designed to work with OpenAI's chat completion API.

    From: /tf/active/vicechatdev/QA_updater/core/llm_client.py
  • class OpenAIResponsesLLM 69.3% similar

    Adapter class for OpenAI's Responses API, specifically designed for GPT-5 family models with automatic fallback mechanisms to stable models when responses fail.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
  • class LLMClient 67.2% similar

    Multi-LLM client that provides a unified interface for interacting with OpenAI GPT-4o, Azure OpenAI, Google Gemini, and Anthropic Claude models.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • class LLMClient_v2 65.6% similar

    Client for interacting with LLM providers (OpenAI, Anthropic, Azure, etc.)

    From: /tf/active/vicechatdev/contract_validity_analyzer/utils/llm_client.py
← Back to Browse