Module azure.openai.chat

ballerinax/azure.openai.chat Ballerina library

4.0.0
Ballerina Azure OpenAI Chat Completions connector

Build GitHub Last Commit GitHub Issues

Overview

Azure OpenAI provides access to OpenAI's powerful language models including GPT-4o, GPT-4, and o-series models through Microsoft Azure's enterprise-grade infrastructure. It combines OpenAI's advanced AI capabilities with Azure's security, compliance, and regional availability features.

The ballerinax/azure.openai.chat package offers functionality to connect and interact with the Chat Completions API of the Azure AI Foundry Models Service. The Chat Completions API enables you to build conversational AI applications with features like multi-turn conversations, function/tool calling, structured outputs, and vision capabilities.

Setup guide

To use the Azure OpenAI Chat Completions Connector, you must have access to an Azure OpenAI resource through a Microsoft Azure account. If you do not have an Azure account, you can sign up for one here.

Create an Azure OpenAI resource and obtain the API key

  1. Sign in to the Azure Portal.

  2. Search for "Azure OpenAI" in the top search bar and select Azure OpenAI from the results.

  3. Click Create to create a new Azure OpenAI resource. Fill in the required details such as subscription, resource group, region, and resource name, then click Review + create and finally Create.

  4. Once the resource is deployed, navigate to your Azure OpenAI resource.

  5. In the left-hand menu, go to Resource Management -> Keys and Endpoint.

  6. Copy one of the provided keys (Key 1 or Key 2) and the endpoint URL. Store them securely to use in your application.

  7. Append /openai/v1 to the endpoint URL. The portal shows the resource root (for example https://<resource-name>.openai.azure.com/), while this connector targets the v1 API surface, so the value to pass as serviceUrl is https://<resource-name>.openai.azure.com/openai/v1. Resources provisioned through Azure AI Foundry may instead show https://<resource-name>.services.ai.azure.com/, which becomes https://<resource-name>.services.ai.azure.com/openai/v1.

Quickstart

To use the Azure OpenAI Chat Completions connector in your Ballerina application, update the .bal file as follows:

Step 1: Import the module

Import the ballerinax/azure.openai.chat module.

Copy
import ballerinax/azure.openai.chat;

Step 2: Create a new connector instance

Create a chat:Client with your Azure OpenAI resource endpoint and one credential. Azure accepts either an API key or a Microsoft Entra ID access token, and the two are alternatives — provide whichever you have, not both.

serviceUrl must be the endpoint including the /openai/v1 base path, as described in step 7 of the setup guide — https://<resource-name>.openai.azure.com/openai/v1. Passing the bare resource root makes every request return 404.

Using the API key obtained in the setup guide:

Copy
configurable string apiKey = ?;
configurable string serviceUrl = ?;

final chat:Client azureOpenAIChat = check new ({
    auth: {
        api\-key: apiKey
    }
}, serviceUrl);

Alternatively, using a Microsoft Entra ID access token, which is sent as an Authorization: Bearer header:

Copy
configurable string token = ?;
configurable string serviceUrl = ?;

final chat:Client azureOpenAIChat = check new ({
    auth: {
        token
    }
}, serviceUrl);

Step 3: Invoke the connector operation

Now, you can utilize available connector operations.

Create a chat completion

Copy
public function main() returns error? {

    chat:ChatCompletionsBody request = {
        model: "gpt-4o-mini",
        messages: [
            {role: "user", "content": "What is the Ballerina programming language?"}
        ]
    };

    chat:InlineResponse200 response = check azureOpenAIChat->/chat/completions.post(request);
}

Step 4: Run the Ballerina application

Copy
bal run

Examples

The Azure OpenAI Chat Completions connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering the following use cases:

  1. Chat completion - Create a basic chat completion using the Azure OpenAI Chat Completions API.
  2. Function calling - Use function/tool calling to extend the model's capabilities with custom functions.

Import

import ballerinax/azure.openai.chat;Copy

Other versions

See more...

Metadata

Released date: about 13 hours ago

Version: 4.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 25638

Current verison: 9


Weekly downloads


Source repository


Keywords

AI/Chat

Azure

Azure OpenAI

Chat Completions

Vendor/Microsoft


Contributors