Config loader utility functions
Config loader utility functions
For an introduction to configuring LLMs, refer to the main configuration docs. This guide will run through examples of the more advanced utility functions for managing API configurations.
Managing API configurations can be tricky, especially when dealing with
multiple models and API versions. The provided utility functions assist
users in managing these configurations effectively. Ensure your API keys
and other sensitive data are stored securely. You might store keys in
.txt
or .env
files or environment variables for local development.
Never expose your API keys publicly. If you insist on storing your key
files locally on your repo (you shouldn’t), ensure the key file path is
added to the .gitignore
file.
Storing API keys
- Obtain API keys from OpenAI and optionally from Azure OpenAI (or other provider).
- Store them securely using either:
- Environment Variables:
export OPENAI_API_KEY='your-key'
in your shell. - Text File: Save the key in a
key_openai.txt
file. - Env File: Save the key to a
.env
file eg:OPENAI_API_KEY=sk-********************
- Environment Variables:
Utility functions
There are several utility functions for loading LLM config lists that may be useful depending on the situation.
get_config_list
: Generates configurations for API calls, primarily from provided API keys.config_list_openai_aoai
: Constructs a list of configurations using both Azure OpenAI and OpenAI endpoints, sourcing API keys from environment variables or local files.config_list_from_json
: Loads configurations from a JSON structure, either from an environment variable or a local JSON file, with the flexibility of filtering configurations based on given criteria.config_list_from_models
: Creates configurations based on a provided list of models, useful when targeting specific models without manually specifying each configuration.config_list_from_dotenv
: Constructs a configuration list from a.env
file, offering a consolidated way to manage multiple API configurations and keys from a single file.
get_config_list
Used to generate configurations for API calls.
config_list_openai_aoai
This method creates a list of configurations using Azure OpenAI endpoints and OpenAI endpoints. It tries to extract API keys and bases from environment variables or local text files.
Steps: - Store OpenAI API key in: - Environment variable:
OPENAI_API_KEY
- or Local file: key_openai.txt
- Store Azure OpenAI
API key in: - Environment variable: AZURE_OPENAI_API_KEY
- or Local
file: key_aoai.txt
(Supports multiple keys, one per line) - Store
Azure OpenAI API base in: - Environment variable:
AZURE_OPENAI_API_BASE
- or Local file: base_aoai.txt
(Supports
multiple bases, one per line)
config_list_from_json
This method loads configurations from an environment variable or a JSON file. It provides flexibility by allowing users to filter configurations based on certain criteria.
Steps: - Setup the JSON Configuration: 1. Store configurations in an
environment variable named OAI_CONFIG_LIST
as a valid JSON string. 2.
Alternatively, save configurations in a local JSON file named
OAI_CONFIG_LIST.json
3. Add OAI_CONFIG_LIST
to your .gitignore
file on your local repository.
Your JSON structure should look something like this:
config_list_from_models
This method creates configurations based on a provided list of models.
It’s useful when you have specific models in mind and don’t want to
manually specify each configuration. The
config_list_from_models
function tries to create a list of configurations using Azure OpenAI
endpoints and OpenAI endpoints for the provided list of models. It
assumes the api keys and api bases are stored in the corresponding
environment variables or local txt files. It’s okay to only have the
OpenAI API key, OR only the Azure OpenAI API key + base. For Azure the
model name refers to the OpenAI Studio deployment name.
Steps: - Similar to method 1, store API keys and bases either in
environment variables or .txt
files.
config_list_from_dotenv
If you are interested in keeping all of your keys in a single location
like a .env
file rather than using a configuration specifically for
OpenAI, you can use config_list_from_dotenv
. This allows you to
conveniently create a config list without creating a complex
OAI_CONFIG_LIST
file.
The model_api_key_map
parameter is a dictionary that maps model names
to the environment variable names in the .env
file where their
respective API keys are stored. It lets the code know which API key to
use for each model.
If not provided, it defaults to using OPENAI_API_KEY
for gpt-4
and
OPENAI_API_KEY
for gpt-3.5-turbo
.
Here is an example .env
file:
You can also provide additional configurations for APIs, simply by
replacing the string value with a dictionary expanding on the
configurations. See the example below showing the example of using
gpt-4
on openai
by default, and using gpt-3.5-turbo
with
additional configurations for aoai
.