{ "cells": [ { "cell_type": "markdown", "id": "6eb94b72", "metadata": {}, "source": [ "## Following is a demo on running PromptWizard under different scenarios " ] }, { "cell_type": "markdown", "id": "52c7ee0a", "metadata": {}, "source": [ "#### Set environment variables in [.env](.env) for LLM API calling" ] }, { "cell_type": "markdown", "id": "3cffa5ef", "metadata": {}, "source": [ "#### Import Dependencies" ] }, { "cell_type": "code", "execution_count": null, "id": "11efa138", "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.insert(0, \"../../\")\n", "import promptwizard\n", "from promptwizard.glue.promptopt.instantiate import GluePromptOpt\n", "from promptwizard.glue.promptopt.techniques.common_logic import DatasetSpecificProcessing\n", "from promptwizard.glue.common.utils.file import save_jsonlist\n", "from typing import Any\n", "from tqdm import tqdm\n", "from re import compile, findall\n", "import os\n", "from datasets import load_dataset\n", "import yaml\n", "from dotenv import load_dotenv\n", "load_dotenv(override = True)" ] }, { "cell_type": "code", "execution_count": 2, "id": "9be22d5d", "metadata": {}, "outputs": [], "source": [ "def update_yaml_file(file_path,config_dict):\n", "\n", " with open(file_path, 'r') as file:\n", " data = yaml.safe_load(file)\n", "\n", "\n", " for field,value in config_dict.items():\n", " data[field] = value\n", "\n", " with open(file_path, 'w') as file:\n", " yaml.dump(data, file, default_flow_style=False)\n", "\n", " print(\"YAML file updated successfully!\")" ] }, { "cell_type": "markdown", "id": "78abb34a", "metadata": {}, "source": [ "Set the paths" ] }, { "cell_type": "code", "execution_count": 3, "id": "14399d47", "metadata": {}, "outputs": [], "source": [ "path_to_config = \"configs\"\n", "promptopt_config_path = os.path.join(path_to_config, \"promptopt_config.yaml\")\n", "setup_config_path = os.path.join(path_to_config, \"setup_config.yaml\")" ] }, { "cell_type": "markdown", "id": "0f274af9", "metadata": {}, "source": [ "### Now let us consider the three scenarios with respect to availability of training data" ] }, { "cell_type": "markdown", "id": "5aaed236", "metadata": {}, "source": [ "#### Scenario 1 : We have no training data , but we also don't want in-context examples in final prompt" ] }, { "cell_type": "markdown", "id": "4c34423d", "metadata": {}, "source": [ "Set the configurations to generate mutations" ] }, { "cell_type": "code", "execution_count": null, "id": "ec4e7607", "metadata": {}, "outputs": [], "source": [ "file_path = 'configs/promptopt_config.yaml' \n", "# Set the following based on the use case\n", "config_dict = {\n", " \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n", " \"base_instruction\": \"Lets think step by step.\",\n", " \"mutation_rounds\": 5\n", " }\n", "update_yaml_file(file_path,config_dict)" ] }, { "cell_type": "markdown", "id": "d984e84e", "metadata": {}, "source": [ "Create an object for calling prompt optimization and inference functionalities" ] }, { "cell_type": "code", "execution_count": null, "id": "c7aa4ccb", "metadata": {}, "outputs": [], "source": [ "gp = GluePromptOpt(promptopt_config_path,\n", " setup_config_path,\n", " dataset_jsonl=None,\n", " data_processor=None)" ] }, { "cell_type": "markdown", "id": "8d587065", "metadata": {}, "source": [ "Call the optimization function" ] }, { "cell_type": "code", "execution_count": null, "id": "afe8de4f", "metadata": {}, "outputs": [], "source": [ "best_prompt, expert_profile = gp.get_best_prompt(use_examples=False,run_without_train_examples=True,generate_synthetic_examples=False)" ] }, { "cell_type": "markdown", "id": "a30db274", "metadata": {}, "source": [ "Output : Five mutated prompts are printed on the termial as shown below :" ] }, { "cell_type": "code", "execution_count": 7, "id": "e5cb1a65", "metadata": {}, "outputs": [], "source": [ "OUTPUT = \"\"\"\n", "Variations 1:\n", "Expert Profile:\n", "You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to approach mathematical problems methodically, breaking them down into manageable steps and applying appropriate techniques to find solutions. You are familiar with both theoretical and applied mathematics, and you can explain your reasoning and solutions in a clear and concise manner. Your ability to solve mathematical problems efficiently and accurately makes you an invaluable resource for anyone seeking help with mathematics.:\n", "Prompt:\n", "You are a mathematics expert. You will be given a mathematics problem which you need to solve\n", "Lets think step by step.\n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\n", "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n", "_______________________________________________________________________\n", "\n", "Variations 2:\n", "Expert Profile:\n", "You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to approach mathematical problems methodically, breaking them down into manageable steps and applying appropriate techniques to find solutions. You are familiar with both theoretical and applied mathematics, and you can explain your reasoning and solutions in a clear and concise manner. Your ability to solve mathematical problems efficiently and accurately makes you an invaluable resource for anyone seeking help with mathematics.:\n", "Prompt:\n", "Let's break this problem down step by step and devise an experiment to help solve it.\n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\n", "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n", "_______________________________________________________________________\n", "\n", "Variations 3:\n", "Expert Profile:\n", "You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier to find solutions. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to solve problems. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear and accurate solution. Your ability to explain your reasoning and methodology ensures that others can follow and understand your approach, making you an invaluable resource for tackling challenging mathematical problems.:\n", "Prompt:\n", "Let's think through this problem step by step and make a list of ideas to solve it.\n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\n", "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n", "_______________________________________________________________________\n", "\n", "Variations 4:\n", "Expert Profile:\n", "You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier for others to follow your reasoning. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to find solutions. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear, accurate, and well-explained solution. Your ability to communicate complex mathematical concepts in an understandable way makes you an invaluable resource for anyone seeking to solve mathematical problems.:\n", "Prompt:\n", "Let's approach this problem step by step and measure our progress as we go.\n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\n", "Keywords: mathematics, problem-solving, step-by-step, logical reasoning, expert\n", "Iterations completed: 0%| | 0/3 [00:24 None:\n", " def extract_answer_from_output(completion):\n", " # Your functions for metrics and prompt building\n", " ans_re = compile(r\"#### (\\-?[0-9\\.\\,]+)\")\n", " self.INVALID_ANS = \"[invalid]\"\n", "\n", " match = ans_re.search(completion)\n", " if match:\n", " match_str = match.group(1).strip()\n", " match_str = match_str.replace(\",\", \"\")\n", " return match_str\n", " else:\n", " return self.INVALID_ANS\n", "\n", " examples_set = []\n", "\n", " for _, sample in tqdm(enumerate(kwargs[\"dataset\"]), desc=\"Evaluating samples\"):\n", " example = {\n", " DatasetSpecificProcessing.QUESTION_LITERAL: sample['question'],\n", " DatasetSpecificProcessing.ANSWER_WITH_REASON_LITERAL: sample['answer'],\n", " DatasetSpecificProcessing.FINAL_ANSWER_LITERAL: extract_answer_from_output(sample[\"answer\"])\n", " }\n", " examples_set.append(example)\n", "\n", " save_jsonlist(dataset_jsonl, examples_set, \"w\")\n", "\n", " def extract_final_answer(self, answer: str):\n", " \n", " if not answer:\n", " return self.INVALID_ANS\n", "\n", " model_pred = answer.lower()\n", " preds = model_pred.split(self.ANSWER_START.lower())\n", " answer_flag = True if len(preds) > 1 else False\n", "\n", " pred = preds[-1].replace(\",\", \"\")\n", " pred = [s for s in findall(r'-?\\d+\\.?\\d*', pred)]\n", "\n", " if len(pred) == 0:\n", " return self.INVALID_ANS\n", "\n", " if answer_flag:\n", " # choose the first element in list\n", " pred = pred[0]\n", " else:\n", " # choose the last element in list\n", " pred = pred[-1]\n", "\n", " # (For arithmetic tasks) if a word ends with period, it will be omitted ...\n", " if pred[-1] == \".\":\n", " pred = pred[:-1]\n", " return pred" ] }, { "cell_type": "code", "execution_count": 12, "id": "212bea42", "metadata": {}, "outputs": [], "source": [ "gsm8k_processor = GSM8k()" ] }, { "cell_type": "markdown", "id": "36ae1f65", "metadata": {}, "source": [ "Set the configurations to optimize the prompt on the synthetic data" ] }, { "cell_type": "code", "execution_count": null, "id": "67db60b0", "metadata": {}, "outputs": [], "source": [ "file_path = 'configs/promptopt_config.yaml' \n", "config_dict = {\n", " \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n", " \"base_instruction\": \"Lets think step by step.\",\n", " \"mutation_rounds\": 2,\n", " \"few_shot_count\": 5,\n", " \"generate_reasoning\": True,\n", " \"mutate_refine_iterations\" : 3,\n", " \"seen_set_size\":20\n", " }\n", "update_yaml_file(file_path,config_dict)" ] }, { "cell_type": "markdown", "id": "fc8eb2c5", "metadata": {}, "source": [ "Call the optimization function " ] }, { "cell_type": "code", "execution_count": null, "id": "e53934e6", "metadata": {}, "outputs": [], "source": [ "gp = GluePromptOpt(promptopt_config_path,\n", " setup_config_path,\n", " dataset_jsonl = \"train_synthetic.jsonl\",\n", " data_processor=gsm8k_processor)\n", "best_prompt, expert_profile = gp.get_best_prompt(use_examples=True,run_without_train_examples=False,generate_synthetic_examples=False)" ] }, { "cell_type": "markdown", "id": "b4bcd46b", "metadata": {}, "source": [ "Output : Following Prompt and Expert Profile are generated " ] }, { "cell_type": "code", "execution_count": null, "id": "ee6006f0", "metadata": {}, "outputs": [], "source": [ "OUTPUT = \"\"\"\n", "Generating Expert Identity....\n", "Expert Identity: You are a mathematician with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your analytical skills and logical reasoning enable you to break down problems into manageable steps and find accurate solutions efficiently. You are familiar with a wide range of mathematical techniques and tools, and you can apply them to solve problems in both theoretical and applied contexts. Your expertise allows you to explain your solutions clearly and concisely, making complex concepts accessible to others. Whether the problem involves solving equations, proving theorems, or analyzing data, you are well-equipped to provide a thorough and correct solution.\n", "Final best prompt: Provide a clear and detailed solution, breaking down all necessary steps. Ensure that the final answer is clearly marked and separated from the solution steps. Use proper mathematical notation and formatting throughout. Verify the final answer by checking the solution steps for accuracy. Simplify all expressions and fractions where possible. Handle special cases or edge cases appropriately, and clearly state any assumptions or conditions applied during the solution process. Finally, review the entire solution to ensure logical consistency and correct formatting.\n", "\n", "[Question] Solve for \\( x \\) in the equation \\( 2x + 3 = 11 \\).\n", "[Answer] To solve for \\( x \\) in the equation \\( 2x + 3 = 11 \\), we will follow these steps:\n", "\n", "1. **Isolate the term containing \\( x \\)**:\n", " We start by isolating the term with \\( x \\) on one side of the equation. To do this, we need to eliminate the constant term on the left side of the equation.\n", "\n", " \\[\n", " 2x + 3 = 11\n", " \\]\n", "\n", " Subtract 3 from both sides of the equation:\n", "\n", " \\[\n", " 2x + 3 - 3 = 11 - 3\n", " \\]\n", "\n", " Simplifying this, we get:\n", "\n", " \\[\n", " 2x = 8\n", " \\]\n", "\n", "2. **Solve for \\( x \\)**:\n", " Now, we need to solve for \\( x \\) by isolating \\( x \\) itself. Since \\( x \\) is multiplied by 2, we will divide both sides of the equation by 2 to solve for \\( x \\).\n", "\n", " \\[\n", " \\frac{2x}{2} = \\frac{8}{2}\n", " \\]\n", "\n", " Simplifying this, we get:\n", "\n", " \\[\n", " x = 4\n", " \\]\n", "\n", "3. **Verify the solution**:\n", " To ensure our solution is correct, we substitute \\( x = 4 \\) back into the original equation and check if both sides are equal.\n", "\n", " Original equation:\n", "\n", " \\[\n", " 2x + 3 = 11\n", " \\]\n", "\n", " Substitute \\( x = 4 \\):\n", "\n", " \\[\n", " 2(4) + 3 = 11\n", " \\]\n", "\n", " Simplifying this, we get:\n", "\n", " \\[\n", " 8 + 3 = 11\n", " \\]\n", "\n", " \\[\n", " 11 = 11\n", " \\]\n", "\n", " Since both sides of the equation are equal, our solution is verified to be correct.\n", "\n", "**Final Answer**: \\( x = 4 \\) \\( x = 4 \\) \n", "\n", "[Question] Solve for \\( x \\) in the equation \\( x^2 - 4x + 4 = 0 \\).\n", "[Answer] To solve the quadratic equation \\( x^2 - 4x + 4 = 0 \\), we will follow these steps:\n", "\n", "1. **Identify the quadratic equation**: The given equation is \\( x^2 - 4x + 4 = 0 \\).\n", "\n", "2. **Recognize the standard form**: The standard form of a quadratic equation is \\( ax^2 + bx + c = 0 \\). Here, \\( a = 1 \\), \\( b = -4 \\), and \\( c = 4 \\).\n", "\n", "3. **Factor the quadratic expression**: We need to factor the quadratic expression on the left-hand side of the equation. We look for two numbers that multiply to \\( c \\) (which is 4) and add up to \\( b \\) (which is -4). These numbers are -2 and -2.\n", "\n", "4. **Write the factored form**: The quadratic expression \\( x^2 - 4x + 4 \\) can be factored as \\( (x - 2)(x - 2) \\) or \\( (x - 2)^2 \\).\n", "\n", "5. **Set the factored form equal to zero**: We now have \\( (x - 2)^2 = 0 \\).\n", "\n", "6. **Solve for \\( x \\)**: To find the value of \\( x \\), we take the square root of both sides of the equation:\n", " \\[\n", " \\sqrt{(x - 2)^2} = \\sqrt{0}\n", " \\]\n", " This simplifies to:\n", " \\[\n", " x - 2 = 0\n", " \\]\n", "\n", "7. **Isolate \\( x \\)**: Add 2 to both sides of the equation to solve for \\( x \\):\n", " \\[\n", " x = 2\n", " \\]\n", "\n", "8. **Verify the solution**: Substitute \\( x = 2 \\) back into the original equation to ensure it satisfies the equation:\n", " \\[\n", " (2)^2 - 4(2) + 4 = 4 - 8 + 4 = 0\n", " \\]\n", " Since the left-hand side equals the right-hand side (0), the solution \\( x = 2 \\) is verified.\n", "\n", "**Final Answer**: \\( x = 2 \\) \\( x = 2 \\) \n", "\n", "[Question] Find the derivative of \\( f(x) = 3x^2 \\cdot \\sin(x) \\).\n", "[Answer] To find the derivative of the function \\( f(x) = 3x^2 \\cdot \\sin(x) \\), we will use the product rule of differentiation. The product rule states that if we have a function \\( f(x) = u(x) \\cdot v(x) \\), then its derivative \\( f'(x) \\) is given by:\n", "\n", "\\[ f'(x) = u'(x) \\cdot v(x) + u(x) \\cdot v'(x) \\]\n", "\n", "Here, we identify \\( u(x) = 3x^2 \\) and \\( v(x) = \\sin(x) \\).\n", "\n", "Step 1: Differentiate \\( u(x) = 3x^2 \\)\n", "\\[ u'(x) = \\frac{d}{dx}(3x^2) = 3 \\cdot 2x = 6x \\]\n", "\n", "Step 2: Differentiate \\( v(x) = \\sin(x) \\)\n", "\\[ v'(x) = \\frac{d}{dx}(\\sin(x)) = \\cos(x) \\]\n", "\n", "Step 3: Apply the product rule\n", "\\[ f'(x) = u'(x) \\cdot v(x) + u(x) \\cdot v'(x) \\]\n", "\\[ f'(x) = (6x) \\cdot \\sin(x) + (3x^2) \\cdot \\cos(x) \\]\n", "\n", "Step 4: Simplify the expression\n", "\\[ f'(x) = 6x \\sin(x) + 3x^2 \\cos(x) \\]\n", "\n", "Thus, the derivative of the function \\( f(x) = 3x^2 \\cdot \\sin(x) \\) is:\n", "\n", "\\[ \\boxed{f'(x) = 6x \\sin(x) + 3x^2 \\cos(x)} \\]\n", "\n", "To verify the final answer, we can recheck each step to ensure accuracy:\n", "- The derivative of \\( 3x^2 \\) is correctly calculated as \\( 6x \\).\n", "- The derivative of \\( \\sin(x) \\) is correctly calculated as \\( \\cos(x) \\).\n", "- The product rule is correctly applied, and the terms are correctly combined and simplified.\n", "\n", "Therefore, the final answer is confirmed to be correct. \\( f'(x) = 3x^2 \\cos(x) + 6x \\sin(x) \\) \n", "\n", "[Question] Evaluate the definite integral \\( \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx \\).\n", "[Answer] To evaluate the definite integral \\( \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx \\), we will follow these steps:\n", "\n", "1. **Find the antiderivative** of the integrand \\( 4x^3 - 2x + 1 \\).\n", "2. **Evaluate the antiderivative** at the upper limit of integration (1).\n", "3. **Evaluate the antiderivative** at the lower limit of integration (0).\n", "4. **Subtract the value** of the antiderivative at the lower limit from the value at the upper limit to find the definite integral.\n", "\n", "### Step-by-Step Solution:\n", "\n", "1. **Find the antiderivative**:\n", " - The antiderivative of \\( 4x^3 \\) is \\( \\frac{4x^4}{4} = x^4 \\).\n", " - The antiderivative of \\( -2x \\) is \\( -\\frac{2x^2}{2} = -x^2 \\).\n", " - The antiderivative of \\( 1 \\) is \\( x \\).\n", "\n", " Therefore, the antiderivative of \\( 4x^3 - 2x + 1 \\) is:\n", " \\[\n", " F(x) = x^4 - x^2 + x\n", " \\]\n", "\n", "2. **Evaluate the antiderivative at the upper limit (1)**:\n", " \\[\n", " F(1) = 1^4 - 1^2 + 1 = 1 - 1 + 1 = 1\n", " \\]\n", "\n", "3. **Evaluate the antiderivative at the lower limit (0)**:\n", " \\[\n", " F(0) = 0^4 - 0^2 + 0 = 0\n", " \\]\n", "\n", "4. **Subtract the value at the lower limit from the value at the upper limit**:\n", " \\[\n", " \\int_{0}^{1} (4x^3 - 2x + 1) \\, dx = F(1) - F(0) = 1 - 0 = 1\n", " \\]\n", "\n", "### Final Answer:\n", "\\[\n", "\\boxed{1}\n", "\\] \\( 1 \\) \n", "\n", "[Question] Solve the system of equations:\n", "\\[ \\begin{cases} \n", "x + 2y + z = 6 \\\\\n", "2x - y + 3z = 14 \\\\\n", "3x + y - z = 2 \n", "\\end{cases} \\]\n", "[Answer] To solve the system of equations:\n", "\\[ \\begin{cases} \n", "x + 2y + z = 6 \\\\\n", "2x - y + 3z = 14 \\\\\n", "3x + y - z = 2 \n", "\\end{cases} \\]\n", "\n", "we will use the method of elimination and substitution to find the values of \\(x\\), \\(y\\), and \\(z\\).\n", "\n", "**Step 1: Eliminate \\(z\\) from the first two equations.**\n", "\n", "First, we multiply the first equation by 3 to align the coefficients of \\(z\\):\n", "\\[ 3(x + 2y + z) = 3 \\cdot 6 \\]\n", "\\[ 3x + 6y + 3z = 18 \\]\n", "\n", "Now, we subtract the second equation from this result:\n", "\\[ (3x + 6y + 3z) - (2x - y + 3z) = 18 - 14 \\]\n", "\\[ 3x + 6y + 3z - 2x + y - 3z = 4 \\]\n", "\\[ x + 7y = 4 \\]\n", "\\[ \\text{(Equation 4)} \\]\n", "\n", "**Step 2: Eliminate \\(z\\) from the first and third equations.**\n", "\n", "Next, we multiply the first equation by 1 and the third equation by 1 to align the coefficients of \\(z\\):\n", "\\[ 1(x + 2y + z) = 1 \\cdot 6 \\]\n", "\\[ x + 2y + z = 6 \\]\n", "\n", "\\[ 1(3x + y - z) = 1 \\cdot 2 \\]\n", "\\[ 3x + y - z = 2 \\]\n", "\n", "Now, we add these two equations:\n", "\\[ (x + 2y + z) + (3x + y - z) = 6 + 2 \\]\n", "\\[ x + 2y + z + 3x + y - z = 8 \\]\n", "\\[ 4x + 3y = 8 \\]\n", "\\[ \\text{(Equation 5)} \\]\n", "\n", "**Step 3: Solve the system of equations formed by Equation 4 and Equation 5.**\n", "\n", "We now have:\n", "\\[ \\begin{cases} \n", "x + 7y = 4 \\\\\n", "4x + 3y = 8 \n", "\\end{cases} \\]\n", "\n", "First, we solve Equation 4 for \\(x\\):\n", "\\[ x = 4 - 7y \\]\n", "\n", "Substitute \\(x = 4 - 7y\\) into Equation 5:\n", "\\[ 4(4 - 7y) + 3y = 8 \\]\n", "\\[ 16 - 28y + 3y = 8 \\]\n", "\\[ 16 - 25y = 8 \\]\n", "\\[ -25y = 8 - 16 \\]\n", "\\[ -25y = -8 \\]\n", "\\[ y = \\frac{8}{25} \\]\n", "\n", "**Step 4: Substitute \\(y\\) back into Equation 4 to find \\(x\\).**\n", "\n", "\\[ x + 7\\left(\\frac{8}{25}\\right) = 4 \\]\n", "\\[ x + \\frac{56}{25} = 4 \\]\n", "\\[ x = 4 - \\frac{56}{25} \\]\n", "\\[ x = \\frac{100}{25} - \\frac{56}{25} \\]\n", "\\[ x = \\frac{44}{25} \\]\n", "\n", "**Step 5: Substitute \\(x\\) and \\(y\\) back into the first original equation to find \\(z\\).**\n", "\n", "\\[ x + 2y + z = 6 \\]\n", "\\[ \\frac{44}{25} + 2\\left(\\frac{8}{25}\\right) + z = 6 \\]\n", "\\[ \\frac{44}{25} + \\frac{16}{25} + z = 6 \\]\n", "\\[ \\frac{60}{25} + z = 6 \\]\n", "\\[ \\frac{60}{25} = 2.4 \\]\n", "\\[ 2.4 + z = 6 \\]\n", "\\[ z = 6 - 2.4 \\]\n", "\\[ z = 3.6 \\]\n", "\n", "**Final Answer:**\n", "\\[ x = \\frac{44}{25}, y = \\frac{8}{25}, z = 3.6 \\]\n", "\n", "We have verified each step and simplified all expressions. The solution is logically consistent and correctly formatted. \\( x = \\frac{44}{25}, y = \\frac{8}{25}, z = 3.6 \\) \n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "c61c2f84", "metadata": {}, "source": [ "#### Scenario 3 : We have training data and also want in-context examples in final prompt" ] }, { "cell_type": "markdown", "id": "11d2de75", "metadata": {}, "source": [ "Load and save the dataset " ] }, { "cell_type": "code", "execution_count": null, "id": "976681bd-4f43-4dbc-947e-cdb94d4824f0", "metadata": {}, "outputs": [], "source": [ "if not os.path.exists(\"data\"):\n", " os.mkdir(\"data\")\n", " \n", "dataset = load_dataset(\"openai/gsm8k\", \"main\")\n", "num_samples = 0\n", "for dataset_type in ['train','test']:\n", " data_list = []\n", " for data in dataset[dataset_type]:\n", " data_list.append({\"question\": data['question'], \"answer\": data['answer']})\n", " if num_samples == 100 and dataset_type == 'train': # We sample only 100 train examples and use 25 out them for training randomly\n", " break\n", " num_samples += 1\n", " gsm8k_processor.dataset_to_jsonl(\"data/\"+ dataset_type+'.jsonl', dataset=data_list)" ] }, { "cell_type": "markdown", "id": "abf1671a", "metadata": {}, "source": [ "Set the configurations" ] }, { "cell_type": "code", "execution_count": null, "id": "cc841576", "metadata": {}, "outputs": [], "source": [ "file_path = 'configs/promptopt_config.yaml' \n", "config_dict = {\n", " \"task_description\": \"You are a mathematics expert. You will be given a mathematics problem which you need to solve\",\n", " \"base_instruction\": \"Lets think step by step.\",\n", " \"mutation_rounds\": 2,\n", " \"few_shot_count\": 5,\n", " \"generate_reasoning\": True,\n", " \"mutate_refine_iterations\" : 3,\n", " \"seen_set_size\":20\n", " }\n", "update_yaml_file(file_path,config_dict)" ] }, { "cell_type": "markdown", "id": "3392594d", "metadata": {}, "source": [ "Create an object for calling prompt optimization and inference functionalities" ] }, { "cell_type": "code", "execution_count": null, "id": "8af4246f-db32-4b37-a73a-f9e2e5125d09", "metadata": {}, "outputs": [], "source": [ "gp = GluePromptOpt(promptopt_config_path,\n", " setup_config_path,\n", " dataset_jsonl = os.path.join(\"data\", \"train.jsonl\"),\n", " data_processor = gsm8k_processor)" ] }, { "cell_type": "markdown", "id": "6f421ce9", "metadata": {}, "source": [ "Call the optimization function " ] }, { "cell_type": "code", "execution_count": null, "id": "09e3e6e1", "metadata": {}, "outputs": [], "source": [ "best_prompt, expert_profile = gp.get_best_prompt(use_examples=True,run_without_train_examples=False,generate_synthetic_examples=False)" ] }, { "cell_type": "markdown", "id": "15bb0e80", "metadata": {}, "source": [ "Output : Following Prompt and Expert Profile are generated " ] }, { "cell_type": "code", "execution_count": null, "id": "696e6612", "metadata": {}, "outputs": [], "source": [ "OUTPUT = \"\"\"Expert Identity: You are a mathematics expert with a strong background in various fields of mathematics, including algebra, calculus, geometry, and statistics. You have a deep understanding of mathematical theories and principles, and you are skilled at solving complex problems with precision and clarity. Your expertise allows you to break down intricate problems into manageable steps, making it easier for others to follow your reasoning. You are familiar with a wide range of mathematical techniques and tools, and you can apply them effectively to find solutions. Whether the problem involves solving equations, proving theorems, or analyzing data, you can provide a clear, accurate, and well-explained solution. Your ability to communicate complex mathematical concepts in an understandable way makes you an invaluable resource for anyone seeking help with mathematics.\n", "\n", "Final best prompt: \n", "\n", "You are a mathematics expert. Your task is to solve a given mathematics problem accurately and provide a clear, detailed explanation of your solution process. Follow these steps to ensure a comprehensive and well-structured solution:\n", "\n", "1. **Understand the Problem**: Carefully read and comprehend the problem statement. Identify the key components and what is being asked.\n", "\n", "2. **Identify Components**: Break down the problem into its fundamental components, such as variables, constants, and relevant quantities (e.g., base pay, overtime pay, distances, speeds, etc.).\n", "\n", "3. **Apply Relevant Principles**: Use appropriate mathematical principles, formulas, and methods to solve the problem step by step.\n", "\n", "4. **Logical Reasoning**: Employ logical reasoning to explain each step of your solution process. Ensure that each step follows logically from the previous one.\n", "\n", "5. **Detailed Explanations**: Provide detailed explanations for each step to ensure clarity and understanding. Include intermediate results and how they contribute to the final solution.\n", "\n", "6. **Explicit Calculation Steps**: Show each calculation step in detail, including intermediate results. Use proper mathematical notation and symbols.\n", "\n", "7. **Verify Each Step**: Recheck each intermediate step of your calculation to verify the correctness of the final answer. Ensure that all arithmetic and algebraic operations are accurate.\n", "\n", "8. **Combine Results**: Clearly combine different components of the problem (e.g., base pay and overtime pay) before arriving at the final answer.\n", "\n", "9. **Simplify and Notate**: Simplify the final answer where possible, and use proper mathematical notation and symbols.\n", "\n", "10. **Mark the Final Answer**: Clearly mark the final answer within and tags.\n", "\n", "Ensure that your approach is tailored to the specific type of mathematical problem being solved, whether it involves arithmetic, algebra, geometry, calculus, or any other area of mathematics. Present the solutions in a clear and organized manner.\n", "\n", "**Additional Guidelines:**\n", "- **Contextual Understanding**: Pay close attention to the context of the problem to ensure that all relationships and quantities are correctly interpreted.\n", "- **Correct Application of Arithmetic Operations**: Double-check that all arithmetic operations are applied correctly and align with the problem's requirements.\n", "- **Verification of Final Answer**: Dedicate a step to verify the final answer by rechecking all intermediate steps and ensuring they logically lead to the correct final result.\n", "- **Clarity in Marking Final Answer**: Use the and tags to clearly mark the final answer.\n", "\n", "By following these steps and additional guidelines, you will ensure that the solution is accurate, well-explained, and clearly presented.\n", "\n", "\n", "[Question] Bella bought stamps at the post office. Some of the stamps had a snowflake design, some had a truck design, and some had a rose design. Bella bought 11 snowflake stamps. She bought 9 more truck stamps than snowflake stamps, and 13 fewer rose stamps than truck stamps. How many stamps did Bella buy in all?\n", "[Answer] 1. **Understand the Problem**: Bella bought three types of stamps: snowflake, truck, and rose. We need to determine the total number of stamps she bought, given the relationships between the quantities of each type.\n", "\n", "2. **Identify Components**:\n", " - Number of snowflake stamps: 11.\n", " - Number of truck stamps: 9 more than the number of snowflake stamps.\n", " - Number of rose stamps: 13 fewer than the number of truck stamps.\n", "\n", "3. **Apply Relevant Principles**: Use basic arithmetic operations to find the quantities of truck and rose stamps, and then sum all the quantities to find the total number of stamps.\n", "\n", "4. **Logical Reasoning**:\n", " - Number of snowflake stamps: 11.\n", " - Number of truck stamps: 11 (snowflake stamps) + 9 = 20.\n", " - Number of rose stamps: 20 (truck stamps) - 13 = 7.\n", "\n", "5. **Detailed Explanations**:\n", " - Calculate the number of truck stamps: 11 (snowflake stamps) + 9 = 20.\n", " - Calculate the number of rose stamps: 20 (truck stamps) - 13 = 7.\n", " - Calculate the total number of stamps: 11 (snowflake) + 20 (truck) + 7 (rose) = 38.\n", "\n", "6. **Explicit Calculation Steps**:\n", " - Truck stamps: 11 + 9 = $<11+9=20>20.\n", " - Rose stamps: 20 - 13 = $<20-13=7>7.\n", " - Total stamps: 11 + 20 + 7 = $<11+20+7=38>38.\n", "\n", "7. **Verify Each Step**: Recheck each calculation step to ensure correctness:\n", " - Truck stamps: 11 + 9 = 20.\n", " - Rose stamps: 20 - 13 = 7.\n", " - Total stamps: 11 + 20 + 7 = 38.\n", "\n", "8. **Combine Results**: Combine the number of each type of stamp correctly to find the total number of stamps.\n", "\n", "9. **Simplify and Notate**: The final answer is already simplified.\n", "\n", "10. **Mark the Final Answer**: 38\n", "\n", "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. 38\n", "\n", "[Question] It takes Roque two hours to walk to work and one hour to ride his bike to work. Roque walks to and from work three times a week and rides his bike to and from work twice a week. How many hours in total does he take to get to and from work a week with walking and biking?\n", "[Answer] 1. **Understand the Problem**: Roque has two modes of transportation to work: walking and biking. We need to calculate the total time he spends traveling to and from work in a week, considering the different times and frequencies for each mode.\n", "\n", "2. **Identify Components**:\n", " - Time to walk to work: 2 hours (one way).\n", " - Time to bike to work: 1 hour (one way).\n", " - Frequency of walking: 3 times a week (to and from work).\n", " - Frequency of biking: 2 times a week (to and from work).\n", "\n", "3. **Apply Relevant Principles**: Use basic arithmetic to calculate the total time spent walking and biking separately, then sum these times to get the total weekly travel time.\n", "\n", "4. **Logical Reasoning**:\n", " - Calculate the total walking time for a week:\n", " - One round trip (to and from work) by walking takes 2 hours (to work) + 2 hours (from work) = 4 hours.\n", " - Roque walks to and from work 3 times a week, so the total walking time is 4 hours per round trip * 3 round trips = 12 hours.\n", " - Calculate the total biking time for a week:\n", " - One round trip (to and from work) by biking takes 1 hour (to work) + 1 hour (from work) = 2 hours.\n", " - Roque bikes to and from work 2 times a week, so the total biking time is 2 hours per round trip * 2 round trips = 4 hours.\n", "\n", "5. **Detailed Explanations**:\n", " - Walking time calculation:\n", " - One round trip walking: 2 hours (to work) + 2 hours (from work) = 4 hours.\n", " - Total walking time for the week: 4 hours per round trip * 3 round trips = 12 hours.\n", " - Biking time calculation:\n", " - One round trip biking: 1 hour (to work) + 1 hour (from work) = 2 hours.\n", " - Total biking time for the week: 2 hours per round trip * 2 round trips = 4 hours.\n", " - Combine the total walking and biking times to get the total weekly travel time:\n", " - Total weekly travel time: 12 hours (walking) + 4 hours (biking) = 16 hours.\n", "\n", "6. **Explicit Calculation Steps**:\n", " - Walking time: 2 hours (one way) * 2 (round trip) * 3 (times a week) = $<2*2*3=12>12 hours.\n", " - Biking time: 1 hour (one way) * 2 (round trip) * 2 (times a week) = $<1*2*2=4>4 hours.\n", " - Total time: 12 hours (walking) + 4 hours (biking) = $<12+4=16>16 hours.\n", "\n", "7. **Verify Each Step**: Recheck each calculation step to ensure correctness. Confirm that the arithmetic operations and logic used are accurate.\n", "\n", "8. **Combine Results**: Combine the total walking and biking times correctly to ensure the final answer is accurate.\n", "\n", "9. **Simplify and Notate**: The final answer is already simplified and clearly presented.\n", "\n", "10. **Mark the Final Answer**: 16\n", "\n", "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. 16\n", "\n", "[Question] Betty is saving money for a new wallet which costs $100. Betty has only half of the money she needs. Her parents decided to give her $15 for that purpose, and her grandparents twice as much as her parents. How much more money does Betty need to buy the wallet?\n", "[Answer] 1. **Understand the Problem**: Betty is saving money for a wallet that costs $100. She currently has half of the money she needs. Her parents and grandparents are contributing additional amounts to help her reach her goal. We need to determine how much more money Betty needs to buy the wallet.\n", "\n", "2. **Identify Components**:\n", " - Total cost of the wallet: $100.\n", " - Amount Betty currently has: half of $100.\n", " - Contribution from parents: $15.\n", " - Contribution from grandparents: twice the amount given by parents.\n", "\n", "3. **Apply Relevant Principles**: Use basic arithmetic to calculate the total amount of money Betty will have after receiving contributions from her parents and grandparents, and then determine how much more she needs to reach $100.\n", "\n", "4. **Logical Reasoning**:\n", " - Calculate the amount Betty currently has: $100 / 2 = $50.\n", " - Calculate the contribution from grandparents: 2 * $15 = $30.\n", " - Calculate the total amount of money Betty will have: $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution).\n", "\n", "5. **Detailed Explanations**:\n", " - Betty currently has $50 because half of $100 is $50.\n", " - Her parents give her $15.\n", " - Her grandparents give her twice the amount her parents give, which is 2 * $15 = $30.\n", " - The total amount of money Betty will have is $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution) = $95.\n", "\n", "6. **Explicit Calculation Steps**:\n", " - Current amount: $100 / 2 = $<100/2=50>50.\n", " - Grandparents' contribution: 2 * $15 = $<2*15=30>30.\n", " - Total amount: $50 + $15 + $30 = $<50+15+30=95>95.\n", "\n", "7. **Verify Each Step**: Recheck each calculation step to ensure correctness.\n", " - Current amount: $100 / 2 = $50.\n", " - Grandparents' contribution: 2 * $15 = $30.\n", " - Total amount: $50 + $15 + $30 = $95.\n", "\n", "8. **Combine Results**: Combine the total amount of money Betty will have correctly.\n", " - Total amount: $50 (current amount) + $15 (parents' contribution) + $30 (grandparents' contribution) = $95.\n", "\n", "9. **Simplify and Notate**: The final answer is already simplified.\n", "\n", "10. **Mark the Final Answer**: \n", " - Amount Betty needs to buy the wallet: $100 - $95 = $<100-95=5>5.\n", "\n", "5 5\n", "\n", "[Question] A rectangle has a length of 10 cm and a width of 5 cm. What is the area and perimeter of the rectangle?\n", "[Answer] 1. **Understand the Problem**: We need to find both the area and the perimeter of a rectangle given its length and width.\n", "\n", "2. **Identify Components**: \n", " - Length of the rectangle (L) = 10 cm\n", " - Width of the rectangle (W) = 5 cm\n", "\n", "3. **Apply Relevant Principles**: \n", " - The formula for the area of a rectangle is \\( \\text{Area} = \\text{Length} \\times \\text{Width} \\).\n", " - The formula for the perimeter of a rectangle is \\( \\text{Perimeter} = 2 \\times (\\text{Length} + \\text{Width}) \\).\n", "\n", "4. **Logical Reasoning**:\n", " - To find the area, multiply the length by the width.\n", " - To find the perimeter, add the length and the width, then multiply the result by 2.\n", "\n", "5. **Detailed Explanations**:\n", " - Calculate the area: \\( \\text{Area} = 10 \\, \\text{cm} \\times 5 \\, \\text{cm} \\).\n", " - Calculate the perimeter: \\( \\text{Perimeter} = 2 \\times (10 \\, \\text{cm} + 5 \\, \\text{cm}) \\).\n", "\n", "6. **Explicit Calculation Steps**:\n", " - Area: \\( 10 \\times 5 = 50 \\, \\text{cm}^2 \\).\n", " - Perimeter: \\( 2 \\times (10 + 5) = 2 \\times 15 = 30 \\, \\text{cm} \\).\n", "\n", "7. **Verify Each Step**: \n", " - Recheck the area calculation: \\( 10 \\times 5 = 50 \\, \\text{cm}^2 \\).\n", " - Recheck the perimeter calculation: \\( 2 \\times 15 = 30 \\, \\text{cm} \\).\n", "\n", "8. **Combine Results**: \n", " - The area of the rectangle is \\( 50 \\, \\text{cm}^2 \\).\n", " - The perimeter of the rectangle is \\( 30 \\, \\text{cm} \\).\n", "\n", "9. **Simplify and Notate**: \n", " - The final answers are already simplified.\n", "\n", "10. **Mark the Final Answer**: \n", " - Area: 50 \\, \\text{cm}^2\n", " - Perimeter: 30 \\, \\text{cm}\n", "\n", "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. 50\n", "\n", "[Question] Solve for x in the equation 2x + 3 = 11.\n", "[Answer] **Understand the Problem**: We need to solve for the variable \\( x \\) in the given linear equation \\( 2x + 3 = 11 \\).\n", "\n", "**Identify Components**: \n", "- The equation is \\( 2x + 3 = 11 \\).\n", "- We need to isolate \\( x \\) on one side of the equation.\n", "\n", "**Apply Relevant Principles**: \n", "- Use basic algebraic principles to isolate \\( x \\).\n", "\n", "**Logical Reasoning**:\n", "1. Start with the given equation: \\( 2x + 3 = 11 \\).\n", "2. Subtract 3 from both sides of the equation to isolate the term with \\( x \\):\n", " \\[\n", " 2x + 3 - 3 = 11 - 3\n", " \\]\n", "3. Simplify both sides:\n", " \\[\n", " 2x = 8\n", " \\]\n", "4. Divide both sides by 2 to solve for \\( x \\):\n", " \\[\n", " \\frac{2x}{2} = \\frac{8}{2}\n", " \\]\n", "5. Simplify the division:\n", " \\[\n", " x = 4\n", " \\]\n", "\n", "**Detailed Explanations**:\n", "- Subtracting 3 from both sides removes the constant term on the left side, leaving \\( 2x \\) isolated.\n", "- Dividing both sides by 2 isolates \\( x \\) by removing the coefficient of 2.\n", "\n", "**Explicit Calculation Steps**:\n", "1. \\( 2x + 3 = 11 \\)\n", "2. \\( 2x + 3 - 3 = 11 - 3 \\)\n", "3. \\( 2x = 8 \\)\n", "4. \\( \\frac{2x}{2} = \\frac{8}{2} \\)\n", "5. \\( x = 4 \\)\n", "\n", "**Verify Each Step**:\n", "- Recheck each step to ensure no arithmetic errors:\n", " - Subtracting 3 from 11 gives 8.\n", " - Dividing 8 by 2 gives 4.\n", "\n", "**Combine Results**: The final value of \\( x \\) is correctly isolated and calculated.\n", "\n", "**Simplify and Notate**: The final answer is already simplified.\n", "\n", "**Mark the Final Answer**: 4\n", "\n", "By following these steps, we ensure that the solution is accurate, well-explained, and clearly presented. 4\n", "\n", "\n", "For each question present the reasoning followed by the correct answer.\"\"\"" ] } ], "metadata": { "kernelspec": { "display_name": "general", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }