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.
langchain/docs/modules/indexes/getting_started.ipynb

327 lines
8.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "2244801b",
"metadata": {},
"source": [
"# Getting Started\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8d369452",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import VectorDBQA\n",
"from langchain.llms import OpenAI"
]
},
{
"cell_type": "markdown",
"id": "07c1e3b9",
"metadata": {},
"source": [
"Next in the generic setup, let's specify the document loader we want to use."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "33958a86",
"metadata": {},
"outputs": [],
"source": [
"from langchain.document_loaders import TextLoader\n",
"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": [
"Now that the index is created, we can use it in a VectorDBQAChain to ask questions of the data!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "23d0d234",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a consensus builder, and has gained a broad range of support. He also said that 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.\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", vectorstore=index)\n",
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"qa.run(query)"
]
},
{
"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": [
"documents = loader.load()"
]
},
{
"cell_type": "markdown",
"id": "9fdc0fc2",
"metadata": {},
"source": [
"Next, we will split the documents into chunks."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "afecb8cf",
"metadata": {},
"outputs": [],
"source": [
"from langchain.text_splitter import CharacterTextSplitter\n",
"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",
"execution_count": 10,
"id": "9eaaa735",
"metadata": {},
"outputs": [],
"source": [
"from langchain.embeddings import OpenAIEmbeddings\n",
"embeddings = OpenAIEmbeddings()"
]
},
{
"cell_type": "markdown",
"id": "24612905",
"metadata": {},
"source": [
"We now create the vectorstore to use as the index."
]
},
{
"cell_type": "code",
"execution_count": 11,
"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": [
"from langchain.vectorstores import Chroma\n",
"db = Chroma.from_documents(texts, embeddings)"
]
},
{
"cell_type": "markdown",
"id": "30c4e5c6",
"metadata": {},
"source": [
"Then, as before, we create a chain and use it to answer questions!"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3018f865",
"metadata": {},
"outputs": [],
"source": [
"qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", vectorstore=db)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "032a47f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" 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.\""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"qa.run(query)"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd7257bf",
"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
}