DSPy: The framework for programming—not prompting—language models
Python
fix(dspy): PR feedback : typos, save as json, print for score
williambrach4c5da9920742
diff --git a/examples/multi-input-output/beginner-multi-input-output.ipynb b/examples/multi-input-output/beginner-multi-input-output.ipynbindex 3a40eb810..bec090bf2 100644--- a/examples/multi-input-output/beginner-multi-input-output.ipynb+++ b/examples/multi-input-output/beginner-multi-input-output.ipynb@@ -72,7 +72,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 24,"metadata": {},"outputs": [],"source": [@@ -124,9 +124,104 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 25,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "data": {+ "text/html": [+ "<div>\n",+ "<style scoped>\n",+ " .dataframe tbody tr th:only-of-type {\n",+ " vertical-align: middle;\n",+ " }\n",+ "\n",+ " .dataframe tbody tr th {\n",+ " vertical-align: top;\n",+ " }\n",+ "\n",+ " .dataframe thead th {\n",+ " text-align: right;\n",+ " }\n",+ "</style>\n",+ "<table border=\"1\" class=\"dataframe\">\n",+ " <thead>\n",+ " <tr style=\"text-align: right;\">\n",+ " <th></th>\n",+ " <th>sample_1</th>\n",+ " <th>sample_2</th>\n",+ " <th>plagiarized</th>\n",+ " <th>reason</th>\n",+ " </tr>\n",+ " </thead>\n",+ " <tbody>\n",+ " <tr>\n",+ " <th>0</th>\n",+ " <td>public class T1 { public static void main(Str...</td>\n",+ " <td>/* * To change this license header, choose Li...</td>\n",+ " <td>False</td>\n",+ " <td>The two code samples, while producing similar ...</td>\n",+ " </tr>\n",+ " <tr>\n",+ " <th>1</th>\n",+ " <td>public class T1 { public static void main(Str...</td>\n",+ " <td>/** * * @author 65FBEF05E01FAC390CB3FA073FB3...</td>\n",+ " <td>False</td>\n",+ " <td>The code samples demonstrate different approac...</td>\n",+ " </tr>\n",+ " <tr>\n",+ " <th>2</th>\n",+ " <td>public class T1 { public static void main(Str...</td>\n",+ " <td>/** * * @author CB6AB3315634A1E4D11B091BA48B...</td>\n",+ " <td>False</td>\n",+ " <td>The two code samples produce the same output b...</td>\n",+ " </tr>\n",+ " <tr>\n",+ " <th>3</th>\n",+ " <td>public class T1 { public static void main(Str...</td>\n",+ " <td>* * To change this license header, choose Lice...</td>\n",+ " <td>True</td>\n",+ " <td>The two code samples are nearly identical in t...</td>\n",+ " </tr>\n",+ " <tr>\n",+ " <th>4</th>\n",+ " <td>public class T1 { public static void main(Str...</td>\n",+ " <td>/* * To change this license header, choose Li...</td>\n",+ " <td>True</td>\n",+ " <td>The two code samples contain identical main me...</td>\n",+ " </tr>\n",+ " </tbody>\n",+ "</table>\n",+ "</div>"+ ],+ "text/plain": [+ " sample_1 \\\n",+ "0 public class T1 { public static void main(Str... \n",+ "1 public class T1 { public static void main(Str... \n",+ "2 public class T1 { public static void main(Str... \n",+ "3 public class T1 { public static void main(Str... \n",+ "4 public class T1 { public static void main(Str... \n",+ "\n",+ " sample_2 plagiarized \\\n",+ "0 /* * To change this license header, choose Li... False \n",+ "1 /** * * @author 65FBEF05E01FAC390CB3FA073FB3... False \n",+ "2 /** * * @author CB6AB3315634A1E4D11B091BA48B... False \n",+ "3 * * To change this license header, choose Lice... True \n",+ "4 /* * To change this license header, choose Li... True \n",+ "\n",+ " reason \n",+ "0 The two code samples, while producing similar ... \n",+ "1 The code samples demonstrate different approac... \n",+ "2 The two code samples produce the same output b... \n",+ "3 The two code samples are nearly identical in t... \n",+ "4 The two code samples contain identical main me... "+ ]+ },+ "execution_count": 25,+ "metadata": {},+ "output_type": "execute_result"+ }+ ],"source": ["df = pd.read_csv(\"train.csv\", sep=\"\\t\")\n","df.head()"@@ -159,7 +254,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 26,"metadata": {},"outputs": [],"source": [@@ -196,7 +291,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 53,"metadata": {},"outputs": [],"source": [@@ -261,7 +356,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 28,"metadata": {},"outputs": [],"source": [@@ -285,9 +380,9 @@"class PlagiarismCoT(dspy.Module):\n"," def __init__(self) -> None:\n"," super().__init__()\n",- " # Define the program as a ChainOfThought object\n",- " # self.prog = dspy.PredictiveText(PlagiarismSignature)\n",- " self.prog = dspy.ChainOfThought(PlagiarismSignature)\n",+ "\n",+ " # self.prog = dspy.ChainOfThought(PlagiarismSignature)\n",+ " self.prog = dspy.Predict(PlagiarismSignature)\n","\n"," def forward(self, code_sample_1: str, code_sample_2: str) -> PlagiarismSignature:\n","\n",@@ -309,9 +404,18 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 29,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "plagiarized : Yes\n",+ "explanation : Explanation: The two code samples are identical in every aspect, including the class name, method name, and the repeated print statements. There are no differences in the structure, logic, or content of the code.\n"+ ]+ }+ ],"source": ["output = PlagiarismCoT()(examples[0].code_sample_1, examples[0].code_sample_1)\n","print(f\"plagiarized : {output.plagiarized}\")\n",@@ -345,7 +449,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 30,"metadata": {},"outputs": [],"source": [@@ -408,9 +512,17 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 31,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "Average Metric: 39 / 63 (61.9): 100%|██████████| 63/63 [00:00<00:00, 1136.76it/s]\n"+ ]+ }+ ],"source": ["evaluate = Evaluate(\n"," devset=examples,\n",@@ -424,6 +536,24 @@"score = evaluate(PlagiarismCoT())"]},+ {+ "cell_type": "code",+ "execution_count": 32,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Zero-shot score: 61.9\n"+ ]+ }+ ],+ "source": [+ "# score for zero-shot evaluation with `validate_answer` function\n",+ "print(f\"Zero-shot score: {score}\")"+ ]+ },{"cell_type": "markdown","metadata": {},@@ -439,7 +569,7 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 33,"metadata": {},"outputs": [],"source": [@@ -489,9 +619,17 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 34,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "Average Metric: 37 / 63 (58.7): 100%|██████████| 63/63 [00:00<00:00, 1220.76it/s]\n"+ ]+ }+ ],"source": ["evaluate_with_explanation = Evaluate(\n"," devset=examples,\n",@@ -505,6 +643,24 @@"score = evaluate_with_explanation(PlagiarismCoT())"]},+ {+ "cell_type": "code",+ "execution_count": 35,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Zero-shot score for validate_answer_with_explanation : 58.73\n"+ ]+ }+ ],+ "source": [+ "# score for zero-shot evaluation with `validate_answer_with_explanation` function\n",+ "print(f\"Zero-shot score for validate_answer_with_explanation : {score}\")"+ ]+ },{"cell_type": "markdown","metadata": {},@@ -548,16 +704,70 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 36,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ " 6%|▋ | 4/63 [00:00<00:00, 3197.49it/s]"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Bootstrapped 4 full traces after 5 examples in round 0.\n"+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "\n",+ "Average Metric: 35 / 63 (55.6): 100%|██████████| 63/63 [00:00<00:00, 2608.37it/s]\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "[('prog', Predict(PlagiarismSignature(code_sample_1, code_sample_2 -> explanation, plagiarized\n",+ " instructions='Detect if two code samples are plagiarized. In plagiarized field answer only : Yes if the code samples are plagiarized, No otherwise. In explenation field add the reason why the code samples are/ are not plagiarized.'\n",+ " code_sample_1 = Field(annotation=str required=True json_schema_extra={'desc': 'The first code sample to compare', '__dspy_field_type': 'input', 'prefix': 'Code Sample 1:'})\n",+ " code_sample_2 = Field(annotation=str required=True json_schema_extra={'desc': 'The second code sample to compare', '__dspy_field_type': 'input', 'prefix': 'Code Sample 2:'})\n",+ " explanation = Field(annotation=str required=True json_schema_extra={'desc': 'Explanation or reason why the code samples are/ are not plagiarized', '__dspy_field_type': 'output', 'prefix': 'Explanation:'})\n",+ " plagiarized = Field(annotation=str required=True json_schema_extra={'desc': 'Yes/No indicating if code samples are plagiarized', '__dspy_field_type': 'output', 'prefix': 'Plagiarized:'})\n",+ ")))]\n"+ ]+ }+ ],"source": [- "config = {\"max_bootstrapped_demos\": 8, \"max_labeled_demos\": 8}\n",+ "config = {\"max_bootstrapped_demos\": 4, \"max_labeled_demos\": 4}\n","\n","optimizer = BootstrapFewShot(metric=validate_answer, **config)\n","optimized_program = optimizer.compile(PlagiarismCoT(), trainset=examples)\n","score = evaluate(optimized_program)\n",- "optimized_program.save(\"BootstrapFewShot_program\")"+ "optimized_program.save(\"BootstrapFewShot_program.json\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 37,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "BootstrapFewShot score for validate_answer : 55.56\n"+ ]+ }+ ],+ "source": [+ "# score for BootstrapFewShot evaluation with `validate_answer` function\n",+ "print(f\"BootstrapFewShot score : {score}\")"]},{@@ -576,8 +786,8 @@"outputs": [],"source": ["config = {\n",- " \"max_bootstrapped_demos\": 8,\n",- " \"max_labeled_demos\": 8,\n",+ " \"max_bootstrapped_demos\": 4,\n",+ " \"max_labeled_demos\": 4,\n"," \"num_candidate_programs\": 5,\n"," \"num_threads\": 4,\n","}\n",@@ -585,7 +795,25 @@"optimizer = BootstrapFewShotWithRandomSearch(metric=validate_answer, **config)\n","optimized_program = optimizer.compile(PlagiarismCoT(), trainset=examples)\n","score = evaluate(optimized_program)\n",- "optimized_program.save(\"BootstrapFewShotWithRandomSearch_program\")"+ "optimized_program.save(\"BootstrapFewShotWithRandomSearch_program.json\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 39,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "BootstrapFewShotWithRandomSearch score for validate_answer : 76.19\n"+ ]+ }+ ],+ "source": [+ "# score for BootstrapFewShotWithRandomSearch evaluation with `validate_answer` function\n",+ "print(f\"BootstrapFewShotWithRandomSearch score : {score}\")"]},{@@ -607,7 +835,25 @@"optimizer = KNNFewShot(k, examples)\n","optimized_program = optimizer.compile(PlagiarismCoT(), trainset=examples)\n","score = evaluate(optimized_program)\n",- "optimized_program.save(\"KNNFewShot_program\")"+ "optimized_program.save(\"KNNFewShot_program.json\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 41,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "KNNFewShot score : 85.71\n"+ ]+ }+ ],+ "source": [+ "# score for KNNFewShot evaluation with `validate_answer` function\n",+ "print(f\"KNNFewShot score : {score}\")"]},{@@ -625,7 +871,7 @@"metadata": {},"outputs": [],"source": [- "n = 20 # The number of instructions and fewshot examples that we will generate and optimize over\n",+ "n = 5 # The number of instructions and fewshot examples that we will generate and optimize over\n","batches = 20 # The number of optimization trials to be run (we will test out a new combination of instructions and fewshot examples in each trial)\n","temperature = 1 # The temperature configured for generating new instructions\n","eval_kwargs = {\"num_threads\": 4, \"display_progress\": False, \"display_table\": 0}\n",@@ -647,7 +893,25 @@" eval_kwargs=eval_kwargs,\n",")\n","score = evaluate(optimized_program)\n",- "optimized_program.save(\"MIPROv2_program\")"+ "optimized_program.save(\"MIPROv2_program.json\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 50,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "MIPROv2 score : 87.85\n"+ ]+ }+ ],+ "source": [+ "# score for MIPROv2 evaluation with `validate_answer` function\n",+ "print(f\"MIPROv2 score : {score}\")"]},{@@ -661,12 +925,12 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 51,"metadata": {},"outputs": [],"source": ["loaded_program = PlagiarismCoT()\n",- "loaded_program.load(path=\"BootstrapFewShotWithRandomSearch_program\")"+ "loaded_program.load(path=\"BootstrapFewShotWithRandomSearch_program.json\")"]},{