Skip to main content

Unstructured

This page covers how to use Unstructured within LangChain.

What is Unstructured?

Unstructured is an open source Python package for extracting text from raw documents for use in machine learning applications. Currently, Unstructured supports partitioning Word documents (in .doc or .docx format), PowerPoints (in .ppt or .pptx format), PDFs, HTML files, images, emails (in .eml or .msg format), epubs, markdown, and plain text files.

unstructured is a Python package and cannot be used directly with TS/JS, however Unstructured also maintains a REST API to support pre-processing pipelines written in other programming languages. The endpoint for the hosted Unstructured API is https://api.unstructured.io/general/v0/general, or you can run the service locally using the instructions found here.

Check out the Unstructured documentation page for instructions on how to obtain an API key.

Quick start

You can use Unstructured in langchain with the following code. Replace the filename with the file you would like to process. If you are running the container locally, switch the url to http://127.0.0.1:8000/general/v0/general. Check out the API documentation page for additional details.

import { UnstructuredLoader } from "langchain/document_loaders/fs/unstructured";

const options = {
apiKey: "MY_API_KEY",
};

const loader = new UnstructuredLoader(
"src/document_loaders/example_data/notion.md",
options
);
const docs = await loader.load();

API Reference:

Directories

You can also load all of the files in the directory using UnstructuredDirectoryLoader, which inherits from DirectoryLoader:

import { UnstructuredDirectoryLoader } from "langchain/document_loaders/fs/unstructured";

const options = {
apiKey: "MY_API_KEY",
};

const loader = new UnstructuredDirectoryLoader(
"langchain/src/document_loaders/tests/example_data",
options
);
const docs = await loader.load();

API Reference:

Currently, the UnstructuredLoader supports the following document types:

  • Plain text files (.txt/.text)
  • PDFs (.pdf)
  • Word Documents (.doc/.docx)
  • PowerPoints (.ppt/.pptx)
  • Images (.jpg/.jpeg)
  • Emails (.eml/.msg)
  • HTML (.html)
  • Markdown Files (.md)

The output from the UnstructuredLoader will be an array of Document objects that looks like the following:

[
Document {
pageContent: `Decoder: The decoder is also composed of a stack of N = 6
identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a
third sub-layer, wh
ich performs multi-head attention over the output of the encoder stack. Similar to the encoder, we
employ residual connections around each of the sub-layers, followed by layer normalization. We also
modify the self
-attention sub-layer in the decoder stack to prevent positions from attending to subsequent
positions. This masking, combined with fact that the output embeddings are offset by one position,
ensures that the predic
tions for position i can depend only on the known outputs at positions less than i.`,
metadata: {
page_number: 3,
filename: '1706.03762.pdf',
category: 'NarrativeText'
}
},
Document {
pageContent: '3.2 Attention',
metadata: { page_number: 3, filename: '1706.03762.pdf', category: 'Title'
}
]

Help us out by providing feedback on this documentation page: