{ "cells": [ { "cell_type": "markdown", "id": "55e0d5644a165439", "metadata": {}, "source": [ "# Chain of Thought\n", "\n", "[Chain of Thought](https://arxiv.org/pdf/2201.11903) (CoT) is a common prompt engineering technique which asks the LLM to step through its reasoning and thinking process to answer a question. In its simplest form, it can be implemented by asking asking the LLM to step through a problem step by step, but is more effective when you leverage examples and patterns of reasoning similar to your query in a few shot prompt. Chain of Thought is most effective for mathematical and reasoning tasks.\n", "\n", "
\n", "

Mirascope Concepts Used

\n", "\n", "
\n", "\n", "## Zero Shot CoT\n", "\n", "
\n", "

Note

\n", "

Recent models will automatically explain their reasoning (to a degree) for most reasoning tasks, but explicitly asking for a step by step solution can sometimes produce better solutions and explanations.

\n", "
\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "db95d202c4327c24", "metadata": { "ExecuteTime": { "end_time": "2024-10-02T03:27:44.497513Z", "start_time": "2024-10-02T03:27:42.244266Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First, let's determine how much money Olivia spent on the bagels.\n", "\n", "1. **Calculate the total cost of the bagels:**\n", " - Price of one bagel = $3\n", " - Number of bagels = 5\n", " - Total cost = Price of one bagel × Number of bagels = $3 × 5 = $15\n", "\n", "2. **Subtract the total cost from Olivia's initial amount:**\n", " - Initial amount = $23\n", " - Amount spent = $15\n", " - Amount left = Initial amount - Amount spent = $23 - $15 = $8\n", "\n", "So, Olivia has $8 left after buying the bagels.\n" ] } ], "source": [ "from mirascope.core import openai, prompt_template\n", "\n", "cot_augment = \"\\nLet's think step by step.\"\n", "\n", "\n", "@openai.call(model=\"gpt-4o-mini\")\n", "@prompt_template(\"{query} {cot_augment}\")\n", "def call(query: str, cot_prompt: bool = False) -> openai.OpenAIDynamicConfig:\n", " return {\n", " \"computed_fields\": {\n", " \"cot_augment\": cot_augment if cot_prompt else \"\",\n", " }\n", " }\n", "\n", "\n", "prompt = \"\"\"Olivia has $23. She bought five bagels for $3 each.\n", "How much money does she have left?\"\"\"\n", "\n", "print(call(query=prompt, cot_prompt=True))" ] }, { "cell_type": "markdown", "id": "db55324749d6ee29", "metadata": {}, "source": "## Few Shot CoT" }, { "cell_type": "code", "execution_count": 6, "id": "7f3c8002dd2e25f7", "metadata": { "ExecuteTime": { "end_time": "2024-10-02T03:28:18.204170Z", "start_time": "2024-10-02T03:28:16.932950Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Olivia bought 5 bagels for $3 each, which costs her a total of \\(5 \\times 3 = 15\\) dollars. \n", "\n", "She started with $23, so after the purchase, she has \\(23 - 15 = 8\\) dollars left. \n", "\n", "The answer is $8.\n" ] } ], "source": [ "from mirascope.core import openai\n", "from openai.types.chat import ChatCompletionMessageParam\n", "\n", "few_shot_examples = [\n", " {\n", " \"question\": \"There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?\",\n", " \"answer\": \"\"\"There are 15 trees originally. Then there were 21 trees after some more were planted. So there must have been 21 - 15 = 6. The answer is 6.\"\"\",\n", " },\n", " {\n", " \"question\": \"If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?\",\n", " \"answer\": \"\"\"There are originally 3 cars. 2 more cars arrive. 3 + 2 = 5. The answer is 5.\"\"\",\n", " },\n", " {\n", " \"question\": \"Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?\",\n", " \"answer\": \"\"\"Originally, Leah had 32 chocolates. Her sister had 42. So in total they had 32 + 42 = 74. After eating 35, they had 74 - 35 = 39. The answer is 39.\"\"\",\n", " },\n", " {\n", " \"question\": \"Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?\",\n", " \"answer\": \"\"\"Jason started with 20 lollipops. Then he had 12 after giving some to Denny. So he gave Denny 20 - 12 = 8. The answer is 8.\"\"\",\n", " },\n", " {\n", " \"question\": \"Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?\",\n", " \"answer\": \"\"\"Shawn started with 5 toys. If he got 2 toys each from his mom and dad, then that is 4 more toys. 5 + 4 = 9. The answer is 9.\"\"\",\n", " },\n", " {\n", " \"question\": \"There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?\",\n", " \"answer\": \"\"\"There were originally 9 computers. For each of 4 days, 5 more computers were added. So 5 * 4 = 20 computers were added. 9 + 20 is 29. The answer is 29.\"\"\",\n", " },\n", " {\n", " \"question\": \"Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?\",\n", " \"answer\": \"\"\"Michael started with 58 golf balls. After losing 23 on tuesday, he had 58 - 23 = 35. After losing 2 more, he had 35 - 2 = 33 golf balls. The answer is 33.\"\"\",\n", " },\n", "]\n", "\n", "\n", "@openai.call(model=\"gpt-4o-mini\")\n", "@prompt_template(\n", " \"\"\"\n", " MESSAGES: {example_prompts}\n", " USER: {query}\n", " \"\"\"\n", ")\n", "def call(query: str, num_examples: int = 0) -> openai.OpenAIDynamicConfig:\n", " if num_examples < 0 or num_examples > len(few_shot_examples):\n", " raise ValueError(\n", " \"num_examples cannot be negative or greater than number of available examples.\"\n", " )\n", " example_prompts: list[ChatCompletionMessageParam] = []\n", " for i in range(num_examples):\n", " example_prompts.append(\n", " {\"role\": \"user\", \"content\": few_shot_examples[i][\"question\"]}\n", " )\n", " example_prompts.append(\n", " {\"role\": \"assistant\", \"content\": few_shot_examples[i][\"answer\"]}\n", " )\n", " return {\"computed_fields\": {\"example_prompts\": example_prompts}}\n", "\n", "\n", "prompt = \"\"\"Olivia has $23. She bought five bagels for $3 each.\n", "How much money does she have left?\"\"\"\n", "\n", "print(call(query=prompt, num_examples=len(few_shot_examples)))" ] }, { "cell_type": "markdown", "id": "3d3f0005550195c7", "metadata": {}, "source": [ "
\n", "

Effective Chain of Thought Usage

\n", "\n", "
\n", "\n", "By leveraging the Chain of Thought technique, you can make the LLM's reasoning process more transparent and obtain more accurate and explainable answers to complex problems. This technique is particularly useful for mathematical problems and tasks that require multi-step reasoning." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }