Azure AI Provider integration for MemberJunction, providing access to Microsoft Azure AI services including Azure OpenAI models, embeddings, and the Phi-4 reasoning model.
This package implements the MemberJunction AI provider interfaces for Azure AI services, enabling seamless integration of Azure's language models and embedding services into your MemberJunction applications. It supports both standard Azure OpenAI deployments and Azure AI Studio models.
- Chat Completions: Full support for Azure-hosted language models including GPT-4, GPT-3.5-Turbo, and Phi-4
- Streaming Support: Real-time streaming responses for enhanced user experience
- Text Embeddings: Generate vector embeddings for semantic search and similarity matching
- Dual Authentication: Support for both API key and Azure Active Directory (Entra ID) authentication
- Text Processing: Built-in text summarization and classification capabilities
- Type Safety: Full TypeScript support with comprehensive type definitions
- Factory Pattern: Seamless integration with MemberJunction's AI factory system
npm install @memberjunction/ai-azure
- An Azure subscription with Azure AI or Azure OpenAI service deployed
- Either:
- An API key for your Azure AI resource, or
- Azure AD credentials configured for your application
- The endpoint URL for your Azure AI resource
import { LLMFactory } from '@memberjunction/ai';
import { LoadAzureLLM } from '@memberjunction/ai-azure';
// Register the Azure provider
LoadAzureLLM();
// Create instance with API key
const azureLLM = LLMFactory.Create("AzureLLM", "your-api-key");
// Configure endpoint
azureLLM.SetAdditionalSettings({
endpoint: "https://your-resource.openai.azure.com/"
});
import { LLMFactory } from '@memberjunction/ai';
import { LoadAzureLLM } from '@memberjunction/ai-azure';
// Register the Azure provider
LoadAzureLLM();
// Create instance (empty string for API key when using Azure AD)
const azureLLM = LLMFactory.Create("AzureLLM", "");
// Configure for Azure AD
azureLLM.SetAdditionalSettings({
endpoint: "https://your-resource.openai.azure.com/",
useAzureAD: true
});
import { LLMFactory } from '@memberjunction/ai';
import { LoadAzureLLM } from '@memberjunction/ai-azure';
// Setup
LoadAzureLLM();
const azureLLM = LLMFactory.Create("AzureLLM", "your-api-key");
azureLLM.SetAdditionalSettings({
endpoint: "https://your-resource.openai.azure.com/"
});
// Simple chat completion
const result = await azureLLM.ChatCompletion({
model: "gpt-4", // or "gpt-35-turbo", "phi-4", etc.
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain the theory of relativity in simple terms." }
],
maxOutputTokens: 500,
temperature: 0.7
});
if (result.success) {
console.log(result.data.choices[0].message.content);
console.log(`Tokens used: ${result.data.usage.totalTokens}`);
}
// Clean up when done
azureLLM.ClearAdditionalSettings();
const streamResult = await azureLLM.ChatCompletion({
model: "gpt-4",
messages: [
{ role: "user", content: "Write a detailed story about space exploration." }
],
streaming: true,
streamingCallbacks: {
OnContent: (content, isComplete) => {
// Handle each chunk of content as it arrives
process.stdout.write(content);
},
OnComplete: (finalResult) => {
console.log("\n\nStreaming complete!");
console.log(`Total tokens: ${finalResult.data.usage.totalTokens}`);
},
OnError: (error) => {
console.error("Streaming error:", error);
}
}
});
const jsonResult = await azureLLM.ChatCompletion({
model: "gpt-4",
messages: [
{
role: "user",
content: "List 3 benefits of exercise as a JSON array with 'benefit' and 'description' fields."
}
],
responseFormat: "JSON"
});
if (jsonResult.success) {
const benefits = JSON.parse(jsonResult.data.choices[0].message.content);
console.log(benefits);
}
const summary = await azureLLM.SummarizeText({
model: "gpt-35-turbo",
messages: [
{
role: "user",
content: "[Your long text to summarize here...]"
}
],
maxOutputTokens: 150
});
if (summary.success) {
console.log("Summary:", summary.summary);
}
const classification = await azureLLM.ClassifyText({
model: "gpt-35-turbo",
messages: [
{
role: "user",
content: "I absolutely loved this product! Best purchase ever!"
}
]
});
if (classification.success) {
console.log("Category:", classification.tags[0].name);
console.log("Confidence:", classification.tags[0].confidence);
}
import { EmbeddingModelFactory } from '@memberjunction/ai';
import { LoadAzureEmbedding } from '@memberjunction/ai-azure';
// Setup
LoadAzureEmbedding();
const embedder = EmbeddingModelFactory.Create("AzureEmbedding", "your-api-key");
embedder.SetAdditionalSettings({
endpoint: "https://your-resource.openai.azure.com/"
});
// Single text embedding
const embedding = await embedder.EmbedText({
model: "text-embedding-ada-002",
text: "The quick brown fox jumps over the lazy dog."
});
console.log(`Embedding dimension: ${embedding.vector.length}`);
console.log(`First 5 values: ${embedding.vector.slice(0, 5)}`);
// Batch embeddings
const batchEmbeddings = await embedder.EmbedTexts({
model: "text-embedding-ada-002",
texts: [
"First document to embed",
"Second document to embed",
"Third document to embed"
]
});
console.log(`Generated ${batchEmbeddings.vectors.length} embeddings`);
// Clean up
embedder.ClearAdditionalSettings();
constructor(apiKey: string)
SetAdditionalSettings(settings: Record<string, any>): void
Configures Azure-specific settings including endpoint and authentication method.
ClearAdditionalSettings(): void
Clears all additional settings and resets the client connection.
ChatCompletion(params: ChatParams): Promise<ChatResult>
Performs a chat completion request with optional streaming support.
SummarizeText(params: SummarizeParams): Promise<SummarizeResult>
Summarizes the provided text using the specified model.
ClassifyText(params: ClassifyParams): Promise<ClassifyResult>
Classifies the provided text into categories.
get SupportsStreaming(): boolean
Returns true
- Azure AI supports streaming responses.
get Endpoint(): string
Returns the configured Azure endpoint URL.
EmbedText(params: EmbedTextParams): Promise<EmbedTextResult>
Generates an embedding vector for a single text.
EmbedTexts(params: EmbedTextsParams): Promise<EmbedTextsResult>
Generates embedding vectors for multiple texts in a single request.
GetEmbeddingModels(): Promise<any>
Returns available embedding models.
Setting | Type | Required | Default | Description |
---|---|---|---|---|
endpoint |
string |
Yes | - | Your Azure AI resource endpoint URL |
useAzureAD |
boolean |
No | false |
Use Azure AD authentication instead of API key |
- GPT-4: Latest GPT-4 models including GPT-4-Turbo
- GPT-3.5: GPT-3.5-Turbo models
- Phi-4: Microsoft's efficient reasoning model
- Any other models deployed in your Azure AI resource
- text-embedding-ada-002: 1536-dimensional embeddings
- Other embedding models available in your Azure deployment
-
@azure-rest/ai-inference
: Azure AI inference REST client -
@azure/core-auth
: Azure authentication core -
@azure/identity
: Azure identity and credential management -
@memberjunction/ai
: Core MemberJunction AI interfaces -
@memberjunction/global
: MemberJunction global utilities
This package integrates seamlessly with other MemberJunction AI packages:
-
@memberjunction/ai: Implements core interfaces like
BaseLLM
andBaseEmbeddings
- @memberjunction/ai-vectors: Can be used with Azure embeddings for vector storage
- @memberjunction/ai-prompts: Compatible with prompt templates and management
- @memberjunction/ai-agents: Can power AI agents with Azure models
try {
const result = await azureLLM.ChatCompletion({
model: "gpt-4",
messages: [{ role: "user", content: "Hello!" }]
});
if (!result.success) {
console.error("Request failed:", result.errorMessage);
}
} catch (error) {
console.error("Exception occurred:", error);
}
- Always set endpoint: The endpoint must be configured before making any requests
-
Clean up settings: Call
ClearAdditionalSettings()
when switching configurations -
Handle errors: Always check the
success
property of results - Use appropriate models: Choose models based on your task requirements and cost considerations
-
Batch requests: Use
EmbedTexts
for multiple embeddings to reduce API calls -
Monitor usage: Track token usage through the
usage
property in results
-
"Azure client not initialized": Ensure
SetAdditionalSettings
is called with a valid endpoint - Authentication failures: Verify your API key or Azure AD credentials
- Model not found: Ensure the model is deployed in your Azure resource
- Rate limiting: Implement retry logic for high-volume applications
The Azure provider supports the following LLM parameters (same as OpenAI since it uses OpenAI models):
Supported:
-
temperature
- Controls randomness in the output (0.0-2.0) -
maxOutputTokens
- Maximum number of tokens to generate -
topP
- Nucleus sampling threshold (0.0-1.0) -
frequencyPenalty
- Reduces repetition of token sequences (-2.0 to 2.0) -
presencePenalty
- Reduces repetition of specific tokens (-2.0 to 2.0) -
seed
- For deterministic outputs -
stopSequences
- Array of sequences where the API will stop generating -
includeLogProbs
- Whether to return log probabilities -
responseFormat
- Output format (Text, JSON, Markdown, etc.)
Not Supported:
-
topK
- Not available in Azure OpenAI API -
minP
- Not available in Azure OpenAI API
MIT - See LICENSE file in the repository root