Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Update data functions called in examples

dilara2589c52ad1ad
diff --git a/examples/finetuning/openai_finetuning_hpqa.ipynb b/examples/finetuning/openai_finetuning_hpqa.ipynb
index bd9643bd7..f77e6affb 100644
--- a/examples/finetuning/openai_finetuning_hpqa.ipynb
+++ b/examples/finetuning/openai_finetuning_hpqa.ipynb
@@ -1,5 +1,33 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# OpenAI Fine-tuning Demo with HotPotQA"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Finetuning demo with DSPy."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Notebook Preparation"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Magic commands and secrets."
+ ]
+ },
{
"cell_type": "code",
"execution_count": 1,
@@ -7,7 +35,23 @@
"outputs": [],
"source": [
"%load_ext autoreload\n",
- "%autoreload 2"
+ "%autoreload 2\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "assert \"DSP_CACHEDIR\" in os.environ\n",
+ "assert \"OPENAI_API_KEY\" in os.environ\n",
+ "\n",
+ "# Altenatively, you can set the environment variables in code\n",
+ "# os.environ[\"DSP_CACHEDIR\"] = <YOUR_CACHE_DIR>\n",
+ "# os.environ[\"OPENAI_API_KEY\"] = <OPENAI_API_KEY>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Task Setup"
]
},
{
@@ -19,32 +63,17 @@
"name": "stderr",
"output_type": "stream",
"text": [
- "/Users/isaac.miller/projects/dspy-finetuning/.venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020\n",
- " warnings.warn(\n",
- "/Users/isaac.miller/projects/dspy-finetuning/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
+ "/scr-ssd/dilara/.miniconda3/envs/dspyprod/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
- "import sys\n",
- "import os\n",
- "\n",
- "import zipfile\n",
- "import os\n",
- "\n",
- "import dotenv\n",
- "\n",
- "dotenv.load_dotenv()\n",
"import dspy\n",
- "\n",
+ "import dspy.evaluate\n",
"from dspy.datasets import HotPotQA\n",
- "import re\n",
"from dspy.evaluate import Evaluate\n",
- "\n",
- "from dsp.utils import EM\n",
- "from dsp.utils.utils import deduplicate\n",
- "import collections"
+ "from dsp.utils.utils import deduplicate"
]
},
{
@@ -53,14 +82,10 @@
"metadata": {},
"outputs": [],
"source": [
- "import openai\n",
- "\n",
- "openai.api_key = os.environ.get('OPENAI_API_KEY')\n",
- "\n",
"mini = \"gpt-4o-mini-2024-07-18\"\n",
"base_temp = 0.9\n",
"\n",
- "lm = dspy.OpenAIModel(model=mini, max_tokens=500, temperature=base_temp)\n",
+ "lm = dspy.TrainableOpenAI(model=mini, max_tokens=500, temperature=base_temp)\n",
"\n",
"colbert_v2_endpoint = \"http://20.102.90.50:2017/wiki17_abstracts\"\n",
"colbertv2 = dspy.ColBERTv2(url=colbert_v2_endpoint)\n",
@@ -74,8 +99,6 @@
"metadata": {},
"outputs": [],
"source": [
- "import dspy.evaluate\n",
- "\n",
"metric = dspy.evaluate.answer_exact_match"
]
},
@@ -164,54 +187,56 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
- "Average Metric: 259 / 500 (51.8): 100%|██████████| 500/500 [00:01<00:00, 321.91it/s]\n"
+ "Average Metric: 257 / 500 (51.4): 100%|██████████| 500/500 [00:01<00:00, 493.06it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
- "Writing dataset with length 777 to trainset_data.jsonl\n"
+ "Writing dataset with length 771 to trainset_data.jsonl\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
- "Average Metric: 76 / 125 (60.8): 100%|██████████| 125/125 [00:00<00:00, 350.95it/s]\n"
+ "Average Metric: 106 / 183 (57.9): 37%|███▋ | 183/500 [06:26<11:09, 2.11s/it]\n",
+ "Average Metric: 70 / 125 (56.0): 100%|██████████| 125/125 [01:00<00:00, 2.07it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
- "Writing dataset with length 228 to devset_data.jsonl\n"
+ "Writing dataset with length 210 to devset_data.jsonl\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\n"
]
}
],
"source": [
- "from collections import Counter\n",
- "from dspy.teleprompt.finetune_teleprompter import bootstrap_data_multiple_rounds, DataCollectionCallback, build_prompt_completion_data_from_trace\n",
- "from typing import Callable\n",
+ "from dspy.teleprompt.finetune_teleprompter import bootstrap_data_for_round, convert_to_prompt_completion_data\n",
"import ujson\n",
"\n",
"samples = 500\n",
"\n",
- "callback = DataCollectionCallback(num_correct=1, max_attempts=2)\n",
- "\n",
"dspy.settings.configure(experimental=True)\n",
"dc_kwargs = {\n",
- " # \"exclude_demos\":True, \n",
- " \"sampling_temperature_base\": base_temp,\n",
+ " \"sampling_temperature\": base_temp,\n",
" \"sampling_temperature_delta\":0.0001,\n",
- " \"next_round_dataset_callback\": callback.move_on_callback_correct_with_max,\n",
" \"num_threads\": NUM_THREADS,\n",
"}\n",
"\n",
@@ -219,11 +244,16 @@
"\n",
"def write_data(data, filename):\n",
" # get the bootstrapped data for num_rounds=1, but using the callback\n",
- " dataset = bootstrap_data_multiple_rounds(basicmh_bs, data, metric, num_rounds=1, **dc_kwargs)\n",
- " # Flatmap the lists of prompt completions gives by each trace\n",
- " dataset = sum([build_prompt_completion_data_from_trace(result[\"trace\"], program=basicmh_bs, exclude_demos=True) for result in dataset if result[\"score\"]], [])\n",
+ " data = bootstrap_data_for_round(basicmh_bs, data, metric, sampling_round=1, **dc_kwargs)\n",
+ "\n",
+ " # Post process the data to remove any entries with no score\n",
+ " filtered_data = [d for d in data if d[\"score\"]]\n",
+ "\n",
+ " # Convert the data to prompt completion format\n",
+ " dataset = convert_to_prompt_completion_data(filtered_data, program=basicmh_bs, exclude_demos=True)\n",
+ " \n",
" # Format the data for finetuning using the LM\n",
- " dataset = lm.format_data_for_vanilla_finetuning(dataset)\n",
+ " dataset = lm._format_data_for_vanilla_finetuning(dataset)\n",
" print(\"Writing dataset with length\", len(dataset), \"to\", filename)\n",
" with open(filename, \"w\") as f:\n",
" for line in dataset:\n",
@@ -239,9 +269,6 @@
"metadata": {},
"outputs": [],
"source": [
- "import concurrent.futures\n",
- "# lm.()\n",
- "# lm.get_finetune(\"trainset_data.jsonl\", \"devset_data.jsonl\", method=\"SFT\")\n",
"future_lm = dspy.OpenAIModel.load_from_job_id(\"ftjob-tmccSJv170p4gNFJUYwXdCSq\")\n",
"finetuned_lm = future_lm\n",
"finetuned_lm.kwargs[\"temperature\"] = 0.0"
@@ -738,7 +765,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.6"
+ "version": "3.9.19"
}
},
"nbformat": 4,