A method and system for generating large model instruction dataset based on knowledge graph
By building knowledge seeds in the knowledge graph and using preset large language models to generate instruction data, the problem of large models creating hallucinations in the fine-tuning of domain data is solved, high-quality fine-tuning data generation is achieved, and the performance of the model in specific fields is improved. .
Patent Information
- Application Number
- CN202411126004.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-16
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-08-16
AI Technical Summary
When using field data to fine-tune the large model, the model itself mines the relationship between the entity and knowledge, and it is easy to have hallucinations, resulting in incorrect instruction data.
Using a knowledge graph-based method, we construct knowledge seeds including anchor entities, positive sample entities and negative sample entities, generate instruction data through preset large language models, and fine-tune the open source large language model through comparative learning to reduce the model's own illusion The impact of
有效区分紧密关系的正确知识与错误知识,提高了指令数据的质量,减少了模型自身幻觉的影响,提高了大模型在特定领域的表现。
Smart Images

Figure CN118643171B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of artificial intelligence, and specifically relates to a method and system for generating a large model instruction data set based on a knowledge graph. Background Art
[0002] In recent years, large models based on deep learning have made significant progress and demonstrated powerful language understanding and generation capabilities. However, for applications in specific vertical fields, general large models often cannot meet actual needs. Therefore, fine-tuning data for specific fields has become a common method to improve the performance of large models in vertical fields.
[0003] Currently, a common method for fine-tuning large models using domain data is to generate instruction data from advanced black box models such as ChatGPT, and then use these instruction data to fine-tune open source large models. This method can generate high-quality instruction data at a lower cost, thereby building a large model that performs well in a specific field.
[0004] For example, the Chinese patent application number CN202311795841.1 proposes a method for generating instruction fine-tuning data, and its specific steps are as follows: S1, obtain the first knowledge base; S2, use the fragments in the first knowledge base to extract multiple entities; S3, use the above entity and background knowledge to generate the first instruction fine-tuning data; S4, determine whether the second instruction fine-tuning data exceeds the average value, if so, return to step 3, otherwise enter step 5; S5, process the first instruction fine-tuning data to generate the second instruction fine-tuning data. This method extracts entities from texts and generates instruction data based on entities and related texts, thereby improving the quality of data generation. However, the above generation method mines the relationship between entities and related knowledge by the large model itself and generates instruction data, which is still affected by the illusion of the model itself and generates incorrect instructions. Summary of the invention
[0005] Based on the above-mentioned shortcomings and deficiencies in the prior art, one of the objects of the present invention is to at least solve one or more of the above-mentioned problems in the prior art. In other words, one of the objects of the present invention is to provide a method and device for generating a large model instruction data set based on a knowledge graph that meets one or more of the above-mentioned needs.
[0006] In order to achieve the above-mentioned object of the invention, the present invention adopts the following technical solutions:
[0007] A method and system for generating a large model instruction data set based on a knowledge graph, comprising the steps of:
[0008] S1. Generate knowledge seeds based on the domain knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1. In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and each element has the same relationship with the anchor entity, and this same relationship is recorded as a relationship. Each element in the negative sample entity set 1 is a negative sample entity, and each element can be connected to the anchor entity through several hops in the knowledge graph, and each hop is a relationship. Each knowledge seed also includes a relationship.
[0009] S2. Generate positive sample instruction data using a preset large language model according to the knowledge seed;
[0010] S3, based on the positive sample instruction data, using the negative sample entity set 1 in the knowledge seed, to generate negative sample instruction data;
[0011] S4, combining the obtained positive sample instruction data and negative sample instruction data to obtain a fine-tuning instruction data set;
[0012] S5. Based on the fine-tuning instruction dataset, fine-tune the open source large language model through contrastive learning.
[0013] As a preferred solution, step S1 specifically includes:
[0014] S1.1 Obtain a domain knowledge graph, obtain all entities in the domain knowledge graph, and use any entity obtained as an anchor entity ;
[0015] S1.2 According to the anchor entity , obtain all first-order neighbor entities of the entity and their relationships, merge the first-order neighbor entities with the same relationship, and obtain the second positive sample entity set, which is recorded as:
[0016] ,
[0017] in Represents an anchor entity No. The relationship First-order neighbor entities;
[0018] S1.3 For each of the above positive sample entity set 2 , for each positive sample neighbor entity , select the distance anchor point entity among its multi-order neighbor entities The shortest path length is between 2 and d and the relationship between each hop on the path is The neighbor entities of are taken as negative sample entities, and the negative sample entity set 2 is obtained, which is recorded as:
[0019] ;
[0020] Wherein, d is a positive integer greater than 2;
[0021] S1.4 Obtain i knowledge seeds based on the anchor entity, positive sample entity set 2 and negative sample entity set 2 ,in, is the positive sample entity set one, and is the negative sample entity set one.
[0022] As a preferred solution, step S2 specifically includes:
[0023] According to the knowledge seed , construct prompt words, requiring the preset large language model to be based on Generate positive sample instruction data ;in, are c positive sample entities randomly selected from the positive sample entity set 1, where c is a preset positive integer not greater than j;
[0024] It is also required that in the instruction data generated by the preset large language model, Contains anchor entities and relationship information, Include The information in the sample is used to obtain the positive sample instruction data. .
[0025] As a preferred solution, step S3 includes:
[0026] From the seeds of knowledge The negative sample entity set in In the example, c negative sample entities are randomly selected and recorded as , use c of the negative sample entities to replace the positive sample instruction data In c positive sample entities in the , and get the negative sample instruction data .
[0027] As a preferred solution, in step S2, the preset large language model is any one of GPT-4, ChatGLM, LLaMA3-70B, Claude3, and ChatGLM3.
[0028] As a preferred solution, in step S5, the open source large language model is any one of LLaMA-2, Baichuan-2, InternLM, etc.
[0029] To achieve the above objectives, the present invention provides a large model instruction data generation system based on knowledge graph, which specifically includes:
[0030] The knowledge seed generation module is used to generate knowledge seeds according to the domain knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1;
[0031] In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and the relationship between each element and the anchor entity are the same, and this same relationship is recorded as relationship ; Each element in the negative sample entity set 1 is a negative sample entity. Each element in the knowledge graph can be connected to the anchor entity through several jumps, and the relationship represented by each jump is a relationship ;
[0032] Each knowledge seed also includes the relationship ;
[0033] The instruction data generation module is used to: construct a prompt word according to the knowledge seed, and generate positive sample instruction data according to the prompt word using a preset large language model; based on the positive sample instruction data, generate negative sample instruction data using a negative sample entity set 1 in the knowledge seed; and merge the obtained positive sample instruction data and negative sample instruction data to obtain a fine-tuning instruction data set;
[0034] The model fine-tuning module is used to: fine-tune the open source large language model through contrastive learning based on the fine-tuning instruction dataset.
[0035] Compared with the prior art, the present invention has the following beneficial effects:
[0036] The method of the present invention utilizes the distance relationship between entities on the graph to construct a knowledge seed including anchor entities, positive and negative sample entities and relationships, which can distinguish correct knowledge from incorrect knowledge in entities with relatively close relationships, thereby reducing the illusion of the model itself; and then utilizes the preset large language model to generate instruction data, thereby integrating the powerful capabilities of the preset large language model into the instruction data, and efficiently and low-costly obtaining high-quality fine-tuning data containing relevant knowledge. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 It is a flow chart of a method and system for generating a large model instruction data set based on a knowledge graph according to an embodiment of the present invention; DETAILED DESCRIPTION
[0038] In order to more clearly illustrate the embodiments of the present invention, the specific implementation methods of the present invention will be described below with reference to the accompanying drawings. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those of ordinary skill in the art, other accompanying drawings and other implementation methods can be obtained based on these accompanying drawings without creative work.
[0039] Embodiment 1:
[0040] This embodiment provides a method and system for generating a large model instruction data set based on a knowledge graph, and its flow chart is as follows: Figure 1 As shown, in this embodiment, the medical field knowledge graph is used as an example to generate an instruction data set.
[0041] In this embodiment, the medical knowledge graph DiseaseKG is selected as an example. The knowledge graph originates from the OpenKG open source project. The knowledge graph mainly includes 8 types of entities with a total of 44,656 data items and 11 types of relationships with a total of 312,159 data items.
[0042] Generate knowledge seeds based on the domain knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1. In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and the relationship between each element and the anchor entity are the same, and this same relationship is recorded as relationship ; Each element in the negative sample entity set 1 is a negative sample entity. Each element in the knowledge graph can be connected to the anchor entity through several hops, and each hop is a relationship ; Each knowledge seed also includes the relationship ;
[0043] Specifically include:
[0044] S1.1 Obtain a domain knowledge graph, obtain all entities in the domain knowledge graph, and use any entity obtained as an anchor entity ;
[0045] S1.2 According to the anchor entity , obtain all first-order neighbor entities of the entity and their relationships, merge the first-order neighbor entities with the same relationship, and obtain the second positive sample entity set:
[0046] ,
[0047] in Represents an anchor entity No. The relationship First-order neighbor entities;
[0048] For example, in this embodiment, the entity whooping cough is selected as the anchor entity, and the positive sample entity set 2 ((symptoms, chest tightness, lung yin deficiency, convulsion, low fever, convulsion, ...), (drugs, Baikejing syrup, andrographolide tablets, erythromycin ethylsuccinate tablets...), ...) can be obtained.
[0049] S1.3 For each of the above positive sample entity set 2 , for each positive sample neighbor entity , select the distance anchor point entity among its multi-order neighbor entities The shortest path length is between 2 and d and the relationship between each hop on the path is The neighbor entities of are taken as negative sample entities, and the negative sample entity set 2 is obtained, which is recorded as:
[0050] ;
[0051] Wherein, d is a positive integer greater than 2;
[0052] In this embodiment, the distance anchor point entity is selected The shortest path length is between 2 and 3 and the relationships on the path are The neighbor entities of the anchor entity are taken as negative sample entities. The way to obtain the negative sample entities that meet the requirements is to use the breadth-first algorithm to obtain the second-order and third-order neighbor entities of the anchor entity, obtain the first-order and second-order neighbor entities of the positive sample entity, take the intersection of the second-order neighbor entities of the anchor entity and the first-order neighbor entities of the positive sample entity, and take the intersection of the third-order neighbor entities of the anchor entity and the second-order neighbor entities of the positive sample entity to obtain the negative sample entity set. Through the above steps, a negative sample entity set (symptoms, shock, urticaria, barrel chest, respiratory failure, cerebral hypoxia, ...) can be obtained.
[0053] As an improvement to the above solution, the number, distance and sampling strategy of negative samples can also be adjusted according to the actual usage scenario.
[0054] S1.4 Obtain i knowledge seeds based on the anchor entity, positive sample entity set 2 and negative sample entity set 2 ,in, is the set of positive sample entities. is the negative sample entity set one.
[0055] For example, for the anchor entity whooping cough, the relationship symptom can obtain a knowledge seed (whooping cough, symptoms, (chest tightness, lung yin deficiency, convulsions, low fever, convulsions, ...), (shock, urticaria, barrel chest, respiratory failure, cerebral hypoxia, ...)).
[0056] In text materials, negative entities and positive entities sometimes appear in context, causing hallucinations in the model. For example, shock often appears together with convulsions and chest tightness, which are symptoms of whooping cough but not shock. This construction method can effectively reduce the impact of difficult negative samples, thereby reducing intrinsic hallucinations.
[0057] Then, step S2 is performed to generate positive sample instruction data, which specifically includes:
[0058] According to the knowledge seed , construct prompt words, requiring the preset large language model to be based on Generate positive sample instruction data ;in, is the positive sample entity set 1 c positive sample entities randomly selected from , where c is a preset positive integer not greater than j;
[0059] It is also required that in the instruction data generated by the preset large language model, Contains anchor entities and relationship information, Include The information in the sample is used to obtain the positive sample instruction data. .
[0060] In this embodiment, different task templates are set for scenarios such as medical consultation, common sense reasoning, writing, and classification. For example, for the medical consultation scenario, the template used is as follows:
[0061] Generate according to the scene and given entities and relationships ( <input> , <output>), the entity and relationship are ( , , ) Now it is a hospital consultation scene, the patient's questions need to include , , the doctor's answer needs to include The question and answer pairs you generate are required to be in json format. Try not to appear <input> middle.
[0062] in( , , ) Fill in the corresponding real value when using it, for example For whooping cough, For symptoms, These are three randomly selected positive sample entities (convulsion, chest tightness, and convulsion).
[0063] In this embodiment, the generated prompt words are input into ChatGPT, and the generated data is:
[0064] {
[0065] "input": "When I cough, I feel a tightness in my chest and sometimes my body twitches. Is this related to whooping cough?",
[0066] "output": "Chest tightness and convulsions may be complications of whooping cough. If accompanied by convulsions, the situation may be serious and requires further examination and treatment as soon as possible."
[0067] }
[0068] "input" indicates the required generated , "output" means the generated , thus obtaining a positive sample instruction data as ("I feel chest tightness when I cough, and sometimes I feel body twitching. Is this related to whooping cough?", "Chest tightness and twitching may be complications of whooping cough. If accompanied by convulsions, the situation may be more serious and requires further examination and treatment as soon as possible.").
[0069] Then, step S3 is performed to generate negative sample instruction data, which specifically includes:
[0070] From the seeds of knowledge The negative sample entity set in In the example, c negative sample entities are randomly selected and recorded as , use c of the negative sample entities to replace the positive sample instruction data In c positive sample entities in the , and get the negative sample instruction data .
[0071] For example, three negative sample entities (shock, rubella, barrel chest) are randomly selected to replace the positive sample entities (convulsion, chest tightness, convulsion), and the generated negative sample instruction data is "Shock and rubella may be complications of whooping cough. If accompanied by barrel chest, the situation may be serious and requires further examination and treatment as soon as possible."
[0072] Then, step S4 is performed to merge the obtained positive sample instruction data and negative sample instruction data to obtain a fine-tuning instruction data set. ;
[0073] In this embodiment, one piece of data in the fine-tuning instruction data set is: ("When I cough, I feel chest tightness and sometimes body twitching. Is this related to whooping cough?", "Chest tightness and twitching may be complications of whooping cough. If accompanied by convulsions, the situation may be serious and further examination and treatment are required as soon as possible.", "Shock and urticaria may be complications of whooping cough. If accompanied by barrel chest, the situation may be serious and further examination and treatment are required as soon as possible.")
[0074] Then, step S5 is performed to fine-tune the large model through comparative learning based on the fine-tuning instruction dataset.
[0075] In this embodiment, the model used is LLAMA-7B, and the loss function is used to calculate the loss value of the output positive and negative sample instruction data of the model, so that the parameters of the model are trained toward the goal of reducing the loss, wherein the loss function is:
[0076]
[0077] in express The i-th token of express The i-th token of Indicated in Whether the i-th token in is obtained by replacing the positive sample entity with the negative sample entity, 1 means it is not replaced, and 0 means it is replaced.
[0078] Embodiment 2:
[0079] Based on the same inventive concept as Example 1, this example introduces a system for generating a large model instruction dataset based on a knowledge graph, including:
[0080] The knowledge seed generation module is used to generate knowledge seeds based on the domain knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1. In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and the relationship between each element and the anchor entity are the same, and this same relationship is recorded as relationship ; Each element in the negative sample entity set 1 is a negative sample entity. Each element in the knowledge graph can be connected to the anchor entity through several hops, and each hop is a relationship ; Each knowledge seed also includes the relationship ;
[0081] The instruction data generation module is used to: generate positive sample instruction data using a preset large language model according to the knowledge seed; generate negative sample instruction data based on the positive sample instruction data using a negative sample entity set 1 in the knowledge seed; and combine the obtained positive sample instruction data and negative sample instruction data to obtain a fine-tuning instruction data set;
[0082] The model fine-tuning module is used to: fine-tune the open source large language model through contrastive learning based on the fine-tuning instruction dataset.
[0083] In summary, the present invention utilizes the distance relationship between entities on the graph to construct a knowledge seed including anchor entities, positive and negative sample entities and relationships, which can distinguish correct knowledge from incorrect knowledge in entities with relatively close relationships, thereby reducing the illusion of the model itself; and then utilizes the preset large language model to generate instruction data, thereby integrating the powerful capabilities of the preset large language model into the instruction data, and efficiently and low-costly obtaining high-quality fine-tuning data containing relevant knowledge.
[0084] It should be noted that the above is only a detailed description of the preferred embodiments and principles of the present invention. For ordinary technicians in this field, there will be changes in the specific implementation methods based on the ideas provided by the present invention, and these changes should also be regarded as the scope of protection of the present invention.< / output>
Claims
1. A method for generating a large model instruction dataset based on a knowledge graph, characterized in that: The following steps are involved: S1. Generate knowledge seeds according to the domain knowledge graph, where the domain knowledge graph is a medical knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1. In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and the relationship between each element and the anchor entity is r. i are the same, and this same relationship is recorded as relationship r i ; Each element in the negative sample entity set 1 is a negative sample entity. Each element in the knowledge graph can be connected to the anchor entity through several hops, and each hop is a relation r i ; Each knowledge seed also includes the relation r i ; S2. Generate positive sample instruction data of a medical consultation scenario using a preset large language model according to the knowledge seed; the positive sample instruction data of the medical consultation scenario includes the patient's questions and the doctor's answers; S3, based on the positive sample instruction data of the medical consultation scenario, using the negative sample entity set 1 in the knowledge seed, generating negative sample instruction data of the medical consultation scenario; the negative sample instruction data of the medical consultation scenario includes the patient's questions and the doctor's answers; S4, combining the obtained positive sample instruction data of the medical consultation scenario and the negative sample instruction data of the medical consultation scenario to obtain a medical consultation scenario fine-tuning instruction dataset; S5. Based on the medical consultation scenario fine-tuning instruction dataset, fine-tune the open source medical consultation scenario large language model through comparative learning; In step S1, generating knowledge seeds according to the domain knowledge graph specifically includes the following steps: S1.1 obtain a domain knowledge graph, obtain all entities in the domain knowledge graph, and use any entity obtained as an anchor entity e; S1.2 According to the anchor entity e, all first-order neighbor entities of the entity and their relationships are obtained, and the first-order neighbor entities with the same relationship are merged to obtain the second positive sample entity set, which is recorded as: in Represents the jth first-order neighbor entity of the i-th relationship of the anchor entity e; S1.3 For each of the above positive sample entity set 2 For each positive sample neighbor entity Select the multi-order neighbor entities whose shortest path length from the anchor entity e is between 2 and d and the relationship of each hop on the path is r i The neighbor entities of are taken as negative sample entities, and the negative sample entity set 2 is obtained, which is recorded as: Wherein, d is a positive integer greater than 2; S1.4 Obtain i knowledge seeds based on the anchor entity, positive sample entity set 2 and negative sample entity set 2 in, is the set of positive sample entities. is the negative sample entity set 1; The step S2 specifically includes: According to the knowledge seed Constructing the prompt words for the medical consultation scenario requires a large language model to be preset based on Generate positive sample instruction data for medical consultation scenarios in, is the positive sample entity set 1 c positive sample entities randomly selected from , where c is a pre-set positive integer not greater than j; It is also required that in the instruction data generated by the preset large language model, x i,m Contains anchor entity e and relationship r i information, Include The information in the query scene is used to obtain the positive sample instruction data The step S3 specifically includes: From the seeds of knowledge The negative sample entity set in In the example, c negative sample entities are randomly selected and recorded as Use c of the negative sample entities to replace the positive sample instruction data in the medical consultation scenario In c positive sample entities in the questionnaire to obtain negative sample instruction data for the consultation scenario 2. The method for generating a large model instruction dataset based on a knowledge graph according to claim 1, characterized in that: In step S2, the preset large language model is any one of GPT-4, ChatGLM, LLaMA3-70B, Claude3, and ChatGLM3.
3. The method for generating a large model instruction dataset based on a knowledge graph as claimed in claim 1, characterized in that: In step S5, the open source large language model for medical consultation scenarios is any one of LLaMA-2, Baichuan-2, and InternLM.
4. A large model instruction dataset generation system based on knowledge graph, characterized in that: include: The knowledge seed generation module is used to generate knowledge seeds according to the domain knowledge graph. Each knowledge seed includes an anchor entity, a positive sample entity set 1, and a negative sample entity set 1. In each knowledge seed, each element in the positive sample entity set 1 is a positive sample entity, and the relationship between each element and the anchor entity is r i are the same, and this same relationship is recorded as relationship r i ; Each element in the negative sample entity set 1 is a negative sample entity. Each element in the knowledge graph can be connected to the anchor entity through several hops, and each hop is a relation r i ; Each knowledge seed also includes the relation r i ; Generating knowledge seeds based on the domain knowledge graph specifically includes the following steps: S1.1 obtain a domain knowledge graph, obtain all entities in the domain knowledge graph, and use any entity obtained as an anchor entity e; S1.2 According to the anchor entity e, all first-order neighbor entities of the entity and their relationships are obtained, and the first-order neighbor entities with the same relationship are merged to obtain the second positive sample entity set, which is recorded as: in Represents the jth first-order neighbor entity of the i-th relationship of the anchor entity e; S1.3 For each of the above positive sample entity set 2 For each positive sample neighbor entity Select the multi-order neighbor entities whose shortest path length from the anchor entity e is between 2 and d and the relationship of each hop on the path is r i The neighbor entities of are taken as negative sample entities, and the negative sample entity set 2 is obtained, which is recorded as: Wherein, d is a positive integer greater than 2; S1.4 Obtain i knowledge seeds based on the anchor entity, positive sample entity set 2 and negative sample entity set 2 in, is the set of positive sample entities. is the negative sample entity set 1; The instruction data generation module is used to: generate positive sample instruction data of the consultation scenario using a preset large language model according to the knowledge seed, wherein the positive sample instruction data of the consultation scenario includes the patient's questions and the doctor's answers; specifically comprises the following steps: Constructing prompt words requires presetting a large language model based on Generate positive sample instruction data for medical consultation scenarios in, is the positive sample entity set 1 c positive sample entities are randomly selected from the set, where c is a pre-set positive integer not greater than j; it is also required that in the instruction data generated by the preset large language model, x i,m Contains anchor entity e and relationship r i information, Include The information in the query scene is used to obtain the positive sample instruction data Based on the positive sample instruction data of the consultation scenario, using the negative sample entity set 1 in the knowledge seed, generating negative sample instruction data of the consultation scenario, wherein the negative sample instruction data of the consultation scenario includes the patient's questions and the doctor's answers; specifically comprising the following steps: generating negative sample instruction data of the consultation scenario from the knowledge seed The negative sample entity set in In the example, c negative sample entities are randomly selected and recorded as Use c of the negative sample entities to replace the positive sample instruction data in the medical consultation scenario In c positive sample entities in the questionnaire to obtain negative sample instruction data for the consultation scenario The obtained positive sample instruction data of the medical consultation scenario and the negative sample instruction data of the medical consultation scenario are combined to obtain a medical consultation scenario fine-tuning instruction dataset; The model fine-tuning module is used to fine-tune the open source large language model through contrastive learning based on the medical consultation scenario fine-tuning instruction dataset.
