{ "cells": [ { "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", "## 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]==1.2.1\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]==1.2.1 datasets" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Set your OpenAI key:" ] }, { "cell_type": "code", "execution_count": 2, "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": [ "import os\n", "\n", "if \"OPENAI_API_KEY\" not in os.environ:\n", " os.environ[\"OPENAI_API_KEY\"] = \"\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Uncomment the following to use Azure OpenAI:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-02-13T23:40:52.333547Z", "iopub.status.busy": "2023-02-13T23:40:52.333249Z", "iopub.status.idle": "2023-02-13T23:40:52.336508Z", "shell.execute_reply": "2023-02-13T23:40:52.335858Z" } }, "outputs": [], "source": [ "# import openai\n", "# openai.api_type = \"azure\"\n", "# openai.api_base = \"https://.openai.azure.com/\"\n", "# openai.api_version = \"2023-03-15-preview\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Load dataset\n", "\n", "First, 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": "c23bfd043e284ea29f8a6b4de2974637", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2 [00:00