docs: Docs (sample notebook) for Vertex DIY RAG Ranking API (#21054)

Vertex DIY RAG APIs helps to build complex RAG systems and provide more
granular control, and are suited for custom use cases.

The Ranking API takes in a list of documents and reranks those documents
based on how relevant the documents are to a given query. Compared to
embeddings that look purely at the semantic similarity of a document and
a query, the ranking API can give you a more precise score for how well
a document answers a given query.


[Reference](https://cloud.google.com/generative-ai-app-builder/docs/ranking)

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Abhishek Bhagwat 2024-05-01 13:39:39 +08:00 committed by GitHub
parent 8a01760a0f
commit 86fe484e24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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",
" <div id=\"df-43c4f5f2-c31d-4664-85dd-60cad39bd5fa\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Unranked Documents</th>\n",
" <th>Ranked Documents</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (17): 107117. 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.</td>\n",
" <td>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</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>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]</td>\n",
" <td>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]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>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</td>\n",
" <td>^ 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.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>^ 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.</td>\n",
" <td>^ a b Brin, Sergey; Page, Lawrence (1998). \"The anatomy of a large-scale hypertextual Web search engine\" (PDF). Computer Networks and ISDN Systems. 30 (17): 107117. 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.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>^ 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.</td>\n",
" <td>^ 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.</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-43c4f5f2-c31d-4664-85dd-60cad39bd5fa')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-43c4f5f2-c31d-4664-85dd-60cad39bd5fa button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-43c4f5f2-c31d-4664-85dd-60cad39bd5fa');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-fff80078-f146-44f5-9eff-d91c9305c276\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-fff80078-f146-44f5-9eff-d91c9305c276')\"\n",
" title=\"Suggest charts\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-fff80078-f146-44f5-9eff-d91c9305c276 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
"\n",
" <div id=\"id_7648ee4a-f747-429c-820f-e03d3c59f765\">\n",
" <style>\n",
" .colab-df-generate {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-generate:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
" <button class=\"colab-df-generate\" onclick=\"generateWithVariable('comparison_df')\"\n",
" title=\"Generate code using this dataframe.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n",
" </svg>\n",
" </button>\n",
" <script>\n",
" (() => {\n",
" const buttonEl =\n",
" document.querySelector('#id_7648ee4a-f747-429c-820f-e03d3c59f765 button.colab-df-generate');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" buttonEl.onclick = () => {\n",
" google.colab.notebook.generateWithVariable('comparison_df');\n",
" }\n",
" })();\n",
" </script>\n",
" </div>\n",
"\n",
" </div>\n",
" </div>\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 (17): 107117. 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 (17): 107117. 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",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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",
"<context>\n",
"{context}\n",
"</context>\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",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}