Role playing chat robot implementation method based on human background large language model
Through large language model and fine-tuning technology based on character background, the limitations of chatbots in role simulation and personalized dialogue are solved, and a high-quality, personalized and coherent role-playing chat experience is achieved.
Patent Information
- Application Number
- CN202411673706.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-21
- Publication Date
- 2025-03-18
AI Technical Summary
Existing chatbots have limitations in simulating roles with customized personalized characteristics, making it difficult to provide personalized style and emotional expressions, and the user's customized interactive experience is difficult to last, which requires users to have certain technical knowledge.
A large language model based on character background is adopted, by collecting and chunking literary works text data, character dialogue, actions and abstracts are extracted, and the model is fine-tuned in combination with LoRA and DPO methods, a dynamic prompt word framework is constructed, and a character information, background story and historical dialogue is integrated to form an intelligent chat framework to improve the quality of dialogue and user experience.
It effectively improves the quality of conversation, role personalization, role consistency, interactive consistency and user experience, providing a more personalized and lasting role-playing chat experience.
Smart Images

Figure CN119646140A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of information technology, and in particular to a method for realizing a role-playing chat robot based on a large language model of a person setting background. Background Art
[0002] Chatbots, also known as dialogue agents, are artificial intelligence systems that integrate natural language processing technology and are dedicated to achieving smooth communication with human users. They are not just simple question-answering machines, but are gradually evolving into intelligent agents that can provide personalized services and in-depth interactions. These agents are able to understand users' inquiries, provide information feedback, perform specific tasks, and even in some cases, guide users through complex processes. Chatbots have a wide range of use cases, from online customer service, personal assistants to medical consultations, and they have shown great potential to improve efficiency, enhance user satisfaction, and promote task automation in various industrial fields. Despite the continuous advancement of technology, existing chatbots on the market still face some challenges and limitations.
[0003] First, traditionally many chatbots still rely on fixed dialogue templates and rules, which limits their performance in terms of personalization and adaptability. When communicating with these robots, users often find it difficult to obtain a communication experience that matches a specific role or situation. For example, a user may want to have an in-depth conversation with a virtual character with a specific background story and personality traits, but existing robots find it difficult to provide such personalized interactions. Moreover, existing chatbots are deficient in handling complex conversations and long-term contextual memory. They often have difficulty understanding and maintaining a coherent conversation flow, especially in scenarios involving role-playing and situational simulation. This results in users being unable to obtain a smooth and immersive experience when playing role-playing games or simulating specific situations.
[0004] In recent years, advances in deep learning have greatly promoted the development of chatbot technology. Chat agents based on large pre-trained language models (such as GPT, Llama, ChatGLM, qwen, etc.) have demonstrated more advanced language processing capabilities by learning on massive texts. These models can not only understand complex language structures, but also generate more natural and diverse responses, allowing chat agents to adapt to a wider range of dialogue environments and task requirements. At the same time, it can also be combined with other technologies, such as RAG (Retrieval-Augmented Generation), by retrieving relevant information from large amounts of data and combining this information to generate richer, more accurate and personalized responses. This significantly improves the ability of chatbots to handle complex queries and provide in-depth conversations.
[0005] However, despite the progress made by these chat agents based on large language models in intelligent conversations, they still have limitations in simulating characters with customized personalized features. Most system-generated conversations lack personalized style and emotional expression, and are difficult to adapt to the specific tastes and preferences of different users. In addition, although users can customize the interactive experience through specific instructions, such customization is often difficult to sustain and requires users to have certain technical knowledge. Therefore, developing chat agents that can provide deep personalization and role-playing capabilities to meet users' needs for rich interactive experiences is an important direction of current technological development. Summary of the invention
[0006] The present invention provides a method for realizing a role-playing chat robot based on a large language model of human setting background, which can effectively improve the dialogue quality, role personalization, role consistency, interaction coherence and user experience.
[0007] A method for implementing a role-playing chat robot based on a large language model of a persona and background, comprising the following steps:
[0008] (1) Collect a large amount of text data from literary works, divide the text data into blocks according to chapters or plot development, and use a large language model to extract character dialogues, actions, and summaries, as well as character setting information; use sliding window technology to extract two-person multi-round dialogues from character dialogues, match them with character settings, and form a preliminary data set;
[0009] (2) Convert the preliminary dataset into Json format and construct a structured multi-round dialogue dataset; use the LoRA method and DPO method to fine-tune the large language model;
[0010] (3) Building a chatbot based on the fine-tuned large language model, integrating character information, background stories, historical dialogue summaries, and a small number of example dialogues to form a dynamic prompt word framework;
[0011] (4) Build an intelligent chat framework for the chatbot, including a time planning module, an active push module, and a delayed chat module.
[0012] In step (1), we first collect a large number of literary works including modern literature, classical literature, science fiction, fantasy, romance, etc. to ensure the diversity and coverage of text data. We clean the collected texts, remove irrelevant content such as page numbers and comments, and process them into sentences or paragraphs to adapt to the subsequent dialogue extraction. That is, we divide the text data into blocks according to chapters or plot development.
[0013] For each block, a large model is used to extract the dialogues and action scenarios between characters and summarize the block content to form a multi-round dialogue text with context.
[0014] Preferably, in order to have contextual associations between different blocks, in step (1), the plot summary of the previous block is added to the prompt words of each block, and then the big model is used to extract the character dialogue, action and summary. The character setting information includes personality, background story, and language habits, and the big model is used to express the character setting in a diversified way.
[0015] In particular, a sliding window method is used to form two-person multi-round dialogue data from the above-extracted multi-person multi-round dialogue. The dialogue is matched with the role setting and context, and finally, after cleaning and mixing the general data set, a multi-round dialogue data set is constructed.
[0016] In step (2), a structured multi-round dialogue dataset is constructed. The specific process is as follows:
[0017] Convert the preliminary data set into Json format, fill it with structured data through various manual instruction templates, and finally build a structured multi-round dialogue data set;
[0018] The constructed multi-round dialogue dataset contains multiple rounds of dialogue. Except for the first round of dialogue which contains instruction information, each subsequent round of dialogue only contains the dialogue data of the two parties in the dialogue as input and output; and the instruction and input parts do not participate in the calculation of the loss function during fine-tuning.
[0019] Based on the multi-round dialogue data set, the LoRA method in PEFT is used to fine-tune the large language model so that the model can learn to simulate the personality and speaking style of a specific character according to the settings. In particular, in order to better adapt to the application scenarios of multi-round dialogues, the weight of multi-round dialogue data is increased during training, and the loss is calculated for each pair of dialogue replies, that is:
[0020]
[0021] Among them, for N samples in the data set, the number of dialogue turns of the i-th sample is T i β is a hyperparameter used to control the weight of multiple rounds of dialogue; in the tth round of dialogue, y represents its predicted label, y * represents its true label.
[0022] In step (2), on the basis of the above-mentioned fine-tuning, fine-tuning based on human reinforcement feedback will also be used. The role-playing multi-round dialogue dataset required for the above-mentioned human reinforcement feedback training will be used, and the direct policy optimization (DPO) method will be used for further fine-tuning to generate dialogues that are more in line with human communication habits and user needs.
[0023] In step (3), the dynamic prompt word framework is the key link to ensure that the dialogue content is consistent with the character setting and background story. It includes the following steps:
[0024] (3-1) Information provided:
[0025] Set the core characteristics and background of the role played by the big model and the role played by the user, such as name, age, appearance, personality, gender, occupation, habits, values, historical background, and relationship with other roles, etc., to provide basic information for the model to play a specific role. And provide background stories related to the role.
[0026] (3-2) Retrieve the background story:
[0027] In the dynamic prompt word framework, the background story related to the dialogue is obtained through the search enhancement generation technology, and the background story is added to the dialogue context of the large model prompt word; if the relevant background story cannot be retrieved, the large model is used to dynamically generate a new background story based on the existing background story, character settings and context. This fills in the gaps in certain settings in the dialogue and ensures the consistency of the context roles.
[0028] (3-3) History record function:
[0029] Using advanced conversation analysis technology, the robot automatically summarizes past communication information and gives it "memory", so that each communication can be in-depth based on the previous one, enhancing the continuity and authenticity of the interaction. It automatically analyzes and summarizes past conversation records, extracts key information, and converts it into prompt words to ensure the coherence of the conversation and the consistency of the roles.
[0030] Historical conversation summaries are obtained by summarizing and analyzing conversation records. They are used to ensure the consistency of conversation content with character settings and background stories, enhance the coherence and immersion of interactions, and alleviate the forgetting caused by the input upper limit of the model.
[0031] (3-4) A few sample conversations:
[0032] Provide a small number of sample dialogues to help the model quickly adapt to new roles or situations and grasp the role's language style and behavioral habits.
[0033] (3-5) Intelligent prompt word framework integration:
[0034] Integrate all the above elements to form a prompt for role-playing dialogue chat, including role settings, background stories, historical record summaries and a small number of sample dialogues. Combine role settings, background stories, historical dialogue summaries and sample dialogues to form a dynamic prompt word framework to guide the chatbot to respond in accordance with the role in different situations.
[0035] In step (4), the intelligent chat framework can achieve more natural and smoother interaction with users by introducing a time planning module, an active push module and a delayed chat module.
[0036] The time plan module is used to generate and maintain a preset time plan table, which records various events on the virtual timeline and the time points at which they occur, and supports the setting of a non-real time system;
[0037] The time planning module also includes dynamic update and multi-source event integration functions, which can respond to user input and contextual information, or the occurrence of special events in real time to adjust the time plan; at the same time, it supports multiple event sources, including current affairs, special holidays, preset key events and chat record summaries.
[0038] The active push module is used to actively initiate a chat with the user at a preset time point based on the events and topics in the time plan table. Topics can come from preset templates, current hot spots, or user interests.
[0039] The delayed chat module is used to intelligently decide whether to postpone the reply when the user initiates a chat request based on the time plan and the current status of the chat robot. By simulating the scenario of real-person chat, the chat robot can automatically postpone the reply when it is busy or unavailable, and send the chat content at the appropriate time through the active push module.
[0040] Compared with the prior art, the present invention has the following beneficial effects:
[0041] 1. Improved conversation quality: The high-quality multi-round conversation dataset and targeted fine-tuning strategies have made the robot's role-playing conversations closer to human language habits and emotional expressions, and more in line with the settings.
[0042] 2. Enhanced character consistency: User-defined character settings and background stories, through the integration of RAG technology, allow chatbots to show more unique and coherent character personalities to meet the personalized needs of users. Adding background stories to the context also improves the consistency of character dialogues.
[0043] 3. Improved interaction consistency: With the help of historical record summarization and dynamic prompt word generation, the robot can maintain role consistency and dialogue logic coherence even in complex dialogue scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 This is an overall architecture diagram of a method for implementing a role-playing chat robot based on a large language model of a person setting and background according to an embodiment of the present invention. DETAILED DESCRIPTION
[0045] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be pointed out that the embodiments described below are intended to facilitate the understanding of the present invention and do not have any limiting effect on the present invention.
[0046] like Figure 1 As shown, a method for implementing a role-playing chat robot based on a large language model of a person setting and background includes the following steps:
[0047] S1. Dataset Construction Preparation
[0048] 1.1 Data Collection and Preprocessing
[0049] Before developing a role-playing chatbot based on a large language model of a persona, a key step is to prepare sufficient text data to build a high-quality dataset. This data should cover a wide range of conversation scenarios and language styles, and usually come from rich literary works such as novels, film and television scripts, and online literature. A specific dataset construction preprocessing process is as follows:
[0050] 1.1.1 Data collection: First, we collected a wide range of literary works to ensure the diversity and coverage of text data, including but not limited to modern literature, classical literature, science fiction, fantasy, romance and other genres.
[0051] 1.1.2 Text preprocessing: Clean the collected text, remove irrelevant content such as page numbers, comments, etc., and divide it into sentences or paragraphs to adapt to subsequent dialogue extraction.
[0052] 1.1.3 Block processing: Since text data is usually long, it needs to be reasonably divided into blocks according to chapters or plot development for easy management and processing. The content after block division is as follows:
[0053] {In the ancient castle, a brave knight and an evil dragon had a fierce duel. The knight brandished his sword and shouted loudly: "For the glory of the kingdom, I must defeat you!" The dragon responded with fire, "Bold human!" The princess watched the battle in front of her nervously, "Come on! Sir Knight!"}
[0054] 1.2 Dialogue Extraction
[0055] After preprocessing, the text is divided into blocks, and the blocks are processed further. The block processing flow is as follows:
[0056] 1.2.1 Role-playing dialogue conversion: Use a large language model to process each block, convert the narrative text into a dialogue between characters, and generate a summary of the block. The summary should include key dialogues, character actions, and emotional changes to provide contextual information for constructing the dataset. An example is as follows:
[0057] {Abstract: The knight challenged the dragon and had a fierce duel, with the princess cheering for the knight.
[0058] Knight: "(waving sword and shouting) For the glory of the kingdom, I must defeat you!"
[0059] Dragon: "(breathing fire) Bold human!"
[0060] Princess: "(watching the battle in front of her nervously) Come on! Sir Knight!"}
[0061] 1.2.2 Character setting extraction: Use a large language model to analyze multiple blocks (the specific size is limited by the upper limit of the model's processing tokens), analyze the text in them, and extract the detailed settings of each main character, including personality traits, background stories, language habits, etc. The example is as follows:
[0062] {Knight-brave, upright, and not afraid of strong enemies.
[0063] Dragon - Powerful, cunning, and loves to collect treasure.
[0064] Princess-kind and beautiful.}
[0065] 1.2.3 Extracting two-person dialogues: Using sliding window technology, extract multiple rounds of dialogues between two characters from the blocks. The purpose of the sliding window is to extract multiple local continuous longest two-person dialogues. The example is as follows:
[0066] Assuming ABC is the corresponding dialogue content of the three characters, then for a multi-round dialogue, we have the following dialogue sequence: ABABCBCA.
[0067] Therefore, based on the extraction of the sliding window, we can extract the two-person conversation data as follows (the extracted conversation is enclosed in brackets):
[0068] [ABAB]CBCA, ABA[BCBC]A, ABABCB[CA]
[0069] For the previous example, we extracted the following two-person conversation example:
[0070] Dialogue 1:
[0071] {Knight: "(Waving sword and shouting) For the glory of the kingdom, I must defeat you!"
[0072] Dragon: "(breathing fire) Bold human!"}
[0073] Dialogue 2:
[0074] {Dragon: "(breathing fire) Bold human!"
[0075] Princess: "(watching the battle in front of her nervously) Come on! Sir Knight!"}
[0076] 1.2.4 Role setting matching: Match the role setting with the dialogue content. Each round of dialogue in the final data set contains the name and setting of the role played by the model and the player, a brief introduction, and the dialogue roles and content (including dialogue content and action content). An example is as follows:
[0077] {"Background":"The knight accepted the king's commission and went to defeat the dragon.",
[0078] "user_name":"Knight",
[0079] "assistant_name":"Dragon",
[0080] "User role setting": "Brave, upright, and fearless of powerful enemies.",
[0081] "assistant role setting": "powerful, cunning, likes to collect treasures.",
[0082] "Dialogue":{"user":"(waving sword, shouting) For the glory of the kingdom, I must defeat you!","assistant":"(breathing flames) Bold human!"}
[0083] }
[0084] S2 model fine-tuning
[0085] 2.1 Dataset Format Construction
[0086] After the dataset is constructed, the data needs to be formatted in depth to meet the specific requirements of the model fine-tuning phase:
[0087] 2.1.1 Dialogue pair formatting: The formatting of each dialogue data is the key to ensure the integrity of the information. Each data must not only cover the basic role settings and the background summary, but also include the input dialogue used for training and the expected output dialogue.
[0088] 2.1.2 Diverse prompt templates: In order to enhance the adaptability and flexibility of the model, a variety of manually set prompt templates are designed to construct instructions. These templates will guide the model to generate dialogues according to different role settings and situations. Examples are as follows:
[0089] {"You are now in role-playing dialogue mode. Please play as {assistant_name}, whose role settings are as follows: {assistant role settings}\nI will play as {user_name} and talk to you, set to {user role settings}\nPreface: {Preface}\n",
[0090] "You will play as {assistant_name}, and your roleplay will follow the following character settings: {assistant role settings}\nI will play as {user_name} and talk to you, and my settings follow {user role settings}\nPreface: {Preface}\n"}
[0091] 2.1.3JSON structure design: Use a specific JSON format to organize data for easy model reading and processing. Reconstruct the above dataset into the required json format as a multi-round dialogue dataset for fine-tuning. The dataset is a multi-round dialogue dataset, which contains multiple pairs of dialogues. The dialogues have three key-value pairs: system, input, and output. Except for the first pair of dialogues that contain system instructions, subsequent dialogues do not contain them. System represents system instructions, which are used to guide the model in some details of role-playing; input represents the words of the user role; output represents the response of the corresponding large model role. Use the format of multi-round dialogues to organize data. The example is as follows:
[0092] {"conversation":[{"system":"You will play according to the descriptions of these characters...","input":"(Waving the sword and shouting loudly) For the glory of the kingdom, I must defeat you!","output":"(Spraying flames) Bold human!"},
[0093] {"input":"xxx","output":"xxx"}]}
[0094] 2.2 Supervised Fine-tuning
[0095] After the dataset is constructed, it can be used to fine-tune the large model. Based on the carefully constructed multi-round dialogue dataset, we will use the parameter-efficient fine-tuning method (PEFT) to fine-tune the selected large language models (such as ChatGLM3, qwen2, Llama and other open source large models). The purpose of fine-tuning is to enable the large model to learn to simulate the personality and speaking style of the character in the setting according to the specified role setting, so as to achieve the intelligence and persistence of personalized dialogue.
[0096] 2.2.1PEFT method selection: Choose a PEFT method such as the LoRA algorithm, which reduces computational and storage requirements by fine-tuning only a small portion of the model's parameters while maintaining training efficiency and model performance.
[0097] 2.2.2 Multi-round dialogue training: Organize the data in the format of multi-round dialogues, paying special attention to the coherence of the dialogue and the consistency of the context. During the training process, we mask the input and system texts in the multi-round dialogues when calculating the loss to simulate the incomplete information in the real dialogue, and ensure that the output part participates in the loss calculation to enhance the generation ability of the model. In particular, in order to better adapt to the application scenarios of multiple dialogue rounds, the weight of the multi-round dialogue data is increased during training. At the same time, when training multi-round dialogues, the loss is calculated for each pair of dialogue replies, that is:
[0098]
[0099] Among them, for N samples in the data set, the number of dialogue turns for each sample is T i , β is a hyperparameter used to control the weight of multiple rounds of dialogue. y represents the predicted label, y * represents the true label.
[0100] 2.2.3 Continuous Optimization: After fine-tuning, we will evaluate the performance of the model on the validation set and perform iterative optimization based on the feedback to ensure the accuracy and naturalness of the model in role-playing dialogues.
[0101] 2.3 Fine-tuning based on human reinforcement feedback:
[0102] In order to further improve the conversation quality of our role-playing chatbot, we will use direct policy optimization (DPO) based on human reinforcement feedback for fine-tuning. The core of DPO fine-tuning is to use the actual feedback from human evaluators to guide model learning, so that the chatbot can generate conversations that are more in line with human communication habits.
[0103] 2.3.1 Implementation of DPO method: The DPO method allows the model to directly learn the optimal dialogue strategy and adjust it in real time according to the preferences of human evaluators. This method optimizes the model's ability to select the best response in a specific context through policy gradient technology, enhancing the personalization and adaptability of the dialogue.
[0104] 2.3.2 Human evaluator feedback data collection: By collecting human evaluators' evaluations of generated conversations, the present invention provides a clear direction for improving the model. The evaluators provide corrective feedback on the answers given by the model, and these feedbacks will be converted into reward signals to guide the fine-tuning process of the model. The Conversations field corresponds to the context information, the chosen field represents the user's corrected reply, and rejected represents the model's original reply. The collected data set examples are as follows:
[0105] {"conversations":[{"from":"system","value":"You will play according to the descriptions of these characters..."},{"from":"user","value":"(Waving the sword and shouting loudly) For the glory of the kingdom, I must defeat you!"},{"from":"assistant","value":"(Breaking flames) Bold human!"},{"from":"user","value":"(Drawing the sword) Duel, greedy dragon! I will take away all your treasures!"}],
[0106] "chosen":{"from":"assistant","value":"(Confused) Greedy human! Aren't you here to save the princess?"},
[0107] "rejected":{"from":"assistant","value":"(spitting out flames) Greedy human!"}
[0108] }
[0109] 2.3.3 Continuous Optimization: Through DPO fine-tuning based on human reinforcement feedback, our chatbot can not only provide a more accurate and personalized conversation experience, but also show higher flexibility and adaptability in a variety of conversation scenarios. This process can be continued to continuously enhance the capabilities of the model.
[0110] S3 Prompt Word Project
[0111] In building an efficient role-playing chatbot, prompt engineering plays a vital role. It not only guides the model along the correct dialogue path, but also ensures that the dialogue content is consistent with the character's setting and background story. The following are the main contents of prompt engineering, which together form a comprehensive and multi-dimensional dialogue guidance system.
[0112] 3.1 Personality Information
[0113] Personality information is the basis of the prompt word project, which provides the core characteristics and background of the character for the large model. This includes the character's name, age, appearance, personality, gender, occupation, habits, values, historical background, and relationship with other characters. It can be provided in the form of structured data or natural language description. The level of detail of the personality information directly affects the depth of the model's understanding of the character and the naturalness of the conversation. The setting of the provided model role needs to be as detailed as possible, while the setting of the user's role can be relatively simple. The setting of the role does not necessarily need to be the same as the role in the data set. The meaning of the field is relatively clear and no unnecessary explanation is given. The following is an example:
[0114] {"user_name":"Edmund",
[0115] "user_set":"Edmund is a dragon-fighting knight, known for his fearless courage and unwavering sense of justice. He is tall and strong, with a strong face, long blond hair fluttering in the wind, and a desire for adventure in his eyes. Wearing a well-crafted silver armor engraved with the family's dragon emblem, he holds a sharp sword, ready to fight the dragon that threatens the kingdom at any time.",
[0116] "assistant_name":"Grendel",
[0117] "assistant_set":"Grendel is an ancient and wise dragon, known for his deep blue scales and eyes like the night sky. He inherited his father's legacy, carrying the glory and power of the dragon clan, and has the ability to control natural elements and cast dragon magic.",
[0118] "scene":"Knight Edmund is on his way to defeat the dragon Grendel to save Her Royal Highness the Princess."
[0119] }
[0120] 3.2 RAG-related background story
[0121] In the process of building a role-playing chatbot, the introduction of background stories is crucial to improving the authenticity and immersion of the conversation. To this end, we use the Retrieval-Augmented Generation (RAG) technology to achieve accurate retrieval and integration of background stories. Adding the retrieved background stories to the context can enhance the consistency of the large model character dialogue in terms of relevant content.
[0122] 3.2.1 Context-aware retrieval
[0123] The system extracts key information and constructs a query vector by analyzing the context of the current conversation. Subsequently, we use cosine similarity to quantify the similarity between the input context and the background story. By calculating the cosine of the angle between the context vector and the background story vector, the system can evaluate the correlation between them. A high cosine similarity means that the context and the background story are highly semantically consistent, thus ensuring that the retrieved story is closely related to the current conversation. The most relevant background story set that exceeds the threshold γ before the current conversation is retrieved (k, γ are manually defined parameters). This process is dynamic and can update the retrieval results in real time according to the development of the conversation. Example:
[0124] Assuming the input context:
[0125] {Knight: (draws sword) Fight, greedy dragon! I will take all your treasure!}
[0126] Related fragments retrieved through RAG:
[0127] {Fragment 1: In ancient times, there was a dragon named Sephiroth, known for his endless greed and obsession with treasure. Deep in Sephiroth's cave lies a huge treasure chest filled with gold, gems, and ancient relics looted from all over the world.
[0128] Clip 2: Sephiroth not only has an immense desire for treasure, but is also obsessed with protecting it. It uses fire and claws to resist all adventurers who dare to covet its treasure. In the dragon's heart, these treasures are not only a symbol of wealth, but also a manifestation of its power and majesty.}
[0129] 3.2.2 Dynamic Story Generation
[0130] In addition to retrieving existing background stories, dynamic story generation should also be supported. In some cases, if the similarity between the retrieved story and the current context is not high enough, the system will creatively generate new background story fragments based on the character settings and the most similar story elements retrieved to fill in the gaps in certain settings in the dialogue and ensure the consistency of the context roles. This content can also be retrieved by RAG later. Example:
[0131] Assuming the input context:
[0132] {Giant Dragon: (angry and sad, feeling his father's presence on the knight's sword) How did you get this dragon claw sword!}
[0133] If no relevant segments are found, a new segment is generated based on the character and context using the big model and added to the story collection:
[0134] {Grendel's father was once the most powerful dragon in the land, with claws so sharp that they could tear through the strongest armor. However, the old dragon's claws were cut off by a knight and made into a legendary sword - the Dragon Claw Sword. This sword is not only extremely sharp, but also contains the power of the dragon. It has become a treasure passed down from generation to generation in the knight family.}
[0135] 3.3 Historical Record Summary
[0136] In role-playing dialogues, summarizing historical records is crucial to maintaining the coherence of the dialogue and the consistency of the roles. Due to the limitations of context input, the prompt word engineering will automatically analyze and summarize past dialogue records, extract key information, and convert it into prompt words to reduce the length of the context and maintain role consistency. In this way, even in multiple rounds of dialogue, the model can review the previous communication content to ensure the fluency and logic of the dialogue.
[0137] Example: For the following chat records:
[0138] {Knight: "(Waving sword and shouting) For the glory of the kingdom, I must defeat you!"
[0139] Dragon: "(breathing fire) Bold human!"
[0140] Knight: "(draws sword) Fight, greedy dragon! I will take all your treasure!"
[0141] Dragon: "(Angry) I will take your soul!"}
[0142] We can use a large model to summarize it, which is:
[0143] {The knight brandished his sword and challenged the dragon, and a fierce duel ensued.}
[0144] 3.4 A few sample conversations
[0145] In order to enable the model to quickly adapt to new roles or situations, the prompt word project also includes a small number of sample dialogues (few-shot dialogues). These sample dialogues show how the characters communicate in specific situations and provide direct learning samples for the model. By learning from these examples, the model can grasp the language style and behavioral habits of the characters more quickly. Small sample dialogues of different personalities are directly given.
[0146] 3.5 Intelligent prompt word integration
[0147] Integrate all the above elements to form a prompt for role-playing dialogue chat, including role settings, background stories, historical records summary, a small number of sample dialogues and chat context. Combine role settings, background stories, historical dialogue summaries and sample dialogues to form a dynamic prompt word framework to guide the chatbot to respond in a role-playing dialogue chat in different situations. The final prompt words are integrated in a form similar to the following as a prompt for role-playing dialogue chat:
[0148] {"system":"You will play {assistant_name}, the dragon Grendel. {assistant_set} Grendel is an ancient and wise dragon, known for its deep blue scales and eyes like the night sky. It inherited its father's will, carrying the glory and power of the dragon clan, and has the ability to control natural elements and cast dragon magic. I will talk to you as {user name} Edmund. {user set} Edmund is a dragon knight, known for his fearless courage and firm sense of justice. He is tall and strong, with a strong face, long golden hair fluttering in the wind, and a desire for adventure in his eyes. {scene} Knight Edmund is on his way to defeat the dragon Grendel to save Her Royal Highness the Princess.
[0149] {RAG stories}
[0150] Clip 1: In ancient times, there was a dragon named Sephiroth, known for his endless greed and obsession with treasure. Deep in Sephiroth's cave lies a huge treasure, filled with gold, gems, and ancient relics looted from all over the world.
[0151] Clip 2: Sephiroth not only has an immense desire for treasure, but is also obsessed with protecting it. It uses fire and claws to resist all adventurers who dare to covet its treasure. In the dragon's heart, these treasures are not only a symbol of wealth, but also a manifestation of its power and majesty.
[0152] {Historical Record Summary}Edmund accepted the king's commission and went to defeat the dragon. ",
[0153] "user":"{few shots}(waving the sword and shouting loudly)For the glory of the kingdom, I must defeat you!",
[0154] "assistant":"{few shots}((spits out flames)Bold human!",
[0155] "user":"{user input}(draw your sword)Duel, greedy dragon! I will take all your treasures!"
[0156] }
[0157] S4 Intelligent Chat Framework
[0158] This paper proposes an innovative intelligent chat framework, which aims to simulate the real interpersonal communication experience and achieve a more natural and smooth interaction with users by introducing active push and delayed chat mechanisms. The framework consists of the following core modules, which work together to create a highly intelligent and personalized chat environment.
[0159] 4.1 Time Planning Module
[0160] 4.1.1 Preset time plan table
[0161] During the character building phase, we select one of the wide range of preset schedules to suit the needs of different character types and situations. These schedules cover a variety of character types and situations, providing a rich basis for time planning for the chatbot. For example: For a writer, her daily schedule may be as follows:
[0162] Table 1
[0163] 9:00-12:00 Focus on writing articles 12:30-13:30 Rest and meals 14:00-17:00 Explore the city or natural landscape 18:30-19:30 Preparing dinner or eating out 20:00-22:00 Meet up with friends or relax at home
[0164] This table not only records the events on the virtual timeline and the time points at which they occur, but also supports the setting of non-realistic time systems, such as the years of another world or other fictional time systems. The system can define the corresponding time flow rules so that the time schedule can adapt to the needs of various fictional scenes, greatly enhancing the authenticity and immersion of role-playing. For example, the following Table 2 and Table 3:
[0165] Table 2 Months in the Yuanqi Year 1234
[0166] Yin-Mao Praying in the church
[0167] Table 3 Time lapse rules
[0168] Midnight 0:00-2:00....1234 corresponds to 2024...and other time settings.
[0169] 4.1.2 Dynamic Update and Multi-Source Event Integration
[0170] One of the key features of the time plan module is its dynamic update capability. After the event schedule is preset, the system can intelligently generate and maintain a dynamic schedule using a large language model, responding to user input and contextual information in real time, or adjusting the schedule when some special events occur (such as birthdays). This means that as the chat progresses, the schedule can reflect new events and changes, maintaining the continuity and interactivity of the conversation. For example:
[0171] Assume that this morning Amy received a temporary task to edit an urgent manuscript between 10:00 and 11:00 am and update the schedule as shown in Table 4.
[0172] Table 4
[0173] 9:00-10:00 Adjust your writing time 10:00-11:00 Urgent Manuscript Editing 11:00-12:00 Keep writing 12:30-13:30 Lunch break / lunch 14:00-17:00 Outdoor Activities 18:30-19:30 dinner 20:00-22:00 Social / Relax
[0174] The sources of events can be diverse, including but not limited to current hot topics, manually preset key events, and events generated based on chat content. This dynamic update mechanism enables the chatbot to adapt to different conversation situations more flexibly and provide a more personalized and timely interactive experience, as shown in Table 5 below.
[0175] Table 5
[0176] type Example Current Affairs Chinese diving won the gold medal in the Olympics. (Detailed description) Special Holidays Today is Valentine's Day, let's prepare a candlelight dinner Preset key events The Demon King is resurrected and the Novice Village is destroyed Chat record summary The surprise gift I mentioned today is ready.
[0177] Through the time schedule, we can see what state the role-playing chatbot should be in and integrate it into the previous prompt word project. At the same time, it can also better support the use of the following active push and delayed chat modules.
[0178] 4.2 Active Push Module
[0179] The active push module is an innovative conversation triggering mechanism that aims to proactively initiate chats with users through intelligent analysis and prediction, thereby achieving a highly personalized and timely chat experience. According to the events and topics of the proactively initiated conversation type in the time plan table, a timer is added. When the time is reached, the chatbot proactively initiates a chat with the user based on the specific content. Topics and events can come from preset templates, current hot spots, or user interests. For example, the timer contains the following proactively pushed events, which will be triggered at the appropriate time point, as shown in Table 6 below:
[0180] Table 6
[0181] 18:30 Invite Mr. to dinner The next day 11:00 The ordered cake was delivered and I took it to my husband.
[0182] 4.3 Delayed chat module
[0183] The module is another key component of the present invention. When a user initiates a chat request, it intelligently decides whether to postpone the reply based on the time plan and the current state of the robot. This simulation of real-person chat allows the chat robot to automatically postpone the reply (not too long) when it is busy or unavailable, and send the chat content at the right time through the active push module. This mechanism not only improves the user experience, but also makes the chat robot's behavior more in line with the natural laws of human communication. For example, as shown in Table 7:
[0184] Table 7
[0185] 19:00 Since I couldn't reply due to the previous 'showering' status, I will reply to the other party's message again now.
[0186] S5 Deployment
[0187] Combining the final model with the prompt word engineering, we get a highly flexible and customizable role-playing chatbot system that can not only be freely deployed on different platforms and environments, but also be quickly adjusted and optimized according to the specific needs of users.
[0188] The embodiments described above provide a detailed description of the technical solutions and beneficial effects of the present invention. It should be understood that the above are only specific embodiments of the present invention and are 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 method for implementing a role-playing chat robot based on a large language model of a person setting and background, characterized in that: The following steps are involved: (1) Collect a large amount of text data from literary works, divide the text data into blocks according to chapters or plot development, and use a large language model to extract character dialogues, actions, and summaries, as well as character setting information; Apply sliding window technology to extract two-person multi-round dialogues from character dialogues, match them with character settings, and form a preliminary data set; (2) Convert the preliminary dataset into Json format and construct a structured multi-round dialogue dataset; Use LoRA and DPO methods to fine-tune the large language model; (3) Building a chatbot based on the fine-tuned large language model, integrating character information, background stories, historical dialogue summaries, and a small number of example dialogues to form a dynamic prompt word framework; (4) Build an intelligent chat framework for the chatbot, including a time planning module, an active push module, and a delayed chat module.
2. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (1), the plot summary of the previous block is added to the prompt words of each block, and then the big model is used to extract the character dialogues, actions and summaries.
3. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (1), the character setting information includes personality, background story, and language habits, and the big model is used to express the character setting in a diversified way.
4. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (2), a structured multi-round dialogue dataset is constructed. The specific process is as follows: Convert the preliminary data set into Json format, fill it with structured data through various manual instruction templates, and finally build a structured multi-round dialogue data set; The constructed multi-round dialogue dataset contains multiple rounds of dialogue. Except for the first round of dialogue which contains instruction information, each subsequent round of dialogue only contains the dialogue data of the two parties in the dialogue as input and output; and the instruction and input parts do not participate in the calculation of the loss function during fine-tuning.
5. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (2), when the LoRA method is used to fine-tune the large language model, the weight of the multi-round dialogue data is increased, and the loss is calculated for each pair of dialogue replies, that is: Among them, for N samples in the data set, the number of dialogue turns of the i-th sample is T i ; β is a hyperparameter used to control the weight of multiple rounds of dialogue; in the tth round of dialogue, y represents its predicted label, y * represents its true label.
6. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (3), in the dynamic prompt word framework, the background story related to the dialogue is obtained through the retrieval enhancement generation technology, and the background story is added to the dialogue context of the large model prompt word; If no relevant background story can be retrieved, a new background story is dynamically generated based on the existing background story, character settings and context using the big model.
7. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (3), the historical dialogue summary is obtained by summarizing and analyzing the dialogue records to ensure the consistency of the dialogue content with the character settings and background stories, enhance the coherence and immersion of the interaction, and alleviate the forgetting caused by the input upper limit of the model.
8. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (4), the time planning module is used to generate and maintain a preset time planning table, which records various events on the virtual timeline and the time points at which they occur, and supports the setting of a non-real time system; The time planning module also includes dynamic update and multi-source event integration functions, which can respond to user input and contextual information, or the occurrence of special events in real time to adjust the time plan; at the same time, it supports multiple event sources, including current affairs, special holidays, preset key events and chat record summaries.
9. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (4), the active push module is used to actively initiate a chat with the user at a preset time point based on the events and topics in the time plan table.
10. The method for implementing a role-playing chat robot based on a large language model of a person setting and background according to claim 1, characterized in that: In step (4), the delayed chat module is used to intelligently decide whether to postpone the reply based on the time plan and the current state of the chat robot when the user initiates a chat request; by simulating a real-life chat scenario, the chat content is sent at the appropriate time.
Citation Information
Patent Citations
Application method of dialogue system based on human big language model in industrial scene
CN117932012A
Achieving method and device of callback function based on olan framework, medium and equipment
CN118568250A
Model training method, reply statement generation method and device and electronic equipment
CN118981523A
Cited By
Character memory alignment method for role-based large language model
CN120525051A
Chat topic guiding method and device, equipment and medium
CN120567820A
Construction method and system of social hotspot content semantic deduction agent, medium and product
CN121303146A
Method, system, medium and product for constructing social hot content semantic deduction intelligent agent
CN121303146B
Role playing data set construction method based on image enhancement
CN121416006A