Semantic similarity-based large language model control type Token initialization and fine adjustment method and device
Through semantic similarity initialization and synthetic data set fine-tuning, the problem of insufficient understanding of control tokens by large language models is solved, the output stability and security are improved, the ability to distinguish control tokens from ordinary words is enhanced, and prompts are resisted Injection attack.
Patent Information
- Application Number
- CN202410576613.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-10
- Publication Date
- 2024-08-30
AI Technical Summary
Existing large language models use random vectors when initializing control tokens, making it difficult for the model to understand the semantics of control tokens. The output is unstable and vulnerable to prompt injection attacks. It lacks the ability to distinguish control tokens from ordinary words.
By introducing semantically similar English word Embedding, the vocabulary is expanded, and a synthetic data set is constructed, including ordinary words, control tokens and mixed corpus, and fine-tuning is performed to enhance the model's ability to understand and distinguish control tokens.
It improves the model's semantic understanding of control tokens, improves output stability and security, reduces the risk of prompt injection attacks, and enhances the security of applications in vertical fields.
Smart Images

Figure CN118569268A
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of artificial intelligence technology, and in particular relates to a large language model controlled Token initialization and fine-tuning method and device based on semantic similarity. Background Art
[0002] Terminology explanation:
[0003] Word Embedding: A method of mapping discrete variables such as words into continuous vector representations. It is often used to convert words into dense vectors for input into neural networks.
[0004] Vocabulary: The set of all words or symbols that the model can recognize and use when training and generating text. These words or symbols include the vocabulary necessary for the model to understand and generate language, and are usually extracted and optimized from large amounts of text data. The size and composition of the vocabulary can directly affect the performance of the model, including its ability to understand and generate text.
[0005] Large Language Model (LLM) is a deep neural network model trained on massive text data that can understand and generate natural language. In the practical application of specific tasks such as document summarization, question-answering system, sentiment analysis, etc. in special fields such as customer service and finance, it is usually necessary to fine-tune the large language model for specific vertical fields based on the pre-trained general LLM to complete tasks such as dialogue, information extraction, and text generation.
[0006] In order to control the behavior of LLM in the task, it is usually necessary to introduce a control token and insert it into the input text to instruct the model to perform specific operations or change the state. For example, in OpenAI's GPT-3.5-Turbo model, "<|endoftext|>" is used to indicate the end of output, "<|im_start|>" is used to indicate the start of a conversation, and "<|im_end|>" is used to indicate the end of a conversation.
[0007] In the prior art, these control tokens are usually randomly initialized to generate their initial embedding representations, and then learn how to use them in downstream task fine-tuning. This is a common practice in the pre-training stage of large language models, that is, using random vectors to initialize the embeddings of all tokens, and then learning the semantic representation of words during the pre-training process. However, this method has the following shortcomings:
[0008] (1) Since control tokens are artificially defined and are not part of any natural language, natural language corpora will not contain references or explanations of artificially defined control tokens. As a result, the model may only understand the usage rules and format of control tokens but not their semantics.
[0009] (2) There is a lack of reasonable semantic association between control tokens and concepts in natural language, which leads to unstable model output.
[0010] (3) The control tokens are not distinguished from the specific meanings of ordinary words, which makes them vulnerable to prompt injection attacks. Prompt injection attacks are a type of attack on language models. Malicious users can cause the server-side model to bypass restrictions and output unexpected results by carefully constructing input prompts. Summary of the invention
[0011] In view of the above, the purpose of the present invention is to provide a method and device for initializing and fine-tuning controlled tokens of a large language model based on semantic similarity, by introducing embeddings of English words semantically related to controlled tokens and constructing synthetic data sets to enhance the model's ability to distinguish different token types, so as to improve the model's understanding and application of controlled tokens and enhance output stability and security.
[0012] To achieve the above-mentioned purpose of the invention, the embodiment provides a large language model controlled token initialization and fine-tuning method based on semantic similarity, comprising the following steps:
[0013] The vocabulary of the control token is expanded based on the pre-trained large language model to obtain an expanded vocabulary;
[0014] Get the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the corresponding embedding matrix of the expanded vocabulary to initialize the embedding of the control token;
[0015] Construct a synthetic dataset containing three types of corpora, including corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. Mix the three types of corpora and randomly shuffle them.
[0016] The processed synthetic dataset is used to fine-tune the pre-trained large language model so that the model can distinguish between control tokens and ordinary words and understand the meaning of control tokens.
[0017] Preferably, the control token includes but is not limited to at least one of the following tokens:
[0018] Token used to identify the dialogue role;
[0019] Token used to identify the function of the question-answering task text block;
[0020] Token used to identify programming language code;
[0021] And a token used to identify the text generation task.
[0022] Preferably, the pre-trained large language model includes but is not limited to at least one of the following models:
[0023] Language model based on Transformer structure;
[0024] Pre-trained models based on text datasets;
[0025] And pre-trained models based on domain-specific text.
[0026] Preferably, the method further comprises:
[0027] Get the English word token sequence corresponding to each control token, and use the average value of the embedding sequence of the English word token sequence in the corresponding embedding matrix of the expanded vocabulary to initialize the embedding of the control token.
[0028] Preferably, the method further comprises:
[0029] The fine-tuned large language model is used to fine-tune the downstream vertical domain task data to obtain a task-based model that meets the vertical domain tasks.
[0030] To achieve the above-mentioned purpose of the invention, the embodiment further provides a large language model controlled token initialization and fine-tuning system based on semantic similarity, including:
[0031] The vocabulary expansion module is used to expand the vocabulary by adding control tokens based on the pre-trained large language model to obtain an expanded vocabulary;
[0032] The Embedding initialization module is used to obtain the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the Embedding matrix corresponding to the expanded vocabulary to initialize the embedding of the control token;
[0033] A synthetic data generation module is used to construct a synthetic data set containing three types of corpora, wherein the three types of corpora include corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. The three types of corpora are mixed and randomly shuffled.
[0034] The model fine-tuning module is used to fine-tune the pre-trained large language model using the processed synthetic dataset, so that the model can distinguish between control tokens and ordinary words and understand the meaning of control tokens.
[0035] Preferably, the system further comprises:
[0036] The downstream task adaptation module is used to use the fine-tuned large language model to fine-tune the downstream vertical domain task data to obtain a task-based model that meets the vertical domain tasks.
[0037] To achieve the above-mentioned purpose of the invention, an embodiment further provides a computing device, including a memory and one or more processors, wherein the memory stores executable code, and when the one or more processors execute the executable code, they are used to implement the above-mentioned large language model controlled Token initialization and fine-tuning method.
[0038] To achieve the above-mentioned purpose of the invention, the embodiment further provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the above-mentioned large language model controlled Token initialization and fine-tuning method is implemented.
[0039] To achieve the above-mentioned purpose of the invention, an embodiment further provides a computer product, which includes a computer program. When the computer program is executed by a processor, the above-mentioned large language model controlled Token initialization and fine-tuning method is implemented.
[0040] Compared with the prior art, the present invention has the following beneficial effects:
[0041] (1) Through semantically relevant initialization, the control token has reasonable semantic attributes and the model’s understanding ability is enhanced.
[0042] (2) Through fine-tuning of the synthetic dataset, the model’s ability to distinguish different token types is improved, the misuse rate of control tokens is reduced, and the output stability is improved.
[0043] (3) It distinguishes between controlled tokens and ordinary words, enhances the model's ability to resist prompt injection attacks, and improves the security of vertical field applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0045] Figure 1 It is a flow chart of a large language model controlled Token initialization and fine-tuning method based on semantic similarity provided in an embodiment;
[0046] Figure 2 It is a structural diagram of a large language model controlled Token initialization and fine-tuning system based on semantic similarity provided in an embodiment;
[0047] Figure 3 It is a schematic diagram of the structure of a computing device provided in an embodiment. DETAILED DESCRIPTION
[0048] To make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific implementation methods described herein are only used to explain the present invention and do not limit the scope of protection of the present invention.
[0049] The technical problems to be solved by the present invention include: (1) How to make the control token have reasonable semantic attributes and enhance the model's ability to understand and use it correctly; (2) How to improve the stability of the model output and reduce the failure caused by the misuse of the control token; (3) How to enhance the model's ability to resist prompt injection attacks and improve the security of vertical field applications. The vertical field refers to specific downstream fields such as customer service and finance, and the vertical field tasks refer to tasks such as document summarization, question-answering system, sentiment analysis, code generation, and code interpretation in the vertical field.
[0050] To solve the above technical problems, an embodiment of the present invention provides a large language model controlled Token initialization and fine-tuning method based on semantic similarity, such as Figure 1 As shown, the following steps are included:
[0051] S1, vocabulary expansion.
[0052] In the embodiment, the vocabulary is expanded based on the pre-trained large language model, and special control tokens are added to the vocabulary to obtain an expanded vocabulary. The control tokens include but are not limited to one or more of the following:
[0053] (1) Tokens used to identify dialogue roles, such as <|user|>, <|assistant|>, etc., which are used to identify user input sentences and system response sentences respectively.
[0054] (2) Tokens used to identify the function of the text block of the question-answering task, such as <|question|>, <|answer|>, <|context|>, etc., which are used to identify the question sentence, answer sentence, and text sentence respectively.
[0055] (3) Tokens used to identify programming language codes, such as <|code|>, <|natural|>, <|comment|>, etc., which are used to identify code blocks, natural language interpretations, and code comments.
[0056] (4) Tokens used to identify text generation tasks, such as <|title|>, <|story|>, <|summarize|>, etc., which are used to identify text titles, text contents, and text summaries.
[0057] In an embodiment, the pre-trained large language model includes but is not limited to one or more of the following:
[0058] (1) Language models based on the Transformer structure, such as the GPT series and BERT series:
[0059] (2) Pre-trained models based on programming languages, such as CodeGen, CodeBERT, etc.
[0060] (3) Pre-trained models based on domain-specific text, such as BioBERT, SciBERT, etc.
[0061] S2, Embedding initialization.
[0062] In the embodiment, the Embedding (embedding vector) of the English word corresponding to the control token is used to initialize the Embedding of the control token based on semantic similarity, specifically including:
[0063] Get the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the corresponding embedding matrix of the expanded vocabulary to initialize the embedding of the control token.
[0064] In another possible implementation, when initializing the Embedding of the control token, the semantically related English word token sequence corresponding to each control token can also be obtained, and the Embedding of the control token can be initialized using the average value of the Embedding sequence of the English word token sequence in the Embedding matrix corresponding to the expanded vocabulary. That is, when the control token semantically corresponds to multiple English words, the Embedding of the control token can be initialized using the average value of the Embeddings of the corresponding multiple English words.
[0065] S3, construct synthetic dataset.
[0066] In the embodiment, the constructed synthetic data set includes three types of corpora, namely: corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. The three types of corpora are then mixed in an adjustable ratio, and then the corpora are randomly shuffled. The processed synthetic data set is used for fine-tuning the large language model to optimize the performance of the model.
[0067] S4, large language model fine-tuning.
[0068] In the embodiment, the pre-trained large language model is fine-tuned using the processed synthetic dataset. During fine-tuning, training samples are generated from the processed synthetic dataset by random sampling to enhance data diversity. Such training enables the model to distinguish between control tokens and ordinary words and understand the meaning of control tokens.
[0069] S5, downstream task adaptation.
[0070] In the embodiment, on the basis of S4, a fine-tuned large language model is also used to perform fine-tuning on the downstream vertical field task data, and the Embedding of the control token is fine-tuned to obtain a task-based model that meets the vertical field tasks.
[0071] The embodiments also provide specific examples based on the above methods.
[0072] Example 1
[0073] For a task-based dialogue system, two control tokens, <|user|> and <|assistant|>, are introduced to identify user input sentences and system response sentences respectively.
[0074] Step 1: Load the pre-trained Llama3 model and Embedding matrix, expand the vocabulary, and add <|user|> and <|assistant|>.
[0075] Step 2: Get the Embedding vectors of the two English words user and assistant in the Llama3 vocabulary.
[0076] Step 3: Use the embedding vectors of user and assistant to initialize the embeddings of <|user|> and <|assistant|> respectively.
[0077] Step 4: Construct a synthetic dataset, including 10,000 sentences with only common words, 10,000 sentences with only control tokens, and 10,000 sentences mixed with both tokens, a total of 30,000 sentences, which are randomly shuffled.
[0078] Step 5: Fine-tune the model for 3 epochs on Llama3 using the synthetic dataset, with the learning rate set to 1e-5 and the optimizer using Adam.
[0079] Step 6: Fine-tune the model in step 5 on 50,000 task-based dialogue data to obtain a customized dialogue model. Compared with the original Llama3, this model can better understand the role switching in the dialogue and generate more coherent and stable dialogues. At the same time, it improves the ability to resist malicious prompt injection.
[0080] Example 2
[0081] In the code generation task, <|code|> and <|natural|> are introduced to identify code blocks and natural language interpretations.
[0082] Steps 1 to 4 are similar to Example 1. They initialize the embeddings of <|code|> and <|natural|> respectively and construct 10,000 synthetic data.
[0083] Step 5: Fine-tune the CodeLlama2 model for 3 epochs using the synthetic dataset, with a learning rate of 1e-5 and optimizer AdamW.
[0084] Step 6: Fine-tune the model on the Python code intelligent generation dataset to generate corresponding code based on natural language descriptions. Compared with the original CodeLlama2, the task completion capability is more in line with the requirements and more robust.
[0085] In the above method, the control token has reasonable semantic properties through semantically related embedding initialization, which can enhance the model's semantic understanding ability of the control token compared with random embedding initialization; through fine-tuning of synthetic data sets, the model's ability to distinguish different token types is improved, the probability of control tokens and misuse is reduced, and the output stability is improved; the control token and ordinary word token are distinguished, the model's ability to resist prompt injection attacks is enhanced, and the security of vertical field applications is improved.
[0086] like Figure 2 As shown, the embodiment also provides a large language model control token initialization and fine-tuning system 20 based on semantic similarity, including a vocabulary expansion module 21, which is used to expand the vocabulary of the control token based on the pre-trained large language model to obtain an expanded vocabulary; an embedding initialization module 22, which is used to obtain the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the embedding matrix corresponding to the expanded vocabulary to initialize the embedding of the control token; a synthetic data set construction module 23, which is used to construct a synthetic data set containing A synthetic dataset of three types of corpora, wherein the three types of corpora include corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. The three types of corpora are mixed and randomly shuffled; a model fine-tuning module 24, which is used to use the processed synthetic dataset to fine-tune the pre-trained large language model so that the model can distinguish between control tokens and common words and understand the meaning of control tokens; a downstream task adaptation module 25, which is used to use the fine-tuned large language model to fine-tune the downstream vertical domain task data to obtain a task-based model that meets the vertical domain tasks.
[0087] It should be noted that the large language model-controlled Token initialization and fine-tuning system based on semantic similarity provided in the above-mentioned embodiment should be illustrated by the division of the above-mentioned functional modules when performing large language model-controlled Token initialization and fine-tuning. The above-mentioned function allocation can be completed by different functional modules as needed, that is, the internal structure of the terminal or server is divided into different functional modules to complete all or part of the functions described above. In addition, the large language model-controlled Token initialization and fine-tuning system based on semantic similarity provided in the above-mentioned embodiment belongs to the same concept as the large language model-controlled Token initialization and fine-tuning construction method embodiment based on semantic similarity. The specific implementation process is detailed in the large language model-controlled Token initialization and fine-tuning method embodiment based on semantic similarity, which will not be repeated here.
[0088] Based on the same inventive concept, an embodiment further provides a computing device, including a memory and one or more processors, wherein executable code is stored in the memory, and when the one or more processors execute the executable code, the method for initializing and fine-tuning a large language model controlled token based on semantic similarity is implemented, specifically including the following steps:
[0089] S1, vocabulary expansion;
[0090] S2, Embedding initialization;
[0091] S3, construct synthetic dataset;
[0092] S4, large language model fine-tuning;
[0093] S5, downstream task adaptation.
[0094] like Figure 3 As shown, the computing device provided in the embodiment, at the hardware level, in addition to the processor and memory, also includes the hardware required for other services such as internal bus, network interface, memory, etc. The memory is a non-volatile memory, and the processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to implement the large language model controlled token initialization and fine-tuning method based on semantic similarity described in S1-S5 above. Of course, in addition to the software implementation method, the present invention does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc., that is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0095] Based on the same inventive concept, the embodiment further provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the above-mentioned large language model controlled token initialization and fine-tuning method based on semantic similarity is implemented, which specifically includes the following steps:
[0096] S1, vocabulary expansion;
[0097] S2, Embedding initialization;
[0098] S3, construct synthetic dataset;
[0099] S4, large language model fine-tuning;
[0100] S5, downstream task adaptation.
[0101] Based on the same inventive concept, an embodiment further provides a computer product, which includes a computer program. When the computer program is executed by a processor, the large language model controlled token initialization and fine-tuning method is implemented, including the following steps:
[0102] S1, vocabulary expansion;
[0103] S2, Embedding initialization;
[0104] S3, construct synthetic dataset;
[0105] S4, large language model fine-tuning;
[0106] S5, downstream task adaptation.
[0107] The specific implementation methods described above provide a detailed description of the technical solutions and beneficial effects of the present invention. It should be understood that the above is only the most preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, supplements and equivalent substitutions made within the scope of the principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A large language model controlled token initialization and fine-tuning method based on semantic similarity, characterized in that: The following steps are involved: The vocabulary of the control token is expanded based on the pre-trained large language model to obtain an expanded vocabulary; Get the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the corresponding embedding matrix of the expanded vocabulary to initialize the embedding of the control token; Construct a synthetic dataset containing three types of corpora, including corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. Mix the three types of corpora and randomly shuffle them. The processed synthetic dataset is used to fine-tune the pre-trained large language model so that the model can distinguish between control tokens and ordinary words and understand the meaning of control tokens.
2. The method for initializing and fine-tuning a large language model controlled token based on semantic similarity according to claim 1, characterized in that: The control token includes at least one of the following tokens: Token used to identify the dialogue role; Token used to identify the function of the question-answering task text block; Token used to identify programming language code; And a token used to identify the text generation task.
3. The method for initializing and fine-tuning a large language model controlled token based on semantic similarity according to claim 1, characterized in that: The pre-trained large language model includes at least one of the following models: Language model based on Transformer structure; Pre-trained models based on text datasets; And pre-trained models based on domain-specific text.
4. The method for initializing and fine-tuning a large language model controlled token based on semantic similarity according to claim 1, characterized in that: Also includes: Get the semantically related English word token sequence corresponding to each control token, and use the average value of the embedding sequence of the English word token sequence in the corresponding embedding matrix of the expanded vocabulary to initialize the embedding of the control token.
5. The method for initializing and fine-tuning a large language model controlled token based on semantic similarity according to claim 1, characterized in that: Also includes: The fine-tuned large language model is used to fine-tune the downstream vertical domain task data to obtain a task-based model that meets the vertical domain tasks.
6. A large language model controlled token initialization and fine-tuning system based on semantic similarity, characterized in that: include: The vocabulary expansion module is used to expand the vocabulary by adding control tokens based on the pre-trained large language model to obtain an expanded vocabulary; The Embedding initialization module is used to obtain the semantically similar English word token corresponding to each control token, and use the embedding of the English word token in the Embedding matrix corresponding to the expanded vocabulary to initialize the embedding of the control token; A synthetic dataset construction module is used to construct a synthetic dataset containing three types of corpora, wherein the three types of corpora include corpora containing only common words, corpora containing only control tokens, and corpora containing both common words and control tokens. The three types of corpora are mixed and randomly shuffled. The model fine-tuning module is used to fine-tune the pre-trained large language model using the processed synthetic dataset, so that the model can distinguish between control tokens and ordinary words and understand the meaning of control tokens.
7. The large language model controlled token initialization and fine-tuning system based on semantic similarity according to claim 6 is characterized in that: Also includes: The downstream task adaptation module is used to use the fine-tuned large language model to fine-tune the downstream vertical domain task data to obtain a task-based model that meets the vertical domain tasks.
8. A computing device comprising a memory and one or more processors, wherein the memory stores executable code, characterized in that: When the one or more processors execute the executable code, they are used to implement the large language model controlled Token initialization and fine-tuning method described in any one of claims 1-5.
9. A computer-readable storage medium, characterized in that: A program is stored thereon, and when the program is executed by a processor, the large language model controlled Token initialization and fine-tuning method described in any one of claims 1-7 is implemented.
10. A computer product comprising a computer program, characterized in that When the computer program is executed by a processor, the large language model controlled Token initialization and fine-tuning method described in any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Large model fine tuning method and system for increasing proportion of key information and application
CN119090018A
A large model fine-tuning method, system and application for increasing the proportion of key information
CN119090018B
Method and software for optimizing big language model training on computing power cloud
CN119670848A
Method and software for training large language model on computing power cloud
CN119670848B
