LangChain4j integration for Azure OpenAI providing chat, streaming, embeddings, image generation, audio transcription, and token counting capabilities
—
—
Does it follow best practices?
Impact
—
No eval scenarios have been run
—
The risk profile of this skill
LangChain4j integration for Azure OpenAI services, providing comprehensive support for chat completion, streaming, text embeddings, image generation, audio transcription, and token counting using Azure-hosted OpenAI models.
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-azure-open-ai</artifactId>
<version>1.11.0</version>
</dependency>// Main model classes
import dev.langchain4j.model.azure.AzureOpenAiChatModel;
import dev.langchain4j.model.azure.AzureOpenAiStreamingChatModel;
import dev.langchain4j.model.azure.AzureOpenAiEmbeddingModel;
import dev.langchain4j.model.azure.AzureOpenAiImageModel;
import dev.langchain4j.model.azure.AzureOpenAiLanguageModel;
import dev.langchain4j.model.azure.AzureOpenAiStreamingLanguageModel;
import dev.langchain4j.model.azure.AzureOpenAiAudioTranscriptionModel;
import dev.langchain4j.model.azure.AzureOpenAiTokenCountEstimator;
// Model name enums
import dev.langchain4j.model.azure.AzureOpenAiChatModelName;
import dev.langchain4j.model.azure.AzureOpenAiEmbeddingModelName;
import dev.langchain4j.model.azure.AzureOpenAiImageModelName;
import dev.langchain4j.model.azure.AzureOpenAiLanguageModelName;import dev.langchain4j.model.azure.AzureOpenAiChatModel;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
// Create a chat model with Azure OpenAI
AzureOpenAiChatModel model = AzureOpenAiChatModel.builder()
.endpoint("https://your-resource.openai.azure.com/")
.apiKey("your-api-key")
.deploymentName("gpt-4")
.serviceVersion("2024-02-15-preview")
.temperature(0.7)
.build();
// Generate a chat completion
String userMessage = "What is the capital of France?";
Response<String> response = model.generate(userMessage);
System.out.println(response.content());All models support three authentication methods. Exactly one must be specified.
/**
* Standard Azure OpenAI resource authentication using an API key.
* @param apiKey The API key from Azure Portal (32-character hex string)
* @return Builder instance for chaining
* @throws IllegalArgumentException if apiKey is null or empty
*/
Builder apiKey(String apiKey);/**
* Authenticate with the non-Azure OpenAI service.
* Automatically configures endpoint to https://api.openai.com/v1.
* Do not call endpoint() when using this method.
* @param apiKey OpenAI API key starting with "sk-"
* @return Builder instance for chaining
* @throws IllegalArgumentException if apiKey is null or empty
*/
Builder nonAzureApiKey(String apiKey);/**
* Authenticate using Azure Active Directory / Microsoft Entra ID credentials.
* @param credential TokenCredential implementation (e.g., DefaultAzureCredential, ManagedIdentityCredential)
* @return Builder instance for chaining
* @throws IllegalArgumentException if credential is null
*/
Builder tokenCredential(com.azure.core.credential.TokenCredential credential);Chat models provide conversational AI capabilities using models like GPT-3.5-turbo and GPT-4. They support multi-turn conversations, function calling, structured output, and Azure-specific features like data sources and content enhancements.
Main Class:
package dev.langchain4j.model.azure;
/**
* Azure OpenAI chat model implementation for synchronous operations.
* Thread-safe: Yes - instances can be safely shared across threads.
* Immutable: Yes - all configuration is set at build time.
*/
class AzureOpenAiChatModel implements dev.langchain4j.model.chat.ChatModel {
/**
* Creates a new builder for configuring AzureOpenAiChatModel.
* @return A new Builder instance
*/
static Builder builder();
/**
* Executes a chat request synchronously.
* @param request ChatRequest containing messages, tools, and parameters
* @return ChatResponse with AI message, token usage, and finish reason
* @throws dev.langchain4j.exception.ContentFilteredException if content violates Azure safety policies
* @throws java.util.concurrent.TimeoutException if request exceeds configured timeout
* @throws IllegalArgumentException if request is invalid
* @throws RuntimeException for network or API errors
*/
dev.langchain4j.model.chat.ChatResponse chat(dev.langchain4j.model.chat.ChatRequest request);
}Streaming Class:
package dev.langchain4j.model.azure;
/**
* Azure OpenAI streaming chat model for asynchronous token-by-token responses.
* Thread-safe: Yes - instances can be safely shared across threads.
* Handler callbacks: Called on Azure SDK's event loop threads, not application threads.
*/
class AzureOpenAiStreamingChatModel implements dev.langchain4j.model.chat.StreamingChatModel {
/**
* Creates a new builder for configuring AzureOpenAiStreamingChatModel.
* @return A new Builder instance
*/
static Builder builder();
/**
* Executes a streaming chat request asynchronously.
* Handler methods are called on Azure SDK threads - handlers must be thread-safe.
* @param request ChatRequest containing messages, tools, and parameters
* @param handler StreamingChatResponseHandler for receiving tokens and completion
*/
void chat(dev.langchain4j.model.chat.ChatRequest request,
dev.langchain4j.model.chat.StreamingChatResponseHandler handler);
}Supported Features:
Embedding models convert text into vector representations for semantic search, clustering, and similarity comparisons. Supports models like text-embedding-ada-002 and text-embedding-3-small/large.
Main Class:
package dev.langchain4j.model.azure;
/**
* Azure OpenAI embedding model for converting text to vector representations.
* Thread-safe: Yes - instances can be safely shared across threads.
* Batch limit: Maximum 16 text segments per request.
* Token limit: Maximum 8191 tokens per segment.
*/
class AzureOpenAiEmbeddingModel extends dev.langchain4j.model.embedding.DimensionAwareEmbeddingModel {
/**
* Creates a new builder for configuring AzureOpenAiEmbeddingModel.
* @return A new Builder instance
*/
static Builder builder();
/**
* Embeds multiple text segments in a single batch request.
* @param textSegments List of 1-16 text segments, each up to 8191 tokens
* @return Response with embeddings list and token usage
* @throws IllegalArgumentException if textSegments is null, empty, or contains more than 16 segments
* @throws IllegalArgumentException if any segment exceeds 8191 tokens
* @throws java.util.concurrent.TimeoutException if request exceeds configured timeout
* @throws RuntimeException for network or API errors
*/
dev.langchain4j.model.output.Response<java.util.List<dev.langchain4j.data.embedding.Embedding>>
embedAll(java.util.List<dev.langchain4j.data.segment.TextSegment> textSegments);
}Key Features:
Image generation using DALL-E models to create images from text prompts. Supports quality, size, and style customization.
Main Class:
package dev.langchain4j.model.azure;
/**
* Azure OpenAI image generation model using DALL-E.
* Thread-safe: Yes - instances can be safely shared across threads.
* Generation limit: 1 image per request (DALL-E 3).
* Prompt limit: Maximum 4000 characters.
* File size limit: Generated images via URL expire after 1 hour.
*/
class AzureOpenAiImageModel implements dev.langchain4j.model.image.ImageModel {
/**
* Creates a new builder for configuring AzureOpenAiImageModel.
* @return A new Builder instance
*/
static Builder builder();
/**
* Generates an image from a text prompt.
* @param prompt Text description, maximum 4000 characters
* @return Response containing Image with URL or base64 data
* @throws dev.langchain4j.exception.ContentFilteredException if prompt violates content policy
* @throws IllegalArgumentException if prompt is null, empty, or exceeds 4000 characters
* @throws java.util.concurrent.TimeoutException if generation exceeds configured timeout (typically 90-120s)
* @throws RuntimeException for network or API errors
*/
dev.langchain4j.model.output.Response<dev.langchain4j.data.image.Image> generate(String prompt);
}Key Features:
Legacy text completion models using GPT-3.5-turbo-instruct and davinci-002. Deprecated in favor of Chat Models.
Main Classes:
package dev.langchain4j.model.azure;
/**
* @deprecated Use AzureOpenAiChatModel instead for better performance and features.
* Legacy language model for text completion.
* Thread-safe: Yes
*/
@Deprecated
class AzureOpenAiLanguageModel implements dev.langchain4j.model.language.LanguageModel {
static Builder builder();
/**
* Generates text completion for a prompt.
* @param prompt Input text prompt
* @return Response with generated completion text
*/
dev.langchain4j.model.output.Response<String> generate(String prompt);
}
/**
* @deprecated Use AzureOpenAiStreamingChatModel instead.
* Legacy streaming language model.
* Thread-safe: Yes
* Handler callbacks: Called on Azure SDK threads.
*/
@Deprecated
class AzureOpenAiStreamingLanguageModel implements dev.langchain4j.model.language.StreamingLanguageModel {
static Builder builder();
/**
* Generates streaming text completion.
* @param prompt Input text prompt
* @param handler Handler for receiving tokens and completion
*/
void generate(String prompt, dev.langchain4j.model.language.StreamingResponseHandler<String> handler);
}Speech-to-text transcription using Whisper models. Convert audio files to text with support for various audio formats.
Main Class:
package dev.langchain4j.model.azure;
/**
* @Experimental - API may change in future versions.
* Azure OpenAI audio transcription using Whisper.
* Thread-safe: Yes - instances can be safely shared across threads.
* File size limit: Maximum 25 MB per audio file.
* Supported formats: MP3, MP4, MPEG, MPGA, M4A, WAV, WEBM.
*/
@dev.langchain4j.Experimental
class AzureOpenAiAudioTranscriptionModel implements dev.langchain4j.model.audio.AudioTranscriptionModel {
/**
* Creates a new builder for configuring AzureOpenAiAudioTranscriptionModel.
* @return A new Builder instance
*/
static Builder builder();
/**
* Transcribes audio file to text.
* @param request AudioTranscriptionRequest with file path and options
* @return AudioTranscriptionResponse with transcribed text and optional timestamps
* @throws IllegalArgumentException if audio file does not exist, exceeds 25MB, or has unsupported format
* @throws java.util.concurrent.TimeoutException if transcription exceeds configured timeout
* @throws RuntimeException for network or API errors
*/
dev.langchain4j.model.audio.AudioTranscriptionResponse
transcribe(dev.langchain4j.model.audio.AudioTranscriptionRequest request);
}Key Features:
Audio Transcription Documentation
Estimate token counts for cost calculation and request validation before making API calls.
Main Class:
package dev.langchain4j.model.azure;
/**
* Token count estimator for Azure OpenAI models.
* Uses the same tokenization algorithm as the target model.
* Thread-safe: Yes - instances can be safely shared across threads.
* Accuracy: Typically ±1-2 tokens for text, ±2-5 for single messages, ±5-10 for message lists.
*/
class AzureOpenAiTokenCountEstimator implements dev.langchain4j.model.Tokenizer {
/**
* Creates estimator for a chat model.
* @param modelName Chat model name enum
* @throws IllegalArgumentException if modelName is null
*/
AzureOpenAiTokenCountEstimator(AzureOpenAiChatModelName modelName);
/**
* Creates estimator for an embedding model.
* @param modelName Embedding model name enum
* @throws IllegalArgumentException if modelName is null
*/
AzureOpenAiTokenCountEstimator(AzureOpenAiEmbeddingModelName modelName);
/**
* Creates estimator for a language model.
* @param modelName Language model name enum
* @throws IllegalArgumentException if modelName is null
*/
AzureOpenAiTokenCountEstimator(AzureOpenAiLanguageModelName modelName);
/**
* Creates estimator for a custom model name.
* Falls back to cl100k_base encoding if model is unknown.
* @param modelName Model name as string
* @throws IllegalArgumentException if modelName is null or empty
*/
AzureOpenAiTokenCountEstimator(String modelName);
/**
* Estimates token count in plain text.
* @param text Text to estimate, can be empty but not null
* @return Estimated token count (0 for empty string)
* @throws NullPointerException if text is null
*/
int estimateTokenCountInText(String text);
/**
* Estimates token count in a single message including formatting overhead.
* @param message ChatMessage to estimate
* @return Estimated token count including message structure tokens
* @throws NullPointerException if message is null
*/
int estimateTokenCountInMessage(dev.langchain4j.data.message.ChatMessage message);
/**
* Estimates token count in multiple messages including conversation structure overhead.
* @param messages Iterable of messages, can be empty but not null
* @return Estimated total token count including all formatting
* @throws NullPointerException if messages is null or contains null elements
*/
int estimateTokenCountInMessages(Iterable<dev.langchain4j.data.message.ChatMessage> messages);
}Supports:
All model builders share common configuration options for HTTP client customization, retry policies, proxy settings, logging, and custom headers.
Common Builder Methods:
/**
* Common configuration interface implemented by all model builders.
* @param <T> Builder type for fluent chaining
*/
interface CommonConfigurationMethods<T> {
/**
* Sets the Azure OpenAI resource endpoint.
* Not required when using nonAzureApiKey().
* @param endpoint Full endpoint URL, e.g., "https://your-resource.openai.azure.com/"
* @return Builder instance
* @throws IllegalArgumentException if endpoint is null, empty, or malformed
*/
T endpoint(String endpoint);
/**
* Sets the Azure OpenAI API version.
* Required for all Azure deployments.
* @param serviceVersion API version string, e.g., "2024-02-15-preview"
* @return Builder instance
* @throws IllegalArgumentException if serviceVersion is null or empty
*/
T serviceVersion(String serviceVersion);
/**
* Sets the deployment name of the model in Azure.
* This is YOUR deployment name, not the model name.
* @param deploymentName Your Azure deployment name
* @return Builder instance
* @throws IllegalArgumentException if deploymentName is null or empty
*/
T deploymentName(String deploymentName);
/**
* Sets request timeout.
* @param timeout Timeout duration, must be positive
* @return Builder instance
* @default 60 seconds for most models, 120 seconds for streaming
* @throws IllegalArgumentException if timeout is null or non-positive
*/
T timeout(java.time.Duration timeout);
/**
* Sets simple retry count.
* Mutually exclusive with retryOptions().
* @param maxRetries Number of retries, 0-10
* @return Builder instance
* @default 3
* @throws IllegalArgumentException if maxRetries is negative or > 10
*/
T maxRetries(Integer maxRetries);
/**
* Sets advanced retry options with exponential backoff.
* Mutually exclusive with maxRetries().
* @param retryOptions Azure SDK retry configuration
* @return Builder instance
* @default Exponential backoff with 3 retries
*/
T retryOptions(com.azure.core.http.policy.RetryOptions retryOptions);
/**
* Sets HTTP proxy configuration.
* @param proxyOptions Proxy settings including authentication
* @return Builder instance
* @default No proxy
*/
T proxyOptions(com.azure.core.http.ProxyOptions proxyOptions);
/**
* Enables detailed HTTP logging.
* WARNING: Logs full request/response including prompts and completions.
* @param logRequestsAndResponses true to enable logging
* @return Builder instance
* @default false
*/
T logRequestsAndResponses(Boolean logRequestsAndResponses);
/**
* Sets custom User-Agent suffix for request identification.
* @param userAgentSuffix Suffix to append, e.g., "MyApp/1.0.0"
* @return Builder instance
* @default None
*/
T userAgentSuffix(String userAgentSuffix);
/**
* Sets custom HTTP headers for all requests.
* @param customHeaders Map of header name to value
* @return Builder instance
* @default Empty map
*/
T customHeaders(java.util.Map<String, String> customHeaders);
/**
* Sets custom HTTP client provider.
* @param httpClientProvider Provider for creating HTTP client instance
* @return Builder instance
* @default Azure SDK default HTTP client
*/
T httpClientProvider(com.azure.core.http.HttpClientProvider httpClientProvider);
}Predefined enum constants for Azure OpenAI model names with version information.
package dev.langchain4j.model.azure;
/**
* Enum of supported Azure OpenAI chat model names.
* Each constant maps to a specific model version.
*/
enum AzureOpenAiChatModelName {
/** gpt-35-turbo (GPT-3.5 Turbo, 4K context) */
GPT_3_5_TURBO,
/** gpt-35-turbo-0301 (March 2023 snapshot) */
GPT_3_5_TURBO_0301,
/** gpt-35-turbo-0613 (June 2023 snapshot, function calling) */
GPT_3_5_TURBO_0613,
/** gpt-35-turbo-1106 (November 2023 snapshot, improved function calling) */
GPT_3_5_TURBO_1106,
/** gpt-35-turbo-16k (16K context window) */
GPT_3_5_TURBO_16K,
/** gpt-35-turbo-16k-0613 (16K context, June 2023) */
GPT_3_5_TURBO_16K_0613,
/** gpt-4 (GPT-4, 8K context) */
GPT_4,
/** gpt-4-0613 (June 2023 snapshot) */
GPT_4_0613,
/** gpt-4-0125-preview (January 2025 preview) */
GPT_4_0125_PREVIEW,
/** gpt-4-1106-preview (November 2023 preview, 128K context) */
GPT_4_1106_PREVIEW,
/** gpt-4-turbo (GPT-4 Turbo, 128K context) */
GPT_4_TURBO,
/** gpt-4-turbo-2024-04-09 (April 2024 snapshot) */
GPT_4_TURBO_2024_04_09,
/** gpt-4-32k (32K context window) */
GPT_4_32K,
/** gpt-4-32k-0613 (32K context, June 2023) */
GPT_4_32K_0613,
/** gpt-4-vision-preview (GPT-4 with vision, multimodal) */
GPT_4_VISION_PREVIEW,
/** gpt-4o (GPT-4 Omni, multimodal, 128K context) */
GPT_4_O;
/**
* Returns the full model name string.
* @return Model name as used in API requests
*/
String modelName();
/**
* Returns the model type without version.
* @return Base model type, e.g., "gpt-35-turbo"
*/
String modelType();
/**
* Returns the model version suffix.
* @return Version string, e.g., "0613", or empty string
*/
String modelVersion();
/**
* Returns the string representation.
* @return Same as modelName()
*/
String toString();
}package dev.langchain4j.model.azure;
/**
* Enum of supported Azure OpenAI embedding model names.
* Each constant includes dimension information.
*/
enum AzureOpenAiEmbeddingModelName {
/** text-embedding-3-small (1536 dimensions, supports custom dimensions) */
TEXT_EMBEDDING_3_SMALL,
/** text-embedding-3-small-1 (1536 dimensions, version 1) */
TEXT_EMBEDDING_3_SMALL_1,
/** text-embedding-3-large (3072 dimensions, supports custom dimensions) */
TEXT_EMBEDDING_3_LARGE,
/** text-embedding-3-large-1 (3072 dimensions, version 1) */
TEXT_EMBEDDING_3_LARGE_1,
/** text-embedding-ada-002 (1536 dimensions, legacy) */
TEXT_EMBEDDING_ADA_002,
/** text-embedding-ada-002-1 (1536 dimensions, version 1) */
TEXT_EMBEDDING_ADA_002_1,
/** text-embedding-ada-002-2 (1536 dimensions, version 2) */
TEXT_EMBEDDING_ADA_002_2;
/**
* Returns the full model name string.
* @return Model name as used in API requests
*/
String modelName();
/**
* Returns the model type without version.
* @return Base model type
*/
String modelType();
/**
* Returns the model version suffix.
* @return Version string or empty
*/
String modelVersion();
/**
* Returns the default embedding dimension.
* @return Number of dimensions (1536 or 3072)
*/
Integer dimension();
String toString();
/**
* Static method to lookup dimension by model name string.
* @param modelName Model name to lookup
* @return Known dimension or null if unknown
*/
static Integer knownDimension(String modelName);
}package dev.langchain4j.model.azure;
/**
* Enum of supported Azure OpenAI image generation model names.
*/
enum AzureOpenAiImageModelName {
/** dall-e-3 (DALL-E 3) */
DALL_E_3,
/** dall-e-3-30 (DALL-E 3, version 30) */
DALL_E_3_30;
String modelName();
String modelType();
String modelVersion();
String toString();
}package dev.langchain4j.model.azure;
/**
* Enum of supported Azure OpenAI language model names (deprecated models).
* @deprecated Use AzureOpenAiChatModelName instead
*/
@Deprecated
enum AzureOpenAiLanguageModelName {
/** gpt-35-turbo-instruct (instruction-following variant) */
GPT_3_5_TURBO_INSTRUCT,
/** gpt-35-turbo-instruct-0914 (September 2023 snapshot) */
GPT_3_5_TURBO_INSTRUCT_0914,
/** davinci-002 (legacy model) */
TEXT_DAVINCI_002,
/** davinci-002-1 (legacy model, version 1) */
TEXT_DAVINCI_002_1;
String modelName();
String modelType();
String modelVersion();
String toString();
}The package maps Azure OpenAI API errors to LangChain4j exception types with specific meanings.
package dev.langchain4j.exception;
/**
* Thrown when content is filtered by Azure OpenAI safety policies.
* Indicates prompt or response violated content filtering rules.
* Not retried automatically.
*/
class ContentFilteredException extends RuntimeException {
/**
* Creates exception with message.
* @param message Description of which filter was triggered
*/
ContentFilteredException(String message);
/**
* Creates exception with message and cause.
* @param message Description of filter
* @param cause Underlying exception
*/
ContentFilteredException(String message, Throwable cause);
}package java.util.concurrent;
/**
* Thrown when request exceeds configured timeout duration.
* Automatically retried according to retry policy.
*/
class TimeoutException extends Exception {
}package java.lang;
/**
* Thrown for invalid configuration or parameters.
* Examples: null required parameter, value out of range, invalid combination.
* Not retried automatically.
*/
class IllegalArgumentException extends RuntimeException {
}/**
* General runtime exception for network errors, API errors, authentication failures.
* May be retried depending on HTTP status code:
* - 429 (rate limit): Retried with exponential backoff
* - 500-599 (server error): Retried according to policy
* - 401, 403 (auth error): Not retried
* - 400, 404 (client error): Not retried
*/
class RuntimeException extends Exception {
}import dev.langchain4j.exception.ContentFilteredException;
import java.util.concurrent.TimeoutException;
try {
Response<?> response = model.generate(input);
} catch (ContentFilteredException e) {
// Content violated safety policy - do not retry
// Check Azure OpenAI Studio for content filter configuration
logger.warn("Content filtered: {}", e.getMessage());
// Handle gracefully or request user to modify input
} catch (TimeoutException e) {
// Request timed out - safe to retry with exponential backoff
logger.error("Request timed out after {}ms", timeout);
// Implement retry with backoff or increase timeout
} catch (IllegalArgumentException e) {
// Invalid configuration or parameters - fix code
logger.error("Invalid configuration: {}", e.getMessage());
// Do not retry, fix the issue in code
} catch (RuntimeException e) {
// Network, API, or authentication error
// Check if retryable based on cause
logger.error("Unexpected error", e);
// Implement retry logic or escalate
}For advanced use cases, the package provides SPI interfaces for custom builder factory implementations via Java ServiceLoader mechanism.
package dev.langchain4j.model.azure.spi;
/**
* SPI for custom AzureOpenAiChatModel builder factory.
* Register via META-INF/services/dev.langchain4j.model.azure.spi.AzureOpenAiChatModelBuilderFactory
*/
interface AzureOpenAiChatModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiChatModel.Builder> {
/**
* Provides a new builder instance.
* @return New AzureOpenAiChatModel.Builder
*/
AzureOpenAiChatModel.Builder get();
}
/**
* SPI for custom AzureOpenAiStreamingChatModel builder factory.
*/
interface AzureOpenAiStreamingChatModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiStreamingChatModel.Builder> {
AzureOpenAiStreamingChatModel.Builder get();
}
/**
* SPI for custom AzureOpenAiEmbeddingModel builder factory.
*/
interface AzureOpenAiEmbeddingModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiEmbeddingModel.Builder> {
AzureOpenAiEmbeddingModel.Builder get();
}
/**
* SPI for custom AzureOpenAiImageModel builder factory.
*/
interface AzureOpenAiImageModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiImageModel.Builder> {
AzureOpenAiImageModel.Builder get();
}
/**
* SPI for custom AzureOpenAiLanguageModel builder factory.
* @deprecated Language models are deprecated
*/
@Deprecated
interface AzureOpenAiLanguageModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiLanguageModel.Builder> {
AzureOpenAiLanguageModel.Builder get();
}
/**
* SPI for custom AzureOpenAiStreamingLanguageModel builder factory.
* @deprecated Language models are deprecated
*/
@Deprecated
interface AzureOpenAiStreamingLanguageModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiStreamingLanguageModel.Builder> {
AzureOpenAiStreamingLanguageModel.Builder get();
}
/**
* SPI for custom AzureOpenAiAudioTranscriptionModel builder factory.
* @Experimental API may change
*/
@dev.langchain4j.Experimental
interface AzureOpenAiAudioTranscriptionModelBuilderFactory
extends java.util.function.Supplier<dev.langchain4j.model.azure.AzureOpenAiAudioTranscriptionModel.Builder> {
AzureOpenAiAudioTranscriptionModel.Builder get();
}SPI Registration Example:
Create file: META-INF/services/dev.langchain4j.model.azure.spi.AzureOpenAiChatModelBuilderFactory
Content:
com.example.CustomAzureOpenAiChatModelBuilderFactoryImplementation:
package com.example;
import dev.langchain4j.model.azure.AzureOpenAiChatModel;
import dev.langchain4j.model.azure.spi.AzureOpenAiChatModelBuilderFactory;
public class CustomAzureOpenAiChatModelBuilderFactory
implements AzureOpenAiChatModelBuilderFactory {
@Override
public AzureOpenAiChatModel.Builder get() {
// Return custom-configured builder
return AzureOpenAiChatModel.builder()
.timeout(Duration.ofMinutes(2))
.maxRetries(5);
}
}All model instances are thread-safe and immutable after construction:
Streaming handlers:
Best practices: