Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Colab fix

dilara85f9a44fc497
diff --git a/examples/finetuning/finetuning_demo.ipynb b/examples/finetuning/finetuning_demo.ipynb
index 13d764abf..5fe41ba8c 100644
--- a/examples/finetuning/finetuning_demo.ipynb
+++ b/examples/finetuning/finetuning_demo.ipynb
@@ -8,7 +8,7 @@
"\n",
"# DSPy Finetuning Demo\n",
"\n",
- "[<img align=\"center\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" />](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/finetuning/finetuning_demo.ipynb)"
+ "[<img align=\"center\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" />](https://colab.research.google.com/github/stanfordnlp/dspy/blob/demo_finetune/examples/finetuning/finetuning_demo.ipynb)"
]
},
{
@@ -32,7 +32,7 @@
"metadata": {},
"source": [
"The fine-tuning features are available on a special branch named `demo_finetune`.\n",
- "The following code block checks out the DSPy repository, switches to this branch, and installs DSPy locally."
+ "The following code block checks out the DSPy repository and switches to this branch."
]
},
{
@@ -46,18 +46,58 @@
"# don't specify a path, we will clone the repository to the current directory.\n",
"# \n",
"# Leave as is to run on Google Colab.\n",
- "repo_path = ''\n",
+ "repo_path = None\n",
"\n",
"if not repo_path:\n",
" try:\n",
- " repo_path = 'dspy'\n",
+ " repo_path = \"repository\"\n",
" !git clone https://github.com/stanfordnlp/dspy $repo_path\n",
" except:\n",
" raise Exception(\"Could not clone and install the repository!\")\n",
"\n",
+ "!cd $repo_path && git checkout demo_finetune # Switch to the branch"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We are now ready to install the DSPy package from the branch we just checked out.\n",
+ "If running this in Google Colab, you should run the next cell and go to `Runtime -> Restart Session` to allow Colab to load the newly installed package.\n",
+ "If re-loading this way, run the notebook starting from the cell after the installation cell below."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# This cell installs the dspy package from the local path specified in\n",
+ "# \"repo_path\". If you already have an environment in which you have a local\n",
+ "# installation of DSPy, you do not need to install the DSPy package again and\n",
+ "# set this flag.\n",
+ "SKIP_INSTALLATION = False\n",
+ "\n",
+ "if not SKIP_INSTALLATION:\n",
+ " !pip install -U pip\n",
+ " !pip install -e $repo_path # Install the local package"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Start running from this cell if re-started the session!\n",
+ "try:\n",
+ " repo_path\n",
+ "except NameError:\n",
+ " print(\"The variable repo_path isn't defined. This might be due to re-loading the session. Re set it in this cell so that we can locate the demo cache.\" )\n",
"\n",
- "!cd $repo_path && git checkout demo_finetune # Switch to the branch\n",
- "!pip install -e $repo_path # Install the local package"
+ "# Set the repo_path again\n",
+ "# repo_path = \"repository\""
]
},
{
@@ -121,7 +161,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -146,7 +186,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@@ -234,7 +274,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -243,7 +283,7 @@
"True"
]
},
- "execution_count": 5,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -313,6 +353,20 @@
"We start by prompt optimizing the base model, which will allow us to get higher quality bootstraps for training!"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Prepare the training and validation sets for the optimizer using the original\n",
+ "# trainset. This ensures that our \"devset\" is left untouched.\n",
+ "shuffled_trainset = [d for d in trainset]\n",
+ "rng.shuffle(shuffled_trainset)\n",
+ "optimizer_trainset = shuffled_trainset[:OPTIMIZER_NUM_TRAIN]\n",
+ "optimizer_valset = shuffled_trainset[OPTIMIZER_NUM_TRAIN:OPTIMIZER_NUM_TRAIN+OPTIMIZER_NUM_VAL]"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 7,
@@ -617,13 +671,6 @@
}
],
"source": [
- "# Prepare the training and validation sets for the optimizer using the original\n",
- "# trainset. This ensures that our \"devset\" is left untouched.\n",
- "shuffled_trainset = [d for d in trainset]\n",
- "rng.shuffle(shuffled_trainset)\n",
- "optimizer_trainset = shuffled_trainset[:OPTIMIZER_NUM_TRAIN]\n",
- "optimizer_valset = shuffled_trainset[OPTIMIZER_NUM_TRAIN:OPTIMIZER_NUM_TRAIN+OPTIMIZER_NUM_VAL]\n",
- "\n",
"# Initialize the optimizer\n",
"bfrs_optimizer = BootstrapFewShotWithRandomSearch(\n",
" metric=metric,\n",
@@ -690,7 +737,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -1010,7 +1057,7 @@
"source": [
"from dsp.modules.lm import TrainingMethod\n",
"\n",
- "USE_CACHED_MODEL = False\n",
+ "USE_CACHED_MODEL = True\n",
"\n",
"if USE_CACHED_MODEL:\n",
" CACHED_FINETUNED_MODEL_ID = \"\"\n",