{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Copyright (c) Microsoft Corporation. All rights reserved. \n", "\n", "Licensed under the MIT License.\n", "\n", "# Use FLAML to Tune ChatGPT\n", "\n", "FLAML offers a cost-effective hyperparameter optimization technique [EcoOptiGen](https://arxiv.org/abs/2303.04673) for tuning Large Language Models. Our study finds that tuning hyperparameters can significantly improve the utility of LLMs.\n", "\n", "In this notebook, we tune OpenAI ChatGPT (both GPT-3.5 and GPT-4) models for math problem solving. We use [the MATH benchmark](https://crfm.stanford.edu/helm/latest/?group=math_chain_of_thought) for measuring mathematical problem solving on competition math problems with chain-of-thoughts style reasoning.\n", "\n", "Related link: [Blogpost](https://microsoft.github.io/FLAML/blog/2023/04/21/LLM-tuning-math) based on this experiment.\n", "\n", "## Requirements\n", "\n", "FLAML requires `Python>=3.7`. To run this notebook example, please install flaml with the [openai,blendsearch] option:\n", "```bash\n", "pip install flaml[openai,blendsearch]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-02-13T23:40:52.317406Z", "iopub.status.busy": "2023-02-13T23:40:52.316561Z", "iopub.status.idle": "2023-02-13T23:40:52.321193Z", "shell.execute_reply": "2023-02-13T23:40:52.320628Z" } }, "outputs": [], "source": [ "# %pip install flaml[openai,blendsearch] datasets" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Import the oai and tune subpackages from flaml\n", "\n", "FLAML has provided an API for hyperparameter optimization of OpenAI ChatGPT models: `oai.ChatCompletion.tune` and to make a request with the tuned config: `oai.ChatCompletion.create`. First, we import oai from flaml:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-02-13T23:40:54.634335Z", "iopub.status.busy": "2023-02-13T23:40:54.633929Z", "iopub.status.idle": "2023-02-13T23:40:56.105700Z", "shell.execute_reply": "2023-02-13T23:40:56.105085Z" }, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "from flaml import oai" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Set your API Endpoint\n", "\n", "The [`config_list_openai_aoai`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_openai_aoai) function tries to create a list of Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n", "\n", "- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n", "- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n", "- Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n", "\n", "It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-02-13T23:40:52.324240Z", "iopub.status.busy": "2023-02-13T23:40:52.323783Z", "iopub.status.idle": "2023-02-13T23:40:52.330570Z", "shell.execute_reply": "2023-02-13T23:40:52.329750Z" } }, "outputs": [], "source": [ "config_list = oai.config_list_openai_aoai()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The config list looks like the following:\n", "```python\n", "config_list = [\n", " {'api_key': ''}, # only if OpenAI API key is found\n", " {\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-03-15-preview',\n", " }, # only if the at least one Azure OpenAI API key is found\n", " {\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-03-15-preview',\n", " }, # only if the second Azure OpenAI API key is found\n", "]\n", "```\n", "\n", "You can directly override it if the above function returns an empty list, i.e., it doesn't find the keys in the specified locations." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Load dataset\n", "\n", "We load the competition_math dataset. The dataset contains 201 \"Level 2\" Algebra examples. We use a random sample of 20 examples for tuning the generation hyperparameters and the remaining for evaluation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-02-13T23:40:52.339977Z", "iopub.status.busy": "2023-02-13T23:40:52.339556Z", "iopub.status.idle": "2023-02-13T23:40:54.603349Z", "shell.execute_reply": "2023-02-13T23:40:54.602630Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using custom data configuration default\n", "Found cached dataset competition_math (/home/vscode/.cache/huggingface/datasets/competition_math/default/1.0.0/2a2a2995c2847186883ecd64f69be7d602b8a6f6b51950624d4dc2263f93333b)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9e8a0c134dcc447db7395ad52f12f8bd", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2 [00:00