# Mosel: scripts for flagging hallucinated text #### Last update: 1 Oct 2024 ### Overview In order to automatically decide whether transcriptions of audio documents generated automatically are reliable, and therefore useful for training, or not, and therefore to be discarded, we focused on the detection of *hallucinations*. In general, “Hallucination in the context of LLMs refers to the generation of text that is erroneous, nonsensical, or detached from reality”. Here, referring to surface hallucinations we consider the subset of the phenomenon which refer to anomalies of the text, disregarding hallucinations related to contents. In fact, it happens that pre-trained LLMs generate **anomalous repetitions** of the same pattern, like (real examples): - Hey, hey, hey, here, hey. - No, no, no, no, no, no, no, no. or output **very long** and noisy **strings**: - T-J-N-D-F-Z-3-2-8-W-M-L-G-0-Z-P-O-2-M-2-M-M-O-G-W. or still produce **single word lines** using a common token but a suspiciously high number of times. For each of the three types of hallucinations a specific script has been designed, whose usage is listed in the following: ``` flagHallucinations.py -h, --help show this help message and exit --tsv-InFile TSV_INFILE, -i TSV_INFILE The input TSV file [Mandatory] --tsv-OutFile TSV_OUTFILE, -o TSV_OUTFILE The output TSV file [Mandatory. If equal to input TSV file, the new column is added to the original file] --column COLUMN, -c COLUMN Column name of the text to process [Optional] (default: source) --thresh1grams THRESH1GRAMS, -u THRESH1GRAMS Threshold for 1-2_word hallucinations [Optional] (default: 4) --threshNgrams THRESHNGRAMS, -n THRESHNGRAMS Threshold for 3-5_word hallucinations [Optional] (default: 2) --quiet, -q Print only True/False, no explanation for True's --version, -v Print version of the script and exit ``` ``` flagAnomalousStrings.py -h, --help show this help message and exit --tsv-InFile TSV_INFILE, -i TSV_INFILE The input TSV file [Mandatory] --tsv-OutFile TSV_OUTFILE, -o TSV_OUTFILE The output TSV file [Mandatory. If equal to input TSV file, the new column is added to the original file] --column COLUMN, -c COLUMN Column name of the text to process [Optional] (default: source) --thresh THRESH, -t THRESH Max number of chars of a string to be unflagged [Optional] (default: 40) --quiet, -q Print only True/False, no explanation for True's --version, -v Print version of the script and exit ``` ``` flagSuspiciousSingleWord.py -h, --help show this help message and exit --tsv-InFile TSV_INFILE, -i TSV_INFILE The input TSV file [Mandatory] --tsv-OutFile TSV_OUTFILE, -o TSV_OUTFILE The output TSV file [Mandatory. If equal to input TSV file, the new column ('suspicious single word') is added to the original file] --tsv-SuspiciousWordFiles TSV_SUSPICIOUSWORDFILES [TSV_SUSPICIOUSWORDFILES ...], -s TSV_SUSPICIOUSWORDFILES [TSV_SUSPICIOUSWORDFILES ...] The TSV file(s) used to look for the suspicious word [Optional. If not present, the input TSV file is used instead] --column COLUMN, -c COLUMN Column name of the text to process [Optional] (default: source) --suspiciousWord SUSPICIOUSWORD, -w SUSPICIOUSWORD suspicious word [if not specified, found in other TSV files passed as parameters] --quiet, -q Print only True/False, no explanation for True's --version, -v Print version of the script and exit ``` All of them read a TSV file from input (-i) which is expected to include a column (-c) with the text to process; they output a TSV file (-o) with an additional column flagging the specific hallucination each script looks for. If input and output files are the same, the former is replaced by the latter. The -q option suppresses the verbosity on hallucinations that have been found.
Here the processing carried out by the three scripts: - **flagHallucinations.py** It flags (by setting True the corresponding entry of the *hall_repeated_ngrams* column) those sentences where a pattern (*n*-gram, that is a sequence of *n* words) is repeated at least a given number of times; for patterns of size 1 to 2, the minimum number of times for flagging it is set by the thresh1grams parameter (default value: 4), for those of size 3-5 by threshNgrams (2). - **flagAnomalousStrings.py** It flags (by setting True the corresponding entry of the *hall_long_word* column) those sentences where at least one word is abnormally long; the abnormality is set through the thresh parameter, whose default value is 40. - **flagSuspiciousSingleWord.py** It flags (by setting True the corresponding entry of the *hall_frequent_single_word*) those sentences which consists of only one single suspicious word. This word can be either passed as a parameter (*suspiciousWord* option) or found inside the TSV input files. In the latter case, it is set as the most frequent word in the text included in files to be inspected. The TSV files to inspect can be passed through the *tsv-SuspiciousWordFiles* option. If no explicit *suspiciousWord* nor *tsv-SuspiciousWordFiles* is passed, the *tsv-InFile* is inspected.