Package com.tnsai.llm.providers
Class AzureOpenAIClient
java.lang.Object
com.tnsai.llm.providers.AzureOpenAIClient
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAuthentication type for Azure OpenAI.static classException thrown when Azure OpenAI content filter rejects the request. -
Method Summary
Modifier and TypeMethodDescriptioncom.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) static AzureOpenAIClientfromEnv()Creates an AzureOpenAIClient from environment variables.static AzureOpenAIClientfromEnv(float temperature) Creates an AzureOpenAIClient from environment variables with custom temperature.static AzureOpenAIClientCreates an AzureOpenAIClient from environment variables with custom temperature, topP, and max tokens.static AzureOpenAIClientCreates an AzureOpenAIClient from environment variables with custom temperature and max tokens.Returns the API version.Returns the authentication type.Returns the deployment ID.Returns the Azure OpenAI endpoint.getModel()floatgetTopP()streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) static AzureOpenAIClientwithApiKey(String endpoint, String apiKey, String deploymentId) Creates an AzureOpenAIClient with API key authentication.static AzureOpenAIClientwithApiKey(String endpoint, String apiKey, String deploymentId, String apiVersion, float temperature, Integer maxTokens) Creates an AzureOpenAIClient with API key authentication and custom settings.static AzureOpenAIClientwithAzureAD(String endpoint, String azureAdToken, String deploymentId) Creates an AzureOpenAIClient with Azure AD (Entra ID) authentication.static AzureOpenAIClientwithAzureAD(String endpoint, String azureAdToken, String deploymentId, String apiVersion, float temperature, Integer maxTokens) Creates an AzureOpenAIClient with Azure AD authentication and custom settings.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getCapabilities, getProviderName, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Method Details
-
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
Creates an AzureOpenAIClient from environment variables with custom temperature.- Parameters:
temperature- The temperature for response generation- Returns:
- A new AzureOpenAIClient instance
-
fromEnv
Creates an AzureOpenAIClient from environment variables with custom temperature and max tokens.- Parameters:
temperature- The temperature for response generationmaxTokens- The maximum number of tokens to generate- Returns:
- A new AzureOpenAIClient instance
-
fromEnv
Creates an AzureOpenAIClient from environment variables with custom temperature, topP, and max tokens.- Parameters:
temperature- The temperature for response generationtopP- 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
Creates an AzureOpenAIClient with API key authentication.- Parameters:
endpoint- The Azure OpenAI resource endpointapiKey- The API keydeploymentId- 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 endpointapiKey- The API keydeploymentId- The deployment IDapiVersion- The API version (null for default)temperature- The temperature for response generationmaxTokens- 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 endpointazureAdToken- The Azure AD bearer tokendeploymentId- 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 endpointazureAdToken- The Azure AD bearer tokendeploymentId- The deployment IDapiVersion- The API version (null for default)temperature- The temperature for response generationmaxTokens- The maximum number of tokens to generate- Returns:
- A new AzureOpenAIClient instance
-
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getTopP
- Specified by:
getTopPin interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.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:
chatin interfacecom.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:
streamChatin interfacecom.tnsai.llm.LLMClient
-
getEndpoint
Returns the Azure OpenAI endpoint.- Returns:
- The endpoint URL
-
getDeploymentId
Returns the deployment ID.- Returns:
- The deployment ID
-
getApiVersion
Returns the API version.- Returns:
- The API version
-
getAuthType
Returns the authentication type.- Returns:
- The auth type
-