Module openai.responses

ballerinax/openai.responses Ballerina library

1.0.0

Overview

OpenAI, an AI research organization focused on creating friendly AI for humanity, offers the OpenAI API to access its powerful AI models for tasks like natural language processing and image generation.

The ballerinax/openai.responses package offers functionality to connect and interact with the Responses API of the OpenAI REST API. The Responses API is OpenAI's most advanced interface for generating model responses — an evolution of Chat Completions that brings added simplicity and powerful agentic primitives such as built-in tools for web search and file search.

The package currently exposes the create model response operation (POST /responses), which covers text and image inputs, text and JSON outputs, function calling, and the built-in tools.

Setup guide

To use the OpenAI Connector, you must have access to the OpenAI API through an OpenAI Platform account and a project under it. If you do not have a OpenAI Platform account, you can sign up for one here.

Create an OpenAI API Key

  1. Open the OpenAI Platform Dashboard.

  2. Navigate to Dashboard -> API keys.

    OpenAI Platform
  3. Click on the "Create new secret key" button.

    OpenAI Platform
  4. Fill the details and click on Create secret key.

    OpenAI Platform
  5. Store the API key securely to use in your application.

    OpenAI Platform

Quickstart

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

Step 1: Import the module

Import the ballerinax/openai.responses module.

Copy
import ballerinax/openai.responses;

Step 2: Create a new connector instance

Create a responses:Client with the obtained API Key and initialize the connector.

Copy
configurable string token = ?;

final responses:Client openAIResponses = check new ({
    auth: {
        token
    }
});

Step 3: Invoke the connector operation

Now, you can utilize the available connector operation.

Create a model response

Copy
public function main() returns error? {
    responses:CreateResponse request = {
        model: "gpt-4o-mini",
        input: "What is Ballerina programming language?"
    };

    responses:Response response =
        check openAIResponses->/responses.post(request);
}

Step 4: Run the Ballerina application

Copy
bal run

Examples

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

  1. CLI assistant - Execute the user's task description by generating and running the appropriate command in the command line interface of their selected operating system.
  2. Image to markdown document converter - Generate detailed markdown documentation based on the image content.

Import

import ballerinax/openai.responses;Copy

Other versions

1.0.0

Metadata

Released date: about 13 hours ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 6

Current verison: 6


Weekly downloads


Source repository


Keywords

AI/Chat

OpenAI

Cost/Paid

GPT-4

ChatGPT

Vendor/OpenAI


Contributors