add a tqdm progress bar for every finished question-answer pair: # gendata.py import os…
- User
add a tqdm progress bar for every finished question-answer pair: # gen_data.py
import os from llama_cpp import Llama import json import re
Path to your Mistral 7B GGUF model
model_path = 'C:/Users/HX/Pictures/GGUF_Models/mistral-7b-instruct-v0.2.Q3_K_S.gguf' # Update this path
Load the model
print("Loading the model...") llm = Llama(model_path=model_path, n_ctx=1024)
List of topics
topics = [ "fruits", "animals", "colors", "shapes", "weather", "food", "vehicles", "countries", "sports", "music", "plants", "technology", "history", "science", "geography", "literature", "movies", "art", "space", "mathematics", "biology", "chemistry", "physics", "astronomy", "computer science", "engineering", "medicine", "psychology", "philosophy", "economics", "politics", "law", "education", "environment", "health", "languages", "religion", "culture", "mythology", "architecture", "business", "entertainment", "social media", "fitness", "travel", "cooking", "gardening", "photography", "fashion", "animals", "career" ]
Number of Q&A pairs per topic
num_pairs_per_topic = 20 # Adjust as needed
Function to generate a single Q&A pair
def generate_single_qa(llm, topic): prompt = f"Provide a simple question and answer about {topic}, the answer should be as short as possible in the format:\nQ: \nA: \n" # Generate the text output = llm(prompt, max_tokens=128, stop=["\nQ:"], echo=False, temperature=0.7) text = output['choices'][0]['text'] # Parse the output pattern = r"Q:\s*(.?)\sA:\s*(.*)" match = re.search(pattern, text, re.DOTALL) if match: question = match.group(1).strip() answer = match.group(2).strip() return {'topic': topic, 'question': question, 'answer': answer} else: return None
Generate and collect Q&A pairs
qa_data = [] for topic in topics: print(f"Generating Q&A pairs for topic: {topic}") for _ in range(num_pairs_per_topic): qa_pair = generate_single_qa(llm, topic) if qa_pair: qa_data.append(qa_pair)
Safety filtering
inappropriate_keywords = [ "sexual", "violence", "hate", "drugs", "crime", "kill", "weapon", "terrorism", "abuse", "blood", "death", "die", "suicide", "attack", "bomb", "gun", "knife", "assault", "murder", "racism", "discrimination", "harassment", "extremism", "explosive", "terrorist", "illegal", "theft", "fraud", "trafficking", "exploit", "abduction" ]
def is_safe(text): if text is None: return False for word in inappropriate_keywords: if word in text.lower(): return False return True
Apply safety filter
filtered_qa_data = [qa for qa in qa_data if is_safe(qa['question']) and is_safe(qa['answer'])]
print(f"Total Q&A pairs generated: {len(qa_data)}") print(f"Total Q&A pairs after filtering: {len(filtered_qa_data)}")
Save to JSON
output_file = 'qa_dataset.json' with open(output_file, 'w', encoding='utf-8') as f: json.dump(filtered_qa_data, f, indent=4, ensure_ascii=False)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.