ballerinax/openai.chat Ballerina library

2.0.1

Overview

This is a generated connector for the OpenAI Chat API OpenAPI Specification. OpenAI is an American artificial intelligence research laboratory consisting of a non-profit corporation and a for-profit subsidiary. OpenAI conducts AI research with the declared intention of promoting and developing friendly AI. The OpenAI Chat API provides a way to access the state-of-the-art ChatGPT models developed by OpenAI for a variety of tasks.

Prerequisites

Before using this connector in your Ballerina application, complete the following:

Quick start

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

Step 1: Import the connector

First, import the ballerinax/openai.chat module into the Ballerina project.

Copy
import ballerinax/openai.chat;

Step 2: Create a new connector instance

Create and initialize a chat:Client with the obtained apiKey.

Copy
chat:Client chatClient = check new ({
    auth: {
        token: "sk-XXXXXXXXX"
    }
});

Step 3: Invoke the connector operation

  1. Now you can use the operations available within the connector.

Following is an example on creating a conversation with the GPT-3.5 model. ballerina public function main() returns error? { chat:CreateChatCompletionRequest req = { model: "gpt-3.5-turbo", messages: [{"role": "user", "content": "What is Ballerina?"}] }; chat:CreateChatCompletionResponse res = check chatClient->/chat/completions.post(req); }

Following is an example of using OpenAI vision capabilities when chatting.

```ballerina
public function main() returns error? {
    chat:CreateChatCompletionResponse response = check chatClient->/chat/completions.post(
        {
            model: "gpt-4-vision-preview",
            messages: [
                {
                    "role": "system",
                    "content": "You are a helpful assistant."
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": "Describe the image."
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": "<image_url>"
                            }
                        }
                    ]

                }
            ]
        }
    );
    chat:CreateChatCompletionResponse_choices[] choices = response.choices;
    io:println(choices[0].message?.content);
}
```

2. Use bal run command to compile and run the Ballerina program.

Import

import ballerinax/openai.chat;Copy

Metadata

Released date: 4 months ago

Version: 2.0.1

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.8.0

GraalVM compatible: Yes


Pull count

Total: 4043

Current verison: 959


Weekly downloads


Source repository


Keywords

AI/Chat

OpenAI

Cost/Paid

GPT-3.5

ChatGPT

Vendor/OpenAI


Contributors

Other versions

See more...