LLMWorkbook

LLMWorkbook - Batch and Row Processing Documentation

Overview

The LLMDataFrameIntegrator class allows users to integrate Large Language Models (LLMs) into a pandas DataFrame pipeline. This class provides flexibility by allowing both row-wise processing and batch processing of prompts, helping optimize the use of LLMs while ensuring the token limit of the LLM is respected. This documentation explains how the data is processed, with a detailed breakdown of row processing and batch processing modes.


Data Processing Overview

In the LLMDataFrameIntegrator class, data processing can occur in two primary ways:

  1. Row-wise Processing: Each row is processed separately by sending one prompt at a time to the LLM.
  2. Batch Processing: Multiple rows are grouped together, and the entire batch is sent to the LLM in a single request.

The user can choose between row-wise and batch processing depending on their needs. Both modes allow for flexibility in how the data is sent to the LLM, and batch processing, in particular, is designed to improve efficiency by reducing the number of API calls.

Key Features


1. Row-wise Processing

How Row-wise Processing Works

Row-wise processing is the default behavior when using the LLMDataFrameIntegrator. Each row in the DataFrame is treated as an independent prompt, and the LLM is called for each row’s prompt sequentially.

When to Use Row-wise Processing

Example: Row-wise Processing

updated_df = integrator.add_llm_responses(
    prompt_column="prompt_column",
    response_column="llm_response",
    batch_size=None  # Default to row-wise processing
)

In this case, the integrator will process each prompt row by row and store the response in the llm_response column for each row.


2. Batch Processing

How Batch Processing Works

Batch processing groups multiple rows together and sends them as a single request to the LLM. The prompt texts from the specified prompt_column for each batch of rows are concatenated into a single string, separated by newline characters (\n), and then sent together to the LLM.

When to Use Batch Processing

Batch Mode Behavior

Overflow Handling

Example: Batch Processing with Split Responses

updated_df = integrator.add_llm_responses(
    prompt_column="prompt_column",
    response_column="llm_response",
    batch_size=5,  # Process 5 rows together
    split_response=True  # Split responses row by row
)

Here, the integrator will process 5 rows at once, send them as a single request, and assign the individual responses to the corresponding rows. If there are fewer responses than expected, the remaining rows will have the "Overflow" tag.

Example: Batch Processing Without Splitting Responses

updated_df = integrator.add_llm_responses(
    prompt_column="prompt_column",
    response_column="llm_response",
    batch_size=5,  # Process 5 rows together
    split_response=False  # Store the entire batch's response in the first row
)

In this case, the response from the LLM for the batch is stored in the first row, and the other rows in the batch remain empty.


3. Handling Overflow Responses

When processing in batch mode, it is possible for the LLM to return:

Overflow Example

If 3 rows are processed together, but the LLM returns 5 responses:

Batch: ["Prompt 1", "Prompt 2", "Prompt 3"]
Responses: ["Response 1", "Response 2", "Response 3", "Response 4", "Response 5"]

The response will be processed as:

Row 1: "Response 1"
Row 2: "Response 2"
Row 3: "Response 3 | Overflow: Response 4 <sep> Response 5"

4. Token Limit and Batch Size

Token Limit Validation


Function Overview

Key Methods


Final Thoughts

Batch vs Row-wise Processing

By offering both options, this feature allows users to decide whether to send prompts individually or in batches, optimizing for performance and cost, while ensuring that token limits are respected.