LLM Utils
build_LLM_prompt
Util function to build the LLM prompt from input text data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ds |
Dataset
|
Input dataset containing text |
required |
ds_column_mapping |
dict
|
Dictionary mapping prompt entities to dataset column names. |
required |
prompt_template_prefix |
Union[str, None]
|
Text instruction to prepend to each transformed input text sample. Defaults to "". |
''
|
answer_start_token |
str
|
Token to append to the prompt to indicate start of the answer. Defaults to "" |
''
|
llm_prompt_col_name |
str
|
Name of the column for the built LLM prompts. Defaults to 'llm_prompt' |
'llm_prompt'
|
Returns: Dataset: Dataset with generated predictions.
Source code in dqc/llm_utils/inference.py
infer_LLM
Util function to run LLM inference
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
AutoModelForCausalLM
|
LLM artifact. |
required |
tokenizer |
Autotokenizer)
|
LLM tokenizer object |
required |
input_ds |
Dataset
|
Input dataset containing text prompts. |
required |
llm_prompt_col_name |
str
|
Name of the column containing text prompts. Defaults to 'llm_prompt'. |
'llm_prompt'
|
llm_response_raw_col_name |
str
|
Name of the column containing prediction. Defaults to 'llm_response'. |
'llm_response'
|
Returns:
Name | Type | Description |
---|---|---|
dataset |
Dataset
|
Dataset with generated predictions. |
Source code in dqc/llm_utils/inference.py
run_LLM
Run end-to-end LLM inference (from pre-processing input data to post-processing the predictions) and return the computed performance metrics on input validation data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val_data |
Union[DataFrame, Dataset]
|
Validation data with labels |
required |
model |
AutoModelForCausalLM
|
LLM artifact. |
required |
tokenizer |
Autotokenizer)
|
LLM tokenizer object |
required |
ds_column_mapping |
dict
|
Dictionary mapping prompt entities to dataset column names. |
required |
prompt_template_prefix |
Union[str, None]
|
Text instruction to prepend to each transformed input text sample. Defaults to "". |
''
|
llm_prompt_col_name |
str
|
Name of the column with the built LLM prompts. Defaults to 'llm_prompt' |
'llm_prompt'
|
llm_response_raw_col_name |
str
|
Name of the column containing prediction. Defaults to 'llm_response'. |
'llm_response'
|
llm_response_cleaned_col_name |
str
|
Name of the column containing the final post processed result. Defaults to 'llm_response_cleaned' |
'llm_response_cleaned'
|
answer_start_token |
str
|
Token that indicates the start of answer generation. Defaults to '' |
''
|
answer_end_token |
str
|
Token that indicates the end of answer generation. Defaults to '' |
''
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing F1 score. |
Source code in dqc/llm_utils/inference.py
compute_selfensembling_confidence_score
Util function to compute confidence score of a given target text using LLM generated reference texts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
example |
LazyRow
|
A row of data from a dataset containing the target and reference texts. |
required |
target_column |
str
|
Name of the column containing the target text for estimation of confidence score. |
required |
reference_column_list |
List[str]
|
Names of the columns containing the reference texts to be compared with the target text. |
required |
scoring_method |
Union[Callable[[str, str], float], str]
|
A function or the string 'exact_match' to compute the confidence score. Defaults to 'exact_match'. |
'exact_match'
|
case_sensitive |
bool
|
|
False
|
Raises:
ValueError: If scoring_method
is neither 'exact_match' nor a valid callable function
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Score between 0 and 1 quantifying the confidence score for the target text |