openai.text
Module openai.text
ballerinax/openai.text Ballerina library
Overview
This is a generated connector for the OpenAI Completions 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 Completions API provides a way to access new Text AI models developed by OpenAI for a variety of tasks.
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 text connector in your Ballerina application, update the .bal
file as follows:
Step 1: Import the connector
First, import the ballerinax/openai.text
module (along with the other required imports) as given below.
import ballerinax/openai.text; import ballerina/io;
Step 2: Create a new connector instance
Create and initialize a text:Client
with the obtained apiKey
.
text:Client textClient = 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 text completion using the OpenAI Davinci model:
public function main() returns error? { text:CreateCompletionRequest createCompletionRequest = { model: "text-davinci-002", prompt: "What is Ballerina?" }; text:CreateCompletionResponse res = check textClient->/completions.post(createCompletionRequest); io:println(res); }
-
Use the
bal run
command to compile and run the Ballerina program.
Clients
openai.text: 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
post completions
function post completions(CreateCompletionRequest payload) returns CreateCompletionResponse|error
Creates a completion for the provided prompt and parameters
Parameters
- payload CreateCompletionRequest -
Return Type
post edits
function post edits(CreateEditRequest payload) returns CreateEditResponse|error
Creates a new edit for the provided input, instruction, and parameters.
Parameters
- payload CreateEditRequest -
Return Type
- CreateEditResponse|error - OK
Records
openai.text: 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.text: 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.text: CreateCompletionRequest
Fields
- model string - ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
- prompt string|string[]|int[]|PromptItemsArray[]?(default "<|endoftext|>") - The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
- suffix string?(default ()) - The suffix that comes after a completion of inserted text.
- temperature decimal?(default 1) - What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or
top_p
but not both.
- top_p decimal?(default 1) - An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or
temperature
but not both.
- n int?(default 1) - How many completions to generate for each prompt.
Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for
max_tokens
andstop
.
- 'stream boolean?(default false) - Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a
data: [DONE]
message.
- logprobs int?(default ()) - Include the log probabilities on the
logprobs
most likely tokens, as well the chosen tokens. For example, iflogprobs
is 5, the API will return a list of the 5 most likely tokens. The API will always return thelogprob
of the sampled token, so there may be up tologprobs+1
elements in the response. The maximum value forlogprobs
is 5. If you need more than this, please contact us through our Help center and describe your use case.
- echo boolean?(default false) - Echo back the prompt in addition to the completion
- presence_penalty decimal?(default 0) - Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. See more information about frequency and presence penalties.
- frequency_penalty decimal?(default 0) - Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. See more information about frequency and presence penalties.
- best_of int?(default 1) - Generates
best_of
completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed. When used withn
,best_of
controls the number of candidate completions andn
specifies how many to return –best_of
must be greater thann
. Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings formax_tokens
andstop
.
- logit_bias record {}? - Modify the likelihood of specified tokens appearing in the completion.
Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this tokenizer tool (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
As an example, you can pass
{"50256": -100}
to prevent the <|endoftext|> token from being generated.
- user string? - A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
openai.text: CreateCompletionResponse
Fields
- id string -
- 'object string -
- created int -
- model string -
- choices CreateCompletionResponse_choices[] -
- usage CreateCompletionResponse_usage? -
openai.text: CreateCompletionResponse_choices
Fields
- text string? -
- index int? -
- logprobs CreateCompletionResponse_logprobs?(default ()) -
- finish_reason string?(default ()) -
openai.text: CreateCompletionResponse_logprobs
Fields
- tokens string[]? -
- token_logprobs decimal[]? -
- top_logprobs record {}[]? -
- text_offset int[]? -
openai.text: CreateCompletionResponse_usage
Fields
- prompt_tokens int -
- completion_tokens int -
- total_tokens int -
openai.text: CreateEditRequest
Fields
- model string - ID of the model to use. You can use the
text-davinci-edit-001
orcode-davinci-edit-001
model with this endpoint.
- input string?(default "") - The input text to use as a starting point for the edit.
- instruction string - The instruction that tells the model how to edit the prompt.
- n int?(default 1) - How many edits to generate for the input and instruction.
- temperature decimal?(default 1) - What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or
top_p
but not both.
- top_p decimal?(default 1) - An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or
temperature
but not both.
openai.text: CreateEditResponse
Fields
- 'object string -
- created int -
- choices CreateEditResponse_choices[] -
- usage CreateCompletionResponse_usage -
openai.text: CreateEditResponse_choices
Fields
- text string? -
- index int? -
- logprobs CreateCompletionResponse_logprobs?(default ()) -
- finish_reason string? -
openai.text: 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.text;
Metadata
Released date: over 1 year ago
Version: 1.0.4
License: Apache-2.0
Compatibility
Platform: any
Ballerina version: 2201.4.1
GraalVM compatible: Yes
Pull count
Total: 4429
Current verison: 852
Weekly downloads
Keywords
AI/Text
OpenAI
Cost/Paid
Completions
GPT-3
GPT3.5
Vendor/OpenAI
Contributors