Module ai.openai

ballerinax/ai.openai Ballerina library
1.2.0
Overview
This module offers APIs for connecting with OpenAI Large Language Models (LLM).
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);
Step 4: Invoke chat completion
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}]; ai:ChatAssistantMessage response = check openAiModel->chat(chatMessages, tools = []); chatMessages.push(response);