The ExampleData class represents a single training or example data instance used for few-shot learning and structured prompting. It pairs raw input text with its corresponding extractions.
Constructor
ExampleData(
text: str,
extractions: list[Extraction] = []
)
The raw input text (sentence, paragraph, document, etc.) to be annotated
A list of Extraction objects representing information extracted from the text
Attributes
The raw input text (sentence, paragraph, document, etc.)
A list of Extraction objects extracted from the text. Defaults to an empty list if not provided.
Example
Basic Usage
from langextract.core.data import ExampleData, Extraction
# Create example data with extractions
example = ExampleData(
text="Apple Inc. was founded by Steve Jobs in Cupertino.",
extractions=[
Extraction(
extraction_class="ORGANIZATION",
extraction_text="Apple Inc."
),
Extraction(
extraction_class="PERSON",
extraction_text="Steve Jobs"
),
Extraction(
extraction_class="LOCATION",
extraction_text="Cupertino"
)
]
)
print(example.text)
print(f"Found {len(example.extractions)} extractions")
Few-Shot Learning
from langextract import Extractor
from langextract.core.data import ExampleData, Extraction
# Create training examples
examples = [
ExampleData(
text="Microsoft was founded by Bill Gates in 1975.",
extractions=[
Extraction(extraction_class="ORG", extraction_text="Microsoft"),
Extraction(extraction_class="PERSON", extraction_text="Bill Gates"),
Extraction(extraction_class="DATE", extraction_text="1975")
]
),
ExampleData(
text="Tesla is headquartered in Austin, Texas.",
extractions=[
Extraction(extraction_class="ORG", extraction_text="Tesla"),
Extraction(extraction_class="LOCATION", extraction_text="Austin"),
Extraction(extraction_class="LOCATION", extraction_text="Texas")
]
)
]
# Use examples with an extractor
extractor = Extractor(
extraction_classes=["ORG", "PERSON", "LOCATION", "DATE"],
examples=examples
)
result = extractor.run("Amazon was started by Jeff Bezos in Seattle.")
Use Cases
- Few-shot learning: Provide examples to guide the LLM’s extraction behavior
- Training data: Collect and store labeled data for model fine-tuning
- Testing: Create test cases with expected extractions for validation
- Prompt engineering: Demonstrate desired extraction patterns to the model
- Extraction - Represents individual extractions
- Document - Input documents for extraction
- extract() - Main function that uses ExampleData for few-shot learning