ballerinax/np Ballerina library

0.9.0
Overview

This library module provides implementations of ballerina/np:ModelProvider to use explicitly with natural expressions.

Initializing the model

OpenAI

Copy
import ballerinax/np.openai;

configurable string? serviceUrl = ();
configurable string model = ?;
configurable string token = ?;

final openai:ModelProvider openAI = check new ({
        connectionConfig: {
            auth: {
                token
            }
        },
        serviceUrl
    }, 
    model
);

Specify values for the configurable variables in the Config.toml file.

Copy
serviceUrl = "<SERVICE_URL>"
model = "<MODEL>"
token = "<TOKEN>"

Azure OpenAI

Copy
import ballerinax/np.azure.openai as azureOpenAI;

configurable string serviceUrl = ?;
configurable string deploymentId = ?;
configurable string apiVersion = ?;
configurable string apiKey = ?;

final azureOpenAI:ModelProvider azureOpenAIModel = check new (
    {
        serviceUrl, 
        connectionConfig: {
            auth: {
                apiKey
            }
        }
    },
    deploymentId, 
    apiVersion
);

Specify values for the configurable variables in the Config.toml file.

Copy
serviceUrl = "<SERVICE_URL>"
deploymentId = "<DEPLOYMENT_ID>"
apiVersion = "<API_VERSION>"
connectionConfig.auth.apiKey = "<YOUR_API_KEY>"

Using the model

A model of these types can be used in a natural expression.

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

configurable string? serviceUrl = ();
configurable string model = ?;
configurable string token = ?;

type Author record {| 
    string name;
    string dateOfBirth;
    record {|
        string name;
        string yearOfPublication;
        string genre;
    |}[] works;
|};

isolated function getAuthorDetails(string month, int count, np:ModelProvider model) 
        returns Author|error => natural (model) {
    Who is a popular author born in ${month}? What are their ${count} most popular works?
};

public function main() returns error? {
    openai:ModelProvider openAI = check new ({
            connectionConfig: {
                auth: {
                    token
                }
            },
            serviceUrl
        }, 
        model
    );

    Author author = check getAuthorDetails("July", 5, openAI);
    io:println("Name: ", author.name);
    io:println("Date of Birth: ", author.dateOfBirth);
    foreach var work in author.works {
        io:println("Title: ", work.name);
        io:println("Year of Publication: ", work.yearOfPublication);
        io:println("Genre: ", work.genre);
    }
}

Import

import ballerinax/np;Copy

Metadata

Released date: 14 days ago

Version: 0.9.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.13.0-m1

GraalVM compatible: Yes


Pull count

Total: 2269

Current verison: 4


Weekly downloads


Source repository


Keywords

natural programming

ai


Contributors

Other versions

See more...