check_api_key {PacketLLM} | R Documentation |
Check API Key
Description
This function checks if the API key assigned to the OPENAI_API_KEY
variable
exists in the environment variables (e.g., in the .Renviron file).
If the key is not set, the function will raise an error and stop execution.
If the key is set, it returns TRUE invisibly.
Usage
check_api_key()
Value
Invisible TRUE
(invisible(TRUE)
) if the API key is set.
Otherwise, stops execution with an error.
Examples
## Not run:
# This function requires the OPENAI_API_KEY environment variable to be set.
# You can check if the key is set using Sys.getenv("OPENAI_API_KEY").
# If the key is set, calling check_api_key() will return TRUE invisibly.
# If the key is NOT set, it will stop execution with an error message.
# Example demonstrating how to handle the potential error if the key is missing:
result <- tryCatch({
check_api_key()
"API key found." # Message if check passes
}, error = function(e) {
# Handle the error if the key is missing
paste("Error:", e$message)
})
print(result)
## End(Not run)
[Package PacketLLM version 0.1.0 Index]