autogen/notebook/config_loader_utility_funct...

386 lines
14 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Config loader utility functions\n",
"\n",
"For an introduction to configuring LLMs, refer to the [main configuration docs](https://microsoft.github.io/autogen/docs/topics/llm_configuration). This guide will run through examples of the more advanced utility functions for managing API configurations.\n",
"\n",
"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.\n",
"\n",
"## Storing API keys\n",
"\n",
"1. Obtain API keys from OpenAI and optionally from Azure OpenAI (or other provider).\n",
"2. Store them securely using either:\n",
" - Environment Variables: `export OPENAI_API_KEY='your-key'` in your shell.\n",
" - Text File: Save the key in a `key_openai.txt` file.\n",
" - Env File: Save the key to a `.env` file eg: `OPENAI_API_KEY=sk-********************`\n",
"\n",
"## Utility functions\n",
"\n",
"There are several utility functions for loading LLM config lists that may be useful depending on the situation.\n",
"\n",
"- [`get_config_list`](#get_config_list): Generates configurations for API calls, primarily from provided API keys.\n",
"- [`config_list_openai_aoai`](#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.\n",
"- [`config_list_from_json`](#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.\n",
"- [`config_list_from_models`](#config_list_from_models): Creates configurations based on a provided list of models, useful when targeting specific models without manually specifying each configuration.\n",
"- [`config_list_from_dotenv`](#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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### get_config_list\n",
"\n",
"Used to generate configurations for API calls."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'api_key': 'YOUR_OPENAI_API_KEY', 'api_type': 'openai'}]\n"
]
}
],
"source": [
"import autogen\n",
"\n",
"api_keys = [\"YOUR_OPENAI_API_KEY\"]\n",
"base_urls = None # You can specify API base URLs if needed. eg: localhost:8000\n",
"api_type = \"openai\" # Type of API, e.g., \"openai\" or \"aoai\".\n",
"api_version = None # Specify API version if needed.\n",
"\n",
"config_list = autogen.get_config_list(api_keys, base_urls=base_urls, api_type=api_type, api_version=api_version)\n",
"\n",
"print(config_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### config_list_openai_aoai\n",
"\n",
"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.\n",
"\n",
"Steps:\n",
"- Store OpenAI API key in:\n",
" - Environment variable: `OPENAI_API_KEY`\n",
" - or Local file: `key_openai.txt`\n",
"- Store Azure OpenAI API key in:\n",
" - Environment variable: `AZURE_OPENAI_API_KEY`\n",
" - or Local file: `key_aoai.txt` (Supports multiple keys, one per line)\n",
"- Store Azure OpenAI API base in:\n",
" - Environment variable: `AZURE_OPENAI_API_BASE`\n",
" - or Local file: `base_aoai.txt` (Supports multiple bases, one per line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"config_list = autogen.config_list_openai_aoai(\n",
" key_file_path=\".\",\n",
" openai_api_key_file=\"key_openai.txt\",\n",
" aoai_api_key_file=\"key_aoai.txt\",\n",
" aoai_api_base_file=\"base_aoai.txt\",\n",
" exclude=None, # The API type to exclude, eg: \"openai\" or \"aoai\".\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### config_list_from_json\n",
"\n",
"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.\n",
"\n",
"Steps:\n",
"- Setup the JSON Configuration:\n",
" 1. Store configurations in an environment variable named `OAI_CONFIG_LIST` as a valid JSON string.\n",
" 2. Alternatively, save configurations in a local JSON file named `OAI_CONFIG_LIST.json`\n",
" 3. Add `OAI_CONFIG_LIST` to your `.gitignore` file on your local repository.\n",
"\n",
"Your JSON structure should look something like this:\n",
"\n",
"```json\n",
"# OAI_CONFIG_LIST file example\n",
"[\n",
" {\n",
" \"model\": \"gpt-4\",\n",
" \"api_key\": \"YOUR_OPENAI_API_KEY\"\n",
" },\n",
" {\n",
" \"model\": \"gpt-3.5-turbo\",\n",
" \"api_key\": \"YOUR_OPENAI_API_KEY\",\n",
" \"api_version\": \"2023-03-01-preview\"\n",
" }\n",
"]\n",
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\", # or OAI_CONFIG_LIST.json if file extension is added\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
" },\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### config_list_from_models\n",
"\n",
"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`](/docs/reference/oai/openai_utils#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.\n",
"\n",
"Steps:\n",
"- Similar to method 1, store API keys and bases either in environment variables or `.txt` files."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"config_list = autogen.config_list_from_models(\n",
" key_file_path=\".\",\n",
" openai_api_key_file=\"key_openai.txt\",\n",
" aoai_api_key_file=\"key_aoai.txt\",\n",
" aoai_api_base_file=\"base_aoai.txt\",\n",
" exclude=\"aoai\",\n",
" model_list=[\"gpt-4\", \"gpt-3.5-turbo\", \"gpt-3.5-turbo-16k\"],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### config_list_from_dotenv\n",
"\n",
"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.\n",
"\n",
"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. \n",
"\n",
"If not provided, it defaults to using `OPENAI_API_KEY` for `gpt-4` and `OPENAI_API_KEY` for `gpt-3.5-turbo`.\n",
"\n",
"```python\n",
" # default key map\n",
" model_api_key_map = {\n",
" \"gpt-4\": \"OPENAI_API_KEY\",\n",
" \"gpt-3.5-turbo\": \"OPENAI_API_KEY\",\n",
" }\n",
"```\n",
"\n",
"Here is an example `.env` file:\n",
"\n",
"```bash\n",
"OPENAI_API_KEY=sk-*********************\n",
"HUGGING_FACE_API_KEY=**************************\n",
"ANOTHER_API_KEY=1234567890234567890\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'api_key': 'sk-*********************', 'model': 'gpt-4'},\n",
" {'api_key': 'sk-*********************', 'model': 'gpt-3.5-turbo'}]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config_list = autogen.config_list_from_dotenv(\n",
" dotenv_file_path=\".env\", # If None the function will try to find in the working directory\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
" },\n",
")\n",
"\n",
"config_list"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'api_key': '1234567890234567890', 'model': 'gpt-4'},\n",
" {'api_key': 'sk-*********************', 'model': 'gpt-3.5-turbo'}]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# gpt-3.5-turbo will default to OPENAI_API_KEY\n",
"config_list = autogen.config_list_from_dotenv(\n",
" dotenv_file_path=\".env\", # If None the function will try to find in the working directory\n",
" model_api_key_map={\n",
" \"gpt-4\": \"ANOTHER_API_KEY\", # String or dict accepted\n",
" },\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
" },\n",
")\n",
"\n",
"config_list"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'api_key': 'sk-*********************', 'model': 'gpt-4'},\n",
" {'api_key': '**************************', 'model': 'vicuna'}]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# example using different environment variable names\n",
"config_list = autogen.config_list_from_dotenv(\n",
" dotenv_file_path=\".env\",\n",
" model_api_key_map={\n",
" \"gpt-4\": \"OPENAI_API_KEY\",\n",
" \"vicuna\": \"HUGGING_FACE_API_KEY\",\n",
" },\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"vicuna\",\n",
" }\n",
" },\n",
")\n",
"\n",
"config_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'api_key': 'sk-*********************', 'model': 'gpt-4'},\n",
" {'api_key': '1234567890234567890',\n",
" 'base_url': 'https://api.someotherapi.com',\n",
" 'api_type': 'aoai',\n",
" 'api_version': 'v2',\n",
" 'model': 'gpt-3.5-turbo'}]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config_list = autogen.config_list_from_dotenv(\n",
" dotenv_file_path=\".env\",\n",
" model_api_key_map={\n",
" \"gpt-4\": \"OPENAI_API_KEY\",\n",
" \"gpt-3.5-turbo\": {\n",
" \"api_key_env_var\": \"ANOTHER_API_KEY\",\n",
" \"api_type\": \"aoai\",\n",
" \"api_version\": \"v2\",\n",
" \"base_url\": \"https://api.someotherapi.com\",\n",
" },\n",
" },\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
" },\n",
")\n",
"\n",
"config_list"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "masterclass",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}