"This page covers how to use [RAGatouille](https://github.com/bclavie/RAGatouille) as a retriever in a LangChain chain. RAGatouille makes it as simple as can be to use ColBERT! [ColBERT](https://github.com/stanford-futuredata/ColBERT) is a fast and accurate retrieval model, enabling scalable BERT-based search over large text collections in tens of milliseconds. Since the Ragatouille wrapper is so ergonomic we will only write a simple wrapper class so it conforms to the LangChain api."
"This page covers how to use [RAGatouille](https://github.com/bclavie/RAGatouille) as a retriever in a LangChain chain. RAGatouille makes it as simple as can be to use ColBERT! [ColBERT](https://github.com/stanford-futuredata/ColBERT) is a fast and accurate retrieval model, enabling scalable BERT-based search over large text collections in tens of milliseconds.\n",
"\n",
"We can use this as a [retriever](/docs/modules/data_connection/retrievers). It will show functionality specific to this integration. After going through, it may be useful to explore [relevant use-case pages](/docs/use_cases/question_answering) to learn how to use this vectorstore as part of a larger chain.\n",
"\n",
"## Setup\n",
"\n",
"The integration lives in the `ragatouille` package.\n",
" 50%|█████ | 1/2 [00:03<00:03, 3.00s/it]/Users/harrisonchase/.pyenv/versions/3.10.1/envs/langchain/lib/python3.10/site-packages/torch/amp/autocast_mode.py:250: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling\n",
" 50%|█████ | 1/2 [00:02<00:02, 2.85s/it]/Users/harrisonchase/.pyenv/versions/3.10.1/envs/langchain/lib/python3.10/site-packages/torch/amp/autocast_mode.py:250: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling\n",
" warnings.warn(\n",
"100%|██████████| 2/2 [00:03<00:00, 1.82s/it]\n",
"WARNING clustering 10000 points to 1024 centroids: please provide at least 39936 training points\n"
"100%|██████████| 2/2 [00:03<00:00, 1.74s/it]\n",
"WARNING clustering 10001 points to 1024 centroids: please provide at least 39936 training points\n"
"[{'content': 'In April 1984, Miyazaki opened his own office in Suginami Ward, naming it Nibariki.\\n\\n\\n=== Studio Ghibli ===\\n\\n\\n==== Early films (1985–1996) ====\\nIn June 1985, Miyazaki, Takahata, Tokuma and Suzuki founded the animation production company Studio Ghibli, with funding from Tokuma Shoten. Studio Ghibli\\'s first film, Laputa: Castle in the Sky (1986), employed the same production crew of Nausicaä. Miyazaki\\'s designs for the film\\'s setting were inspired by Greek architecture and \"European urbanistic templates\".',\n",
" 'score': 25.906391143798828,\n",
" 'score': 25.90749740600586,\n",
" 'rank': 1},\n",
" {'content': 'Hayao Miyazaki (宮崎 駿 or 宮﨑 駿, Miyazaki Hayao, [mijaꜜzaki hajao]; born January 5, 1941) is a Japanese animator, filmmaker, and manga artist. A co-founder of Studio Ghibli, he has attained international acclaim as a masterful storyteller and creator of Japanese animated feature films, and is widely regarded as one of the most accomplished filmmakers in the history of animation.\\nBorn in Tokyo City in the Empire of Japan, Miyazaki expressed interest in manga and animation from an early age, and he joined Toei Animation in 1963. During his early years at Toei Animation he worked as an in-between artist and later collaborated with director Isao Takahata.',\n",
" 'score': 25.472686767578125,\n",
" 'score': 25.4748477935791,\n",
" 'rank': 2},\n",
" {'content': 'Glen Keane said Miyazaki is a \"huge influence\" on Walt Disney Animation Studios and has been \"part of our heritage\" ever since The Rescuers Down Under (1990). The Disney Renaissance era was also prompted by competition with the development of Miyazaki\\'s films. Artists from Pixar and Aardman Studios signed a tribute stating, \"You\\'re our inspiration, Miyazaki-san!\"',\n",
" 'score': 24.847288131713867,\n",
" 'score': 24.84897232055664,\n",
" 'rank': 3}]"
]
},
"execution_count": 10,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -330,53 +340,26 @@
},
{
"cell_type": "markdown",
"id": "2f5c7235",
"id": "a0fdf0f9",
"metadata": {},
"source": [
"## Use in LangChain\n",
"\n",
"In order to use as a retriever in LangChain, we need to make a really simple wrapper."
"We can then convert easily to a LangChain retriever! We can pass in any kwargs we want when creating (like `k`)"
" return [Document(page_content=doc[\"content\"]) for doc in results]"
"retriever = RAG.as_langchain_retriever(k=3)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "63da0b8e",
"metadata": {},
"outputs": [],
"source": [
"retriever = CustomRetriever(rag=RAG)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "65954ee4",
"execution_count": 10,
"id": "98be34c4",
"metadata": {},
"outputs": [
{
@ -395,18 +378,28 @@
" Document(page_content='Glen Keane said Miyazaki is a \"huge influence\" on Walt Disney Animation Studios and has been \"part of our heritage\" ever since The Rescuers Down Under (1990). The Disney Renaissance era was also prompted by competition with the development of Miyazaki\\'s films. Artists from Pixar and Aardman Studios signed a tribute stating, \"You\\'re our inspiration, Miyazaki-san!\"')]"
]
},
"execution_count": 17,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retriever.get_relevant_documents(\"What animation studio did Miyazaki found?\")"
"retriever.invoke(\"What animation studio did Miyazaki found?\")"
]
},
{
"cell_type": "markdown",
"id": "2f5c7235",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can easily combine this retriever in to a chain."