{ "cells": [ { "cell_type": "markdown", "id": "05859721", "metadata": {}, "source": [ "# Question Answering\n", "\n", "This notebook walks through how to use LangChain for question answering over a list of documents. It covers four different types of chaings: `stuff`, `map_reduce`, `refine`, `map-rerank`. For a more in depth explanation of what these chain types are, see [here](../combine_docs.md)." ] }, { "cell_type": "markdown", "id": "726f4996", "metadata": {}, "source": [ "## Prepare Data\n", "First we prepare the data. For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents)." ] }, { "cell_type": "code", "execution_count": 11, "id": "17fcbc0f", "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain.vectorstores.faiss import FAISS\n", "from langchain.docstore.document import Document" ] }, { "cell_type": "code", "execution_count": 12, "id": "291f0117", "metadata": {}, "outputs": [], "source": [ "with open('../../state_of_the_union.txt') as f:\n", " state_of_the_union = f.read()\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_text(state_of_the_union)\n", "\n", "embeddings = OpenAIEmbeddings()" ] }, { "cell_type": "code", "execution_count": 13, "id": "fd9666a9", "metadata": {}, "outputs": [], "source": [ "docsearch = FAISS.from_texts(texts, embeddings)" ] }, { "cell_type": "code", "execution_count": 14, "id": "d1eaf6e6", "metadata": {}, "outputs": [], "source": [ "query = \"What did the president say about Justice Breyer\"\n", "docs = docsearch.similarity_search(query)" ] }, { "cell_type": "code", "execution_count": 15, "id": "a16e3453", "metadata": {}, "outputs": [], "source": [ "from langchain.chains.question_answering import load_qa_chain\n", "from langchain.llms import OpenAI" ] }, { "cell_type": "markdown", "id": "f78787a0", "metadata": {}, "source": [ "## The `stuff` Chain\n", "\n", "This sections shows results of using the `stuff` Chain to do question answering." ] }, { "cell_type": "code", "execution_count": 6, "id": "180fd4c1", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"stuff\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "77fdf1aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'output_text': ' The president said that he was honoring Justice Breyer for his service to the country and that he was a Constitutional scholar, Army veteran, and retiring Justice of the United States Supreme Court.'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"What did the president say about Justice Breyer\"\n", "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "markdown", "id": "91522e29", "metadata": {}, "source": [ "## The `map_reduce` Chain\n", "\n", "This sections shows results of using the `map_reduce` Chain to do question answering." ] }, { "cell_type": "code", "execution_count": 8, "id": "b0060f51", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"map_reduce\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "fbdb9137", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'output_text': ' The president said, \"Justice Breyer, thank you for your service.\"'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"What did the president say about Justice Breyer\"\n", "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "markdown", "id": "31478d32", "metadata": {}, "source": [ "**Intermediate Steps**\n", "\n", "We can also return the intermediate steps for `map_reduce` chains, should we want to inspect them. This is done with the `return_map_steps` variable." ] }, { "cell_type": "code", "execution_count": 10, "id": "452c8680", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"map_reduce\", return_map_steps=True)" ] }, { "cell_type": "code", "execution_count": 11, "id": "90b47a75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'intermediate_steps': [' \"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.\"',\n", " ' None',\n", " ' None',\n", " ' None'],\n", " 'output_text': ' The president said, \"Justice Breyer, thank you for your service.\"'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "markdown", "id": "6ea50ad0", "metadata": {}, "source": [ "## The `refine` Chain\n", "\n", "This sections shows results of using the `refine` Chain to do question answering." ] }, { "cell_type": "code", "execution_count": 12, "id": "fb167057", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"refine\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "d8b5286e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'output_text': '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act. He also mentioned his plan to lower costs to give families a fair shot, lower the deficit, and go after criminals who stole pandemic relief funds. He also announced that the Justice Department will name a chief prosecutor for pandemic fraud.'}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"What did the president say about Justice Breyer\"\n", "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "markdown", "id": "f95dfb2e", "metadata": {}, "source": [ "**Intermediate Steps**\n", "\n", "We can also return the intermediate steps for `refine` chains, should we want to inspect them. This is done with the `return_refine_steps` variable." ] }, { "cell_type": "code", "execution_count": 14, "id": "a5c64200", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"refine\", return_refine_steps=True)" ] }, { "cell_type": "code", "execution_count": 15, "id": "817546ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'intermediate_steps': ['\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country and his legacy of excellence.',\n", " '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice.',\n", " '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act.',\n", " '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act. He also mentioned his plan to lower costs to give families a fair shot, lower the deficit, and go after criminals who stole pandemic relief funds. He also announced that the Justice Department will name a chief prosecutor for pandemic fraud.'],\n", " 'output_text': '\\n\\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his commitment to protecting the rights of LGBTQ+ Americans and his support for the bipartisan Equality Act. He also mentioned his plan to lower costs to give families a fair shot, lower the deficit, and go after criminals who stole pandemic relief funds. He also announced that the Justice Department will name a chief prosecutor for pandemic fraud.'}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "markdown", "id": "521a77cb", "metadata": {}, "source": [ "## The `map-rerank` Chain\n", "\n", "This sections shows results of using the `map-rerank` Chain to do question answering with sources." ] }, { "cell_type": "code", "execution_count": 16, "id": "e2bfe203", "metadata": {}, "outputs": [], "source": [ "chain = load_qa_chain(OpenAI(temperature=0), chain_type=\"map_rerank\", return_intermediate_steps=True)" ] }, { "cell_type": "code", "execution_count": 17, "id": "5c28880c", "metadata": {}, "outputs": [], "source": [ "query = \"What did the president say about Justice Breyer\"\n", "results = chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" ] }, { "cell_type": "code", "execution_count": 18, "id": "80ac2db3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' The president thanked Justice Breyer for his service and honored him for dedicating his life to serving the country. '" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results[\"output_text\"]" ] }, { "cell_type": "code", "execution_count": 19, "id": "b428fcb9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'answer': ' The president thanked Justice Breyer for his service and honored him for dedicating his life to serving the country. ',\n", " 'score': '100'},\n", " {'answer': \" The president said that Justice Breyer is a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that since she's been nominated, she's received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans, and that she is a consensus builder.\",\n", " 'score': '100'},\n", " {'answer': ' The president did not mention Justice Breyer in this context.',\n", " 'score': '0'},\n", " {'answer': ' The president did not mention Justice Breyer in the given context. ',\n", " 'score': '0'}]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results[\"intermediate_steps\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "c4f86521", "metadata": {}, "outputs": [], "source": [] } ], "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.10.9" }, "vscode": { "interpreter": { "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103" } } }, "nbformat": 4, "nbformat_minor": 5 }