langchain/docs/modules/indexes/getting_started.ipynb

380 lines
10 KiB
Plaintext
Raw Normal View History

2023-02-20 07:14:50 +00:00
{
"cells": [
{
"cell_type": "markdown",
2023-02-27 08:31:36 +00:00
"id": "2244801b",
2023-02-20 07:14:50 +00:00
"metadata": {},
"source": [
"# Getting Started\n",
"\n",
2023-02-27 08:31:36 +00:00
"This example showcases question answering over documents.\n",
"We have chosen this as the example for getting started because it nicely combines a lot of different elements (Text splitters, embeddings, vectorstores) and then also shows how to use them in a chain.\n",
"\n",
"Question answering over documents consists of three steps:\n",
"\n",
"1. Create an index\n",
"2. Create a question answering chain\n",
"3. Ask questions!\n",
"\n",
"Each of the steps has multiple sub steps and potential configurations. In this notebook we will primarily focus on (1). We will start by showing the one-liner for doing so, but then break down what is actually going on.\n",
"\n",
"First, let's import some common classes we'll use no matter what."
2023-02-20 07:14:50 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
2023-02-27 08:31:36 +00:00
"id": "8d369452",
2023-02-20 07:14:50 +00:00
"metadata": {},
"outputs": [],
"source": [
2023-02-27 08:31:36 +00:00
"from langchain.chains import VectorDBQA\n",
"from langchain.llms import OpenAI"
2023-02-20 07:14:50 +00:00
]
},
{
"cell_type": "markdown",
2023-02-27 08:31:36 +00:00
"id": "07c1e3b9",
2023-02-20 07:14:50 +00:00
"metadata": {},
"source": [
2023-02-27 08:31:36 +00:00
"Next in the generic setup, let's specify the document loader we want to use."
2023-02-20 07:14:50 +00:00
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 2,
"id": "33958a86",
2023-02-20 07:14:50 +00:00
"metadata": {},
"outputs": [],
"source": [
"from langchain.document_loaders import TextLoader\n",
2023-02-27 08:31:36 +00:00
"loader = TextLoader('../state_of_the_union.txt')"
]
},
{
"cell_type": "markdown",
"id": "489c74bb",
"metadata": {},
"source": [
"## One Line Index Creation\n",
"\n",
"To get started as quickly as possible, we can use the `VectorstoreIndexCreator`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "403fc231",
"metadata": {},
"outputs": [],
"source": [
"from langchain.indexes import VectorstoreIndexCreator"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "57a8a199",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running Chroma using direct local API.\n",
"Using DuckDB in-memory for database. Data will be transient.\n"
]
}
],
"source": [
"index = VectorstoreIndexCreator().from_loaders([loader])"
]
},
{
"cell_type": "markdown",
"id": "f3493fa4",
"metadata": {},
"source": [
2023-02-27 15:45:54 +00:00
"Now that the index is created, we can use it to ask questions of the data! Note that under the hood this is actually doing a few steps as well, which we will cover later in this guide."
2023-02-27 08:31:36 +00:00
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "23d0d234",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-02-27 15:45:54 +00:00
"\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, 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 she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\""
2023-02-27 08:31:36 +00:00
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
2023-02-27 15:45:54 +00:00
"index.query(query)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ae46b239",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'question': 'What did the president say about Ketanji Brown Jackson',\n",
" 'answer': \" The president said that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson, one of the nation's top legal minds, to continue Justice Breyer's legacy of excellence, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\\n\",\n",
" 'sources': '../state_of_the_union.txt'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"index.query_with_sources(query)"
]
},
{
"cell_type": "markdown",
"id": "ff100212",
"metadata": {},
"source": [
"What is returned from the `VectorstoreIndexCreator` is `VectorStoreIndexWrapper`, which provides these nice `query` and `query_with_sources` functionality. If we just wanted to access the vectorstore directly, we can also do that."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b04f3c10",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<langchain.vectorstores.chroma.Chroma at 0x113a3a700>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index.vectorstore"
2023-02-27 08:31:36 +00:00
]
},
{
"cell_type": "markdown",
"id": "2cb6d2eb",
"metadata": {},
"source": [
"## Walkthrough\n",
"\n",
"Okay, so what's actually going on? How is this index getting created?\n",
"\n",
"A lot of the magic is being hid in this `VectorstoreIndexCreator`. What is this doing?\n",
"\n",
"There are three main steps going on after the documents are loaded:\n",
"\n",
"1. Splitting documents into chunks\n",
"2. Creating embeddings for each document\n",
"3. Storing documents and embeddings in a vectorstore\n",
"\n",
"Let's walk through this in code"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "54270abc",
"metadata": {},
"outputs": [],
"source": [
2023-02-20 07:14:50 +00:00
"documents = loader.load()"
]
},
{
"cell_type": "markdown",
"id": "9fdc0fc2",
"metadata": {},
"source": [
"Next, we will split the documents into chunks."
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 8,
2023-02-20 07:14:50 +00:00
"id": "afecb8cf",
"metadata": {},
"outputs": [],
"source": [
2023-02-27 08:31:36 +00:00
"from langchain.text_splitter import CharacterTextSplitter\n",
2023-02-20 07:14:50 +00:00
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"texts = text_splitter.split_documents(documents)"
]
},
{
"cell_type": "markdown",
"id": "4bebc041",
"metadata": {},
"source": [
"We will then select which embeddings we want to use."
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 10,
2023-02-20 07:14:50 +00:00
"id": "9eaaa735",
"metadata": {},
"outputs": [],
"source": [
2023-02-27 08:31:36 +00:00
"from langchain.embeddings import OpenAIEmbeddings\n",
2023-02-20 07:14:50 +00:00
"embeddings = OpenAIEmbeddings()"
]
},
{
"cell_type": "markdown",
"id": "24612905",
"metadata": {},
"source": [
"We now create the vectorstore to use as the index."
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 11,
2023-02-20 07:14:50 +00:00
"id": "5c7049db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running Chroma using direct local API.\n",
"Using DuckDB in-memory for database. Data will be transient.\n"
]
}
],
"source": [
2023-02-27 08:31:36 +00:00
"from langchain.vectorstores import Chroma\n",
2023-02-20 07:14:50 +00:00
"db = Chroma.from_documents(texts, embeddings)"
]
},
{
"cell_type": "markdown",
"id": "30c4e5c6",
"metadata": {},
"source": [
2023-02-27 15:45:54 +00:00
"So that's creating the index.\n",
2023-02-27 08:31:36 +00:00
"Then, as before, we create a chain and use it to answer questions!"
2023-02-20 07:14:50 +00:00
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 12,
2023-02-20 07:14:50 +00:00
"id": "3018f865",
"metadata": {},
"outputs": [],
"source": [
"qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", vectorstore=db)"
]
},
{
"cell_type": "code",
2023-02-27 08:31:36 +00:00
"execution_count": 13,
2023-02-20 07:14:50 +00:00
"id": "032a47f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-02-27 08:31:36 +00:00
"\" The President said that Ketanji Brown Jackson is one of the nation's top legal minds and a consensus builder, with a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. She is a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers.\""
2023-02-20 07:14:50 +00:00
]
},
2023-02-27 08:31:36 +00:00
"execution_count": 13,
2023-02-20 07:14:50 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"qa.run(query)"
]
},
2023-02-27 08:31:36 +00:00
{
"cell_type": "markdown",
"id": "9464690e",
"metadata": {},
"source": [
"`VectorstoreIndexCreator` is just a wrapper around all this logic. It is configurable in the text splitter it uses, the embeddings it uses, and the vectorstore it uses. For example, you can configure it as below:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "4001bbc6",
"metadata": {},
"outputs": [],
"source": [
"index_creator = VectorstoreIndexCreator(\n",
" vectorstore_cls=Chroma, \n",
" embedding=OpenAIEmbeddings(),\n",
" text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "78d8d143",
"metadata": {},
"source": [
"Hopefully this highlights what is going on under the hood of `VectorstoreIndexCreator`. While we think it's important to have a simple way to create indexes, we also think it's important to understand what's going on under the hood."
]
},
2023-02-20 07:14:50 +00:00
{
"cell_type": "code",
"execution_count": null,
2023-02-27 08:31:36 +00:00
"id": "dd7257bf",
2023-02-20 07:14:50 +00:00
"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.9.1"
},
"vscode": {
"interpreter": {
"hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}