Prompt Hub Strategy
Here is a step-by-step strategy to build a “Prompt Hub”: ⸻ 1. Input Preparation a. Source Validation Guidelines • Collect existing model validation templates and guideline documents. • Parse each template into sections (e.g., Model Framework, Performance, Assumptions). • Extract each guideline instruction within these sections (e.g., “Assess whether the model objectives align with core model requirements”). ⸻ 2. Prompt Generation a. Language Model Setup • Use DeepSeek-RL-distilled-Qwen-7B via a local inference server (e.g., Hugging Face Transformers or vLLM). b. Generate Multiple Prompts per Guideline For each guideline: • Construct a generation prompt like: Instruction: Assess whether the model objectives align with the core model requirements. Generate a prompt that can be used to instruct an LLM to perform this task. • Generate 3 prompt variants per guideline using temperature sampling (e.g., temperature=0.7). ⸻ 3. Prompt Ranking Using Log-Likelihood a. Compute Log-Probability for Each Prompt • Use the same LLM to compute log-likelihood for each generated prompt. # Pseudocode model.compute_log_likelihood(prompt) • This score estimates how natural/confident the model is in producing that text. b. Sort Prompts by Log-Likelihood • Rank the 3 variants for each guideline based on descending log-likelihood. • Keep the top N if needed. ⸻ 4. Structure into a Prompt Dictionary a. Define Data Structure prompt_hub = { "Template Name": { "Section Name": [ { "guideline": "...", "prompts": [ {"text": "...", "log_likelihood": -12.3}, {"text": "...", "log_likelihood": -13.5}, ... ] }, ... ] } } ⸻ 5. Output Format and Handoff a. Save the Prompt Hub • Serialize the dictionary as a JSON or Python pkl file. • Optionally convert to CSV for easy UI prototyping. b. Handoff to Front-End Team • Provide API-ready structure or file for downstream integration into: • Prompt selection UI • Prompt recommendation systems • Auto-fill interfaces for model validation workflows ⸻ # prompt_hub_builder.py import json import torch from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline from collections import defaultdict from typing import List, Dict # === 1. Load Model and Tokenizer === def load_model(model_name="deepseek-ai/deepseek-llm-7b-chat"): tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16) model.eval() return tokenizer, model # === 2. Generate Prompt Variants === def generate_prompts_for_guideline(guideline: str, tokenizer, model, num_variants=3, temperature=0.7) -> List[str]: prompt_template = f"Instruction: {guideline}\nGenerate a prompt to instruct an LLM to perform this task." input_ids = tokenizer(prompt_template, return_tensors="pt").input_ids.to(model.device) outputs = model.generate( input_ids=input_ids, max_new_tokens=50, num_return_sequences=num_variants, do_sample=True, temperature=temperature ) return [tokenizer.decode(output, skip_special_tokens=True).split("Instruction:")[-1].strip() for output in outputs] # === 3. Score Prompts Using Log-Likelihood === def compute_log_likelihood(prompt: str, tokenizer, model) -> float: inputs = tokenizer(prompt, return_tensors="pt").to(model.device) with torch.no_grad(): outputs = model(**inputs, labels=inputs["input_ids"]) loss = outputs.loss.item() return -loss * inputs["input_ids"].shape[1] # Log-likelihood approximation # === 4. Build Prompt Hub === def build_prompt_hub(validation_data: Dict[str, Dict[str, List[str]]], tokenizer, model) -> Dict: prompt_hub = defaultdict(lambda: defaultdict(list)) for template, sections in validation_data.items(): for section, guidelines in sections.items(): for guideline in guidelines: prompt_variants = generate_prompts_for_guideline(guideline, tokenizer, model) scored_prompts = [ { "text": p, "log_likelihood": compute_log_likelihood(p, tokenizer, model) } for p in prompt_variants ] scored_prompts.sort(key=lambda x: x["log_likelihood"], reverse=True) prompt_hub[template][section].append({ "guideline": guideline, "prompts": scored_prompts }) return prompt_hub # === 5. Example Usage === if __name__ == "__main__": # Example validation data format validation_templates = { "Credit Risk Template": { "Model Framework": [ "Assess whether the model objectives align with the core model requirements.", "Evaluate the appropriateness of the model development methodology." ], "Performance": [ "Determine whether the model meets performance thresholds on test data." ] } } tokenizer, model = load_model() prompt_hub = build_prompt_hub(validation_templates, tokenizer, model) # Print or save to JSON print(json.dumps(prompt_hub, indent=2))
fill the variables
This prompt has 2 variables. Pro fills them into a ready-to-paste prompt for you — no manual find-and-replace.
{"text": "...", "log_likelihood": -13.5}{guideline}
Unlock with Pro →when to use it
Community prompt sourced from the open-source GitHub repo machachlouei/mrm-prompt-bench (MIT). A "Prompt Hub Strategy" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
businesscommunitygeneral
source
machachlouei/mrm-prompt-bench · MIT