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.
In the LLMDataFrameIntegrator
class, data processing can occur in two primary ways:
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.
max_tokens
limit.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.
prompt_column
is used as the prompt for the LLM.response_column
for each row.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.
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.
batch_size
. The prompts for these rows are concatenated into a single string.batch_size
.max_tokens
limit set by the LLM.max_tokens
, an error is raised, and the user is advised to truncate the DataFrame or reduce the batch size."Overflow"
tag.Overflow:
tag, separating the overflowed messages by <sep>
."Overflow"
messages indicating missing 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.
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.
When processing in batch mode, it is possible for the LLM to return:
"Overflow: msg"
."Overflow: msg1 <sep> msg2"
tag.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"
max_tokens
limit.ValueError
is raised with a message asking the user to reduce the batch size or truncate the input.add_llm_responses()
prompt_column
text and stores the response in response_column
.max_tokens
.batch_size
, split_response
, and async_mode
as parameters._process_batches()
_process_individually()
batch_size=None
).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.