ballerinax/openai.text Ballerina library

1.0.2

Overview

This is a generated connector for the OpenAI Completions 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 Completions API provides a way to access new Text AI models developed by OpenAI for a variety of tasks.

Prerequisites

Before using this connector in your Ballerina application, complete the following:

Quick start

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

Step 1: Import the connector

First, import the ballerinax/openai.text module into the Ballerina project.

Copy
import ballerinax/openai.text;
import ballerina/io;

Step 2: Create a new connector instance

Create and initialize a text:Client with the obtained apiKey.

Copy
text:Client textClient = check new ({
    auth: {
        token: "sk-XXXXXXXXX"
    }
});

Step 3: Invoke the connector operation

  1. Now you can use the operations available within the connector.

    Following is an example on text completion using the OpenAI Davinci model:

    Generate text with Davinci

    Copy
    public function main() returns error? {
        text:CreateCompletionRequest createCompletionRequest = {
            model: "text-davinci-002",
            prompt: "What is Ballerina?"
        };
        text:CreateCompletionResponse|error unionResult = check textClient->/completions.post(createCompletionRequest);
        if unionResult is text:CreateCompletionResponse {
            io:println(unionResult);
        } else {
            io:println(unionResult);
        }
    }
  2. Use the bal run command to compile and run the Ballerina program.

Sample

Copy
import ballerinax/openai.text;
import ballerina/io;

text:Client textClient = check new ({
    auth: {
        token: "sk-XXXXXXXXX"
    }
});

public function main() returns error? {
    text:CreateCompletionRequest createCompletionRequest = {
        model: "text-davinci-002",
        prompt: "What is Ballerina?"
    };
    text:CreateCompletionResponse|error unionResult = check textClient->/completions.post(createCompletionRequest);
    if unionResult is text:CreateCompletionResponse {
        io:println(unionResult);
    } else {
        io:println(unionResult);
    }
}

Import

import ballerinax/openai.text;Copy

Metadata

Released date: over 1 year ago

Version: 1.0.2

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.4.1

GraalVM compatible: Yes


Pull count

Total: 4429

Current verison: 53


Weekly downloads


Source repository


Keywords

AI/Text

OpenAI

Cost/Paid

Completions

GPT-3

GPT3.5

Vendor/OpenAI


Contributors

Other versions

See more...