Update README.md
Browse files
README.md
CHANGED
@@ -18,7 +18,79 @@ Given a document, this embedding model helps retrieve instruction templates from
|
|
18 |
## Example Usage
|
19 |
|
20 |
```python3
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
```
|
23 |
|
24 |
---
|
|
|
18 |
## Example Usage
|
19 |
|
20 |
```python3
|
21 |
+
import importlib
|
22 |
+
import json
|
23 |
+
from huggingface_hub import hf_hub_download
|
24 |
|
25 |
+
|
26 |
+
def download_and_import_module(module_name, variable):
|
27 |
+
module = importlib.util.module_from_spec(
|
28 |
+
importlib.util.spec_from_file_location(
|
29 |
+
module_name,
|
30 |
+
hf_hub_download(
|
31 |
+
repo_id="fineinstructions/matching_embedding",
|
32 |
+
filename=f"{module_name}.py",
|
33 |
+
),
|
34 |
+
)
|
35 |
+
)
|
36 |
+
module.__spec__.loader.exec_module(module)
|
37 |
+
return getattr(module, variable)
|
38 |
+
|
39 |
+
|
40 |
+
# Import the retriever helper class
|
41 |
+
InstructionTemplateRetriever = download_and_import_module("instruction_template_retriever", "InstructionTemplateRetriever")
|
42 |
+
|
43 |
+
# Prepare an example document
|
44 |
+
EXAMPLE_DOC = """
|
45 |
+
Title: Surprising Facts about Pigeons
|
46 |
+
Submitted On: September 24, 2008
|
47 |
+
|
48 |
+
Fact 1:
|
49 |
+
During World War I, a homing pigeon named Cher Ami played a critical role in saving nearly 200 soldiers who were trapped behind enemy lines.
|
50 |
+
Despite being injured by enemy fire, Cher Ami managed to deliver a crucial message that led to their rescue. For this act of bravery, the
|
51 |
+
French government awarded the pigeon the Croix de Guerre, a military medal of honor. Cher Ami became a symbol of courage and the extraordinary
|
52 |
+
utility of pigeons in wartime communication.
|
53 |
+
|
54 |
+
Fact 2:
|
55 |
+
Pigeons possess impressive cognitive abilities, one of the most surprising being their capacity for self-recognition in mirrors. This
|
56 |
+
trait is rare in the animal kingdom and is often considered a marker of higher intelligence. Experiments have shown that pigeons can distinguish
|
57 |
+
themselves from other birds when looking into a mirror, suggesting a level of self-awareness previously thought to be unique to primates and a
|
58 |
+
few other animals.
|
59 |
+
|
60 |
+
Fact 3:
|
61 |
+
Thanks to centuries of selective breeding, there are now more than 300 recognized breeds of domestic pigeon. These range from show pigeons with
|
62 |
+
elaborate feather patterns and head crests to performance breeds used in tumbling and racing. The sheer variety reflects the bird’s long history
|
63 |
+
as a companion species to humans.
|
64 |
+
|
65 |
+
Fact 4:
|
66 |
+
The Ancient Romans were known for their elaborate grooming rituals, and pigeons played an unexpected role in their beauty routines. Specifically,
|
67 |
+
they used pigeon droppings as a bleaching agent to style and lighten their hair. This unusual practice was part of the broader Roman obsession with
|
68 |
+
fashion and appearance, demonstrating how even the most unexpected materials found a place in early cosmetic treatments.
|
69 |
+
"""
|
70 |
+
|
71 |
+
|
72 |
+
# Retrieve relevant instruction templates to different chunks / sections of a document
|
73 |
+
retriever = InstructionTemplateRetriever(
|
74 |
+
coverage_chunks=4, sigma=0.05, alpha=1.0
|
75 |
+
) # 4 chunks/sections
|
76 |
+
print(json.dumps(retriever.search(document=EXAMPLE_DOC), indent=4))
|
77 |
+
|
78 |
+
# Results look like:
|
79 |
+
|
80 |
+
# Instruction Templates for Entire Document:
|
81 |
+
# - "What's something <fi>a few word description of something remarkable or noteworthy</fi> you can tell me"
|
82 |
+
|
83 |
+
# Instruction Templates for Chunk 1/4 of the Document:
|
84 |
+
# - "write a <fi>a few word description of the type of message</fi> for <fi>a significant achievement or milestone</fi>"
|
85 |
+
|
86 |
+
# Instruction Templates for Chunk 2/4 of the Document:
|
87 |
+
# - "how are <fi>a type of organism or entity</fi> so <fi>exceptionally strong or notable in some way</fi>?"
|
88 |
+
|
89 |
+
# Instruction Templates for Chunk 3/4 of the Document:
|
90 |
+
# - "what are the common <fi>a type of organism, creature, or entity</fi>?"
|
91 |
+
|
92 |
+
# Instruction Templates for Chunk 4/4 of the Document:
|
93 |
+
# - "how did <fi>a group of people</fi> <fi>perform a common practice or activity</fi>"
|
94 |
```
|
95 |
|
96 |
---
|