Module openai.moderations
![](https://bcentral-packageicons.azureedge.net/images/ballerinax_openai.moderations_1.0.2.png)
ballerinax/openai.moderations Ballerina library
Overview
This is a generated connector for the OpenAI moderations
API OpenAPI Specification. OpenAI is an American artificial intelligence research laboratory consisting of a non-profit corporation and a for-profit subsidiary. OpenAI conducts AI research with the declared intention of promoting and developing friendly AI. The OpenAI Moderations API provides a way to access new moderation models developed by OpenAI for content moderation tasks.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create an OpenAI account.
- Obtain an API key by following these instructions.
Quick start
To use the OpenAI moderations
connector in your Ballerina application, update the .bal
file as follows:
Step 1: Import the connector
First, import the ballerinax/openai.moderations
module into the Ballerina project.
import ballerinax/openai.moderations; import ballerina/io;
Step 2: Create a new connector instance
Create and initialize a moderations:Client
with the obtained apiKey
.
moderations:Client moderationsClient = check new ({ auth: { token: "sk-XXXXXXXXX" } });
Step 3: Invoke the connector operation
-
Now you can use the operations available within the connector.
Following is an example on identifying harmful content using the OpenAI moderations model:
Moderate with Text Moderation model
public function main() returns error? { moderations:CreateModerationRequest createModerationRequest = { input: "I want to kill them.", model: "text-moderation-stable" }; moderations:CreateModerationResponse|error unionResult = check moderationsClient->/moderations.post(createModerationRequest); if unionResult is moderations:CreateModerationResponse { io:println(unionResult); } else { io:println(unionResult); } }
-
Use the
bal run
command to compile and run the Ballerina program.
Sample
import ballerinax/openai.moderations; import ballerina/io; moderations:Client moderationsClient = check new ({ auth: { token: "sk-XXXXXXXXX" } }); public function main() returns error? { moderations:CreateModerationRequest createModerationRequest = { input: "I want to kill them.", model: "text-moderation-stable" }; moderations:CreateModerationResponse|error unionResult = check moderationsClient->/moderations.post(createModerationRequest); if unionResult is moderations:CreateModerationResponse { io:println(unionResult); } else { io:println(unionResult); } }