diff --git a/Solar-Fullstack-LLM-101/14_Reasoning.ipynb b/Solar-Fullstack-LLM-101/14_Reasoning.ipynb index 4e8d3b8..0a4c001 100644 --- a/Solar-Fullstack-LLM-101/14_Reasoning.ipynb +++ b/Solar-Fullstack-LLM-101/14_Reasoning.ipynb @@ -1,596 +1,699 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "\"Open\n", - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 14. Reasoning" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Goal\n", - "Apply reasoning and CoT in the prompt." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "! pip3 install -qU langchain-upstage requests python-dotenv datasets arize-phoenix" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## UPSTAGE_API_KEY\n", - "To obtain your Upstage API key, follow these steps:\n", - "\n", - "1. Visit the Upstage AI console at .\n", - "2. Sign up for an account if you don't already have one.\n", - "3. Log in to your account.\n", - "4. Navigate to the API key section.\n", - "5. Generate your API key.\n", - "6. Copy the key and save it securely.\n", - "\n", - "![Console](./figures/console.upstage.ai.jpg)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# @title set API key\n", - "from pprint import pprint\n", - "import os\n", - "\n", - "import warnings\n", - "\n", - "warnings.filterwarnings(\"ignore\")\n", - "\n", - "if \"google.colab\" in str(get_ipython()):\n", - " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n", - " from google.colab import userdata\n", - "\n", - " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n", - "else:\n", - " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n", - " from dotenv import load_dotenv\n", - "\n", - " load_dotenv()\n", - "\n", - "assert (\n", - " \"UPSTAGE_API_KEY\" in os.environ\n", - "), \"Please set the UPSTAGE_API_KEY environment variable\"" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ + "cells": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "🌍 To view the Phoenix app in your browser, visit http://localhost:6006/\n", - "📖 For more information on how to use Phoenix, check out https://docs.arize.com/phoenix\n" - ] - } - ], - "source": [ - "import phoenix as px\n", - "\n", - "ssession = px.launch_app()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "1wENfKv_jcdq" + }, + "source": [ + "\n", + "\"Open\n", + "" + ] + }, { - "data": { - "text/plain": [ - ">" + "cell_type": "markdown", + "metadata": { + "id": "yL9Ci8Ofjcdr" + }, + "source": [ + "# 14. Reasoning" ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from phoenix.trace.langchain import LangChainInstrumentor\n", - "\n", - "LangChainInstrumentor().instrument" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "from datasets import load_dataset\n", - "\n", - "ds = load_dataset(\"SkunkworksAI/reasoning-0.01\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ + }, { - "data": { - "text/plain": [ - "DatasetDict({\n", - " train: Dataset({\n", - " features: ['instruction', 'reasoning', 'output', 'reasoning_chains'],\n", - " num_rows: 29857\n", - " })\n", - "})" + "cell_type": "markdown", + "metadata": { + "id": "dStdKMBljcds" + }, + "source": [ + "## Goal\n", + "Apply reasoning and CoT in the prompt." ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "('----\\n'\n", - " 'Example 1:\\n'\n", - " '\\n'\n", - " 'Instruction: If a die is rolled three times, what is the probability of '\n", - " 'getting a sum of 11? None\\n'\n", - " '\\n'\n", - " 'Reasoning: 1. Understand the problem: We need to find the probability of '\n", - " 'getting a sum of 11 when rolling a die three times.\\n'\n", - " '2. Calculate total possible outcomes: A die has 6 faces, so for each roll, '\n", - " 'there are 6 possibilities. For three rolls, the total possible outcomes are '\n", - " '6^3 = 216.\\n'\n", - " '3. Identify favorable outcomes: List all combinations of rolls that result '\n", - " 'in a sum of 11. There are 18 such combinations.\\n'\n", - " '4. Calculate probability: Divide the number of favorable outcomes by the '\n", - " 'total possible outcomes: 18 / 216 = 1/12.\\n'\n", - " '5. Conclusion: The probability of getting a sum of 11 when rolling a die '\n", - " 'three times is 1/12.\\n'\n", - " '\\n'\n", - " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", - " 'to find the probability of getting a sum of 11 when rolling a die three '\n", - " \"times.'}, {'step': 2, 'thought': 'Calculate total possible outcomes: A die \"\n", - " 'has 6 faces, so for each roll, there are 6 possibilities. For three rolls, '\n", - " \"the total possible outcomes are 6^3 = 216.'}, {'step': 3, 'thought': \"\n", - " \"'Identify favorable outcomes: List all combinations of rolls that result in \"\n", - " \"a sum of 11. There are 18 such combinations.'}, {'step': 4, 'thought': \"\n", - " \"'Calculate probability: Divide the number of favorable outcomes by the total \"\n", - " \"possible outcomes: 18 / 216 = 1/12.'}, {'step': 5, 'thought': 'Conclusion: \"\n", - " 'The probability of getting a sum of 11 when rolling a die three times is '\n", - " \"1/12.'}]\\n\"\n", - " '----\\n'\n", - " 'Example 2:\\n'\n", - " '\\n'\n", - " 'Instruction: The interactions will be about the science behind culinary '\n", - " 'techniques. The setting is a cooking class where three friends are '\n", - " 'discussing various aspects of cooking and sharing their knowledge.\\n'\n", - " '- USER/Jane: A curious learner who wants to understand the science behind '\n", - " 'cooking\\n'\n", - " '- Mike: An experienced home cook with a passion for experimenting in the '\n", - " 'kitchen\\n'\n", - " '- Sarah: A food scientist who loves explaining the chemistry behind '\n", - " 'different cooking processes\\n'\n", - " '\\n'\n", - " ' None\\n'\n", - " '\\n'\n", - " 'Reasoning: 1. Start with the given setting: a cooking class with three '\n", - " 'friends discussing the science behind culinary techniques.\\n'\n", - " '2. Introduce the topic of resting meat after cooking, with Mike asking Jane '\n", - " \"if she's ever wondered about it.\\n\"\n", - " '3. Have Sarah explain the science behind resting meat, mentioning denatured '\n", - " 'proteins and juice redistribution.\\n'\n", - " \"4. Address the user's question about resting meat, with Sarah confirming \"\n", - " 'that it allows juices to redistribute.\\n'\n", - " '5. Move on to the topic of adding salt to water, with Mike mentioning its '\n", - " 'effect on boiling point.\\n'\n", - " \"6. Have Sarah explain the science behind salt's effect on boiling point, \"\n", - " 'mentioning the higher temperature required for boiling.\\n'\n", - " \"7. Address the user's question about cooking speed, with Sarah explaining \"\n", - " \"that it's slightly faster due to the hotter water.\\n\"\n", - " '8. Introduce the topic of acids in cooking, with Mike mentioning their use '\n", - " 'in brightening dishes.\\n'\n", - " \"9. Have Sarah explain the science behind acids' effects on flavor and \"\n", - " 'tenderizing meats.\\n'\n", - " \"10. Address the user's question about baking, with Mike mentioning the \"\n", - " 'science involved in baking and Sarah explaining the role of gluten and '\n", - " 'leavening agents.\\n'\n", - " '11. Conclude the conversation with the characters expressing their '\n", - " 'fascination with the science behind cooking and their excitement to continue '\n", - " 'learning and experimenting.\\n'\n", - " '\\n'\n", - " \"Reasoning Chains: [{'step': 1, 'thought': 'Start with the given setting: a \"\n", - " 'cooking class with three friends discussing the science behind culinary '\n", - " 'techniques.\\'}, {\\'step\\': 2, \\'thought\\': \"Introduce the topic of resting '\n", - " \"meat after cooking, with Mike asking Jane if she's ever wondered about \"\n", - " 'it.\"}, {\\'step\\': 3, \\'thought\\': \\'Have Sarah explain the science behind '\n", - " \"resting meat, mentioning denatured proteins and juice redistribution.'}, \"\n", - " '{\\'step\\': 4, \\'thought\\': \"Address the user\\'s question about resting meat, '\n", - " 'with Sarah confirming that it allows juices to redistribute.\"}, {\\'step\\': '\n", - " \"5, 'thought': 'Move on to the topic of adding salt to water, with Mike \"\n", - " 'mentioning its effect on boiling point.\\'}, {\\'step\\': 6, \\'thought\\': \"Have '\n", - " \"Sarah explain the science behind salt's effect on boiling point, mentioning \"\n", - " 'the higher temperature required for boiling.\"}, {\\'step\\': 7, \\'thought\\': '\n", - " '\"Address the user\\'s question about cooking speed, with Sarah explaining '\n", - " 'that it\\'s slightly faster due to the hotter water.\"}, {\\'step\\': 8, '\n", - " \"'thought': 'Introduce the topic of acids in cooking, with Mike mentioning \"\n", - " 'their use in brightening dishes.\\'}, {\\'step\\': 9, \\'thought\\': \"Have Sarah '\n", - " \"explain the science behind acids' effects on flavor and tenderizing \"\n", - " 'meats.\"}, {\\'step\\': 10, \\'thought\\': \"Address the user\\'s question about '\n", - " 'baking, with Mike mentioning the science involved in baking and Sarah '\n", - " 'explaining the role of gluten and leavening agents.\"}, {\\'step\\': 11, '\n", - " \"'thought': 'Conclude the conversation with the characters expressing their \"\n", - " 'fascination with the science behind cooking and their excitement to continue '\n", - " \"learning and experimenting.'}]\\n\")\n" - ] - } - ], - "source": [ - "# DatasetDict({\n", - "# train: Dataset({\n", - "# features: ['instruction', 'reasoning', 'output', 'reasoning_chains'],\n", - "# num_rows: 29857\n", - "# })\n", - "# })\n", - "\n", - "# Let's take a look at the first five examples in the training set\n", - "# Skip Output: {example['output']}\n", - "reasoning_chain_examples = \"\"\n", - "for i, example in enumerate(ds[\"train\"].select(range(2))):\n", - " reasoning_chain_examples += f\"\"\"----\n", - "Example {i + 1}:\n", - "\n", - "Instruction: {example['instruction']}\n", - "\n", - "Reasoning: {example['reasoning']}\n", - "\n", - "Reasoning Chains: {example['reasoning_chains']}\n", - "\"\"\"\n", - "\n", - "pprint(reasoning_chain_examples)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "1iCAb4EGjcds" + }, + "outputs": [], + "source": [ + "! pip3 install -qU langchain-upstage requests python-dotenv datasets arize-phoenix openinference-instrumentation-langchain" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "Human: Given Instruction, please generate {what}. Please use the following exampels.\n", - " If reasoning and/or reasoning chains are provided, please use them as context to generate the {what}.\n", - " Please only generate the {what} and do not include others.\n", - " \n", - " See the examples below:\n", - " ----\n", - "Example 1:\n", - "\n", - "Instruction: If a die is rolled three times, what is the probability of getting a sum of 11? None\n", - "\n", - "Reasoning: 1. Understand the problem: We need to find the probability of getting a sum of 11 when rolling a die three times.\n", - "2. Calculate total possible outcomes: A die has 6 faces, so for each roll, there are 6 possibilities. For three rolls, the total possible outcomes are 6^3 = 216.\n", - "3. Identify favorable outcomes: List all combinations of rolls that result in a sum of 11. There are 18 such combinations.\n", - "4. Calculate probability: Divide the number of favorable outcomes by the total possible outcomes: 18 / 216 = 1/12.\n", - "5. Conclusion: The probability of getting a sum of 11 when rolling a die three times is 1/12.\n", - "\n", - "Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need to find the probability of getting a sum of 11 when rolling a die three times.'}, {'step': 2, 'thought': 'Calculate total possible outcomes: A die has 6 faces, so for each roll, there are 6 possibilities. For three rolls, the total possible outcomes are 6^3 = 216.'}, {'step': 3, 'thought': 'Identify favorable outcomes: List all combinations of rolls that result in a sum of 11. There are 18 such combinations.'}, {'step': 4, 'thought': 'Calculate probability: Divide the number of favorable outcomes by the total possible outcomes: 18 / 216 = 1/12.'}, {'step': 5, 'thought': 'Conclusion: The probability of getting a sum of 11 when rolling a die three times is 1/12.'}]\n", - "----\n", - "Example 2:\n", - "\n", - "Instruction: The interactions will be about the science behind culinary techniques. The setting is a cooking class where three friends are discussing various aspects of cooking and sharing their knowledge.\n", - "- USER/Jane: A curious learner who wants to understand the science behind cooking\n", - "- Mike: An experienced home cook with a passion for experimenting in the kitchen\n", - "- Sarah: A food scientist who loves explaining the chemistry behind different cooking processes\n", - "\n", - " None\n", - "\n", - "Reasoning: 1. Start with the given setting: a cooking class with three friends discussing the science behind culinary techniques.\n", - "2. Introduce the topic of resting meat after cooking, with Mike asking Jane if she's ever wondered about it.\n", - "3. Have Sarah explain the science behind resting meat, mentioning denatured proteins and juice redistribution.\n", - "4. Address the user's question about resting meat, with Sarah confirming that it allows juices to redistribute.\n", - "5. Move on to the topic of adding salt to water, with Mike mentioning its effect on boiling point.\n", - "6. Have Sarah explain the science behind salt's effect on boiling point, mentioning the higher temperature required for boiling.\n", - "7. Address the user's question about cooking speed, with Sarah explaining that it's slightly faster due to the hotter water.\n", - "8. Introduce the topic of acids in cooking, with Mike mentioning their use in brightening dishes.\n", - "9. Have Sarah explain the science behind acids' effects on flavor and tenderizing meats.\n", - "10. Address the user's question about baking, with Mike mentioning the science involved in baking and Sarah explaining the role of gluten and leavening agents.\n", - "11. Conclude the conversation with the characters expressing their fascination with the science behind cooking and their excitement to continue learning and experimenting.\n", - "\n", - "Reasoning Chains: [{'step': 1, 'thought': 'Start with the given setting: a cooking class with three friends discussing the science behind culinary techniques.'}, {'step': 2, 'thought': \"Introduce the topic of resting meat after cooking, with Mike asking Jane if she's ever wondered about it.\"}, {'step': 3, 'thought': 'Have Sarah explain the science behind resting meat, mentioning denatured proteins and juice redistribution.'}, {'step': 4, 'thought': \"Address the user's question about resting meat, with Sarah confirming that it allows juices to redistribute.\"}, {'step': 5, 'thought': 'Move on to the topic of adding salt to water, with Mike mentioning its effect on boiling point.'}, {'step': 6, 'thought': \"Have Sarah explain the science behind salt's effect on boiling point, mentioning the higher temperature required for boiling.\"}, {'step': 7, 'thought': \"Address the user's question about cooking speed, with Sarah explaining that it's slightly faster due to the hotter water.\"}, {'step': 8, 'thought': 'Introduce the topic of acids in cooking, with Mike mentioning their use in brightening dishes.'}, {'step': 9, 'thought': \"Have Sarah explain the science behind acids' effects on flavor and tenderizing meats.\"}, {'step': 10, 'thought': \"Address the user's question about baking, with Mike mentioning the science involved in baking and Sarah explaining the role of gluten and leavening agents.\"}, {'step': 11, 'thought': 'Conclude the conversation with the characters expressing their fascination with the science behind cooking and their excitement to continue learning and experimenting.'}]\n", - "\n", - " ---\n", - " \n", - " Instruction: {instruction}\n", - " --\n", - " {reasoning}\n", - " --\n", - " {reasoning_chains} \n", - " \n" - ] - } - ], - "source": [ - "from langchain_core.prompts import PromptTemplate, ChatPromptTemplate\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_upstage import ChatUpstage\n", - "\n", - "reasoning_chain_teample = ChatPromptTemplate.from_messages(\n", - " [\n", - " (\n", - " \"human\",\n", - " \"\"\"Given Instruction, please generate {what}. Please use the following exampels.\n", - " If reasoning and/or reasoning chains are provided, please use them as context to generate the {what}.\n", - " Please only generate the {what} and do not include others.\n", - " \n", - " See the examples below:\n", - " {examples}\n", - " ---\n", - " \n", - " Instruction: {instruction}\n", - " --\n", - " {reasoning}\n", - " --\n", - " {reasoning_chains} \n", - " \"\"\",\n", - " )\n", - " ]\n", - ")\n", - "\n", - "\n", - "promt_example = reasoning_chain_teample.format(\n", - " what=\"{what}\",\n", - " instruction=\"{instruction}\",\n", - " reasoning=\"{reasoning}\",\n", - " reasoning_chains=\"{reasoning_chains}\",\n", - " examples=reasoning_chain_examples,\n", - ")\n", - "print(promt_example)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "llm = ChatUpstage(model=\"solar-pro\")\n", - "chain = reasoning_chain_teample | llm | StrOutputParser()" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "instruction = \"3.11 vs 3.9 which one is greater?\"" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "JtwI22PXjcds" + }, + "source": [ + "## UPSTAGE_API_KEY\n", + "To obtain your Upstage API key, follow these steps:\n", + "\n", + "1. Visit the Upstage AI console at .\n", + "2. Sign up for an account if you don't already have one.\n", + "3. Log in to your account.\n", + "4. Navigate to the API key section.\n", + "5. Generate your API key.\n", + "6. Copy the key and save it securely.\n", + "\n", + "![Console](https://github.com/UpstageAI/cookbook/blob/main/Solar-Fullstack-LLM-101/figures/console.upstage.ai.jpg?raw=1)" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Reasoning: 1. Identify the problem: We need to compare the numerical values '\n", - " 'of 3.11 and 3.9.\\n'\n", - " '2. Understand place values: Recognize that both numbers have the same whole '\n", - " 'number part (3), so we only need to compare the decimal parts (0.11 and '\n", - " '0.9).\\n'\n", - " '3. Compare decimal parts: Since 0.9 is greater than 0.11, the number with '\n", - " '0.9 as its decimal part is greater.\\n'\n", - " '4. Conclude: Based on the comparison, 3.9 is greater than 3.11.\\n'\n", - " '\\n'\n", - " \"Reasoning Chains: [{'step': 1, 'thought': 'Identify the problem: We need to \"\n", - " \"compare the numerical values of 3.11 and 3.9.'}, {'step': 2, 'thought': \"\n", - " \"'Understand place values: Recognize that both numbers have the same whole \"\n", - " 'number part (3), so we only need to compare the decimal parts (0.11 and '\n", - " \"0.9).'}, {'step': 3, 'thought': 'Compare decimal parts: Since 0.9 is greater \"\n", - " \"than 0.11, the number with 0.9 as its decimal part is greater.'}, {'step': \"\n", - " \"4, 'thought': 'Conclude: Based on the comparison, 3.9 is greater than \"\n", - " \"3.11.'}]\")\n" - ] - } - ], - "source": [ - "reasoning = chain.invoke(\n", - " {\n", - " \"instruction\": instruction,\n", - " \"reasoning\": \"\",\n", - " \"reasoning_chains\": \"\",\n", - " \"what\": \"reasoning\",\n", - " \"examples\": reasoning_chain_examples,\n", - " }\n", - ")\n", - "\n", - "pprint(reasoning)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "LiXXGlRJjcds" + }, + "outputs": [], + "source": [ + "# @title set API key\n", + "from pprint import pprint\n", + "import os\n", + "\n", + "import warnings\n", + "\n", + "warnings.filterwarnings(\"ignore\")\n", + "\n", + "if \"google.colab\" in str(get_ipython()):\n", + " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n", + " from google.colab import userdata\n", + "\n", + " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n", + "else:\n", + " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n", + " from dotenv import load_dotenv\n", + "\n", + " load_dotenv()\n", + "\n", + "assert (\n", + " \"UPSTAGE_API_KEY\" in os.environ\n", + "), \"Please set the UPSTAGE_API_KEY environment variable\"" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Reasoning: 1. Understand the problem: We need to compare two decimal '\n", - " 'numbers, 3.11 and 3.9, to determine which one is greater.\\n'\n", - " '2. Compare the whole number parts: Both numbers have the same whole number '\n", - " 'part, which is 3.\\n'\n", - " '3. Compare the decimal parts: Look at the first decimal place. For 3.11, '\n", - " \"it's 1, and for 3.9, it's 9. Since 9 is greater than 1, 3.9 is greater than \"\n", - " '3.11.\\n'\n", - " '4. Conclusion: 3.9 is greater than 3.11.\\n'\n", - " '\\n'\n", - " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", - " 'to compare two decimal numbers, 3.11 and 3.9, to determine which one is '\n", - " \"greater.'}, {'step': 2, 'thought': 'Compare the whole number parts: Both \"\n", - " \"numbers have the same whole number part, which is 3.'}, {'step': 3, \"\n", - " \"'thought': 'Compare the decimal parts: Look at the first decimal place. For \"\n", - " \"3.11, it\\\\'s 1, and for 3.9, it\\\\'s 9. Since 9 is greater than 1, 3.9 is \"\n", - " \"greater than 3.11.'}, {'step': 4, 'thought': 'Conclusion: 3.9 is greater \"\n", - " \"than 3.11.'}]\")\n" - ] - } - ], - "source": [ - "reasoning = chain.invoke(\n", - " {\n", - " \"instruction\": instruction,\n", - " \"reasoning\": \"\",\n", - " \"reasoning_chains\": \"\",\n", - " \"what\": \"reasoning\",\n", - " \"examples\": reasoning_chain_examples,\n", - " }\n", - ")\n", - "\n", - "pprint(reasoning)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "id": "t_IpbqFGjcds", + "outputId": "d914f467-71b2-44df-efd9-ba9b49ba89c3" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "🌍 To view the Phoenix app in your browser, visit https://xylx1gv2ve9-496ff2e9c6d22116-6006-colab.googleusercontent.com/\n", + "📖 For more information on how to use Phoenix, check out https://docs.arize.com/phoenix\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "import phoenix as px\n", + "\n", + "px.launch_app()" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Reasoning: Reasoning: 1. Understand the problem: We need to compare two '\n", - " 'decimal numbers, 3.11 and 3.9, to determine which one is greater.\\n'\n", - " '2. Compare the whole number parts: Both numbers have the same whole number '\n", - " 'part, which is 3.\\n'\n", - " '3. Compare the decimal parts: Look at the first decimal place. For 3.11, '\n", - " \"it's 1, and for 3.9, it's 9. Since 9 is greater than 1, 3.9 is greater than \"\n", - " '3.11.\\n'\n", - " '4. Conclusion: 3.9 is greater than 3.11.\\n'\n", - " '\\n'\n", - " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", - " 'to compare two decimal numbers, 3.11 and 3.9, to determine which one is '\n", - " \"greater.'}, {'step': 2, 'thought': 'Compare the whole number parts: Both \"\n", - " \"numbers have the same whole number part, which is 3.'}, {'step': 3, \"\n", - " \"'thought': 'Compare the decimal parts: Look at the first decimal place. For \"\n", - " \"3.11, it\\\\'s 1, and for 3.9, it\\\\'s 9. Since 9 is greater than 1, 3.9 is \"\n", - " \"greater than 3.11.'}, {'step': 4, 'thought': 'Conclusion: 3.9 is greater \"\n", - " \"than 3.11.'}]\")\n" - ] - } - ], - "source": [ - "reasoning_chain = chain.invoke(\n", - " {\n", - " \"instruction\": instruction,\n", - " \"reasoning\": f\"Reasoning: {reasoning}\",\n", - " \"reasoning_chains\": \"\",\n", - " \"what\": \"reasoning chain\",\n", - " \"examples\": reasoning_chain_examples,\n", - " }\n", - ")\n", - "\n", - "pprint(reasoning_chain)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "source": [ + "from phoenix.otel import register\n", + "\n", + "tracer_provider = register(\n", + " project_name=\"reasoning\", # Default is 'default'\n", + ")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "d5LIEVPZxDxh", + "outputId": "5850b17a-fd37-471d-ab82-3dd03b6733ef" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "🔭 OpenTelemetry Tracing Details 🔭\n", + "| Phoenix Project: reasoning\n", + "| Span Processor: SimpleSpanProcessor\n", + "| Collector Endpoint: localhost:4317\n", + "| Transport: gRPC\n", + "| Transport Headers: {'user-agent': '****'}\n", + "| \n", + "| Using a default SpanProcessor. `add_span_processor` will overwrite this default.\n", + "| \n", + "| `register` has set this TracerProvider as the global OpenTelemetry default.\n", + "| To disable this behavior, call `register` with `set_global_tracer_provider=False`.\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "0Yn4WRGqjcdt" + }, + "outputs": [], + "source": [ + "from openinference.instrumentation.langchain import LangChainInstrumentor\n", + "\n", + "LangChainInstrumentor().instrument(tracer_provider=tracer_provider)" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "'3.9'\n" - ] + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "al1_0GWMjcdt" + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "\n", + "ds = load_dataset(\"SkunkworksAI/reasoning-0.01\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JFuqj2fJjcdt", + "outputId": "19c52097-896a-452a-afd6-28d542672f70" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "DatasetDict({\n", + " train: Dataset({\n", + " features: ['instruction', 'reasoning', 'output', 'reasoning_chains'],\n", + " num_rows: 29857\n", + " })\n", + "})" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "ds" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tLvFMWNPjcdt", + "outputId": "87488305-3661-42e9-b0af-5ee552f4e118" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "('----\\n'\n", + " 'Example 1:\\n'\n", + " '\\n'\n", + " 'Instruction: If a die is rolled three times, what is the probability of '\n", + " 'getting a sum of 11? None\\n'\n", + " '\\n'\n", + " 'Reasoning: 1. Understand the problem: We need to find the probability of '\n", + " 'getting a sum of 11 when rolling a die three times.\\n'\n", + " '2. Calculate total possible outcomes: A die has 6 faces, so for each roll, '\n", + " 'there are 6 possibilities. For three rolls, the total possible outcomes are '\n", + " '6^3 = 216.\\n'\n", + " '3. Identify favorable outcomes: List all combinations of rolls that result '\n", + " 'in a sum of 11. There are 18 such combinations.\\n'\n", + " '4. Calculate probability: Divide the number of favorable outcomes by the '\n", + " 'total possible outcomes: 18 / 216 = 1/12.\\n'\n", + " '5. Conclusion: The probability of getting a sum of 11 when rolling a die '\n", + " 'three times is 1/12.\\n'\n", + " '\\n'\n", + " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", + " 'to find the probability of getting a sum of 11 when rolling a die three '\n", + " \"times.'}, {'step': 2, 'thought': 'Calculate total possible outcomes: A die \"\n", + " 'has 6 faces, so for each roll, there are 6 possibilities. For three rolls, '\n", + " \"the total possible outcomes are 6^3 = 216.'}, {'step': 3, 'thought': \"\n", + " \"'Identify favorable outcomes: List all combinations of rolls that result in \"\n", + " \"a sum of 11. There are 18 such combinations.'}, {'step': 4, 'thought': \"\n", + " \"'Calculate probability: Divide the number of favorable outcomes by the total \"\n", + " \"possible outcomes: 18 / 216 = 1/12.'}, {'step': 5, 'thought': 'Conclusion: \"\n", + " 'The probability of getting a sum of 11 when rolling a die three times is '\n", + " \"1/12.'}]\\n\"\n", + " '----\\n'\n", + " 'Example 2:\\n'\n", + " '\\n'\n", + " 'Instruction: The interactions will be about the science behind culinary '\n", + " 'techniques. The setting is a cooking class where three friends are '\n", + " 'discussing various aspects of cooking and sharing their knowledge.\\n'\n", + " '- USER/Jane: A curious learner who wants to understand the science behind '\n", + " 'cooking\\n'\n", + " '- Mike: An experienced home cook with a passion for experimenting in the '\n", + " 'kitchen\\n'\n", + " '- Sarah: A food scientist who loves explaining the chemistry behind '\n", + " 'different cooking processes\\n'\n", + " '\\n'\n", + " ' None\\n'\n", + " '\\n'\n", + " 'Reasoning: 1. Start with the given setting: a cooking class with three '\n", + " 'friends discussing the science behind culinary techniques.\\n'\n", + " '2. Introduce the topic of resting meat after cooking, with Mike asking Jane '\n", + " \"if she's ever wondered about it.\\n\"\n", + " '3. Have Sarah explain the science behind resting meat, mentioning denatured '\n", + " 'proteins and juice redistribution.\\n'\n", + " \"4. Address the user's question about resting meat, with Sarah confirming \"\n", + " 'that it allows juices to redistribute.\\n'\n", + " '5. Move on to the topic of adding salt to water, with Mike mentioning its '\n", + " 'effect on boiling point.\\n'\n", + " \"6. Have Sarah explain the science behind salt's effect on boiling point, \"\n", + " 'mentioning the higher temperature required for boiling.\\n'\n", + " \"7. Address the user's question about cooking speed, with Sarah explaining \"\n", + " \"that it's slightly faster due to the hotter water.\\n\"\n", + " '8. Introduce the topic of acids in cooking, with Mike mentioning their use '\n", + " 'in brightening dishes.\\n'\n", + " \"9. Have Sarah explain the science behind acids' effects on flavor and \"\n", + " 'tenderizing meats.\\n'\n", + " \"10. Address the user's question about baking, with Mike mentioning the \"\n", + " 'science involved in baking and Sarah explaining the role of gluten and '\n", + " 'leavening agents.\\n'\n", + " '11. Conclude the conversation with the characters expressing their '\n", + " 'fascination with the science behind cooking and their excitement to continue '\n", + " 'learning and experimenting.\\n'\n", + " '\\n'\n", + " \"Reasoning Chains: [{'step': 1, 'thought': 'Start with the given setting: a \"\n", + " 'cooking class with three friends discussing the science behind culinary '\n", + " 'techniques.\\'}, {\\'step\\': 2, \\'thought\\': \"Introduce the topic of resting '\n", + " \"meat after cooking, with Mike asking Jane if she's ever wondered about \"\n", + " 'it.\"}, {\\'step\\': 3, \\'thought\\': \\'Have Sarah explain the science behind '\n", + " \"resting meat, mentioning denatured proteins and juice redistribution.'}, \"\n", + " '{\\'step\\': 4, \\'thought\\': \"Address the user\\'s question about resting meat, '\n", + " 'with Sarah confirming that it allows juices to redistribute.\"}, {\\'step\\': '\n", + " \"5, 'thought': 'Move on to the topic of adding salt to water, with Mike \"\n", + " 'mentioning its effect on boiling point.\\'}, {\\'step\\': 6, \\'thought\\': \"Have '\n", + " \"Sarah explain the science behind salt's effect on boiling point, mentioning \"\n", + " 'the higher temperature required for boiling.\"}, {\\'step\\': 7, \\'thought\\': '\n", + " '\"Address the user\\'s question about cooking speed, with Sarah explaining '\n", + " 'that it\\'s slightly faster due to the hotter water.\"}, {\\'step\\': 8, '\n", + " \"'thought': 'Introduce the topic of acids in cooking, with Mike mentioning \"\n", + " 'their use in brightening dishes.\\'}, {\\'step\\': 9, \\'thought\\': \"Have Sarah '\n", + " \"explain the science behind acids' effects on flavor and tenderizing \"\n", + " 'meats.\"}, {\\'step\\': 10, \\'thought\\': \"Address the user\\'s question about '\n", + " 'baking, with Mike mentioning the science involved in baking and Sarah '\n", + " 'explaining the role of gluten and leavening agents.\"}, {\\'step\\': 11, '\n", + " \"'thought': 'Conclude the conversation with the characters expressing their \"\n", + " 'fascination with the science behind cooking and their excitement to continue '\n", + " \"learning and experimenting.'}]\\n\")\n" + ] + } + ], + "source": [ + "# DatasetDict({\n", + "# train: Dataset({\n", + "# features: ['instruction', 'reasoning', 'output', 'reasoning_chains'],\n", + "# num_rows: 29857\n", + "# })\n", + "# })\n", + "\n", + "# Let's take a look at the first five examples in the training set\n", + "# Skip Output: {example['output']}\n", + "reasoning_chain_examples = \"\"\n", + "for i, example in enumerate(ds[\"train\"].select(range(2))):\n", + " reasoning_chain_examples += f\"\"\"----\n", + "Example {i + 1}:\n", + "\n", + "Instruction: {example['instruction']}\n", + "\n", + "Reasoning: {example['reasoning']}\n", + "\n", + "Reasoning Chains: {example['reasoning_chains']}\n", + "\"\"\"\n", + "\n", + "pprint(reasoning_chain_examples)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1dHvSSSkjcdt", + "outputId": "d877a566-289a-4c5f-eec2-6c9eaad8ae34" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Human: Given Instruction, please generate {what}. Please use the following exampels.\n", + " If reasoning and/or reasoning chains are provided, please use them as context to generate the {what}.\n", + " Please only generate the {what} and do not include others.\n", + " \n", + " See the examples below:\n", + " ----\n", + "Example 1:\n", + "\n", + "Instruction: If a die is rolled three times, what is the probability of getting a sum of 11? None\n", + "\n", + "Reasoning: 1. Understand the problem: We need to find the probability of getting a sum of 11 when rolling a die three times.\n", + "2. Calculate total possible outcomes: A die has 6 faces, so for each roll, there are 6 possibilities. For three rolls, the total possible outcomes are 6^3 = 216.\n", + "3. Identify favorable outcomes: List all combinations of rolls that result in a sum of 11. There are 18 such combinations.\n", + "4. Calculate probability: Divide the number of favorable outcomes by the total possible outcomes: 18 / 216 = 1/12.\n", + "5. Conclusion: The probability of getting a sum of 11 when rolling a die three times is 1/12.\n", + "\n", + "Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need to find the probability of getting a sum of 11 when rolling a die three times.'}, {'step': 2, 'thought': 'Calculate total possible outcomes: A die has 6 faces, so for each roll, there are 6 possibilities. For three rolls, the total possible outcomes are 6^3 = 216.'}, {'step': 3, 'thought': 'Identify favorable outcomes: List all combinations of rolls that result in a sum of 11. There are 18 such combinations.'}, {'step': 4, 'thought': 'Calculate probability: Divide the number of favorable outcomes by the total possible outcomes: 18 / 216 = 1/12.'}, {'step': 5, 'thought': 'Conclusion: The probability of getting a sum of 11 when rolling a die three times is 1/12.'}]\n", + "----\n", + "Example 2:\n", + "\n", + "Instruction: The interactions will be about the science behind culinary techniques. The setting is a cooking class where three friends are discussing various aspects of cooking and sharing their knowledge.\n", + "- USER/Jane: A curious learner who wants to understand the science behind cooking\n", + "- Mike: An experienced home cook with a passion for experimenting in the kitchen\n", + "- Sarah: A food scientist who loves explaining the chemistry behind different cooking processes\n", + "\n", + " None\n", + "\n", + "Reasoning: 1. Start with the given setting: a cooking class with three friends discussing the science behind culinary techniques.\n", + "2. Introduce the topic of resting meat after cooking, with Mike asking Jane if she's ever wondered about it.\n", + "3. Have Sarah explain the science behind resting meat, mentioning denatured proteins and juice redistribution.\n", + "4. Address the user's question about resting meat, with Sarah confirming that it allows juices to redistribute.\n", + "5. Move on to the topic of adding salt to water, with Mike mentioning its effect on boiling point.\n", + "6. Have Sarah explain the science behind salt's effect on boiling point, mentioning the higher temperature required for boiling.\n", + "7. Address the user's question about cooking speed, with Sarah explaining that it's slightly faster due to the hotter water.\n", + "8. Introduce the topic of acids in cooking, with Mike mentioning their use in brightening dishes.\n", + "9. Have Sarah explain the science behind acids' effects on flavor and tenderizing meats.\n", + "10. Address the user's question about baking, with Mike mentioning the science involved in baking and Sarah explaining the role of gluten and leavening agents.\n", + "11. Conclude the conversation with the characters expressing their fascination with the science behind cooking and their excitement to continue learning and experimenting.\n", + "\n", + "Reasoning Chains: [{'step': 1, 'thought': 'Start with the given setting: a cooking class with three friends discussing the science behind culinary techniques.'}, {'step': 2, 'thought': \"Introduce the topic of resting meat after cooking, with Mike asking Jane if she's ever wondered about it.\"}, {'step': 3, 'thought': 'Have Sarah explain the science behind resting meat, mentioning denatured proteins and juice redistribution.'}, {'step': 4, 'thought': \"Address the user's question about resting meat, with Sarah confirming that it allows juices to redistribute.\"}, {'step': 5, 'thought': 'Move on to the topic of adding salt to water, with Mike mentioning its effect on boiling point.'}, {'step': 6, 'thought': \"Have Sarah explain the science behind salt's effect on boiling point, mentioning the higher temperature required for boiling.\"}, {'step': 7, 'thought': \"Address the user's question about cooking speed, with Sarah explaining that it's slightly faster due to the hotter water.\"}, {'step': 8, 'thought': 'Introduce the topic of acids in cooking, with Mike mentioning their use in brightening dishes.'}, {'step': 9, 'thought': \"Have Sarah explain the science behind acids' effects on flavor and tenderizing meats.\"}, {'step': 10, 'thought': \"Address the user's question about baking, with Mike mentioning the science involved in baking and Sarah explaining the role of gluten and leavening agents.\"}, {'step': 11, 'thought': 'Conclude the conversation with the characters expressing their fascination with the science behind cooking and their excitement to continue learning and experimenting.'}]\n", + "\n", + " ---\n", + " \n", + " Instruction: {instruction}\n", + " --\n", + " {reasoning}\n", + " --\n", + " {reasoning_chains} \n", + " \n" + ] + } + ], + "source": [ + "from langchain_core.prompts import PromptTemplate, ChatPromptTemplate\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_upstage import ChatUpstage\n", + "\n", + "reasoning_chain_teample = ChatPromptTemplate.from_messages(\n", + " [\n", + " (\n", + " \"human\",\n", + " \"\"\"Given Instruction, please generate {what}. Please use the following exampels.\n", + " If reasoning and/or reasoning chains are provided, please use them as context to generate the {what}.\n", + " Please only generate the {what} and do not include others.\n", + "\n", + " See the examples below:\n", + " {examples}\n", + " ---\n", + "\n", + " Instruction: {instruction}\n", + " --\n", + " {reasoning}\n", + " --\n", + " {reasoning_chains}\n", + " \"\"\",\n", + " )\n", + " ]\n", + ")\n", + "\n", + "\n", + "promt_example = reasoning_chain_teample.format(\n", + " what=\"{what}\",\n", + " instruction=\"{instruction}\",\n", + " reasoning=\"{reasoning}\",\n", + " reasoning_chains=\"{reasoning_chains}\",\n", + " examples=reasoning_chain_examples,\n", + ")\n", + "print(promt_example)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "LkywI_n_jcdt" + }, + "outputs": [], + "source": [ + "llm = ChatUpstage(model=\"solar-pro\")\n", + "chain = reasoning_chain_teample | llm | StrOutputParser()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "7M-gv72Ijcdu" + }, + "outputs": [], + "source": [ + "instruction = \"3.11 vs 3.9 which one is greater?\"" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iKz80445jcdu", + "outputId": "cac14aa3-83d0-4d9f-f96c-dbbed23362d1" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "('Reasoning: 1. Understand the problem: We need to compare the values 3.11 and '\n", + " '3.9 to determine which one is greater.\\n'\n", + " '2. Analyze the numbers: Both numbers have the same whole number part (3), so '\n", + " 'we need to compare the decimal parts (0.11 and 0.9).\\n'\n", + " '3. Compare the decimal parts: 0.11 is smaller than 0.9 since 11 is less than '\n", + " '90 (when comparing the same number of decimal places).\\n'\n", + " '4. Conclude the comparison: Since 3.11 has a smaller decimal part than 3.9, '\n", + " '3.9 is greater than 3.11.\\n'\n", + " '\\n'\n", + " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", + " \"to compare the values 3.11 and 3.9 to determine which one is greater.'}, \"\n", + " \"{'step': 2, 'thought': 'Analyze the numbers: Both numbers have the same \"\n", + " 'whole number part (3), so we need to compare the decimal parts (0.11 and '\n", + " \"0.9).'}, {'step': 3, 'thought': 'Compare the decimal parts: 0.11 is smaller \"\n", + " 'than 0.9 since 11 is less than 90 (when comparing the same number of decimal '\n", + " \"places).'}, {'step': 4, 'thought': 'Conclude the comparison: Since 3.11 has \"\n", + " \"a smaller decimal part than 3.9, 3.9 is greater than 3.11.'}]\")\n" + ] + } + ], + "source": [ + "reasoning = chain.invoke(\n", + " {\n", + " \"instruction\": instruction,\n", + " \"reasoning\": \"\",\n", + " \"reasoning_chains\": \"\",\n", + " \"what\": \"reasoning\",\n", + " \"examples\": reasoning_chain_examples,\n", + " }\n", + ")\n", + "\n", + "pprint(reasoning)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FRCdBLivjcdu", + "outputId": "8022b484-09ba-49d1-d65f-72dba7443dd2" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "('Reasoning: 1. Understand the problem: We need to compare the values of 3.11 '\n", + " 'and 3.9 to determine which one is greater.\\n'\n", + " '2. Compare the whole number part: Both numbers have the same whole number '\n", + " 'part, which is 3.\\n'\n", + " '3. Compare the decimal part: Compare the decimal parts, 11 and 9, to '\n", + " 'determine which number is greater.\\n'\n", + " '4. Identify the greater decimal part: Since 11 is greater than 9, the number '\n", + " '3.11 has a greater decimal part.\\n'\n", + " '5. Draw a conclusion: Based on the comparison, 3.11 is greater than 3.9.\\n'\n", + " '\\n'\n", + " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", + " \"to compare the values of 3.11 and 3.9 to determine which one is greater.'}, \"\n", + " \"{'step': 2, 'thought': 'Compare the whole number part: Both numbers have the \"\n", + " \"same whole number part, which is 3.'}, {'step': 3, 'thought': 'Compare the \"\n", + " 'decimal part: Compare the decimal parts, 11 and 9, to determine which number '\n", + " \"is greater.'}, {'step': 4, 'thought': 'Identify the greater decimal part: \"\n", + " \"Since 11 is greater than 9, the number 3.11 has a greater decimal part.'}, \"\n", + " \"{'step': 5, 'thought': 'Draw a conclusion: Based on the comparison, 3.11 is \"\n", + " \"greater than 3.9.'}]\")\n" + ] + } + ], + "source": [ + "reasoning = chain.invoke(\n", + " {\n", + " \"instruction\": instruction,\n", + " \"reasoning\": \"\",\n", + " \"reasoning_chains\": \"\",\n", + " \"what\": \"reasoning\",\n", + " \"examples\": reasoning_chain_examples,\n", + " }\n", + ")\n", + "\n", + "pprint(reasoning)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "c3F9HYRtjcdu", + "outputId": "7e08f125-db72-4d79-e841-e4671eceb0ba" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "('Reasoning: Reasoning: 1. Understand the problem: We need to compare the '\n", + " 'values of 3.11 and 3.9 to determine which one is greater.\\n'\n", + " '2. Compare the whole number part: Both numbers have the same whole number '\n", + " 'part, which is 3.\\n'\n", + " '3. Compare the decimal part: Compare the decimal parts, 11 and 9, to '\n", + " 'determine which number is greater.\\n'\n", + " '4. Identify the greater decimal part: Since 11 is greater than 9, the number '\n", + " '3.11 has a greater decimal part.\\n'\n", + " '5. Draw a conclusion: Based on the comparison, 3.11 is greater than 3.9.\\n'\n", + " '\\n'\n", + " \"Reasoning Chains: [{'step': 1, 'thought': 'Understand the problem: We need \"\n", + " \"to compare the values of 3.11 and 3.9 to determine which one is greater.'}, \"\n", + " \"{'step': 2, 'thought': 'Compare the whole number part: Both numbers have the \"\n", + " \"same whole number part, which is 3.'}, {'step': 3, 'thought': 'Compare the \"\n", + " 'decimal part: Compare the decimal parts, 11 and 9, to determine which number '\n", + " \"is greater.'}, {'step': 4, 'thought': 'Identify the greater decimal part: \"\n", + " \"Since 11 is greater than 9, the number 3.11 has a greater decimal part.'}, \"\n", + " \"{'step': 5, 'thought': 'Draw a conclusion: Based on the comparison, 3.11 is \"\n", + " \"greater than 3.9.'}]\")\n" + ] + } + ], + "source": [ + "reasoning_chain = chain.invoke(\n", + " {\n", + " \"instruction\": instruction,\n", + " \"reasoning\": f\"Reasoning: {reasoning}\",\n", + " \"reasoning_chains\": \"\",\n", + " \"what\": \"reasoning chain\",\n", + " \"examples\": reasoning_chain_examples,\n", + " }\n", + ")\n", + "\n", + "pprint(reasoning_chain)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vI4qFp-Zjcdu", + "outputId": "8ba9e301-fbfc-4409-9154-240b315ea969" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "'3.11'\n" + ] + } + ], + "source": [ + "output = chain.invoke(\n", + " {\n", + " \"instruction\": instruction,\n", + " \"reasoning\": f\"Reasoning: {reasoning}\",\n", + " \"reasoning_chains\": f\"Reasoning Chains: {reasoning_chain}\",\n", + " \"examples\": reasoning_chain_examples,\n", + " \"what\": \"Final answer of the instruction\",\n", + " }\n", + ")\n", + "\n", + "pprint(output)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.6" + }, + "colab": { + "provenance": [] } - ], - "source": [ - "output = chain.invoke(\n", - " {\n", - " \"instruction\": instruction,\n", - " \"reasoning\": f\"Reasoning: {reasoning}\",\n", - " \"reasoning_chains\": f\"Reasoning Chains: {reasoning_chain}\",\n", - " \"examples\": reasoning_chain_examples,\n", - " \"what\": \"Final answer of the instruction\",\n", - " }\n", - ")\n", - "\n", - "pprint(output)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file