OpenAI

interface OpenAI

The main interface for the OpenAI API. This interface contains methods for all the API endpoints. To instantiate an instance of this interface, use builder.

All the methods in this class are blocking (except the stream methods, streamCompletion and streamChatCompletion, which return an iterator which blocks the thread).

The methods in this class all throw io exceptions if the request fails. The error message will contain the JSON response from the API (if present). Common errors include:

  1. Not having a valid API key

  2. Passing a bad parameter to a request

Inheritors

Types

Link copied to clipboard
Link copied to clipboard
open class Builder

Constructs a default OpenAI instance.

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
@ApiStatus.Experimental
@Contract(pure = true)
open fun assistants(): AssistantHandler

Returns the handler for the assistants endpoint. This method is purely syntactic sugar for Java users.

Link copied to clipboard
@Contract(pure = true)
abstract fun createChatCompletion(request: ChatRequest): ChatResponse

Calls the chat completions API endpoint. This method is blocking.

Link copied to clipboard
@ApiStatus.Obsolete
@Contract(pure = true)
abstract fun createCompletion(request: CompletionRequest): CompletionResponse

Calls the completions API endpoint. This method is blocking.

Link copied to clipboard
@Contract(pure = true)
@ApiStatus.Experimental
abstract fun createEmbeddings(request: EmbeddingsRequest): EmbeddingsResponse

Calls the embeddings API endpoint to generate the vector representation of text. The returned vector can be used in Machine Learning models. This method is blocking.

Link copied to clipboard
@Contract(pure = true)
open fun files(): FileHandler

Returns the handler for the files endpoint. This method is purely syntactic sugar for Java users.

Link copied to clipboard
@Contract(pure = true)
abstract fun streamChatCompletion(request: ChatRequest): Iterable<ChatResponseChunk>

Calls the chat completions API endpoint and streams each token 1 at a time for a faster response.

Link copied to clipboard

Extension function to stream a chat completion using kotlin coroutines.

Link copied to clipboard
@ApiStatus.Obsolete
@Contract(pure = true)
abstract fun streamCompletion(request: CompletionRequest): Iterable<CompletionResponseChunk>

Calls the completions API endpoint and streams each token 1 at a time for a faster response time.

Link copied to clipboard

Extension function to stream a completion using kotlin coroutines.

Link copied to clipboard
@ApiStatus.Experimental
@Contract(pure = true)
open fun threads(): ThreadHandler

Returns the handler for the threads endpoint. This method is purely syntactic sugar for Java users.

Properties

Link copied to clipboard
@get:ApiStatus.Experimental
abstract val assistants: AssistantHandler

Returns the handler for the assistants endpoint. This handler can be used to create, retrieve, and delete assistants.

Link copied to clipboard
abstract val files: FileHandler

Returns the handler for the files endpoint. This handler can be used to create, retrieve, and delete files.

Link copied to clipboard
@get:ApiStatus.Experimental
abstract val threads: ThreadHandler

Returns the handler for the threads endpoint. This handler can be used to create, retrieve, and delete threads.