LANGCHAIN — What is Use Case Accelerant Extraction Service?

Laxfed Paulacy
3 min readMar 14, 2024

The ultimate promise of technology is to make us master of a world that we command by the push of a button. — Volker Grassmuck

Insights in this article were refined using prompt engineering methods.

Getting Started with LangChain Extraction Service

LangChain has recently released an open-source extraction service designed to leverage Large Language Models (LLMs) for structured data extraction from unstructured sources. This tutorial will provide an overview of the LangChain extraction service, along with detailed examples to guide you through its usage.

Overview of LangChain Extraction Service

The LangChain extraction service is a REST API built on top of FastAPI and Postgresql. It aims to simplify the process of extracting structured data from unstructured text using LLMs. The key components required for configuring an LLM for extraction include a schema that describes the data structure, a prompt that provides context for extraction, and reference examples that illustrate extraction from text.

Components of LangChain Extraction Service

  1. Schema: Describes the structure of the data to be extracted.
  2. Prompt: Provides context for the extraction and primes the LLM.
  3. Reference Examples: Show the model examples of extractions from text.

Essential Concepts for Using LangChain Extraction Service

1. Raw Data Processing

Before utilizing the LangChain extraction service, the raw data must be converted into a text format. LangChain provides document loaders that facilitate the parsing of files such as PDFs, PowerPoint presentations, and HTML documents into text.

2. Describing the Extraction

To effectively extract information using the LLM, a description of what needs to be extracted must be provided. This description consists of three key components:

  • A schema that defines the data structure.
  • A prompt that provides context and instructions for the LLM.
  • Reference examples that demonstrate how to extract information from the text.

3. Ensuring Correct Output Format

When instructing an LLM to output structured data, it’s important to ensure that the output format is correct. Function calling can be used to enforce specific output formats, improving performance and consistency.

Example Usage of LangChain Extraction Service

Let’s explore an example of how to use the LangChain extraction service to create and invoke an extractor for personal information extraction.

Creating an Extractor

curl -X 'POST' \
'http://localhost:8000/extractors' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name": "Personal Information",
"description": "Use to extract personal information",
"schema": {
"type": "object",
"title": "Person",
"required": [
"name",
"age"
],
"properties": {
"age": {
"type": "integer",
"title": "Age"
},
"name": {
"type": "string",
"title": "Name"
}
}
},
"instruction": "Use information about the person from the given user input."
}'

Invoking the Extractor

curl -s -X 'POST' \
'http://localhost:8000/extract' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'extractor_id=32d5324a-8a48-4073-b57c-0a2ebfb0bf5e' \
-F 'text=my name is chester and i am 20 years old. My name is eugene and I am 1 year older than chester.' \
-F 'mode=entire_document' \
-F 'file=' | jq .

In this example, we create an extractor for personal information and then invoke it to extract details such as name and age from the provided text.

Conclusion

In this tutorial, we’ve introduced the LangChain extraction service and provided a practical example of its usage. The LangChain extraction service offers a convenient way to leverage LLMs for structured data extraction from unstructured sources. With the provided code snippets and examples, you can start utilizing the LangChain extraction service for your extraction workflows.

For further details and more complex examples, refer to LangChain-extract repository and LangChain Extraction documentation.

By following this guide, you can explore the potential of LangChain’s extraction service and integrate it into your data extraction processes.

Happy coding with LangChain!

--

--

Laxfed Paulacy
Laxfed Paulacy

Written by Laxfed Paulacy

Delivering Fresh Recipes, Crypto News, Python Tips & Tricks, and Federal Government Shenanigans and Content.

No responses yet