Skip to main content

Extracting structured output

Overviewโ€‹

Large Language Models (LLMs) are emerging as an extremely capable technology for powering information extraction applications.

Classical solutions to information extraction rely on a combination of people, (many) hand-crafted rules (e.g., regular expressions), and custom fine-tuned ML models.

Such systems tend to get complex over time and become progressively more expensive to maintain and more difficult to enhance.

LLMs can be adapted quickly for specific extraction tasks just by providing appropriate instructions to them and appropriate reference examples.

This guide will show you how to use LLMs for extraction applications!

Approachesโ€‹

There are 3 broad approaches for information extraction using LLMs:

  • Tool/Function Calling Mode: Some LLMs support a tool or function calling mode. These LLMs can structure output according to a given schema. Generally, this approach is the easiest to work with and is expected to yield good results.

  • JSON Mode: Some LLMs are can be forced to output valid JSON. This is similar to tool/function Calling approach, except that the schema is provided as part of the prompt. Generally, our intuition is that this performs worse than a tool/function calling approach, but donโ€™t trust us and verify for your own use case!

  • Prompting Based: LLMs that can follow instructions well can be instructed to generate text in a desired format. The generated text can be parsed downstream using existing Output Parsers or using custom parsers into a structured format like JSON. This approach can be used with LLMs that do not support JSON mode or tool/function calling modes. This approach is more broadly applicable, though may yield worse results than models that have been fine-tuned for extraction or function calling.

Quickstartโ€‹

Head to the quickstart to see how to extract information using LLMs using a basic end-to-end example.

The quickstart focuses on information extraction using the tool/function calling approach.

How-To Guidesโ€‹

  • Use Reference Examples: Learn how to use reference examples to improve performance.
  • Handle Long Text: What should you do if the text does not fit into the context window of the LLM?
  • Handle Files: Examples of using LangChain document loaders and parsers to extract from files like PDFs.
  • Use a Parsing Approach: Use a prompt based approach to extract with models that do not support tool/function calling.

Guidelinesโ€‹

Head to the Guidelines page to see a list of opinionated guidelines on how to get the best performance for extraction use cases.

Use Case Accelerantโ€‹

langchain-extract is a starter repo that implements a simple web server for information extraction from text and files using LLMs. It is build using FastAPI, LangChain and Postgresql. Feel free to adapt it to your own use cases.

Other Resourcesโ€‹


Help us out by providing feedback on this documentation page: