Config loader utility functions
.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.
export OPENAI_API_KEY='your-key'
in
your shell.key_openai.txt
file..env
file eg:
OPENAI_API_KEY=sk-********************
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.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)
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
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.
.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
.
.env
file:
gpt-4
on openai
by default, and using gpt-3.5-turbo
with
additional configurations for aoai
.