Module openai.assistants

ballerinax/openai.assistants 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 ballarinax/openai.Assistants connector allows developers to seamlessly integrate OpenAI's advanced language models into their applications by interacting with OpenAI REST API v1. This connector provides tools to build powerful OpenAI Assistants capable of performing a wide range of tasks, such as generating human-like text, managing conversations with persistent threads, and utilizing multiple tools in parallel. OpenAI has recently announced a variety of new features and improvements to the Assistants API, moving their Beta to a new API version, OpenAI-Beta: assistants=v2. The users can interact with both the API v1 and v2 by passing the respective API version header with the request.

Setup guide

To use the OpenAI Connector, you must have access to the OpenAI API through a 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 a 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 Assistants connector in your Ballerina application, update the .bal file as follows:

Step 1: Import the module

Import the openai.assistants module.

Copy
import ballerinax/openai.assistants;

Step 2: Instantiate a new connector

Create a assistants:ConnectionConfig with the obtained access token and initialize the connector with it.

Copy
configurable string token = ?;

final assistants:Client openAIAssistant = check new ({
    auth: {
        token
    }
});

Setting HTTP Headers in Ballerina

Calls to the Assistants API require that you pass a beta HTTP header. In Ballerina, you can define the header as follows:

Copy
final map<string|string[]> headers = {
    "OpenAI-Beta": ["assistants=v2"]
};

Step 3: Invoke the connector operations

Now, utilize the available connector operations.

Copy
public function main() returns error? {

    // define the required tool
    assistants:AssistantToolsCode tool = {
        type: "code_interpreter"
    };

    // define the assistant request object
    assistants:CreateAssistantRequest request = {
        model: "gpt-3.5-turbo",
        name: "Math Tutor",
        description: "An Assistant for personal math tutoring",
        instructions: "You are a personal math tutor. Help the user with their math questions.",
        tools: [tool]
    };

    // call the `post assistants` resource to create an Assistant
    assistants:AssistantObject assistantResponse = check openAIAssistant->/assistants.post(request, headers);
}

Step 4: Run the Ballerina application

Copy
bal run

Examples

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

  1. Math tutor bot - Create an assistant to solve mathematical problems with step-by-step solutions and interactive guidance.

  2. Weather assistant - Develop an assistant to provide weather information by leveraging function calls for temperature and rain probability.

Import

import ballerinax/openai.assistants;Copy

Metadata

Released date: about 1 month ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.9.3

GraalVM compatible: Yes


Pull count

Total: 0

Current verison: 0


Weekly downloads


Source repository


Keywords

AI/assitants

vendor/OpenAI

cost/paid

custom-bot

run

threads


Contributors

Other versions

1.0.0