Module openai

ballerinax/openai Ballerina library

1.0.0

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's RESTful API 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/Open 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.

Copy
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:

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

    Copy
    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

Copy
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

Copy
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.

Import

import ballerinax/openai;Copy

Other versions

1.0.0

Metadata

Released date: 19 days ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 3

Current verison: 3


Weekly downloads


Source repository


Keywords

openai

ai

ChatGPT


Contributors