{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new LLMCheckerChain chain...\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' 不,通常天空是藍色的,但有時候會有不同的顏色。'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import LLMCheckerChain\n", "from langchain.llms import OpenAI\n", "\n", "llm = OpenAI(temperature=0.7)\n", "\n", "text = \"天空是黑色的?中文回答\"\n", "\n", "checker_chain = LLMCheckerChain.from_llm(llm, verbose=True)\n", "\n", "checker_chain.run(text)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new LLMSummarizationCheckerChain chain...\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mGiven some text, extract a list of facts from the text.\n", "\n", "Format your output as a bulleted list.\n", "\n", "Text:\n", "\"\"\"\n", "天空是黑色的?中文回答\n", "\"\"\"\n", "\n", "Facts:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mYou are an expert fact checker. You have been hired by a major news organization to fact check a very important story.\n", "\n", "Here is a bullet point list of facts:\n", "\"\"\"\n", "\n", "- The sky is black.\n", "- The answer is in Chinese.\n", "\"\"\"\n", "\n", "For each fact, determine whether it is true or false about the subject. If you are unable to determine whether the fact is true or false, output \"Undetermined\".\n", "If the fact is false, explain why.\n", "\n", "\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mBelow are some assertions that have been fact checked and are labeled as true of false. If the answer is false, a suggestion is given for a correction.\n", "\n", "Checked Assertions:\n", "\"\"\"\n", "\n", "- The sky is black: False. The sky is usually blue during the day and turns to shades of orange, pink, and purple at sunset. \n", "- The answer is in Chinese: Undetermined. Without more information about the subject, it is impossible to determine whether the answer is in Chinese.\n", "\"\"\"\n", "\n", "Original Summary:\n", "\"\"\"\n", "天空是黑色的?中文回答\n", "\"\"\"\n", "\n", "Using these checked assertions, rewrite the original summary to be completely true.\n", "\n", "The output should have the same structure and formatting as the original summary.\n", "\n", "Summary:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mBelow are some assertions that have been fact checked and are labeled as true or false.\n", "\n", "If all of the assertions are true, return \"True\". If any of the assertions are false, return \"False\".\n", "\n", "Here are some examples:\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is red: False\n", "- Water is made of lava: False\n", "- The sun is a star: True\n", "\"\"\"\n", "Result: False\n", "\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is blue: True\n", "- Water is wet: True\n", "- The sun is a star: True\n", "\"\"\"\n", "Result: True\n", "\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is blue - True\n", "- Water is made of lava- False\n", "- The sun is a star - True\n", "\"\"\"\n", "Result: False\n", "\n", "===\n", "\n", "Checked Assertions:\"\"\"\n", "\n", "- The sky is black: False. The sky is usually blue during the day and turns to shades of orange, pink, and purple at sunset. \n", "- The answer is in Chinese: Undetermined. Without more information about the subject, it is impossible to determine whether the answer is in Chinese.\n", "\"\"\"\n", "Result:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "天空通常在白天是藍色的,在日落時會轉變成橙色、粉紅色和紫色。沒有更多有關主題的資訊,無法確定答案是否是中文。\n", "\n", "\n", "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mGiven some text, extract a list of facts from the text.\n", "\n", "Format your output as a bulleted list.\n", "\n", "Text:\n", "\"\"\"\n", "\n", "天空通常在白天是藍色的,在日落時會轉變成橙色、粉紅色和紫色。沒有更多有關主題的資訊,無法確定答案是否是中文。\n", "\"\"\"\n", "\n", "Facts:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mYou are an expert fact checker. You have been hired by a major news organization to fact check a very important story.\n", "\n", "Here is a bullet point list of facts:\n", "\"\"\"\n", "\n", "- The sky is usually blue during the day.\n", "- At sunset, the sky can change to orange, pink, and purple.\n", "\"\"\"\n", "\n", "For each fact, determine whether it is true or false about the subject. If you are unable to determine whether the fact is true or false, output \"Undetermined\".\n", "If the fact is false, explain why.\n", "\n", "\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mBelow are some assertions that have been fact checked and are labeled as true of false. If the answer is false, a suggestion is given for a correction.\n", "\n", "Checked Assertions:\n", "\"\"\"\n", "\n", "- The sky is usually blue during the day. True\n", "- At sunset, the sky can change to orange, pink, and purple. True\n", "\"\"\"\n", "\n", "Original Summary:\n", "\"\"\"\n", "\n", "天空通常在白天是藍色的,在日落時會轉變成橙色、粉紅色和紫色。沒有更多有關主題的資訊,無法確定答案是否是中文。\n", "\"\"\"\n", "\n", "Using these checked assertions, rewrite the original summary to be completely true.\n", "\n", "The output should have the same structure and formatting as the original summary.\n", "\n", "Summary:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mBelow are some assertions that have been fact checked and are labeled as true or false.\n", "\n", "If all of the assertions are true, return \"True\". If any of the assertions are false, return \"False\".\n", "\n", "Here are some examples:\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is red: False\n", "- Water is made of lava: False\n", "- The sun is a star: True\n", "\"\"\"\n", "Result: False\n", "\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is blue: True\n", "- Water is wet: True\n", "- The sun is a star: True\n", "\"\"\"\n", "Result: True\n", "\n", "===\n", "\n", "Checked Assertions: \"\"\"\n", "- The sky is blue - True\n", "- Water is made of lava- False\n", "- The sun is a star - True\n", "\"\"\"\n", "Result: False\n", "\n", "===\n", "\n", "Checked Assertions:\"\"\"\n", "\n", "- The sky is usually blue during the day. True\n", "- At sunset, the sky can change to orange, pink, and purple. True\n", "\"\"\"\n", "Result:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'天空通常在白天是藍色的,在日落時會變成橙色、粉紅色和紫色。'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import LLMSummarizationCheckerChain\n", "checker_chain = LLMSummarizationCheckerChain.from_llm(llm, verbose=True, max_checks=2)\n", "checker_chain.run(text)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }