{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "3f8b002b",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "46ced011-52fd-4adf-b2ce-c9247e87757d",
"metadata": {},
"source": [
"# File Based Node Parsers\n",
"\n",
"The `SimpleFileNodeParser` and `FlatReader` are designed to allow opening a variety of file types and automatically selecting the best `NodeParser` to process the files. The `FlatReader` loads the file in a raw text format and attaches the file information to the metadata, then the `SimpleFileNodeParser` maps file types to node parsers in `node_parser/file`, selecting the best node parser for the job.\n",
"\n",
"The `SimpleFileNodeParser` does not perform token based chunking of the text, and is intended to be used in combination with a token node parser.\n",
"\n",
"Let's look at an example of using the `FlatReader` and `SimpleFileNodeParser` to load content. For the README file I will be using the LlamaIndex README and the HTML file is the Stack Overflow landing page, however any README and HTML file will work."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c96e7e3e",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e33116cb",
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-readers-file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89026f89",
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae713f58-414a-4dc6-a358-7d07846eddd6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/adamhofmann/opt/anaconda3/lib/python3.9/site-packages/langchain/__init__.py:24: UserWarning: Importing BasePromptTemplate from langchain root module is no longer supported.\n",
" warnings.warn(\n",
"/Users/adamhofmann/opt/anaconda3/lib/python3.9/site-packages/langchain/__init__.py:24: UserWarning: Importing PromptTemplate from langchain root module is no longer supported.\n",
" warnings.warn(\n"
]
}
],
"source": [
"from llama_index.core.node_parser import SimpleFileNodeParser\n",
"from llama_index.readers.file import FlatReader\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46733ed7-54a8-44b1-ad2f-0a5cd0624a71",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'filename': 'stack-overflow.html', 'extension': '.html'}\n",
"Doc ID: a6750408-b0fa-466d-be28-ff2fcbcbaa97\n",
"Text: