Method for discovering large language model fraudulent reply based on genetic algorithm
By combining genetic algorithms and hint injection attacks to identify and defend against offensive replies in large language models, the problem of low identification and defense efficiency in existing technologies is solved, and the security of language model output is improved.
Patent Information
- Application Number
- CN202410602276.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-15
- Publication Date
- 2024-09-17
AI Technical Summary
Existing technologies suffer from inefficiency and insufficient diversity in identifying and defending against offensive replies in large language models, especially the high cost of manually creating test prompts, the low efficiency of automatic discovery methods, and the lack of simple and clear defense methods.
Combining genetic algorithm and hint injection attack, through mutation, crossover and evaluation operations, the offensive text evaluation module is used to identify potentially offensive replies of the language model, and the possibility of offensive replies is reduced through targeted training.
It achieves accurate positioning of the potentially offensive output of the language model and improves the security of the response generated by the model, significantly reducing security threats and enhancing the security and reliability of the model.
Smart Images

Figure CN118657137A
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing, and in particular to a method for discovering offensive replies of a large language model based on a genetic algorithm. Background Art
[0002] Previous research in this area has focused on identifying potential security risks in language models by manually creating test prompts. While this type of research using manually created prompts can reveal bad behavior and offensive responses in a large number of language models, it has limitations and is constrained by the high cost of human annotators, which limits the diversity of prompts that can detect offensive responses from language models. Another type of research aims to supplement manual testing by automatically discovering potentially offensive responses. Innovative methods have been proposed to generate test inputs using the language model itself, thereby discovering a wider range of unsafe model behaviors. However, this type of method is less efficient.
[0003] This application combines the concept of prompt injection attack and the idea of genetic algorithm. Prompt injection attack involves manipulating the output of language model by using instructions as input prompts. Similar to other injection attacks in information security, prompt injection may occur when instructions are connected to the main content, making it difficult for large language models to distinguish them. Prompt injection is a vulnerability that has had a significant impact on language models recently. Injecting malicious instructions as prompts can manipulate or affect the normal output process of the model, resulting in inappropriate, biased or harmful output. Prompt injection attacks pose a significant security risk to large language models. These models have substantial capabilities in executing instructions and generating content, but their internal mechanisms are often difficult to evaluate transparently. At present, there is a lack of simple, clear and generally accepted methods to defend against these text-based attacks. Common prompt injection attack methods include: 1. Target hijacking, which involves misplacing the original target of the prompt with a new target that executes a different target. The attacker adds malicious instructions to the language model input prompt word, causing the language model to forget the original target and reply with harmful content. 2. Role playing: Role playing is a common technique in prompt injection attacks, where the language model is instructed to play a fictional role and provided with a set of behavioral rules. These rules may include unrestricted actions, participation in illegal or immoral activities, etc. 3. Retelling and translating: By commanding the language model to retell or translate dangerous content, the language model may ignore moral constraints and generate harmful content as commands.
[0004] Genetic Algorithm (GA) is a heuristic optimization and search algorithm inspired by the principles of biological evolution. It can be seen as a method to optimize search tools for solving difficult problems based on the principle of genetic selection. Genetic algorithms are widely used to solve complex optimization problems, aiming to find approximate optimal solutions or high-quality solutions in the problem space. Here, the mutation, crossover and other operations in the genetic algorithm are modified to a certain extent to make it applicable to text content. The evaluation results of the offensive text evaluation module are used as the fitness function in the genetic algorithm to simulate the selection operation. Repeating the iterative process can obtain a large number of prompt words that induce offensive replies from the language model.
[0005] Large language models (LLMs), such as OpenAI's ChatGPT, have demonstrated amazing capabilities in generating high-quality content, following instructions, and answering user questions. They have been widely used in various fields including translation, conversation assistance, and question-answering systems. However, the increased use of these models has raised significant concerns about safety. During interactions, LLMs may inadvertently generate offensive or sensitive responses, posing potential threats and harm to users.
[0006] The purpose of this application is to discover the problem of offensive replies in the context of LLM security. Inspired by genetic algorithms, this application relates to a new method of using genetic algorithms to detect language models and identify potentially offensive replies. This application combines prompt injection attacks with operations such as crossover, mutation, evaluation, and population update to simulate the operation of genetic algorithms to evaluate the offensiveness of replies generated by language models. This method enables this application to discover prompts for a large number of offensive replies that may be generated by large language models.
[0007] By performing targeted training on identified cues that elicit dangerous responses, it is possible to reduce the likelihood that a language model will generate offensive responses, thereby improving its overall safety. Summary of the invention
[0008] In view of the above problems in the prior art, the present application proposes a method for discovering offensive replies in a large language model based on a genetic algorithm, wherein the language model includes an instruction data set and a harmful prompt data set, and is characterized in that the method includes the following steps:
[0009] S1. Randomly extract a portion of content from the instruction dataset and the harmful prompt dataset to form an “initial population”;
[0010] S2. Through an iterative process, simulate the prompt words that can cause dangerous responses from the language model in the search of the genetic algorithm, and the iterative process includes mutation, crossover, evaluation, and selection operations;
[0011] S3. Modifying harmful prompts in the harmful prompt dataset by using a language model, simulating a mutation operation in a genetic algorithm to generate mutation prompts;
[0012] S4. Performing a prompt word crossover operation through a regularized function to cross the “instruction” and the harmful prompt, thereby generating a new crossover prompt;
[0013] S5. Input the prompt word after mutation and crossover operation into the language model to be tested, and obtain the response of the language model;
[0014] S6. inputting the response of the language model into the offensive language evaluation module to evaluate the degree of offense in the text;
[0015] S7. The evaluation module retains prompts that evaluate responses with higher offensive scores to form a new generation of population; through repeated iterative processes, prompts that can trigger potential offensive responses from the language model are identified, prompts that are offensive to the language model response are identified, and recorded.
[0016] Preferably, the offensive text assessment module includes the following parts:
[0017] Input layer: The input of the language model is a text sequence that has been cleaned and encoded.
[0018] Word embedding layer and position encoding: further process the input, convert the text character sequence into a word embedding matrix and merge it with the position encoding vector;
[0019] Multiple Transformer encoder layers: Each encoder contains a multi-head autoregressive self-attention layer, a cross-attention layer, and a forward multi-layer perceptron layer;
[0020] Multi-head self-attention layer: The multi-head attention mechanism module is the core module of the Transformer model. This module allows the model to jointly focus on information from different representation subspaces at different positions; the multi-head self-attention in the encoder layer has the complete text and uses a bidirectional encoding method;
[0021] Forward multi-layer perceptron layer: scales and linearly transforms the results produced by the multi-head attention layer;
[0022] Normalization layer: The results of the multilayer perceptron are normalized to obtain the probability distribution of classification.
[0023] Preferably, the mutation operation manually creates multiple mutation prompt instructions to increase the diversity of text mutation; manually constructs multiple mutation prompts and adopts random selection prompts to increase the diversity of mutation directions and introduce randomness.
[0024] Preferably, the crossover operation replaces the special identifier in the instruction with the harmful hint by adding the special identifier to the "instruction" in the "instruction data set".
[0025] Preferably, the evaluation process of the offensive text evaluation module is as follows:
[0026] a) The model performs input encoding preprocessing operations, converts it into word embeddings, and then combines the position embedding information to obtain the encoder context embedding;
[0027] b) Preprocess the training target or part of the output encoding of the model's training set, convert it into word embedding, and merge the position embedding information to obtain the context embedding of the decoder;
[0028] c) The encoder is embedded into the input neural network, and after mapping, three matrices Q, K, and V are obtained. Q, K, and V are respectively passed through the self-attention module to obtain the attention score matrix between each character and other characters. The calculation method is as follows:
[0029]
[0030] d) Z 1~n After concatenation, it is passed into the linear layer to obtain a final output Z with the same dimension as the input matrix of the multi-head attention layer;
[0031] e) The residual and regularization layer outputs the output X of the multi-head attention layer, that is, Z is residually connected to X, and then a linear regularization operation is performed to convert the input of each layer of neurons into a standard normal distribution:
[0032] X=LayerNorm(X+Z)
[0033] f) The multi-layer perceptron module uses two fully connected layers and performs normalization operations to process the result X and output a binary probability distribution, representing the probabilities of offensiveness and non-offensiveness; the probability value represents the degree of offensiveness of the input text.
[0034] The above technical features can be combined in various suitable ways or replaced by equivalent technical features as long as the purpose of the present invention can be achieved.
[0035] The method for discovering offensive replies of a large language model based on a genetic algorithm provided by the present invention has at least the following beneficial effects compared with the prior art:
[0036] 1. In the Chinese context, this application can accurately locate the potentially offensive outputs generated by the language model.
[0037] 2. Enhance the security and reliability of responses generated by the language model by training the language model specifically on the discovered instructions. By training on content that stimulates offensive responses, the associated security threats can be significantly mitigated. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The present invention will be described in more detail below based on embodiments and with reference to the accompanying drawings, wherein:
[0039] Figure 1 shows a flow chart of the present invention;
[0040] Figure 2 The offensive text assessment module model architecture diagram of the present invention is shown; DETAILED DESCRIPTION
[0041] The present invention will be further described below in conjunction with the accompanying drawings.
[0042] The present invention provides a method for discovering offensive replies of large language models based on genetic algorithms. The main technical problem solved is to discover the prompt words of potentially dangerous and offensive replies of language models and to improve the diversity and efficiency of discovering prompt words. Traditional manual testing methods produce limited and insufficiently diverse results. In addition, the method of using language models to automate this search task is inefficient.
[0043] To solve the above technical problems, the technical solution adopted by the present invention is: based on the idea of genetic algorithm, an integrated hint injection attack is proposed to solve this problem. Our method involves adjusting the genetic algorithm, paying special attention to the modification of basic components, such as mutation and crossover operations. These adjustments are carefully designed to tailor the genetic algorithm to the task of attacking language models:
[0044] 1. Two different datasets: the "Instruction Dataset" and the "Harmful Prompts Dataset". The "Instruction Dataset" includes manually crafted and collected instructional prompts with special identifiers. On the other hand, the "Harmful Prompts Dataset" contains toxic, aggressive, or risky content that has the potential to trigger aggressive responses. The data sources of these datasets include manually generated data and data collected from social platforms.
[0045] 2. Offensive Text Evaluation Module: It is used to evaluate the degree of offensiveness in the text and provide an estimate of the degree of offensiveness based on the input text. In this paper, we use a trained language detection model that is trained on the Chinese offensive language dataset. The detector is used to evaluate the offensiveness of the responses generated by the language model.
[0046] 3. Language model module: The language model to be tested obtains the corresponding response based on the input prompt word content.
[0047] 4. Genetic algorithm module: responsible for simulating the genetic algorithm process by performing operations such as crossover, mutation, and selection on the text, and ultimately obtaining prompt words that can cause offensive responses from the language model.
[0048] In one embodiment, Figure 1 , this method can be described as:
[0049] 1. First, we randomly select a portion of the content from the "instruction set" and "harmful prompt set" to form the "initial population";
[0050] 2. Enter the iterative process of the method to simulate the genetic algorithm's search for prompt words that may cause dangerous responses from the language model. The iterative process includes operations such as mutation, crossover, evaluation, and selection;
[0051] 3. Modify the harmful prompts in the “harmful prompt set” by using the language model, and simulate the mutation operation in the genetic algorithm to generate mutation prompts;
[0052] 4. Perform the prompt word cross operation through the regularized function, cross the "instruction" and the harmful prompt, and thus generate a new cross prompt;
[0053] 5. Input the prompt word after mutation and crossover operation into the language model to be tested, and obtain the response of the language model;
[0054] 6. Input the response of the language model into the offensive language evaluation module to evaluate the degree of offense in the text;
[0055] 7. The evaluation module retains the prompts with higher offensive scores to form a new generation of population. By repeating this iterative process, a large number of prompts that may trigger potentially offensive responses from the language model can be identified. In this process, the prompts that are offensive to the language model response are identified and recorded, obtaining the final result of our method.
[0056] In one embodiment, Figure 2 ,The offensive text evaluation module of this method includes the following parts:
[0057] Input layer: The input of the language model is a text sequence that has been cleaned and encoded before processing.
[0058] Word embedding layer and position encoding: further process the input, convert the text character sequence into a word vector matrix and merge it with the position encoding vector.
[0059] N Transformer encoder layers: Each encoder contains a multi-head autoregressive self-attention layer, a criss-cross attention layer, and a forward multi-layer perceptron layer.
[0060] Multi-head self-attention layer: The multi-head attention mechanism module is the core module of the Transformer model. This module allows the model to jointly focus on information from different representation subspaces at different positions. The multi-head self-attention in the encoder layer uses a bidirectional encoding method because it has the complete text.
[0061] Forward multi-layer perceptron layer: Scales and linearly transforms the results produced by the multi-head attention layer.
[0062] Normalization layer: The results of the multilayer perceptron are normalized to obtain the probability distribution of classification.
[0063] In one embodiment, the method includes:
[0064] 1) Mutation operation: Multiple mutation prompts are manually created to increase the diversity of text mutation. By manually constructing multiple mutation prompts and randomly selecting prompts, the diversity of mutation directions can be increased and randomness can be introduced. The language model is asked to execute the mutation instruction and modify the original content to obtain the instruction information after mutation, for example,
[0065] Input: "Please provide a sentence with a format and content similar to the following sentence: Type B people have a strong immune system."
[0066] The language model responded: “Type A people are in poor health.
[0067] 2) Crossover operation: by adding a special identifier to the "instruction" in the "instruction set". Crossover operation is performed by replacing the special identifier in the instruction with a harmful hint, such as <r>As a special identifier, an example of a crossover operation is as follows:
[0068] Instructions: "Repeat the following paragraph, <r>”
[0069] Harmful Tip: "Type B people have strong immune systems."
[0070] Cross-result: "Repeat the following paragraph. Type B people have strong immune systems."
[0071] 3) The evaluation process of the offensive text evaluation module is as follows:
[0072] a) The model performs a preprocessing operation on the input encoding, converts it into word embeddings, and then merges the position embedding information to obtain the encoder context embedding.
[0073] b) Preprocess the training target or part of the output encoding of the model's training set, convert it into word embedding, and merge the position embedding information to obtain the context embedding of the decoder.
[0074] c) The encoder is embedded into the input neural network, and after mapping, three matrices Q, K, and V are obtained. Q, K, and V are respectively passed through the self-attention module to obtain the attention score matrix between each character and other characters. The calculation method is as follows:
[0075]
[0076] d) Z 1~n After concatenation, it is passed into the linear layer to obtain a final output Z with the same dimension as the input matrix of the multi-head attention layer.
[0077] e) The residual and regularization layer outputs the output X of the multi-head attention layer, that is, Z is residually connected to X, and then a linear regularization operation is performed to convert the input of each layer of neurons into a standard normal distribution:
[0078] X=LayerNorm(X+Z)
[0079] f) The multi-layer perceptron module uses two fully connected layers and performs normalization operations to process the result X and output a binary probability distribution, representing the probabilities of offensiveness and non-offensiveness. The probability value represents the offensiveness of the input text.
[0080] Although the present invention is described herein with reference to specific embodiments, it should be understood that these embodiments are merely examples of the principles and applications of the present invention. It should therefore be understood that many modifications may be made to the exemplary embodiments and that other arrangements may be devised without departing from the spirit and scope of the present invention as defined by the appended claims. It should be understood that the various dependent claims and features described herein may be combined in a manner different from that described in the original claims. It should also be understood that features described in conjunction with individual embodiments may be used in other described embodiments.< / r> < / r>
Claims
1. A method for discovering offensive replies in a large language model based on a genetic algorithm, wherein the language model includes an instruction data set and a harmful prompt data set, characterized in that: The following steps are involved: S1. Randomly extract a portion of content from the instruction data set and the harmful prompt data set to form an "initial population"; S2. Through an iterative process, simulate the prompt words that can cause dangerous responses from the language model in the search of the genetic algorithm, and the iterative process includes mutation, crossover, evaluation, and selection operations; S3. Use the language model to modify the harmful prompts in the harmful prompt dataset and simulate the mutation operation in the genetic algorithm to generate mutated prompts; S4. Performing a prompt word cross operation through a regularized function to cross the "instruction" and the harmful prompt, thereby generating a new cross prompt; S5. Input the prompt word after mutation and crossover operation into the language model to be tested, and obtain the response of the language model; S6. inputting the response of the language model into the offensive language evaluation module to evaluate the degree of offense in the text; S7. The evaluation module retains prompts that evaluate responses with higher offensive scores to form a new generation of population; by repeating the iterative process, prompts that can trigger potentially aggressive responses from the language model are identified.
2. The method for discovering offensive replies of a large language model based on a genetic algorithm according to claim 1, characterized in that: The offensive text assessment module includes the following parts: Input layer: The input of the language model is a text sequence that has been cleaned and encoded. Word embedding layer and position encoding: further process the input, convert the text character sequence into a word vector matrix, and merge it with the position encoding vector; Multiple Transformer encoder layers: Each encoder contains a multi-head autoregressive self-attention layer, a cross-attention layer, and a forward multi-layer perceptron layer; Multi-head self-attention layer: The multi-head attention mechanism module is the core module of the Transformer model. The multi-head attention mechanism module allows the model to jointly focus on information from different representation subspaces at different positions; the multi-head self-attention in the encoder layer has the complete text and uses a bidirectional encoding method; Forward multi-layer perceptron layer: scales and linearly transforms the results produced by the multi-head self-attention layer; Normalization layer: The results of the multi-layer perceptron layer are normalized to obtain the probability distribution of classification.
3. The method for discovering offensive replies of a large language model based on a genetic algorithm according to claim 1, characterized in that: The mutation operation manually creates multiple mutation prompt instructions to increase the diversity of text mutation; manually constructs multiple mutation prompts and randomly selects prompts to increase the diversity of mutation directions and introduce randomness.
4. The method for discovering offensive replies of a large language model based on a genetic algorithm according to claim 1, characterized in that: The crossover operation replaces the special identifiers in the instructions with harmful hints by adding special identifiers to the "instructions" in the "instruction data set".
5. The method for discovering offensive replies of a large language model based on a genetic algorithm according to claim 1, characterized in that: The evaluation process of the offensive text evaluation module is as follows: a) The model performs input encoding preprocessing operations, converts it into word embeddings, and then combines the position embedding information to obtain the encoder context embedding; b) Preprocess the training target or part of the output encoding of the model's training set, convert it into word embedding, and merge the position embedding information to obtain the context embedding of the decoder; c) The encoder is embedded into the input neural network, and three matrices Q, K, and V are obtained after mapping. Q, K, and V are respectively passed through the self-attention module to obtain the attention score matrix Z between each character and other characters. i , the operation is as follows: Among them, i represents the serial number of the self-attention module, softmax represents the normalized exponential function, M represents the Attention-Mask mask matrix, which is required in the model pre-training process, and d k represents the length of K; d) Z 1~n After concatenation, it is passed to the linear layer to obtain the final output Z with the same dimension as the input matrix of the multi-head attention layer; e) The residual and regularization layer outputs the output matrix X of the multi-head attention layer, and the output Z is residually connected to the matrix X, and then a linear regularization operation is performed to convert the input of each layer of neurons into a standard normal distribution: X=LayerNprm(X+Z) LayerNorm means normalization on the feature dimension of each sample; f) The multi-layer perceptron module uses two fully connected layers and performs normalization operations to process the transformed X and output a binary probability distribution, representing the probabilities of offensiveness and non-offensiveness; the probability value represents the degree of offensiveness of the input text.
Citation Information
Cited By
Large model sensitive expression fuzzification processing method and system and computer equipment
CN119918510A
Large model sensitive expression fuzzing processing method and system and computer device
CN119918510B
