Learn R Programming

openaiRtools (version 0.2.2)

create_embedding: Create Text Embeddings (Convenience Function)

Description

Shortcut that automatically creates an OpenAI client and calls client$embeddings$create(). The API key is read from the OPENAI_API_KEY environment variable.

Usage

create_embedding(input, model = "text-embedding-ada-002", ...)

Value

A named list with $data[[i]]$embedding containing the numeric embedding vectors.

Arguments

input

Required. A character string or list of strings to embed.

model

Character. Embedding model. Default: "text-embedding-ada-002".

...

Additional parameters passed to EmbeddingsClient$create(), such as dimensions or encoding_format.

Examples

Run this code
if (FALSE) {
Sys.setenv(OPENAI_API_KEY = "sk-xxxxxx")

# Single embedding
resp <- create_embedding("Hello world", model = "text-embedding-3-small")
vec <- unlist(resp$data[[1]]$embedding)
cat("Vector length:", length(vec))
}

Run the code above in your browser using DataLab