Language model hallucination detection
In some embodiments, a language model forward traversal with a few-shot learning forward prompt yields a primary answer from a primary question. Then at least one backward traversal yields at least one candidate question using backward prompt(s) with answer-question pairs derived from the forward prompt's question-answer pairs. Each backward prompt also includes the primary answer but not the primary question. Each backward traversal is through one or more language models, not necessarily including the forward traversal's language model. Sometimes backward traversals vary model temperature, top-p, or top-k. A vector distance calculated between at least some candidate question vectors and a primary question vector indicates whether the primary answer includes hallucination content, and in some cases how much. Some embodiments withhold hallucinated answers from user interfaces and device control interfaces. Some embodiments also loop to obtain an answer with less hallucination content.
1 . A large language model (LLM) hallucination detection method, comprising automatically:
submitting, via a first forward LLM interface, a forward prompt to a first large language model in a computing system, the forward prompt including a question-answer pair and a primary question, the primary question being unaccompanied by an answer;
in response to submitting the forward prompt, obtaining a primary answer generated by the first large language model;
deriving, from the question-answer pair of the forward prompt, an answer-question pair by pair component order reversal of the question-answer pair, and assembling a backward prompt that includes the answer-question pair and the primary answer obtained from the first large language model and excludes the primary question;
submitting, via a backward LLM interface, the backward prompt to a backward large language model set comprising at least two different LLMs in the computing system, at least two times per model under a scheduled variation of a language-model sampling parameter selected from temperature, top-k, or top-p, where successive submissions differ by a stored increment;
in response to submitting the backward prompt, acquiring a plurality of candidate questions generated by the backward large language model set;
computing, using a text-embedding model, a primary question embedding vector which is computationally procured from the primary question, and a plurality of candidate question embedding vectors which are computationally procured from the plurality of candidate questions;
computing an aggregate similarity score between the primary question embedding vector and the plurality of candidate question embedding vectors;
comparing the aggregate similarity score to a stored threshold to assign a hallucination extent; and
responsive to the hallucination extent indicating unacceptability, automatically withholding the primary answer from any user interface display and storing the primary answer in a non-human-perceptible form in memory of the computing system.
2 . The method of claim 1 , comprising at least one of:
deriving the answer-question pair from the question-answer pair by the pair component order reversal and also at least by rephrasing the question or rephrasing the corresponding answer to the question, or rephrasing both; or
changing an order of pairs, such that the backward prompt includes at least two answer-question pairs which individually correspond to respective question-answer pairs in the forward prompt, but the answer-question pairs appear in a different order in the backward prompt than the corresponding pairs in the forward prompt.
3 . The method of claim 1 , further comprising:
in response to the hallucination extent indicating unacceptability, prompting the first large language model for another answer to the primary question, or prompting the first large language model for an answer to a substitute question, wherein the substitute question is computed from at least the primary question or the substitute question is semantically interchangeable with the primary question, or both.
4 . The method of claim 1 , wherein a candidate question of the plurality of candidate questions equals the primary question, and the method further comprises determining whether the primary answer is a hallucination, the determining being independent of the aggregate similarity score.
5 . A computing system configured as a large language model testing framework, the system comprising:
a user interface;
a digital memory set comprising at least one digital memory, the digital memory set containing a forward large language model interface and a backward large language model interface;
a processor set comprising at least one processor, the processor set in operable communication with the digital memory, the processor set configured to perform a large language model hallucination test which includes (a) submitting, via the forward large language model interface, a forward prompt to a first large language model, the forward prompt including a question-answer pair and a primary question, the primary question being unaccompanied by an answer, (b) obtaining a primary answer generated by the first large language model, (c) deriving, from the question-answer pair of the forward prompt, an answer-question pair by pair component order reversal of the question-answer pair, and assembling a backward prompt that includes the answer-question pair and the primary answer obtained from the first large language model and excludes the primary question, submitting, via the backward large language model interface, the backward prompt to a backward large language model set comprising at least two different LLMs, at least two times per model under a scheduled variation of a language-model sampling parameter selected from temperature, top-k, or top-p, where successive submissions differ by a stored increment, (e) acquiring a plurality of candidate questions generated by the backward large language model set, (f) computing, using a text-embedding model, a primary question embedding vector which is computationally procured from the primary question, and a plurality of candidate question embedding vectors which are computationally procured from the plurality of candidate questions, (g) computing an aggregate similarity score between the primary question embedding vector and the plurality of candidate question vectors, (h) comparing the aggregate similarity score to a stored threshold to assign a hallucination extent, and (i) responsive to the hallucination extent indicating unacceptability, automatically withholding the primary answer from the user interface and storing the primary answer in a non-human-perceptible form in the digital memory set.
6 . The computing system of claim 5 , comprising at least one of:
the forward large language model interface interfaces with a forward large language model, and the system comprises the forward large language model;
the backward large language model interface interfaces with a backward large language model set, the system comprises the backward large language model set, and the backward large language model set includes the forward large language model;
the backward large language model interface interfaces with a backward large language model set, the system comprises the backward large language model set, and the backward large language model set does not include the forward large language model;
the backward large language model interface interfaces with a backward large language model set which includes multiple large language models, and the system comprises the backward large language model set;
the backward large language model interface interfaces with a backward large language model set which includes exactly one large language model, and the system comprises the backward large language model set;
a single application program interface serves as both the forward large language model interface and the backward large language model interface; or
a first application program interface serves as the forward large language model interface and a second different application program interface serves as the backward large language model interface.
7 . The computing system of claim 5 , wherein the processor is configured to submit a sequence of backward prompts to the backward large language model interface, to acquire a corresponding sequence of candidate questions from the backward large language model interface, to procure a corresponding sequence of candidate question embedding vectors, and to calculate the aggregate similarity score from at least the primary question embedding vector and the candidate question embedding vectors.
8 . The computing system of claim 5 , wherein responsive to the hallucination extent indicating unacceptability, the processor is configured to prompt through the forward large language model interface for a different answer.
9 . The computing system of claim 5 , wherein responsive to the hallucination extent indicating unacceptability, the system is configured to control a device using acceptable answers without using more than a specified amount of unacceptable answers, and the device includes at least one of: a vehicle, a robot, a communications device in a network, a wearable computing device, an internet of things device, a smart phone, or a device configured for game play.
10 . The computing system of claim 5 , wherein responsive to the hallucination extent indicating unacceptability, the system is configured to send generated output to the user interface using acceptable answers without using more than a specified amount of unacceptable answers, and the generated output includes at least one of: a natural language text, a source code text, a portion of an image, a portion of a video clip, a portion of an audio clip, a portion of a sensor data clip, or a portion of an industrial control sequence.
11 . A computer-readable storage device configured with data and instructions which upon execution by a processor perform a method of testing large language model output, the method comprising automatically:
submitting, via a first forward LLM interface, a forward prompt to a first large language model in a computing system, the forward prompt including a question-answer pair and a primary question, the primary question being unaccompanied by an answer;
in response to submitting the forward prompt, obtaining a primary answer generated by the first large language model;
deriving, from the question-answer pair of the forward prompt, an answer-question pair by pair component order reversal of the question-answer pair, and assembling a backward prompt that includes the answer-question pair and the primary answer obtained from the first large language model and excludes the primary question;
submitting, via a backward LLM interface, the backward prompt to a backward large language model set comprising at least two different LLMs in the computing system, at least two times per model under a scheduled variation of a language-model sampling parameter selected from temperature, top-k, or top-p, where successive submissions differ by a stored increment;
in response to submitting the backward prompt, acquiring a plurality of candidate questions generated by the backward large language model set;
computing, using a text-embedding model, a primary question embedding vector which is computationally procured from the primary question, and a plurality of candidate question embedding vectors which are computationally procured from the plurality of candidate questions;
computing an aggregate similarity score between the primary question embedding vector and the plurality of candidate question embedding vectors;
comparing the aggregate similarity score to a stored threshold to assign a hallucination extent; and
responsive to the hallucination extent indicating unacceptability, automatically withholding the primary answer from any user interface display and storing the primary answer in a non-human-perceptible form in memory of the computing system.
12 . The computer-readable storage device of claim 11 , wherein the primary answer belongs to a first dataset, and at least a portion of the method is repeated with a second dataset, and respective hallucination extents for the datasets are reported via the user interface.
13 . The computer-readable storage device of claim 11 , wherein the primary question includes text.
14 . The computer-readable storage device of claim 11 , wherein at least one of the primary question and the primary answer include source code.
15 . The computer-readable storage device of claim 11 , wherein at least one of the primary question and the primary answer represent at least one of: a time, a calendar date, a duration, a location, an amount of money, a cite to a legal authority, a statement of a human person or a legal person, an output of a computing system, an action of a human person or a legal person or a computing system, a human person's name, a legal person's name, a computing system identifier, a device identifier, a health status, a legal status, a financial status, confidential or proprietary information, medical information, military information, national security information, information which is subject to governmental regulation, or personal identifiable information.