Module ai.openai
ballerinax/ai.openai Ballerina library
Overview
OpenAI provides powerful AI models for natural language processing, image generation, and other advanced tasks.
The OpenAI connector offers APIs for connecting with 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 OpenAI Large Language Models (LLMs)
- Support for GPT-6, GPT-5, GPT-4.1, GPT-4o, and other advanced models
- Efficient handling of conversational prompts and completions
- Secure communication with API key authentication
Prerequisites
Before using this module in your Ballerina application, first you must obtain the nessary configuration to engage the LLM.
- Create an OpenAI account.
- Obtain an API key by following these instructions.
Quickstart
To use the ai.openai module in your Ballerina application, update the .bal file as follows:
Step 1: Import the module
Import the ai.openai; module.
import ballerinax/ai.openai;
Step 2: Intialize the Model Provider
Here's how to initialize the Model Provider:
import ballerina/ai; import ballerinax/ai.openai; final ai:ModelProvider openAiModel = check new openai:ModelProvider("openAiApiKey", modelType = openai:GPT_4O);
Note: Some models are restricted on the default Chat Completions API:
O3_PRO,GPT_5_PRO,GPT_5_2_PRO,GPT_5_3_CODEX,GPT_5_4_PROandGPT_5_5_PROare available only withapiType = openai:RESPONSES.- Tool calling and
generate()withGPT_6_ASTRA,GPT_6_1_SOLandGPT_6_1_TERRArequireapiType = openai:RESPONSES.- Tool calling and
generate()with theGPT_5_6family,GPT_6_SOLandGPT_6_LUNArequirereasoningEffort = openai:NONE, orapiType = openai:RESPONSES.
Step 4: Invoke chat completion
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}]; ai:ChatAssistantMessage response = check openAiModel->chat(chatMessages, tools = []); chatMessages.push(response);