diff --git a/docs/docs/integrations/document_transformers/google_cloud_vertexai_rerank.ipynb b/docs/docs/integrations/document_transformers/google_cloud_vertexai_rerank.ipynb new file mode 100644 index 00000000000..ef3c811b737 --- /dev/null +++ b/docs/docs/integrations/document_transformers/google_cloud_vertexai_rerank.ipynb @@ -0,0 +1,793 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "mZaeRH_SjJWK" + }, + "source": [ + "# Google Cloud Vertex AI Reranker\n", + "\n", + "> The [Vertex Search Ranking API](https://cloud.google.com/generative-ai-app-builder/docs/ranking) is one of the standalone APIs in [Vertex AI Agent Builder](https://cloud.google.com/generative-ai-app-builder/docs/builder-apis). It takes a list of documents and reranks those documents based on how relevant the documents are to a query. Compared to embeddings, which look only at the semantic similarity of a document and a query, the ranking API can give you precise scores for how well a document answers a given query. The ranking API can be used to improve the quality of search results after retrieving an initial set of candidate documents.\n", + "\n", + ">The ranking API is stateless so there's no need to index documents before calling the API. All you need to do is pass in the query and documents. This makes the API well suited for reranking documents from any document retrievers.\n", + "\n", + ">For more information, see [Rank and rerank documents](https://cloud.google.com/generative-ai-app-builder/docs/ranking)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "w51yJNBAirPZ" + }, + "outputs": [], + "source": [ + "%pip install --upgrade --quiet langchain langchain-community langchain-google-community langchain-google-community[vertexaisearch] langchain-google-vertexai langchain-chroma langchain-text-splitters" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5sN2qvW0Wxwj" + }, + "source": [ + "### Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "axookyKSnl3G" + }, + "outputs": [], + "source": [ + "PROJECT_ID = \"\"\n", + "REGION = \"\"\n", + "RANKING_LOCATION_ID = \"global\" # @param {type:\"string\"}\n", + "\n", + "# Initialize GCP project for Vertex AI\n", + "from google.cloud import aiplatform\n", + "\n", + "aiplatform.init(project=PROJECT_ID, location=REGION)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7xie5peQW2Lf" + }, + "source": [ + "### Load and Prepare data\n", + "\n", + "For this example, we will be using the [Google Wiki page](https://en.wikipedia.org/wiki/Google)to demonstrate how the Vertex Ranking API works.\n", + "\n", + "We use a standard pipeline of `load -> split -> embed data`.\n", + "\n", + "The embeddings are created using the [Vertex Embeddings API](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings#supported_models) model - `textembedding-gecko@003`" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3yY5reMbkbFS", + "outputId": "e124299b-0fa2-4acd-aaec-d5361f008d97" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your 1 documents have been split into 266 chunks\n" + ] + } + ], + "source": [ + "from langchain_chroma import Chroma\n", + "from langchain_community.document_loaders import WebBaseLoader\n", + "from langchain_google_vertexai import VertexAIEmbeddings\n", + "from langchain_text_splitters import RecursiveCharacterTextSplitter\n", + "\n", + "vectordb = None\n", + "\n", + "# Load wiki page\n", + "loader = WebBaseLoader(\"https://en.wikipedia.org/wiki/Google\")\n", + "data = loader.load()\n", + "\n", + "# Split doc into chunks\n", + "text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=5)\n", + "splits = text_splitter.split_documents(data)\n", + "\n", + "print(f\"Your {len(data)} documents have been split into {len(splits)} chunks\")\n", + "\n", + "if vectordb is not None: # delete existing vectordb if it already exists\n", + " vectordb.delete_collection()\n", + "\n", + "embedding = VertexAIEmbeddings(model_name=\"textembedding-gecko@003\")\n", + "vectordb = Chroma.from_documents(documents=splits, embedding=embedding)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "jNmGwvrqnFF1" + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "from langchain.retrievers.contextual_compression import ContextualCompressionRetriever\n", + "from langchain_google_community.vertex_rank import VertexAIRank\n", + "\n", + "# Instantiate the VertexAIReranker with the SDK manager\n", + "reranker = VertexAIRank(\n", + " project_id=PROJECT_ID,\n", + " location_id=RANKING_LOCATION_ID,\n", + " ranking_config=\"default_ranking_config\",\n", + " title_field=\"source\",\n", + " top_n=5,\n", + ")\n", + "\n", + "basic_retriever = vectordb.as_retriever(search_kwargs={\"k\": 5}) # fetch top 5 documents\n", + "\n", + "# Create the ContextualCompressionRetriever with the VertexAIRanker as a Reranker\n", + "retriever_with_reranker = ContextualCompressionRetriever(\n", + " base_compressor=reranker, base_retriever=basic_retriever\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uMOPl7ji_nU_" + }, + "source": [ + "### Testing out the Vertex Ranking API\n", + "\n", + "Let's query both the `basic_retriever` and `retriever_with_reranker` with the same query and compare the retrieved documents.\n", + "\n", + "The Ranking API takes in the input from the `basic_retriever` and passes it to the Ranking API.\n", + "\n", + "The ranking API is used to improve the quality of the ranking and determine a score that indicates the relevance of each record to the query.\n", + "\n", + "You can see the difference between the Unranked and the Ranked Documents. The Ranking API moves the most semantically relevant documents to the top of the context window of the LLM thus helping it form a better answer with reasoning." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 484 + }, + "id": "sJDkepoYoc0t", + "outputId": "eac41585-3d53-4dd9-da16-51ec47eedfec" + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "summary": "{\n \"name\": \"comparison_df\",\n \"rows\": 5,\n \"fields\": [\n {\n \"column\": \"Unranked Documents\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38]\",\n \"^ Swant, Marty. \\\"The World's Valuable Brands\\\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \\\"Best Global Brands\\\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \\\"How we started and where we are today \\u2013 Google\\\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st\\u00a0ed.). New York: Rosen Publishing Group. p.\\u00a018. ISBN\\u00a0978-1-4488-6911-4. LCCN\\u00a02011039480.\\n\\n^ a b c \\\"Our history in depth\\\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017.\",\n \"The name \\\"Google\\\" originated from a misspelling of \\\"googol\\\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \\\"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\\\" Having found its way increasingly into everyday language, the verb \\\"google\\\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \\\"to use the Google search engine to obtain information on the Internet.\\\"[213][214] Google's mission statement, from the outset, was \\\"to organize the world's information and make it universally accessible and useful\\\",[215] and its unofficial\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ranked Documents\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38]\",\n \"^ Swant, Marty. \\\"The World's Valuable Brands\\\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \\\"Best Global Brands\\\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \\\"How we started and where we are today \\u2013 Google\\\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st\\u00a0ed.). New York: Rosen Publishing Group. p.\\u00a018. ISBN\\u00a0978-1-4488-6911-4. LCCN\\u00a02011039480.\\n\\n^ a b c \\\"Our history in depth\\\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017.\",\n \"^ Meijer, Bart (January 3, 2019). \\\"Google shifted $23 billion to tax haven Bermuda in 2017: filing\\\". Reuters. Archived from the original on January 3, 2019. Retrieved January 3, 2019. Google moved 19.9 billion euros ($22.7 billion) through a Dutch shell company to Bermuda in 2017, as part of an arrangement that allows it to reduce its foreign tax bill\\n\\n^ Hamburger, Tom; Gold, Matea (April 13, 2014). \\\"Google, once disdainful of lobbying, now a master of Washington influence\\\". The Washington Post. Archived from the original on October 27, 2017. Retrieved August 22, 2017.\\n\\n^ Koller, David (January 2004). \\\"Origin of the name, \\\"Google.\\\"\\\". Stanford University. Archived from the original on June 27, 2012. Retrieved May 28, 2006.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", + "type": "dataframe", + "variable_name": "comparison_df" + }, + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unranked DocumentsRanked Documents
0^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (1–7): 107–117. CiteSeerX 10.1.1.115.5930. doi:10.1016/S0169-7552(98)00110-X. ISSN 0169-7552. S2CID 7587743. Archived (PDF) from the original on September 27, 2015. Retrieved April 7, 2019.\\n\\n^ \"About: RankDex\". Archived from the original on January 20, 2012. Retrieved September 29, 2010., RankDex\\n\\n^ \"Method for node ranking in a linked database\". Google Patents. Archived from the original on October 15, 2015. Retrieved October 19, 2015.\\n\\n^ Koller, David (January 2004). \"Origin of the name \"Google\"\". Stanford University. Archived from the original on June 27, 2012.The name \"Google\" originated from a misspelling of \"googol\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\" Having found its way increasingly into everyday language, the verb \"google\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \"to use the Google search engine to obtain information on the Internet.\"[213][214] Google's mission statement, from the outset, was \"to organize the world's information and make it universally accessible and useful\",[215] and its unofficial
1Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38]Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38]
2The name \"Google\" originated from a misspelling of \"googol\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\" Having found its way increasingly into everyday language, the verb \"google\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \"to use the Google search engine to obtain information on the Internet.\"[213][214] Google's mission statement, from the outset, was \"to organize the world's information and make it universally accessible and useful\",[215] and its unofficial^ Meijer, Bart (January 3, 2019). \"Google shifted $23 billion to tax haven Bermuda in 2017: filing\". Reuters. Archived from the original on January 3, 2019. Retrieved January 3, 2019. Google moved 19.9 billion euros ($22.7 billion) through a Dutch shell company to Bermuda in 2017, as part of an arrangement that allows it to reduce its foreign tax bill\\n\\n^ Hamburger, Tom; Gold, Matea (April 13, 2014). \"Google, once disdainful of lobbying, now a master of Washington influence\". The Washington Post. Archived from the original on October 27, 2017. Retrieved August 22, 2017.\\n\\n^ Koller, David (January 2004). \"Origin of the name, \"Google.\"\". Stanford University. Archived from the original on June 27, 2012. Retrieved May 28, 2006.
3^ Meijer, Bart (January 3, 2019). \"Google shifted $23 billion to tax haven Bermuda in 2017: filing\". Reuters. Archived from the original on January 3, 2019. Retrieved January 3, 2019. Google moved 19.9 billion euros ($22.7 billion) through a Dutch shell company to Bermuda in 2017, as part of an arrangement that allows it to reduce its foreign tax bill\\n\\n^ Hamburger, Tom; Gold, Matea (April 13, 2014). \"Google, once disdainful of lobbying, now a master of Washington influence\". The Washington Post. Archived from the original on October 27, 2017. Retrieved August 22, 2017.\\n\\n^ Koller, David (January 2004). \"Origin of the name, \"Google.\"\". Stanford University. Archived from the original on June 27, 2012. Retrieved May 28, 2006.^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (1–7): 107–117. CiteSeerX 10.1.1.115.5930. doi:10.1016/S0169-7552(98)00110-X. ISSN 0169-7552. S2CID 7587743. Archived (PDF) from the original on September 27, 2015. Retrieved April 7, 2019.\\n\\n^ \"About: RankDex\". Archived from the original on January 20, 2012. Retrieved September 29, 2010., RankDex\\n\\n^ \"Method for node ranking in a linked database\". Google Patents. Archived from the original on October 15, 2015. Retrieved October 19, 2015.\\n\\n^ Koller, David (January 2004). \"Origin of the name \"Google\"\". Stanford University. Archived from the original on June 27, 2012.
4^ Swant, Marty. \"The World's Valuable Brands\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \"Best Global Brands\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \"How we started and where we are today – Google\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st ed.). New York: Rosen Publishing Group. p. 18. ISBN 978-1-4488-6911-4. LCCN 2011039480.\\n\\n^ a b c \"Our history in depth\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017.^ Swant, Marty. \"The World's Valuable Brands\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \"Best Global Brands\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \"How we started and where we are today – Google\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st ed.). New York: Rosen Publishing Group. p. 18. ISBN 978-1-4488-6911-4. LCCN 2011039480.\\n\\n^ a b c \"Our history in depth\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017.
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "text/plain": [ + " Unranked Documents \\\n", + "0 ^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (1–7): 107–117. CiteSeerX 10.1.1.115.5930. doi:10.1016/S0169-7552(98)00110-X. ISSN 0169-7552. S2CID 7587743. Archived (PDF) from the original on September 27, 2015. Retrieved April 7, 2019.\\n\\n^ \"About: RankDex\". Archived from the original on January 20, 2012. Retrieved September 29, 2010., RankDex\\n\\n^ \"Method for node ranking in a linked database\". Google Patents. Archived from the original on October 15, 2015. Retrieved October 19, 2015.\\n\\n^ Koller, David (January 2004). \"Origin of the name \"Google\"\". Stanford University. Archived from the original on June 27, 2012. \n", + "1 Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38] \n", + "2 The name \"Google\" originated from a misspelling of \"googol\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\" Having found its way increasingly into everyday language, the verb \"google\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \"to use the Google search engine to obtain information on the Internet.\"[213][214] Google's mission statement, from the outset, was \"to organize the world's information and make it universally accessible and useful\",[215] and its unofficial \n", + "3 ^ Meijer, Bart (January 3, 2019). \"Google shifted $23 billion to tax haven Bermuda in 2017: filing\". Reuters. Archived from the original on January 3, 2019. Retrieved January 3, 2019. Google moved 19.9 billion euros ($22.7 billion) through a Dutch shell company to Bermuda in 2017, as part of an arrangement that allows it to reduce its foreign tax bill\\n\\n^ Hamburger, Tom; Gold, Matea (April 13, 2014). \"Google, once disdainful of lobbying, now a master of Washington influence\". The Washington Post. Archived from the original on October 27, 2017. Retrieved August 22, 2017.\\n\\n^ Koller, David (January 2004). \"Origin of the name, \"Google.\"\". Stanford University. Archived from the original on June 27, 2012. Retrieved May 28, 2006. \n", + "4 ^ Swant, Marty. \"The World's Valuable Brands\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \"Best Global Brands\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \"How we started and where we are today – Google\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st ed.). New York: Rosen Publishing Group. p. 18. ISBN 978-1-4488-6911-4. LCCN 2011039480.\\n\\n^ a b c \"Our history in depth\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017. \n", + "\n", + " Ranked Documents \n", + "0 The name \"Google\" originated from a misspelling of \"googol\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\" Having found its way increasingly into everyday language, the verb \"google\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \"to use the Google search engine to obtain information on the Internet.\"[213][214] Google's mission statement, from the outset, was \"to organize the world's information and make it universally accessible and useful\",[215] and its unofficial \n", + "1 Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38] \n", + "2 ^ Meijer, Bart (January 3, 2019). \"Google shifted $23 billion to tax haven Bermuda in 2017: filing\". Reuters. Archived from the original on January 3, 2019. Retrieved January 3, 2019. Google moved 19.9 billion euros ($22.7 billion) through a Dutch shell company to Bermuda in 2017, as part of an arrangement that allows it to reduce its foreign tax bill\\n\\n^ Hamburger, Tom; Gold, Matea (April 13, 2014). \"Google, once disdainful of lobbying, now a master of Washington influence\". The Washington Post. Archived from the original on October 27, 2017. Retrieved August 22, 2017.\\n\\n^ Koller, David (January 2004). \"Origin of the name, \"Google.\"\". Stanford University. Archived from the original on June 27, 2012. Retrieved May 28, 2006. \n", + "3 ^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (1–7): 107–117. CiteSeerX 10.1.1.115.5930. doi:10.1016/S0169-7552(98)00110-X. ISSN 0169-7552. S2CID 7587743. Archived (PDF) from the original on September 27, 2015. Retrieved April 7, 2019.\\n\\n^ \"About: RankDex\". Archived from the original on January 20, 2012. Retrieved September 29, 2010., RankDex\\n\\n^ \"Method for node ranking in a linked database\". Google Patents. Archived from the original on October 15, 2015. Retrieved October 19, 2015.\\n\\n^ Koller, David (January 2004). \"Origin of the name \"Google\"\". Stanford University. Archived from the original on June 27, 2012. \n", + "4 ^ Swant, Marty. \"The World's Valuable Brands\". Forbes. Archived from the original on October 18, 2020. Retrieved January 19, 2022.\\n\\n^ \"Best Global Brands\". Interbrand. Archived from the original on February 1, 2022. Retrieved March 7, 2011.\\n\\n^ a b c d \"How we started and where we are today – Google\". about.google. Archived from the original on April 22, 2020. Retrieved April 24, 2021.\\n\\n^ Brezina, Corona (2013). Sergey Brin, Larry Page, Eric Schmidt, and Google (1st ed.). New York: Rosen Publishing Group. p. 18. ISBN 978-1-4488-6911-4. LCCN 2011039480.\\n\\n^ a b c \"Our history in depth\". Google Company. Archived from the original on April 1, 2012. Retrieved July 15, 2017. " + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "\n", + "# Use the basic_retriever and the retriever_with_reranker to get relevant documents\n", + "query = \"how did the name google originate?\"\n", + "retrieved_docs = basic_retriever.invoke(query)\n", + "reranked_docs = retriever_with_reranker.invoke(query)\n", + "\n", + "# Create two lists of results for unranked and ranked docs\n", + "unranked_docs_content = [docs.page_content for docs in retrieved_docs]\n", + "ranked_docs_content = [docs.page_content for docs in reranked_docs]\n", + "\n", + "# Create a comparison DataFrame using the padded lists\n", + "comparison_df = pd.DataFrame(\n", + " {\n", + " \"Unranked Documents\": unranked_docs_content,\n", + " \"Ranked Documents\": ranked_docs_content,\n", + " }\n", + ")\n", + "\n", + "comparison_df" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ud_cnGszb1i9" + }, + "source": [ + "Let's inspect a couple of reranked documents. We observe that the retriever still returns the relevant Langchain type [documents](https://api.python.langchain.com/en/latest/documents/langchain_core.documents.base.Document.html) but as part of the metadata field, we also recieve the `relevance_score` from the Ranking API." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 225 + }, + "id": "FCDvNjPuAYVv", + "outputId": "23454993-0251-457b-8733-bd413e1b1043" + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Document 0\n", + "page_content='The name \"Google\" originated from a misspelling of \"googol\",[211][212] which refers to the number represented by a 1 followed by one-hundred zeros. Page and Brin write in their original paper on PageRank:[33] \"We chose our system name, Google, because it is a common spelling of googol, or 10100[,] and fits well with our goal of building very large-scale search engines.\" Having found its way increasingly into everyday language, the verb \"google\" was added to the Merriam Webster Collegiate Dictionary and the Oxford English Dictionary in 2006, meaning \"to use the Google search engine to obtain information on the Internet.\"[213][214] Google\\'s mission statement, from the outset, was \"to organize the world\\'s information and make it universally accessible and useful\",[215] and its unofficial' metadata={'id': '2', 'relevance_score': 0.9800000190734863, 'source': 'https://en.wikipedia.org/wiki/Google'}\n", + "----------------------------------------------------------\n", + "\n", + "Document 1\n", + "page_content='Eventually, they changed the name to Google; the name of the search engine was a misspelling of the word googol,[21][36][37] a very large number written 10100 (1 followed by 100 zeros), picked to signify that the search engine was intended to provide large quantities of information.[38]' metadata={'id': '1', 'relevance_score': 0.75, 'source': 'https://en.wikipedia.org/wiki/Google'}\n", + "----------------------------------------------------------\n", + "\n" + ] + } + ], + "source": [ + "for i in range(2):\n", + " print(f\"Document {i}\")\n", + " print(reranked_docs[i])\n", + " print(\"----------------------------------------------------------\\n\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hELRT4bMeqcs" + }, + "source": [ + "### Putting it all together\n", + "\n", + "This shows an example of a complete RAG chain with a simple prompt template on how you can perform reranking using the Vertex Ranking API.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 17 + }, + "id": "u1cfbdZyTgeq", + "outputId": "3395ca20-5327-4143-e769-ddefb7e1bed0" + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from langchain.chains import LLMChain\n", + "from langchain.docstore.document import Document\n", + "from langchain.prompts import PromptTemplate\n", + "from langchain_core.prompts import PromptTemplate\n", + "from langchain_core.runnables import RunnableParallel, RunnablePassthrough\n", + "from langchain_google_vertexai import VertexAI\n", + "\n", + "llm = VertexAI(model_name=\"gemini-1.0-pro-002\")\n", + "\n", + "# Instantiate the VertexAIReranker with the SDK manager\n", + "reranker = VertexAIRank(\n", + " project_id=PROJECT_ID,\n", + " location_id=RANKING_LOCATION_ID,\n", + " ranking_config=\"default_ranking_config\",\n", + " title_field=\"source\", # metadata field key from your existing documents\n", + " top_n=5,\n", + ")\n", + "\n", + "# value of k can be set to a higher value as well for tweaking performance\n", + "# eg: # of docs: basic_retriever(100) -> reranker(5)\n", + "basic_retriever = vectordb.as_retriever(search_kwargs={\"k\": 5}) # fetch top 5 documents\n", + "\n", + "# Create the ContextualCompressionRetriever with the VertexAIRanker as a Reranker\n", + "retriever_with_reranker = ContextualCompressionRetriever(\n", + " base_compressor=reranker, base_retriever=basic_retriever\n", + ")\n", + "\n", + "template = \"\"\"\n", + "\n", + "{context}\n", + "\n", + "\n", + "Question:\n", + "{query}\n", + "\n", + "Don't give information outside the context or repeat your findings.\n", + "Answer:\n", + "\"\"\"\n", + "prompt = PromptTemplate.from_template(template)\n", + "\n", + "reranker_setup_and_retrieval = RunnableParallel(\n", + " {\"context\": retriever_with_reranker, \"query\": RunnablePassthrough()}\n", + ")\n", + "\n", + "chain = reranker_setup_and_retrieval | prompt | llm" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 17 + }, + "id": "dv68uTmvT7SJ", + "outputId": "254ebc12-fbb3-4321-9864-604383f071fe" + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "query = \"how did the name google originate?\"" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "id": "taZAoM_bU2_f", + "outputId": "3a0e1c44-8760-479c-d4a9-030929cb442b" + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'The name \"Google\" originated as a misspelling of the word \"googol,\" a mathematical term for the number 1 followed by 100 zeros. Larry Page and Sergey Brin, the founders of Google, chose the name because it reflected their goal of building a search engine that could handle massive amounts of information. \\n'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.invoke(query)" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}