Skip to content

LLM functions

Functions

NameDescription
build_text_generation_udfCreates a Spark UDF for text generation using a Hugging Face model and tokenizer.

build_text_generation_udffunction#

def build_text_generation_udf(model_bc: Broadcast, tokenizer_bc: Broadcast, system_prompt: str) -> UserDefinedFunction

Creates a Spark UDF for text generation using a Hugging Face model and tokenizer.

This function sets up a user-defined function (UDF) that can be used in Spark DataFrames to perform text generation. It uses a broadcasted Hugging Face model and tokenizer to ensure efficient distribution across Spark workers.

Parameters

NameTypeDescription
model_bcBroadcastBroadcasted Hugging Face model.
tokenizer_bcBroadcastBroadcasted Hugging Face tokenizer.
system_promptstrPrompt to prepend to each input string before generation.

Returns

functionUserDefinedFunction
A Spark UDF that takes a string input and returns the generated text.

Raises

TypeError
If model_bc or tokenizer_bc is not a Broadcast instance.