Views
No views yet
compatible_document_description of an instruction template.datasets
faiss
huggingface_hub
numpy
pandas
sentence_transformers1import importlib
2import json
3from huggingface_hub import hf_hub_download
4
5
6def download_and_import_module(module_name, variable):
7 module = importlib.util.module_from_spec(
8 importlib.util.spec_from_file_location(
9 module_name,
10 hf_hub_download(
11 repo_id="fineinstructions/instruction_template_retrieval_embedding",
12 filename=f"{module_name}.py",
13 ),
14 )
15 )
16 module.__spec__.loader.exec_module(module)
17 return getattr(module, variable)
18
19
20# Import the retriever helper class
21InstructionTemplateRetriever = download_and_import_module("instruction_template_retriever", "InstructionTemplateRetriever")
22
23# Prepare an example document
24EXAMPLE_DOC = """
25Title: Surprising Facts about Pigeons
26Submitted On: September 24, 2008
27
28Fact 1:
29During World War I, a homing pigeon named Cher Ami played a critical role in saving nearly 200 soldiers who were trapped behind enemy lines.
30Despite being injured by enemy fire, Cher Ami managed to deliver a crucial message that led to their rescue. For this act of bravery, the
31French government awarded the pigeon the Croix de Guerre, a military medal of honor. Cher Ami became a symbol of courage and the extraordinary
32utility of pigeons in wartime communication.
33
34Fact 2:
35Pigeons possess impressive cognitive abilities, one of the most surprising being their capacity for self-recognition in mirrors. This
36trait is rare in the animal kingdom and is often considered a marker of higher intelligence. Experiments have shown that pigeons can distinguish
37themselves from other birds when looking into a mirror, suggesting a level of self-awareness previously thought to be unique to primates and a
38few other animals.
39
40Fact 3:
41Thanks to centuries of selective breeding, there are now more than 300 recognized breeds of domestic pigeon. These range from show pigeons with
42elaborate feather patterns and head crests to performance breeds used in tumbling and racing. The sheer variety reflects the bird’s long history
43as a companion species to humans.
44
45Fact 4:
46The Ancient Romans were known for their elaborate grooming rituals, and pigeons played an unexpected role in their beauty routines. Specifically,
47they used pigeon droppings as a bleaching agent to style and lighten their hair. This unusual practice was part of the broader Roman obsession with
48fashion and appearance, demonstrating how even the most unexpected materials found a place in early cosmetic treatments.
49"""
50
51
52# Retrieve relevant instruction templates to different chunks / sections of a document
53retriever = InstructionTemplateRetriever(
54 coverage_chunks=4, sigma=0.05, alpha=1.0 # Ensure instruction templates cover information in the document with 4 chunks/sections
55)
56print(json.dumps(retriever.search(document=EXAMPLE_DOC), indent=4))
57
58# ******************************************************
59# Retrieval results look like:
60# ******************************************************
61
62# Instruction Templates for Entire Document:
63# - "What's something <fi>a few word description of something remarkable or noteworthy</fi> you can tell me"
64
65# Instruction Templates for Chunk 1/4 of the Document:
66# - "write a <fi>a few word description of the type of message</fi> for <fi>a significant achievement or milestone</fi>"
67
68# Instruction Templates for Chunk 2/4 of the Document:
69# - "how are <fi>a type of organism or entity</fi> so <fi>exceptionally strong or notable in some way</fi>?"
70
71# Instruction Templates for Chunk 3/4 of the Document:
72# - "what are the common <fi>a type of organism, creature, or entity</fi>?"
73
74# Instruction Templates for Chunk 4/4 of the Document:
75# - "how did <fi>a group of people</fi> <fi>perform a common practice or activity</fi>"
76
77# ******************************************************
78# Increasing diversity:
79# -----------------------
80# You can increase diversity using the `reweight` parameter
81# to increase diversity in instruction length like so:
82# `print(json.dumps(retriever.search(document=EXAMPLE_DOC, reweight=True), indent=4))`
83# ******************************************************
84
85# ******************************************************
86# Documentation:
87# -----------------------
88# You can read the full documentation of the `InstructionTemplateRetriever.search` method:
89# by opening/reading the instruction_template_retriever.py file here:
90# https://huggingface.co/fineinstructions/instruction_template_retrieval_embedding/tree/main
91# ******************************************************@article{patel2026fineinstructions,
title={FineInstructions: Scaling Synthetic Instructions to Pre-Training Scale},
author={Patel, Ajay and Raffel, Colin and Callison-Burch, Chris},
journal={arXiv preprint arXiv:2601.22146},
year={2026},
archivePrefix={arXiv},
primaryClass={cs.CL},
doi={10.48550/arXiv.2601.22146}
}