ballerinax/ai.azure Ballerina library

1.5.0

Overview

Azure OpenAI Service provides access to OpenAI's powerful language models within the Microsoft Azure platform.

The Azure OpenAI connector offers APIs for connecting with Azure OpenAI Large Language Models (LLMs), enabling the integration of advanced conversational AI, text generation, and language processing capabilities into applications.

Key Features

  • Connect and interact with Azure OpenAI Large Language Models (LLMs)
  • Support for GPT-5, GPT-4, GPT-3.5, and other advanced OpenAI models
  • Support for both the Chat Completions API and the Responses API, selected through the apiType parameter
  • Seamless integration with Azure AI infrastructure
  • Secure communication with API key and token authentication

API surfaces

It provides a single chat-model provider class, OpenAiModelProvider, which implements ai:ModelProvider. The provider can target either the Azure OpenAI Chat Completions API (the default) or the Responses API, selected at initialization time through the apiType parameter. The concrete wire route additionally depends on the shape of the serviceUrl: a URL ending with /v1 targets the Azure OpenAI v1 GA surface through the generated ballerinax/azure.openai.chat / ballerinax/azure.openai.responses connectors, while any other URL targets the legacy route (with an ?api-version=... query parameter).

The new v1 GA URL is https://<resource>.services.ai.azure.com/openai/v1; the legacy URL is https://<resource>.openai.azure.com/openai.

apiTypeserviceUrl ends with /v1 (v1 GA)otherwise (legacy)
CHAT_COMPLETIONS (default)POST {serviceUrl}/chat/completionsPOST {legacyBase}/deployments/{deploymentId}/chat/completions?api-version=...
RESPONSESPOST {serviceUrl}/responsesPOST {legacyBase}/responses?api-version=...

On the legacy surface, legacyBase is derived from the serviceUrl as follows:

  • a bare origin (e.g. https://<resource>.openai.azure.com) is completed with /openai, matching Azure's own legacy spec server (https://{endpoint}/openai);
  • a URL that already carries a path is used verbatim. This keeps existing .../openai service URLs working unchanged, and lets callers who front Azure OpenAI through API Management or another gateway (e.g. https://gw.example.com/azure-openai) own their base path without the module rewriting it.

The apiVersion argument is required for legacy (non-/v1) service URLs (e.g. "2024-10-21"). For v1 (/v1) service URLs it is optional and normally omitted; pass "preview" or "v1" to opt into a specific v1 surface.

This module also provides an EmbeddingProvider for Azure OpenAI embedding models. It resolves the apiVersion and the legacy base URL exactly the same way, so one serviceUrl means the same thing to both providers:

serviceUrl ends with /v1 (v1 GA)otherwise (legacy)
POST {serviceUrl}/embeddings (deployment sent as model in the body)POST {legacyBase}/deployments/{deploymentId}/embeddings?api-version=...
Copy
// Legacy service URL — a date-based `apiVersion` is required.
final ai:EmbeddingProvider legacyEmbeddingProvider = check new azure:EmbeddingProvider(
    "https://<resource>.openai.azure.com/openai", "api-key", "2023-05-15", "deployment-id");

// v1 GA service URL — the `apiVersion` is not needed, so pass `()`.
final ai:EmbeddingProvider embeddingProvider = check new azure:EmbeddingProvider(
    "https://<resource>.services.ai.azure.com/openai/v1", "api-key", (), "deployment-id");

Prerequisites

Before using this module in your Ballerina application, first you must obtain the nessary configuration to engage the LLM.

Quickstart

To use the ai.azure module in your Ballerina application, update the .bal file as follows:

Step 1: Import the module

Import the ai.azure; module.

Copy
import ballerinax/ai.azure;

Step 2: Intialize the Model Provider

Initialize the provider. By default it uses the Chat Completions API. On a legacy (non-/v1) service URL a date-based apiVersion is required:

Copy
import ballerina/ai;
import ballerinax/ai.azure;

final ai:ModelProvider azureOpenAiModel = check new azure:OpenAiModelProvider(
    "https://<resource>.openai.azure.com", "api-key", "deployment-id", "2024-10-21");

To use the Responses API instead, set apiType to RESPONSES:

Copy
final ai:ModelProvider azureOpenAiModel = check new azure:OpenAiModelProvider(
    "https://<resource>.openai.azure.com", "api-key", "deployment-id", "2025-03-01-preview",
    apiType = azure:RESPONSES);

To target the Azure OpenAI v1 GA surface, use a /v1-suffixed service URL; the apiVersion is then optional and can be omitted:

Copy
final ai:ModelProvider azureOpenAiModel = check new azure:OpenAiModelProvider(
    "https://<resource>.services.ai.azure.com/openai/v1", "api-key", "deployment-id");

Step 4: Invoke chat completion

Copy
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}];
ai:ChatAssistantMessage response = check azureOpenAiModel->chat(chatMessages, tools = []);

chatMessages.push(response);

Import

import ballerinax/ai.azure;Copy

Other versions

See more...

Metadata

Released date: 7 days ago

Version: 1.5.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 17351

Current verison: 59


Weekly downloads


Source repository


Keywords

Agent

Azure

Model

Provider

Vendor/Microsoft

Area/AI & Machine Learning

Type/Embedding Provider

Type/Model Provider

Type/Connector

Name/Azure Model/Embedding Provider


Contributors