Skip to content

ballerina-platform/module-ballerinax-openai

Repository files navigation

Ballerina OpenAI connector

Build GitHub Last Commit GitHub Issues

Overview

OpenAI provides a suite of powerful AI models and services for natural language processing, code generation, image understanding, and more.

The ballerinax/openai package offers APIs to easily connect and interact with OpenAI API v2.3.0 endpoints, enabling seamless integration with models such as GPT, Whisper, and DALL·E.

Setup guide

To use the OpenAI connector, you must have access to the OpenAI API through an OpenAI account and API key.
If you do not have an OpenAI account, you can sign up for one here.

Step 1: Create an OpenAI account

  1. Visit the OpenAI Platform.
  2. Sign in with your existing credentials, or create a new OpenAI account if you don’t already have one.

Step 2: Create a project

  1. Once logged in, click on your profile icon in the top-right corner.

  2. In the dropdown menu, click "Your Profile".

    Your Profile

  3. Then navigate to the "Projects" section from the sidebar to create a new project.

    Project Portal

  4. Click the "Create Project" button to create a new project.

    Create Project

Step 3: Navigate to API Keys

  1. Navigate to the "API Keys" section from the sidebar and click the “+ Create new secret key” button. to create a new API Key.

    New API key Portal

  2. Provide a name for the key (e.g., "Connector Key") and select Project name and then confirm.

    Create New API Key

  3. Copy the generated API key and store it securely. ( Note: You will not be able to view it again later.)

    Copy Generated Key

Quickstart

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

Step 1: Import the module

Import the openai module.

import ballerinax/openai;

Step 2: Instantiate a new connector

  1. Create a Config.toml file and, configure the obtained credentials in the above steps as follows:

    token = "<Access Token>"
  2. Create a openai:ConnectionConfig with the obtained access token and initialize the connector with it.

    configurable string token = ?;
    
    final openai:Client openai = check new({
       auth: {
          token
       }
    });

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

Create an Assistant

public function main() returns error? {
   openai:CreateAssistantRequest request = {
        model: "gpt-4o",
        name: "Math Tutor",
        description: null,
        instructions: "You are a personal math tutor.",
        tools: [{"type": "code_interpreter"}],
        toolResources: {"code_interpreter": {"file_ids": []}},
        metadata: {},
        topP: 1.0,
        temperature: 1.0,
        responseFormat: {"type": "text"}
   };

   //Note: This header is required because the Assistants API is currently in beta, and OpenAI requires explicit opt-in.
   configurable map<string> headers = {
    "OpenAI-Beta": "assistants=v2"
   };

   openai:AssistantObject response = check openai->/assistants.post(request, headers = headers);
}

Step 4: Run the Ballerina application

bal run

Examples

The ballerinax/openai connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering the following use cases:

  1. Financial Assistant - Build a Personal Finance Assistant that helps users manage their budget, track expenses, and get financial advice.
  2. Marketing Image Generator - Creates an assistant that takes a user’s description from the console, makes a DALL·E image with it.

Build from the source

Setting up the prerequisites

  1. Download and install Java SE Development Kit (JDK) version 21. You can download it from either of the following sources:

    Note: After installation, remember to set the JAVA_HOME environment variable to the directory where JDK was installed.

  2. Download and install Ballerina Swan Lake.

  3. Download and install Docker.

    Note: Ensure that the Docker daemon is running before executing any tests.

  4. Export Github Personal access token with read package permissions as follows,

    export packageUser=<Username>
    export packagePAT=<Personal access token>

Build options

Execute the commands below to build from the source.

  1. To build the package:

    ./gradlew clean build
  2. To run the tests:

    ./gradlew clean test
  3. To build the without the tests:

    ./gradlew clean build -x test
  4. To run tests against different environments:

    ./gradlew clean test -Pgroups=<Comma separated groups/test cases>
  5. To debug the package with a remote debugger:

    ./gradlew clean build -Pdebug=<port>
  6. To debug with the Ballerina language:

    ./gradlew clean build -PbalJavaDebug=<port>
  7. Publish the generated artifacts to the local Ballerina Central repository:

    ./gradlew clean build -PpublishToLocalCentral=true
  8. Publish the generated artifacts to the Ballerina Central repository:

    ./gradlew clean build -PpublishToCentral=true

Contribute to Ballerina

As an open-source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All the contributors are encouraged to read the Ballerina Code of Conduct.

Useful links

About

Ballerina OpenAI Connector

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 5