openai.finetunes
Module openai.finetunes
API
ballerinax/openai.finetunes Ballerina library
Overview
This is a generated connector for the OpenAI Fine-tunes 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 Fine-tunes API provides a way to customize new AI models developed by OpenAI for your specific needs.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create an OpenAI account.
- Obtain an API key by following these instructions.
Quick start
To use the OpenAI Fine-tunes connector in your Ballerina application, update the .bal
file as follows:
Step 1: Import the connector
First, import the ballerinax/openai.finetunes
module (along with the other required imports) as given below.
import ballerinax/openai.finetunes; import ballerina/io;
Step 2: Create a new connector instance
Create and initialize a finetunes:Client
with the obtained apiKey
.
finetunes:Client finetunesClient = check new ({ auth: { token: "sk-XXXXXXXXX" } });
Step 3: Invoke the connector operation
-
Now, you can use the operations available within the connector.
Following is an example on fine tuning the GPT-3 ada model:
public function main() returns error? { finetunes:CreateFileRequest req = { file: {fileContent: check io:fileReadBytes("sample.jsonl"), fileName: "sample.jsonl"}, purpose: "fine-tune" }; finetunes:OpenAIFile fileRes = check finetunesClient->/files.post(req); string fileId = fileRes.id; finetunes:CreateFineTuneRequest createFineTuneRequest = { model: "ada", training_file: fileId }; finetunes:FineTune res = check finetunesClient->/fine\-tunes.post(createFineTuneRequest); io:println(res.id); }
-
Use the
bal run
command to compile and run the Ballerina program.
Clients
openai.finetunes: Client
This is a generated connector for the [OpenAI API] (https://platform.openai.com/docs/api-reference/introduction) specification. Use the OpenAI API to access the state-of-the-art language models that can complete sentences, transcribe audio, and generate images. The API also supports natural language processing tasks such as text classification, entity recognition, and sentiment analysis. By using the OpenAI API, you can incorporate advanced AI capabilities into your own applications and services.
Constructor
Gets invoked to initialize the connector
.
To use the OpenAI API, you will need an API key. You can sign up for an API key by creating an account on the OpenAI website and following the provided instructions.
init (ConnectionConfig config, string serviceUrl)
- config ConnectionConfig - The configurations to be used when initializing the
connector
- serviceUrl string "https://api.openai.com/v1" - URL of the target service
get files
function get files() returns ListFilesResponse|error
Returns a list of files that belong to the user's organization.
Return Type
- ListFilesResponse|error - OK
post files
function post files(CreateFileRequest payload) returns OpenAIFile|error
Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
Parameters
- payload CreateFileRequest -
Return Type
- OpenAIFile|error - OK
get files/[string file_id]
function get files/[string file_id]() returns OpenAIFile|error
Returns information about a specific file.
Return Type
- OpenAIFile|error - OK
delete files/[string file_id]
function delete files/[string file_id]() returns DeleteFileResponse|error
Delete a file.
Return Type
- DeleteFileResponse|error - OK
get files/[string file_id]/content
function get files/[string file_id]/content() returns byte[]|error
Returns the contents of the specified file
Return Type
- byte[]|error - OK
get 'fine-tunes
function get 'fine\-tunes() returns ListFineTunesResponse|error
List your organization's fine-tuning jobs
Return Type
post 'fine-tunes
function post 'fine\-tunes(CreateFineTuneRequest payload) returns FineTune|error
Creates a job that fine-tunes a specified model from a given dataset.
Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
Parameters
- payload CreateFineTuneRequest -
get 'fine-tunes/[string fine_tune_id]
Gets info about the fine-tune job.
post 'fine-tunes/[string fine_tune_id]/cancel
Immediately cancel a fine-tune job.
get 'fine-tunes/[string fine_tune_id]/events
function get 'fine\-tunes/[string fine_tune_id]/events(boolean 'stream) returns ListFineTuneEventsResponse|error
Get fine-grained status updates for a fine-tune job.
Parameters
- 'stream boolean (default false) - Whether to stream events for the fine-tune job. If set to true, events will be sent as data-only server-sent events as they become available. The stream will terminate with a
data: [DONE]
message when the job is finished (succeeded, cancelled, or failed). If set to false, only events generated so far will be returned.
Return Type
get models
function get models() returns ListModelsResponse|error
Lists the currently available models, and provides basic information about each one such as the owner and availability.
Return Type
- ListModelsResponse|error - OK
get models/[string model]
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
delete models/[string model]
function delete models/[string model]() returns DeleteModelResponse|error
Delete a fine-tuned model. You must have the Owner role in your organization.
Return Type
- DeleteModelResponse|error - OK
Records
openai.finetunes: ClientHttp1Settings
Provides settings related to HTTP/1.x protocol.
Fields
- keepAlive KeepAlive(default http:KEEPALIVE_AUTO) - Specifies whether to reuse a connection for multiple requests
- chunking Chunking(default http:CHUNKING_AUTO) - The chunking behaviour of the request
- proxy ProxyConfig? - Proxy server related options
openai.finetunes: ConnectionConfig
Provides a set of configurations for controlling the behaviours when communicating with a remote HTTP endpoint.
Fields
- auth BearerTokenConfig - Configurations related to client authentication
- httpVersion HttpVersion(default http:HTTP_2_0) - The HTTP version understood by the client
- http1Settings ClientHttp1Settings? - Configurations related to HTTP/1.x protocol
- http2Settings ClientHttp2Settings? - Configurations related to HTTP/2 protocol
- timeout decimal(default 60) - The maximum time to wait (in seconds) for a response before closing the connection
- forwarded string(default "disable") - The choice of setting
forwarded
/x-forwarded
header
- poolConfig PoolConfiguration? - Configurations associated with request pooling
- cache CacheConfig? - HTTP caching related configurations
- compression Compression(default http:COMPRESSION_AUTO) - Specifies the way of handling compression (
accept-encoding
) header
- circuitBreaker CircuitBreakerConfig? - Configurations associated with the behaviour of the Circuit Breaker
- retryConfig RetryConfig? - Configurations associated with retrying
- responseLimits ResponseLimitConfigs? - Configurations associated with inbound response size limits
- secureSocket ClientSecureSocket? - SSL/TLS-related options
- proxy ProxyConfig? - Proxy server related options
- validation boolean(default true) - Enables the inbound payload validation functionality which provided by the constraint package. Enabled by default
openai.finetunes: CreateFileRequest
Fields
- file record { fileContent byte[], fileName string } - Name of the JSON Lines file to be uploaded.
If the
purpose
is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your training examples.
- purpose string - The intended purpose of the uploaded documents. Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file.
openai.finetunes: CreateFineTuneRequest
Fields
- training_file string - The ID of an uploaded file that contains training data.
See upload file for how to upload a file.
Your dataset must be formatted as a JSONL file, where each training
example is a JSON object with the keys "prompt" and "completion".
Additionally, you must upload your file with the purpose
fine-tune
. See the fine-tuning guide for more details.
- validation_file string? - The ID of an uploaded file that contains validation data.
If you provide this file, the data is used to generate validation
metrics periodically during fine-tuning. These metrics can be viewed in
the fine-tuning results file.
Your train and validation data should be mutually exclusive.
Your dataset must be formatted as a JSONL file, where each validation
example is a JSON object with the keys "prompt" and "completion".
Additionally, you must upload your file with the purpose
fine-tune
. See the fine-tuning guide for more details.
- n_epochs int?(default 4) - The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
- batch_size int? - The batch size to use for training. The batch size is the number of training examples used to train a single forward and backward pass. By default, the batch size will be dynamically configured to be ~0.2% of the number of examples in the training set, capped at 256 - in general, we've found that larger batch sizes tend to work better for larger datasets.
- learning_rate_multiplier decimal? - The learning rate multiplier to use for training.
The fine-tuning learning rate is the original learning rate used for
pretraining multiplied by this value.
By default, the learning rate multiplier is the 0.05, 0.1, or 0.2
depending on final
batch_size
(larger learning rates tend to perform better with larger batch sizes). We recommend experimenting with values in the range 0.02 to 0.2 to see what produces the best results.
- prompt_loss_weight decimal?(default 0.01) - The weight to use for loss on the prompt tokens. This controls how much the model tries to learn to generate the prompt (as compared to the completion which always has a weight of 1.0), and can add a stabilizing effect to training when completions are short. If prompts are extremely long (relative to completions), it may make sense to reduce this weight so as to avoid over-prioritizing learning the prompt.
- compute_classification_metrics boolean?(default false) - If set, we calculate classification-specific metrics such as accuracy
and F-1 score using the validation set at the end of every epoch.
These metrics can be viewed in the results file.
In order to compute classification metrics, you must provide a
validation_file
. Additionally, you must specifyclassification_n_classes
for multiclass classification orclassification_positive_class
for binary classification.
- classification_n_classes int? - The number of classes in a classification task. This parameter is required for multiclass classification.
- classification_positive_class string? - The positive class in binary classification. This parameter is needed to generate precision, recall, and F1 metrics when doing binary classification.
- classification_betas decimal[]? - If this is provided, we calculate F-beta scores at the specified beta values. The F-beta score is a generalization of F-1 score. This is only used for binary classification. With a beta of 1 (i.e. the F-1 score), precision and recall are given the same weight. A larger beta score puts more weight on recall and less on precision. A smaller beta score puts more weight on precision and less on recall.
- suffix string? - A string of up to 40 characters that will be added to your fine-tuned model name.
For example, a
suffix
of "custom-model-name" would produce a model name likeada:ft-your-org:custom-model-name-2022-02-15-04-21-04
.
openai.finetunes: DeleteFileResponse
Fields
- id string -
- 'object string -
- deleted boolean -
openai.finetunes: DeleteModelResponse
Fields
- id string -
- 'object string -
- deleted boolean -
openai.finetunes: FineTune
Fields
- id string -
- 'object string -
- created_at int -
- updated_at int -
- model string -
- fine_tuned_model string -
- organization_id string -
- status string -
- hyperparams record {} -
- training_files OpenAIFile[] -
- validation_files OpenAIFile[] -
- result_files OpenAIFile[] -
- events FineTuneEvent[]? -
openai.finetunes: FineTuneEvent
Fields
- 'object string -
- created_at int -
- level string -
- message string -
openai.finetunes: ListFilesResponse
Fields
- 'object string -
- data OpenAIFile[] -
openai.finetunes: ListFineTuneEventsResponse
Fields
- 'object string -
- data FineTuneEvent[] -
openai.finetunes: ListFineTunesResponse
Fields
- 'object string -
- data FineTune[] -
openai.finetunes: ListModelsResponse
Fields
- 'object string -
- data Model[] -
openai.finetunes: Model
Fields
- id string -
- 'object string -
- created int -
- owned_by string -
openai.finetunes: OpenAIFile
Fields
- id string -
- 'object string -
- bytes int -
- created_at int -
- filename string -
- purpose string -
- status string? -
- status_details record {}?(default ()) -
openai.finetunes: ProxyConfig
Proxy server configurations to be used with the HTTP client endpoint.
Fields
- host string(default "") - Host name of the proxy server
- port int(default 0) - Proxy server port
- userName string(default "") - Proxy server username
- password string(default "") - Proxy server password
Import
import ballerinax/openai.finetunes;
Metadata
Released date: about 1 year ago
Version: 1.0.5
License: Apache-2.0
Compatibility
Platform: any
Ballerina version: 2201.4.1
Pull count
Total: 17
Current verison: 4
Weekly downloads
Keywords
AI/Fine-tunes
OpenAI
Cost/Paid
Files
Models
Vendor/OpenAI
Contributors