Site icon Efficient Coder

How DocETL Transforms Unstructured Data into Insights with AI

 

DocETL: Simplifying Document Data Processing with AI

A few months ago, I found myself drowning in a chaotic pile of medical transcripts. My task? Extracting medication names and their side effects from these messy, unstructured documents. As someone who’s tackled plenty of data challenges, this one was pushing me to my limits. Manually sifting through the transcripts was out of the question—too time-consuming and error-prone. Traditional tools? They just couldn’t handle the complexity. That’s when I stumbled upon DocETL, a Python library from UC Berkeley that felt like a lifeline. Powered by AI, it transformed my data nightmare into a streamlined process. In this post, I’ll walk you through what DocETL is, how it works, and why it’s a game-changer for anyone dealing with unstructured data.


What is DocETL? Your AI-Powered Data Processing Assistant

Imagine you have a folder full of PDFs, emails, or debate transcripts, and you need to extract specific information—like key themes, contract clauses, or medication details. Doing this manually is a recipe for frustration, and most tools struggle with the complexity of unstructured data. That’s where DocETL comes in. It’s a Python library designed to handle messy, unstructured documents using AI-driven pipelines. These pipelines automate the extraction, transformation, and loading of data, turning chaos into clean, structured information.

DocETL is built for anyone who needs to process documents efficiently. It comes in two flavors:

  • DocWrangler: A web-based playground where you can experiment with pipelines, test ideas, and see results instantly. Think of it as a sandbox for data enthusiasts.
  • Python Package: A powerful tool for running those pipelines in production, automating your workflow like a pro.

In this post, I’ll guide you through both, starting with DocWrangler, which was my first stop on this journey.


DocWrangler: Your Data Processing Playground

DocWrangler was a revelation for me. It’s a web interface that lets you build and test data processing pipelines step-by-step. You can upload a file, write a simple prompt, and see what the AI extracts in real-time. No need to write code upfront—you can experiment, tweak, and refine your approach until it’s just right. Once you’re happy, you can export your pipeline as a YAML file for use in production. It’s like sketching a blueprint before building the final product.

Setting Up DocWrangler: Easier Than You Think

The quickest way to get started with DocWrangler is using Docker. Even if you’re not a Docker expert, the setup is straightforward. Here’s how I did it:

Step 1: Install Docker

If you don’t have Docker installed, head over to docker.com and follow the instructions for your operating system—whether it’s Mac, Windows, or Linux.

Step 2: Configure Environment Files

DocWrangler needs some configuration to connect to an AI model, like OpenAI’s GPT-4o. You’ll need an API key for this. Create a .env file in your project folder and add the following:

OPENAI_API_KEY=your_api_key_here

# BACKEND configuration
BACKEND_ALLOW_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
BACKEND_HOST=localhost
BACKEND_PORT=8000
BACKEND_RELOAD=True

# FRONTEND configuration
FRONTEND_HOST=0.0.0.0
FRONTEND_PORT=3000

# Host port mapping for docker-compose (if not set, defaults are used in docker-compose.yml)
FRONTEND_DOCKER_COMPOSE_PORT=3031
BACKEND_DOCKER_COMPOSE_PORT=8081

# Supported text file encodings
TEXT_FILE_ENCODINGS=utf-8,latin1,cp1252,iso-8859-1

You can get your OpenAI API key from platform.openai.com. Replace your_api_key_here with your actual key.

Next, in a website folder (which you’ll create after cloning the repository), add a .env.local file with the following:

OPENAI_API_KEY=sk-xxx
OPENAI_API_BASE=https://api.openai.com/v1
MODEL_NAME=gpt-4o-mini

NEXT_PUBLIC_BACKEND_HOST=localhost
NEXT_PUBLIC_BACKEND_PORT=8000
NEXT_PUBLIC_HOSTED_DOCWRANGLER=false

Step 3: Launch DocWrangler

Open your terminal and run these commands:

git clone https://github.com/ucbepic/docetl.git
cd docetl
make docker

In a few minutes, the Docker container will be up and running. Open your browser and go to http://localhost:3000 to start experimenting with DocWrangler.

Cleaning Up

When you’re done, stop the container and remove the data with:

make docker-clean

Why DocWrangler is a Game-Changer

DocWrangler saved me countless hours of trial and error. For my medical transcripts, I uploaded a sample, entered a prompt like “List all medications mentioned,” and instantly saw the results. If the AI missed something, I could tweak the prompt right there until it was spot-on. Once I had it dialed in, I exported the pipeline as a YAML file for production. It’s like testing a recipe before cooking for a crowd—efficient and stress-free.


DocETL Python Package: Production-Ready Powerhouse

Once I had my pipeline figured out in DocWrangler, I turned to the DocETL Python package for the heavy lifting. This is where you take your prototype and turn it into an automated, production-ready process. I used it to process an entire folder of medical transcripts, extracting medications and summarizing their side effects with ease.

Setting Up the Python Package

Step 1: Install DocETL

You’ll need Python 3.10 or higher. Install the package with:

pip install docetl

Step 2: Add Your API Key

Create a .env file in your project folder:

OPENAI_API_KEY=your_api_key_here

Step 3: Build a Pipeline with YAML

DocETL uses YAML files to define pipelines, which keeps things simple and readable. Here’s the pipeline I used for my medical transcripts:

# medical_pipeline.yaml
dataset:
  name: medical_transcripts
  source: ./transcripts/*.txt

operations:
  - type: map
    name: extract_medications
    prompt: "List all medications mentioned in the transcript."
    output_schema:
      medications: list[str]
  - type: resolve
    name: resolve_medication_names
    input: extract_medications
    prompt: "Standardize medication names (e.g., 'Tylenol' to 'Acetaminophen')."
    output_schema:
      standardized_medications: list[str]
  - type: map
    name: summarize_side_effects
    input: resolve_medication_names
    prompt: "For each medication, summarize its side effects and uses from the transcript."
    output_schema:
      medication_summary:
        medication: str
        side_effects: str
        therapeutic_uses: str
output:
  destination: ./output/medication_summaries.json

This pipeline does three things:

  1. Extracts medication names from the transcripts.
  2. Standardizes the names (e.g., converting “Tylenol” to “Acetaminophen”).
  3. Summarizes the side effects and uses for each medication, saving everything to a JSON file.

Step 4: Run the Pipeline

You can run the pipeline from the command line:

docetl run medical_pipeline.yaml

Or in Python:

from docetl import Pipeline

pipeline = Pipeline("medical_pipeline.yaml")
pipeline.run()

The result? A clean JSON file with all the extracted data, ready for analysis or reporting.

Why the Python Package Shines

The Python package is a beast when it comes to automation. It’s fast, handles large datasets with ease, and integrates seamlessly into existing Python projects. Plus, it’s smart—features like entity resolution (e.g., standardizing medication names) make it perfect for real-world data challenges.


AWS Bedrock Support: A Bonus for AWS Users

If you’re an AWS enthusiast, DocETL has you covered with AWS Bedrock integration. Bedrock is Amazon’s AI service, and DocETL can leverage its models. Here’s how to set it up:

First, configure your AWS credentials:

aws configure
make test-aws

Then, run DocWrangler with Bedrock support:

AWS_PROFILE=your-profile AWS_REGION=your-region make docker
AWS_PROFILE=your-profile AWS_REGION=your-region docker compose --profile aws up

In your pipeline YAML, you can specify Bedrock models, like bedrock.anthropic.claude-v2. This flexibility is a nice touch for those already invested in the AWS ecosystem.


What Makes DocETL Stand Out?

I’ve used plenty of data processing tools over the years, but DocETL has a few features that make it truly special:

  • It’s Smart: The AI breaks down complex tasks into smaller, accurate steps, ensuring better results.
  • It’s Easy: YAML pipelines are straightforward to write, even if you’re not a coding expert.
  • It’s Free: Open-source with over 1.3k GitHub stars (as of November 2024) and backed by UC Berkeley.
  • It’s Flexible: From legal contracts to debate transcripts, it handles all kinds of messy data with ease.

Another Example: Analyzing Debate Transcripts

To showcase DocETL’s versatility, here’s a pipeline I experimented with to analyze debate transcripts:

# debate_pipeline.yaml
dataset:
  name: debate_transcripts
  source: ./debates/*.txt

operations:
  - type: map
    name: extract_themes
    prompt: "Find key themes and viewpoints in the debate transcript."
    output_schema:
      themes: list[dict[theme: str, viewpoint: str]]
  - type: unnest
    name: flatten_themes
    input: extract_themes
    field: themes
    output_schema:
      theme: str
      viewpoint: str
output:
  destination: ./output/debate_themes.json

Run it with:

docetl run debate_pipeline.yaml

This pipeline extracts themes like “healthcare” or “economy” and their corresponding viewpoints, such as “supports tax cuts.” It’s a lifesaver for summarizing lengthy debates quickly.


How to Get Started with DocETL

Ready to give DocETL a try? Here’s my advice:

  1. Experiment with DocWrangler: Set it up with Docker and play around at http://localhost:3000.
  2. Try the Python Package: Install it with pip install docetl and run the medical transcript pipeline example.
  3. Check the Documentation: Visit docetl.org for tutorials and more examples.
  4. Join the Community: Star the GitHub repo and share your experiences.

DocETL turned my data processing nightmare into a breeze, whether it was medical transcripts or debate records. It’s efficient, user-friendly, and backed by cutting-edge AI. I hope my experience inspires you to try it out and see how it can simplify your data challenges!

Exit mobile version