mirror of https://github.com/hwchase17/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
305 lines
9.2 KiB
Plaintext
305 lines
9.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "13afcae7",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Weaviate\n",
|
|
"\n",
|
|
">[Weaviate](https://weaviate.io/) is an open-source vector database. It allows you to store data objects and vector embeddings from\n",
|
|
">your favorite ML models, and scale seamlessly into billions of data objects.\n",
|
|
"\n",
|
|
"In the notebook, we'll demo the `SelfQueryRetriever` wrapped around a `Weaviate` vector store. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "68e75fb9",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Creating a Weaviate vector store\n",
|
|
"First we'll want to create a Weaviate vector store and seed it with some data. We've created a small demo set of documents that contain summaries of movies.\n",
|
|
"\n",
|
|
"**Note:** The self-query retriever requires you to have `lark` installed (`pip install lark`). We also need the `weaviate-client` package."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "63a8af5b",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install --upgrade --quiet lark weaviate-client"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "cb4a5787",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_community.vectorstores import Weaviate\n",
|
|
"from langchain_core.documents import Document\n",
|
|
"from langchain_openai import OpenAIEmbeddings\n",
|
|
"\n",
|
|
"embeddings = OpenAIEmbeddings()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"id": "bcbe04d9",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"docs = [\n",
|
|
" Document(\n",
|
|
" page_content=\"A bunch of scientists bring back dinosaurs and mayhem breaks loose\",\n",
|
|
" metadata={\"year\": 1993, \"rating\": 7.7, \"genre\": \"science fiction\"},\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" page_content=\"Leo DiCaprio gets lost in a dream within a dream within a dream within a ...\",\n",
|
|
" metadata={\"year\": 2010, \"director\": \"Christopher Nolan\", \"rating\": 8.2},\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" page_content=\"A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea\",\n",
|
|
" metadata={\"year\": 2006, \"director\": \"Satoshi Kon\", \"rating\": 8.6},\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" page_content=\"A bunch of normal-sized women are supremely wholesome and some men pine after them\",\n",
|
|
" metadata={\"year\": 2019, \"director\": \"Greta Gerwig\", \"rating\": 8.3},\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" page_content=\"Toys come alive and have a blast doing so\",\n",
|
|
" metadata={\"year\": 1995, \"genre\": \"animated\"},\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" page_content=\"Three men walk into the Zone, three men walk out of the Zone\",\n",
|
|
" metadata={\n",
|
|
" \"year\": 1979,\n",
|
|
" \"director\": \"Andrei Tarkovsky\",\n",
|
|
" \"genre\": \"science fiction\",\n",
|
|
" \"rating\": 9.9,\n",
|
|
" },\n",
|
|
" ),\n",
|
|
"]\n",
|
|
"vectorstore = Weaviate.from_documents(\n",
|
|
" docs, embeddings, weaviate_url=\"http://127.0.0.1:8080\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5ecaab6d",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Creating our self-querying retriever\n",
|
|
"Now we can instantiate our retriever. To do this we'll need to provide some information upfront about the metadata fields that our documents support and a short description of the document contents."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"id": "86e34dbf",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.chains.query_constructor.base import AttributeInfo\n",
|
|
"from langchain.retrievers.self_query.base import SelfQueryRetriever\n",
|
|
"from langchain_openai import OpenAI\n",
|
|
"\n",
|
|
"metadata_field_info = [\n",
|
|
" AttributeInfo(\n",
|
|
" name=\"genre\",\n",
|
|
" description=\"The genre of the movie\",\n",
|
|
" type=\"string or list[string]\",\n",
|
|
" ),\n",
|
|
" AttributeInfo(\n",
|
|
" name=\"year\",\n",
|
|
" description=\"The year the movie was released\",\n",
|
|
" type=\"integer\",\n",
|
|
" ),\n",
|
|
" AttributeInfo(\n",
|
|
" name=\"director\",\n",
|
|
" description=\"The name of the movie director\",\n",
|
|
" type=\"string\",\n",
|
|
" ),\n",
|
|
" AttributeInfo(\n",
|
|
" name=\"rating\", description=\"A 1-10 rating for the movie\", type=\"float\"\n",
|
|
" ),\n",
|
|
"]\n",
|
|
"document_content_description = \"Brief summary of a movie\"\n",
|
|
"llm = OpenAI(temperature=0)\n",
|
|
"retriever = SelfQueryRetriever.from_llm(\n",
|
|
" llm, vectorstore, document_content_description, metadata_field_info, verbose=True\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ea9df8d4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Testing it out\n",
|
|
"And now we can try actually using our retriever!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"id": "38a126e9",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"query='dinosaur' filter=None limit=None\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose', metadata={'genre': 'science fiction', 'rating': 7.7, 'year': 1993}),\n",
|
|
" Document(page_content='Toys come alive and have a blast doing so', metadata={'genre': 'animated', 'rating': None, 'year': 1995}),\n",
|
|
" Document(page_content='Three men walk into the Zone, three men walk out of the Zone', metadata={'genre': 'science fiction', 'rating': 9.9, 'year': 1979}),\n",
|
|
" Document(page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea', metadata={'genre': None, 'rating': 8.6, 'year': 2006})]"
|
|
]
|
|
},
|
|
"execution_count": 24,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# This example only specifies a relevant query\n",
|
|
"retriever.invoke(\"What are some movies about dinosaurs\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 26,
|
|
"id": "b19d4da0",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"query='women' filter=Comparison(comparator=<Comparator.EQ: 'eq'>, attribute='director', value='Greta Gerwig') limit=None\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='A bunch of normal-sized women are supremely wholesome and some men pine after them', metadata={'genre': None, 'rating': 8.3, 'year': 2019})]"
|
|
]
|
|
},
|
|
"execution_count": 26,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# This example specifies a query and a filter\n",
|
|
"retriever.invoke(\"Has Greta Gerwig directed any movies about women\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "39bd1de1-b9fe-4a98-89da-58d8a7a6ae51",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Filter k\n",
|
|
"\n",
|
|
"We can also use the self query retriever to specify `k`: the number of documents to fetch.\n",
|
|
"\n",
|
|
"We can do this by passing `enable_limit=True` to the constructor."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"id": "bff36b88-b506-4877-9c63-e5a1a8d78e64",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"retriever = SelfQueryRetriever.from_llm(\n",
|
|
" llm,\n",
|
|
" vectorstore,\n",
|
|
" document_content_description,\n",
|
|
" metadata_field_info,\n",
|
|
" enable_limit=True,\n",
|
|
" verbose=True,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"id": "2758d229-4f97-499c-819f-888acaf8ee10",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"query='dinosaur' filter=None limit=2\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose', metadata={'genre': 'science fiction', 'rating': 7.7, 'year': 1993}),\n",
|
|
" Document(page_content='Toys come alive and have a blast doing so', metadata={'genre': 'animated', 'rating': None, 'year': 1995})]"
|
|
]
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# This example only specifies a relevant query\n",
|
|
"retriever.invoke(\"what are two movies about dinosaurs\")"
|
|
]
|
|
}
|
|
],
|
|
"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.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|