Azure Foundry plugin
Azure AI Foundry plugin for Genkit Go that provides text generation and chat capabilities using Azure OpenAI and other models available through Azure AI Foundry.
Installation
Section titled “Installation”go get github.com/xavidop/genkit-azure-foundry-goFeatures
Section titled “Features”- Text Generation: Support for
GPTmodels - Embeddings: Support for
text-embeddingmodels - Image Generation: Support for creating images from text prompts
- Text-to-Speech: Convert text to natural-sounding speech with multiple voices
- Speech-to-Text: Transcribe audio to text with subtitle support
- Streaming: Full streaming support for real-time responses
- Tool Calling: Complete function calling capabilities
- Multimodal Support: Support for text + image inputs
- Multi-turn Conversations: Full support for chat history and context management
- Type Safety: Robust type conversion and schema validation
- Flexible Authentication: Support for API keys, Azure Default Credential, and custom token credentials
Initialize the Plugin
Section titled “Initialize the Plugin”package main
import ( "context" "log" "os"
"github.com/firebase/genkit/go/ai" "github.com/firebase/genkit/go/genkit" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
func main() { ctx := context.Background()
// Initialize Azure AI Foundry plugin azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: os.Getenv("AZURE_OPENAI_ENDPOINT"), APIKey: os.Getenv("AZURE_OPENAI_API_KEY"), }
// Initialize Genkit g := genkit.Init(ctx, genkit.WithPlugins(azurePlugin), genkit.WithDefaultModel("azureaifoundry/gpt-5"), )
// Optional: Define common models for easy access azureaifoundry.DefineCommonModels(azurePlugin, g)
log.Println("Starting basic Azure AI Foundry example...")
// Define a GPT-5 model (use your deployment name) gpt5Model := azurePlugin.DefineModel(g, azureaifoundry.ModelDefinition{ Name: "gpt-5", // Your deployment name in Azure Type: "chat", SupportsMedia: true, }, nil)
// Example: Generate text (basic usage) response, err := genkit.Generate(ctx, g, ai.WithModel(gpt5Model), ai.WithPrompt("What are the key benefits of using Azure AI Foundry?"), ) if err != nil { log.Printf("Error: %v", err) } else { log.Printf("Response: %s", response.Text()) }}Configuration Options
Section titled “Configuration Options”The plugin supports various configuration options:
azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: "https://your-resource.openai.azure.com/", APIKey: "your-api-key", // Use API key // OR use Azure credential // Credential: azidentity.NewDefaultAzureCredential(), APIVersion: "2024-02-15-preview", // Optional}Available Configuration
Section titled “Available Configuration”| Option | Type | Default | Description |
|---|---|---|---|
Endpoint | string | required | Azure OpenAI endpoint URL |
APIKey | string | "" | API key for authentication |
Credential | azcore.TokenCredential | nil | Azure credential (alternative to API key) |
APIVersion | string | Latest | API version to use |
Azure Setup and Authentication
Section titled “Azure Setup and Authentication”Getting Your Endpoint and API Key
Section titled “Getting Your Endpoint and API Key”- Go to Azure Portal
- Navigate to your Azure OpenAI resource
- Go to “Keys and Endpoint” section
- Copy your endpoint URL and API key
Authentication Methods
Section titled “Authentication Methods”The plugin supports multiple authentication methods to suit different deployment scenarios:
1. API Key Authentication (Quick Start)
Section titled “1. API Key Authentication (Quick Start)”Best for: Development, testing, and simple scenarios
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"export AZURE_OPENAI_API_KEY="your-api-key"import ( "os" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: os.Getenv("AZURE_OPENAI_ENDPOINT"), APIKey: os.Getenv("AZURE_OPENAI_API_KEY"),}2. Azure Default Credential (Recommended for Production)
Section titled “2. Azure Default Credential (Recommended for Production)”Best for: Production deployments, Azure-hosted applications
DefaultAzureCredential automatically tries multiple authentication methods in the following order:
- Environment variables (AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID)
- Managed Identity (when deployed to Azure)
- Azure CLI credentials (for local development)
- Azure PowerShell credentials
- Interactive browser authentication
# Required environment variablesexport AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"export AZURE_TENANT_ID="your-tenant-id"
# Optional: For service principal authenticationexport AZURE_CLIENT_ID="your-client-id"export AZURE_CLIENT_SECRET="your-client-secret"import ( "fmt" "os" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
func main() { endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") tenantID := os.Getenv("AZURE_TENANT_ID")
// Create DefaultAzureCredential credential, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{ TenantID: tenantID, }) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) return }
// Initialize plugin with credential azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: endpoint, Credential: credential, }
// Use the plugin with Genkit...}3. Managed Identity (Azure Deployments)
Section titled “3. Managed Identity (Azure Deployments)”Best for: Applications deployed to Azure (App Service, Container Apps, VMs, AKS)
When deployed to Azure, Managed Identity provides authentication without storing credentials:
import ( "os" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
func main() { endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
// Use Managed Identity credential, err := azidentity.NewManagedIdentityCredential(nil) if err != nil { panic(err) }
azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: endpoint, Credential: credential, }}4. Client Secret Credential (Service Principal)
Section titled “4. Client Secret Credential (Service Principal)”Best for: CI/CD pipelines, automated deployments
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"export AZURE_TENANT_ID="your-tenant-id"export AZURE_CLIENT_ID="your-client-id"export AZURE_CLIENT_SECRET="your-client-secret"import ( "os" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
func main() { endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT") tenantID := os.Getenv("AZURE_TENANT_ID") clientID := os.Getenv("AZURE_CLIENT_ID") clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
credential, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil) if err != nil { panic(err) }
azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: endpoint, Credential: credential, }}5. Azure CLI Credential (Local Development)
Section titled “5. Azure CLI Credential (Local Development)”Best for: Local development with Azure CLI installed
# Login to Azure CLI firstaz login
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"import ( "os" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" azureaifoundry "github.com/xavidop/genkit-azure-foundry-go")
func main() { endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
// Use Azure CLI credentials credential, err := azidentity.NewAzureCLICredential(nil) if err != nil { panic(err) }
azurePlugin := &azureaifoundry.AzureAIFoundry{ Endpoint: endpoint, Credential: credential, }}Model Deployments
Section titled “Model Deployments”Important: The Name in ModelDefinition should match your deployment name in Azure, not the model name. For example:
- If you deployed
gpt-5with deployment namemy-gpt5-deployment, use"my-gpt5-deployment" - If you deployed
gpt-4owith deployment namegpt-4o, use"gpt-4o"
For more Genkit features like embeddings, structured output, and flows, refer to the Genkit documentation.
Image Generation
Section titled “Image Generation”Generate images with DALL-E models using the standard genkit.Generate() method:
// Define DALL-E modeldallE3 := azurePlugin.DefineModel(g, azureaifoundry.ModelDefinition{ Name: azureaifoundry.ModelDallE3, Type: "chat",}, nil)
// Generate imageresponse, err := genkit.Generate(ctx, g, ai.WithModel(dallE3), ai.WithPrompt("A serene landscape with mountains at sunset"), ai.WithConfig(map[string]interface{}{ "quality": "hd", "size": "1024x1024", "style": "vivid", }),)
if err != nil { log.Fatal(err)}
log.Printf("Image URL: %s", response.Text())The Configuration options for image generation depends on the model used. Common options include:
| Option | Type | Description |
|---|---|---|
quality | string | Image quality: standard, hd |
size | string | Image size: 256x256, 512x512, 1024x1024 |
style | string | Image style: vivid, photorealistic, cartoon |
Text-to-Speech
Section titled “Text-to-Speech”Convert text to speech using the standard genkit.Generate() method:
import "encoding/base64"
// Define TTS modelttsModel := azurePlugin.DefineModel(g, azureaifoundry.ModelDefinition{ Name: azureaifoundry.ModelTTS1HD, Type: "chat",}, nil)
// Generate speechresponse, err := genkit.Generate(ctx, g, ai.WithModel(ttsModel), ai.WithPrompt("Hello! Welcome to Azure AI Foundry."), ai.WithConfig(map[string]interface{}{ "voice": "nova", "response_format": "mp3", "speed": 1.5, }),)
if err != nil { log.Fatal(err)}
// Decode base64 audio and save fileaudioData, _ := base64.StdEncoding.DecodeString(response.Text())os.WriteFile("output.mp3", audioData, 0644)Speech-to-Text
Section titled “Speech-to-Text”Transcribe audio to text using the standard genkit.Generate() method:
import "encoding/base64"
// Define Whisper model with media support (required for audio input)whisperModel := azurePlugin.DefineModel(g, azureaifoundry.ModelDefinition{ Name: azureaifoundry.ModelWhisper1, Type: "chat", SupportsMedia: true, // Required for media parts (audio)}, nil)
// Read and encode audio fileaudioData, _ := os.ReadFile("audio.mp3")base64Audio := base64.StdEncoding.EncodeToString(audioData)
// Transcribe audioresponse, err := genkit.Generate(ctx, g, ai.WithModel(whisperModel), ai.WithMessages(ai.NewUserMessage( ai.NewMediaPart("audio/mp3", "data:audio/mp3;base64,"+base64Audio), )), ai.WithConfig(map[string]interface{}{ "language": "en", }),)
if err != nil { log.Fatal(err)}
log.Printf("Transcription: %s", response.Text())