Class AzureOpenAIClient

java.lang.Object
com.tnsai.llm.providers.AzureOpenAIClient
All Implemented Interfaces:
com.tnsai.llm.LLMClient

public class AzureOpenAIClient extends Object implements com.tnsai.llm.LLMClient
Azure OpenAI Service client implementation. Provides access to OpenAI models deployed on Azure infrastructure.

Azure OpenAI differs from OpenAI API in several ways:

  • Uses deployment-based routing instead of model names
  • Different endpoint format: https://{resource}.openai.azure.com/openai/deployments/{deployment}/chat/completions
  • Supports both API key and Azure AD (Entra ID) authentication
  • Includes Azure-specific content filtering

Configuration via environment variables:

  • AZURE_OPENAI_API_KEY - API key for authentication
  • AZURE_OPENAI_ENDPOINT - Resource endpoint (e.g., https://my-resource.openai.azure.com)
  • AZURE_OPENAI_DEPLOYMENT_ID - Deployment name (model deployment)
  • AZURE_OPENAI_API_VERSION - API version (default: 2024-08-01-preview)

Usage examples:


 // From environment variables
 var client = AzureOpenAIClient.fromEnv();

 // With explicit API key
 var client = AzureOpenAIClient.withApiKey(
     "https://my-resource.openai.azure.com",
     "your-api-key",
     "gpt-4o-deployment"
 );

 // With Azure AD authentication
 var client = AzureOpenAIClient.withAzureAD(
     "https://my-resource.openai.azure.com",
     "gpt-4o-deployment"
 );
 
See Also:
  • Method Details

    • fromEnv

      public static AzureOpenAIClient fromEnv()
      Creates an AzureOpenAIClient from environment variables.

      Required environment variables:

      • AZURE_OPENAI_ENDPOINT - Resource endpoint
      • AZURE_OPENAI_DEPLOYMENT_ID - Deployment name
      • AZURE_OPENAI_API_KEY - API key

      Optional environment variables:

      • AZURE_OPENAI_API_VERSION - API version (default: 2024-08-01-preview)
      Returns:
      A new AzureOpenAIClient instance
      Throws:
      IllegalArgumentException - if required environment variables are missing
    • fromEnv

      public static AzureOpenAIClient fromEnv(float temperature)
      Creates an AzureOpenAIClient from environment variables with custom temperature.
      Parameters:
      temperature - The temperature for response generation
      Returns:
      A new AzureOpenAIClient instance
    • fromEnv

      public static AzureOpenAIClient fromEnv(float temperature, Integer maxTokens)
      Creates an AzureOpenAIClient from environment variables with custom temperature and max tokens.
      Parameters:
      temperature - The temperature for response generation
      maxTokens - The maximum number of tokens to generate
      Returns:
      A new AzureOpenAIClient instance
    • fromEnv

      public static AzureOpenAIClient fromEnv(float temperature, Float topP, Integer maxTokens)
      Creates an AzureOpenAIClient from environment variables with custom temperature, topP, and max tokens.
      Parameters:
      temperature - The temperature for response generation
      topP - The top-p (nucleus sampling) value (0.0-1.0, null for default)
      maxTokens - The maximum number of tokens to generate
      Returns:
      A new AzureOpenAIClient instance
    • withApiKey

      public static AzureOpenAIClient withApiKey(String endpoint, String apiKey, String deploymentId)
      Creates an AzureOpenAIClient with API key authentication.
      Parameters:
      endpoint - The Azure OpenAI resource endpoint
      apiKey - The API key
      deploymentId - The deployment ID
      Returns:
      A new AzureOpenAIClient instance
    • withApiKey

      public static AzureOpenAIClient withApiKey(String endpoint, String apiKey, String deploymentId, String apiVersion, float temperature, Integer maxTokens)
      Creates an AzureOpenAIClient with API key authentication and custom settings.
      Parameters:
      endpoint - The Azure OpenAI resource endpoint
      apiKey - The API key
      deploymentId - The deployment ID
      apiVersion - The API version (null for default)
      temperature - The temperature for response generation
      maxTokens - The maximum number of tokens to generate
      Returns:
      A new AzureOpenAIClient instance
    • withAzureAD

      public static AzureOpenAIClient withAzureAD(String endpoint, String azureAdToken, String deploymentId)
      Creates an AzureOpenAIClient with Azure AD (Entra ID) authentication.

      Note: You need to obtain the Azure AD token separately using Azure Identity SDK or other means. The token should have the cognitive services scope.

      Parameters:
      endpoint - The Azure OpenAI resource endpoint
      azureAdToken - The Azure AD bearer token
      deploymentId - The deployment ID
      Returns:
      A new AzureOpenAIClient instance
    • withAzureAD

      public static AzureOpenAIClient withAzureAD(String endpoint, String azureAdToken, String deploymentId, String apiVersion, float temperature, Integer maxTokens)
      Creates an AzureOpenAIClient with Azure AD authentication and custom settings.
      Parameters:
      endpoint - The Azure OpenAI resource endpoint
      azureAdToken - The Azure AD bearer token
      deploymentId - The deployment ID
      apiVersion - The API version (null for default)
      temperature - The temperature for response generation
      maxTokens - The maximum number of tokens to generate
      Returns:
      A new AzureOpenAIClient instance
    • getModel

      public String getModel()
      Specified by:
      getModel in interface com.tnsai.llm.LLMClient
    • getTemperature

      public float getTemperature()
      Specified by:
      getTemperature in interface com.tnsai.llm.LLMClient
    • getTopP

      public Optional<Float> getTopP()
      Specified by:
      getTopP in interface com.tnsai.llm.LLMClient
    • getMaxTokens

      public Optional<Integer> getMaxTokens()
      Specified by:
      getMaxTokens in interface com.tnsai.llm.LLMClient
    • chat

      public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String,Object>>> history, Optional<List<Map<String,Object>>> tools)
      Specified by:
      chat in interface com.tnsai.llm.LLMClient
    • streamChat

      public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String,Object>>> history, Optional<List<Map<String,Object>>> tools)
      Specified by:
      streamChat in interface com.tnsai.llm.LLMClient
    • getEndpoint

      public String getEndpoint()
      Returns the Azure OpenAI endpoint.
      Returns:
      The endpoint URL
    • getDeploymentId

      public String getDeploymentId()
      Returns the deployment ID.
      Returns:
      The deployment ID
    • getApiVersion

      public String getApiVersion()
      Returns the API version.
      Returns:
      The API version
    • getAuthType

      public AzureOpenAIClient.AuthType getAuthType()
      Returns the authentication type.
      Returns:
      The auth type