Create and Validate Remote LLM API Credentials
new_RemoteLlmApi.RdThe new_RemoteLlmApi() function constructs an S3 object that stores API credentials for interacting with Large Language Models (LLMs) such as OpenAI's GPT models and DeepSeek models. It reads the API key from a specified file, validates its format, ensures it matches the correct provider, and checks if the key is valid by performing a test request.
Arguments
- api_key_path
Character string specifying the path to a file containing the API key.
- provider
Character string specifying the provider for the API key. Must be either "OpenAI" or "DeepSeek".
- no_internet
Logical, indicating whether to skip internet checks. If `TRUE`, the function will not attempt to validate the API key via a network request.
- exclude_pattern
Character, a regex pattern to exclude certain models from the list of available models, e.g. "babbage|curie|dall-e|davinci|text-embedding|tts|whisper"
Value
An object of class RemoteLlmApi, containing the API key, URL, and provider name (either "OpenAI" or "DeepSeek"), or a list with an "error" attribute if construction fails.
Details
This function includes multiple validation steps: - Reads the API key from the specified file. - Ensures the API key format is correct (e.g., OpenAI keys start with "sk-" and DeepSeek keys contain alphanumeric characters). - Matches the API key to the correct provider. - Prevents incorrect combinations of API keys and providers. - Sends a test request to verify that the API key is active and functional.
If any of these checks fail, an error is returned with details about the issue.
Examples
if (FALSE) { # \dontrun{
# Create API credentials for OpenAI
api <- new_RemoteLlmApi(api_key_path = "path/to/openai_key.txt", provider = "OpenAI")
# Create API credentials for DeepSeek
api <- new_RemoteLlmApi(api_key_path = "path/to/deepseek_key.txt", provider = "DeepSeek")
# Print the API object
print(api)
} # }