Knowledge Base · Learning paths
How to Prepare High-Quality Data for Your LLM: Practical Lessons from Building a Vertical Education Model
Building a custom LLM for the education sector requires more than just clever prompts — you need the right data. This hands-on guide walks through the full process: crawling industry knowledge with Scrapy, structuring internal company documents, and using smart prompting techniques to automatically generate high-quality QA and dialogue datasets for fine-tuning.
Translated from Chinese by AI. Spotted an issue? Use the feedback button.
🔗 Original article: https://mp.weixin.qq.com/s?__biz=Mz...
⏰ Published: 2023-11-23
Original article by Caiyezi, WeChat Official Account: capycapybabala
Back to the topic: suppose we need to build a domain-specific large language model (LLM) for the education sector, tailored to our company’s products and business. How do we prepare the data and put it all together?
This article takes a very practical approach (the advanced stuff is beyond me, too) to collecting and processing datasets, with a brief look at both fine-tuning and knowledge base approaches.
First, let’s break down the data we need. One part consists of datasets covering broad, general knowledge within the industry. The other consists of the company’s internal materials. These are the two main categories.
Part 01 .
Crawling Industry Data
For general knowledge content, you’ll need to confirm the scope with the product and project teams, as well as relevant subject-matter experts. Start by identifying which papers, texts, websites, or files hosted on those websites you need. Papers and texts that have already been downloaded and stored locally can be handled in the second stage. In this first stage, we’ll focus on using web crawlers to collect text and images from web pages.
In my projects, I mainly use the Python library Scrapy for web crawling. With Crawlab, you can run distributed crawls very efficiently. Here are some tutorials.
- Scrapy Beginner’s Tutorial | Runoob (runoob.com)
https://www.runoob.com/w3cnote/scrapy-detail.html
-
Quick Tutorial (crawlab.cn)
The interface is very user-friendly, and you can run lots of tasks at the same time.
You can export the results as CSV or JSON.
For example, here I want to crawl experiment materials from the National Basic Education Resource Network.
Web pages with a clear structure like this are relatively easy to crawl.
Follow the tutorial to analyze the page structure, and you can run crawling tasks for many pages at once. You’ll then get results like these:
For instance, here we’ve retrieved the page content, files linked from the page, and so on...
There’s also a useful open-source crawling tool called GPT Crawler, which primarily uses TypeScript to crawl data. Here’s the GitHub link: https://github.com/BuilderIO/gpt-crawler
A crawler is generally set up to collect a particular set of content from a given website, so you’ll need to customize the crawling script for each site. I’ve also tried GPT Crawler and found it somewhat inflexible—it struggled to handle diverse page layouts. That may also be because I’m not very familiar with TypeScript. I strongly recommend Scrapy or BeautifulSoup. Of course, you can use other languages, but I mainly use Python.
Part 02 .
Integrating Internal Data
Recently, I came across a very popular term: RAG, or Retrieval Augmented Generation. I realized I had actually been using this idea already, just without such sophisticated methods—my approach was pretty low-tech... In enterprise settings, GPT is often used to streamline search workflows. And because large language models (LLMs) can hallucinate, properly organizing and integrating documents containing critical information is especially important.
There are plenty of libraries for processing documents in formats such as docx and pdf, so I won’t go into those here.
In my engineering work, internal documents generally follow a hierarchical structure: top-level headings numbered I, II, III, followed by (1), (2), (3), and then 1, 2, 3, with tables, images, and other elements interspersed throughout.
My preferred approach is to turn these heading levels into nested dictionaries to store each department’s internal documents. Here is a simple example:
XXX Human Resources Department Internal Handbook
I. Company Management Policies
(1) Compensation Policy
(2) Rewards and Disciplinary Policy
II. Personnel Management Policies
(1) Hiring Policy
(2) Training Policy
.....
I would convert this into a dictionary:
{"Company Management Policies":{"Compensation Policy":[xxxx],"Rewards and Disciplinary Policy":[xxxx]},
"Personnel Management Policies":{"Hiring Policy":[xxx],"Training Policy":[xxxx]}}
Other structured texts can be processed in the same way. Of course, you could also use a Neo4j graph structure for this database, essentially turning it into a simple knowledge graph. (I don’t know much about this area, so if anyone knows more, I’d love to learn from you.)
Part 03 .
Creating an LLM Fine-Tuning Dataset
After processing the text as described above, the next step is to generate the format required by the LLM.
Through the steps above, we now have both unstructured text data and structured data in a network format.
My requirements at this stage are relatively simple, focusing mainly on QA and dialogue datasets.
For the QA dataset, I mainly rely on existing large language models (LLMs) to generate question–answer pairs. I tailor the prompts to different texts to extract as much information as possible. First, the text in the files needs to be split into chunks. I put each major section into a separate txt file, with roughly 200 Chinese characters per file (sorry, my use case is not that complex at this early stage), and then define a prompt template.
For a company product manual:
template = f"""You are a knowledgeable product manager for the xxx product at xxx company.
You will be given a passage about the xx product from xxxxx Co., Ltd. Based on this passage, you must come up with ten different questions and their corresponding answers.
Your questions may be used to test the professional knowledge of xxx staff within the company.
Your questions may be questions that users of the company's products would ask.
Your questions and answers must be relevant to the provided passage.
Your answers must be understandable to users of the product.
Once you have come up with the questions and answers, you must respond in the following format:
[
"Question1": "$Put your question here",
"Answer1": "$Put your answer here",
"Question2": "$Put your question here",
"Answer2": "$Put your answer here",
"Question3": "$Put your question here",
"Answer3": "$Put your answer here",
"Question4": "$Put your question here",
"Answer4": "$Put your answer here",
"Question5": "$Put your question here",
"Answer5": "$Put your answer here",
"Question6": "$Put your question here",
"Answer6": "$Put your answer here",
"Question7": "$Put your question here",
"Answer7": "$Put your answer here",
"Question8": "$Put your question here",
"Answer8": "$Put your answer here",
"Question9": "$Put your question here",
"Answer9": "$Put your answer here",
"Question10": "$Put your question here",
"Answer10": "$Put your answer here",
]
Everything between the ``` markers specifies the format you must use in your response.
Please come up with one question and one answer for the following text, and respond using the list format specified above:
{text}"""
In this example, ten questions are enough for a passage of 200 Chinese characters. To avoid repetition, I ask directly for a specific number of questions rather than using a for loop. It is also important to assign different roles based on the text and define what the questions are for. In my case, the goal is to answer questions customers might ask while using the product, so I require the questions to be framed from the customer's perspective. These settings can be adjusted to suit your circumstances. Try to avoid overly broad prompts such as “Please read this passage, ask questions, and answer them,” because different texts have different use cases. I have tried a variety of prompts and found that clearly specifying the roles works better.
As you can see, the generated QA pairs are reasonably good.
Since we also have structured data, we can simply write code to iterate over the dictionary keys and insert them into the prompt template to ask questions about a particular section. This is very convenient.
The second part is creating a dialogue dataset.
My approach is to first collect and annotate a batch of seed dialogues, then use prompts to generate additional dialogues. The seed dialogues require human annotators to define the instruction task type, carry out the dialogue, and collect it for use in generation. I adapted someone else's dialogue collection script to create an annotation tool. Annotators work in pairs to annotate dialogues, record them along with their task types, and export the records.
This is the interface I use to annotate the seed dataset. For details, see dialogue_labeling: an annotation platform for manually collecting LLM dialogue datasets.
https://gitee.com/cyz6668/dialogue_labeling
The above describes my approach to preparing data for fine-tuning large language models. Once generated, the datasets need to be manually reviewed and iteratively improved.
Part 04 .
Organizing Data for the Knowledge Base Approach
Another option is to use a knowledge base. A representative project is langchain-chatGLM. This approach places fairly high demands on how the knowledge base content is organized, but it does not require fine-tuning. Personally, I think it is well suited to data that is structured and has clearly defined boundaries.
This is where the data previously organized in neo4j or dictionaries becomes particularly useful.
Here, I use the langchain library to progressively compare the keyword keys in the dictionary and find the content associated with the most similar relevant keys. This helps retrieve more relevant text, which is then fed into the prompt template (prompt_template) and passed to the LLM for post-processing. My idea is to replace and expand related, similar keywords, borrowing an approach from open-source knowledge graph (KG) code.
prompt_template = """Known information:
{context}
Based on the information above, answer the user's question concisely and professionally. If the answer cannot be found in the information provided, say "The question cannot be answered based on the available information" or "Insufficient relevant information has been provided." Do not include fabricated content in your answer. Please answer in Chinese. The question is: {question}"""
I think the most important part of the knowledge base approach is retrieving sufficiently accurate content for the context. Rather than finding content in the source collection that is similar to the question and adding a small buffer on either side, this approach helps better avoid errors and hallucinations.
This is something I casually wrote for testing. As you can see, the responses are fairly robust and do not vary much.
Summary
This is a very detailed account of how I tackled this messy, difficult, but important work. I hope you find it helpful. If you have a more efficient approach, I would love to learn from you.