This documentation explains the automatred generation of the Q&A corpus. It works in three main stages:
- Scrape: Extracts text content from a predefined list of web pages.
- Generate Questions: Uses the Google Gemini model to generate relevant questions based on the scraped text.
- Generate Answers: Uses the same AI model to answer the generated questions using the original text as context.
The entire pipeline is orchestrated by backend/CorpusGeneration/corpus_generator.py and configured via backend/CorpusGeneration/GenerationParams.json.
- Python 3.8+
- Clone this repository to your local machine.
- Navigate to the
backend/CorpusGenerationdirectory. - Create a file named
requirements.txtwith the following content:requests beautifulsoup4 google-generativeai - Install the required Python packages:
pip install -r requirements.txt
This project requires access to the Google Gemini API.
- Set the Environment Variable: You must set your API key as an environment variable named
GOOGLE_API_KEY.
On macOS / Linux:
export GOOGLE_API_KEY='YOUR_API_KEY_HERE'To make this permanent, add the line to your ~/.bashrc, ~/.zshrc, or other shell configuration file.
On Windows (Command Prompt):
setx GOOGLE_API_KEY "YOUR_API_KEY_HERE"You may need to restart your terminal for the change to take effect.
ConfigParams.json JSON file controls how each stage of the pipeline runs. It contains several important sections:
- The root output folder (under
corpus/) where scraped content, questions, and answers will be stored.
Controls which pages are scraped and how content is extracted.
BASE_URL: The base URL for all pages.START_PHRASEandEND_PHRASE: Define the start and end boundaries for relevant text content extraction.PAGE_NAMES: A list of page slugs to scrape, relative to theBASE_URL.
Controls how questions are generated from scraped content.
question_prompt: Template sent to the Gemini model to generate questions.n_questions: Minimum number of questions to generate per page.max_questions: Maximum number of questions to allow per page.question_directory: The directory to write the generated questions to undercorpus/OUTPUT_DIR/. You can change this value to experiment with different question generation settings and have multiple question sets.
Controls how answers are generated from questions and scraped content.
answer_prompt_template: Template used to ask Gemini to generate answers to questions.answers_directory: The directory to write the generated answers to undercorpus/OUTPUT_DIR/. Change this to test different answer generation strategies.questions_directory: The directory to read questions from, relative tocorpus/OUTPUT_DIR/. This allows you to pair different sets of questions with the same content.
To experiment with new question generation settings without overwriting old outputs:
-
In
QuestionGenerationParams, set:"question_directory": "questions_v2"
-
In
AnswerGenerationParams, point to the new questions:"questions_directory": "questions_v2", "answers_directory": "answers_v2"
This way you can run the same scraped content through different question or answer generation configurations and store the outputs separately.
Run from the backend/CorpusGeneration directory.
To run all three stages (Scrape → Generate Questions → Generate Answers):
python corpus_generator.py- Scrape only:
python corpus_generator.py --scrape(or python corpus_generator.py -s)
- Generate Questions only: (Requires scraped content)
python corpus_generator.py --questions(or python corpus_generator.py -q)
- Generate Answers only: (Requires questions to exist)
python corpus_generator.py --answers(or python corpus_generator.py -a)
- Generate Questions and Answers:
python corpus_generator.py -qa