Summarization
The Summarization task generates concise summaries of the documents.
Usage
from sieves import tasks
task = tasks.Summarization(
model=model,
n_words=100, # Guideline for summary length
)
Results
The Summarization task returns a unified Result object containing the summary and a confidence score.
Confidence scores are self-reported by LLMs and may be None.
class Result(pydantic.BaseModel):
"""Result of a summarization task. Contains the generated summary and a confidence score.
Attributes:
summary: Summary of text.
score: Confidence score.
"""
summary: str = pydantic.Field(description="The generated summary of the input text.")
score: float | None = pydantic.Field(
default=None, description="Provide a confidence score for the generated summary, between 0 and 1."
)
Evaluation
Because summarization is a generative task, evaluation requires a "judge" model to assess semantic similarity.
- Metric: LLM Score (
LLM Score). A model-based similarity score (0.0 to 1.0) provided by a DSPy judge. - Requirement: Each document must have a ground-truth summary stored in
doc.gold[task_id]. - Judge: You must provide a
dspy.LMinstance to theevaluate()method.
# Use an LLM as a judge to evaluate the summaries
report = task.evaluate(docs, judge=dspy_judge)
print(f"Summary Similarity: {report.metrics['LLM Score']}")
Ground Truth Formats
Ground truth has to be specified in doc.meta using Result instances.
Text summarization predictive task.
Summarization
Bases: PredictiveTask[TaskPromptSignature, TaskResult, _TaskBridge]
Summarize documents to a target length using structured model wrappers.
Source code in sieves/tasks/predictive/summarization/core.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
fewshot_example_type
property
fewshot_examples
property
Return few-shot examples.
Returns:
| Type | Description |
|---|---|
Sequence[FewshotExample]
|
Few-shot examples. |
id
property
Return task ID.
Used by pipeline for results and dependency management.
Returns:
| Type | Description |
|---|---|
str
|
Task ID. |
metric
property
Return metric name.
Returns:
| Type | Description |
|---|---|
str
|
Metric name. |
prompt_signature_description
property
Return prompt signature description.
Returns:
| Type | Description |
|---|---|
str | None
|
Prompt signature description. |
prompt_template
property
Return prompt template.
Returns:
| Type | Description |
|---|---|
str
|
Prompt template. |
__add__(other)
Chain this task with another task or pipeline using the + operator.
This returns a new Pipeline that executes this task first, followed by the
task(s) in other. The original task(s)/pipeline are not mutated.
Cache semantics:
- If other is a Pipeline, the resulting pipeline adopts other's
use_cache setting (because the left-hand side is a single task).
- If other is a Task, the resulting pipeline defaults to use_cache=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Task | Pipeline
|
A |
required |
Returns:
| Type | Description |
|---|---|
Pipeline
|
A new |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in sieves/tasks/core.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
__call__(docs)
Execute task with conditional logic.
Checks the condition for each document without materializing all docs upfront. Passes all documents that pass the condition to _call() for proper batching. Documents that fail the condition have results[task_id] set to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Iterable[Doc]
|
Docs to process. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[Doc]
|
Processed docs (in original order). |
Source code in sieves/tasks/core.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
__init__(n_words, model, task_id=None, include_meta=True, batch_size=-1, overwrite=False, prompt_instructions=None, fewshot_examples=(), model_settings=ModelSettings(), condition=None)
Initialize new Summarization task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_words
|
int
|
Maximal number of words (consider this a guideline, not a strict limit). |
required |
model
|
TaskModel
|
Model to use. |
required |
task_id
|
str | None
|
Task ID. |
None
|
include_meta
|
bool
|
Whether to include meta information generated by the task. |
True
|
batch_size
|
int
|
Batch size to use for inference. Use -1 to process all documents at once. |
-1
|
overwrite
|
bool
|
Some tasks, e.g. anonymization or translation, output a modified version of the input text. If True, these tasks overwrite the original document text. If False, the result will just be stored in the documents' |
False
|
prompt_instructions
|
str | None
|
Custom prompt instructions. If None, default instructions are used. |
None
|
fewshot_examples
|
Sequence[FewshotExample]
|
Few-shot examples. |
()
|
model_settings
|
ModelSettings
|
Settings for structured generation. Use the |
ModelSettings()
|
condition
|
Callable[[Doc], bool] | None
|
Optional callable that determines whether to process each document. |
None
|
Source code in sieves/tasks/predictive/summarization/core.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
deserialize(config, **kwargs)
classmethod
Generate PredictiveTask instance from config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
Config to generate instance from. |
required |
kwargs
|
dict[str, Any]
|
Values to inject into loaded config. |
{}
|
Returns:
| Type | Description |
|---|---|
PredictiveTask[TaskPromptSignature, TaskResult, TaskBridge]
|
Deserialized PredictiveTask instance. |
Source code in sieves/tasks/predictive/core.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
evaluate(docs, judge=None, failure_threshold=0.5)
Evaluate task performance using DSPy-based evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Iterable[Doc]
|
Documents to evaluate. |
required |
judge
|
LM | None
|
Optional DSPy LM instance to use as judge for generative tasks. |
None
|
failure_threshold
|
float
|
Decision threshold for whether to mark predicitions as failures. |
0.5
|
Returns:
| Type | Description |
|---|---|
TaskEvaluationReport
|
Evaluation report. |
Source code in sieves/tasks/predictive/core.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
optimize(optimizer, verbose=True)
Optimize task prompt and few-shot examples with the available optimization config.
Updates task to use best prompt and few-shot examples found by the optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
Optimizer to run. |
required |
verbose
|
bool
|
Whether to suppress output. DSPy produces a good amount of logs, so this can be useful to not pollute your terminal. Only warnings and errors will be printed. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[str, Sequence[FewshotExample]]
|
Best found prompt and few-shot examples. |
Source code in sieves/tasks/predictive/core.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
serialize()
Serialize task.
Returns:
| Type | Description |
|---|---|
Config
|
Config instance. |
Source code in sieves/tasks/core.py
147 148 149 150 151 152 | |
Bridges for summarization task.
DSPySummarization
Bases: SummarizationBridge[PromptSignature, Result, InferenceMode]
DSPy bridge for summarization.
Source code in sieves/tasks/predictive/summarization/bridges.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
model_settings
property
Return model settings.
Returns:
| Type | Description |
|---|---|
ModelSettings
|
Model settings. |
model_type
property
Return model type.
Returns:
| Type | Description |
|---|---|
ModelType
|
Model type. |
prompt_signature
property
Create output signature.
E.g.: Signature in DSPy, Pydantic objects in outlines, JSON schema in jsonformers.
This is model type-specific.
Returns:
| Type | Description |
|---|---|
type[TaskPromptSignature] | TaskPromptSignature
|
Output signature object. This can be an instance (e.g. a regex string) or a class (e.g. a Pydantic class). |
prompt_template
property
Return prompt template.
Chains _prompt_instructions, _prompt_example_xml and _prompt_conclusion.
Note: different model have different expectations as to how a prompt should look like. E.g. outlines supports the Jinja 2 templating format for insertion of values and few-shot examples, whereas DSPy integrates these things in a different value in the workflow and hence expects the prompt not to include these things. Mind model-specific expectations when creating a prompt template.
Returns:
| Type | Description |
|---|---|
str
|
Prompt template as string. None if not used by model wrapper. |
__init__(task_id, prompt_instructions, n_words, overwrite, model_settings, prompt_signature, model_type, fewshot_examples=())
Initialize summarization bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
Task ID. |
required |
prompt_instructions
|
str | None
|
Custom prompt instructions. If None, default instructions are used. |
required |
n_words
|
int
|
Maximum number of words for summary. |
required |
overwrite
|
bool
|
Whether to overwrite original text with summary. |
required |
model_settings
|
ModelSettings
|
Settings for structured generation. |
required |
prompt_signature
|
type[BaseModel]
|
Unified Pydantic prompt signature. |
required |
model_type
|
ModelType
|
Model type. |
required |
fewshot_examples
|
Sequence[BaseModel]
|
Few-shot examples. |
()
|
Source code in sieves/tasks/predictive/summarization/bridges.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
extract(docs)
Extract all values from doc instances that are to be injected into the prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Sequence[Doc]
|
Docs to extract values from. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[dict[str, Any]]
|
All values from doc instances that are to be injected into the prompts as a sequence. |
Source code in sieves/tasks/predictive/bridges.py
178 179 180 181 182 183 184 | |
PydanticSummarization
Bases: SummarizationBridge[BaseModel, BaseModel, ModelWrapperInferenceMode]
Pydantic-based summarization bridge.
Source code in sieves/tasks/predictive/summarization/bridges.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
model_settings
property
Return model settings.
Returns:
| Type | Description |
|---|---|
ModelSettings
|
Model settings. |
model_type
property
Return model type.
Returns:
| Type | Description |
|---|---|
ModelType
|
Model type. |
prompt_signature
property
Create output signature.
E.g.: Signature in DSPy, Pydantic objects in outlines, JSON schema in jsonformers.
This is model type-specific.
Returns:
| Type | Description |
|---|---|
type[TaskPromptSignature] | TaskPromptSignature
|
Output signature object. This can be an instance (e.g. a regex string) or a class (e.g. a Pydantic class). |
prompt_template
property
Return prompt template.
Chains _prompt_instructions, _prompt_example_xml and _prompt_conclusion.
Note: different model have different expectations as to how a prompt should look like. E.g. outlines supports the Jinja 2 templating format for insertion of values and few-shot examples, whereas DSPy integrates these things in a different value in the workflow and hence expects the prompt not to include these things. Mind model-specific expectations when creating a prompt template.
Returns:
| Type | Description |
|---|---|
str
|
Prompt template as string. None if not used by model wrapper. |
__init__(task_id, prompt_instructions, n_words, overwrite, model_settings, prompt_signature, model_type, fewshot_examples=())
Initialize summarization bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
Task ID. |
required |
prompt_instructions
|
str | None
|
Custom prompt instructions. If None, default instructions are used. |
required |
n_words
|
int
|
Maximum number of words for summary. |
required |
overwrite
|
bool
|
Whether to overwrite original text with summary. |
required |
model_settings
|
ModelSettings
|
Settings for structured generation. |
required |
prompt_signature
|
type[BaseModel]
|
Unified Pydantic prompt signature. |
required |
model_type
|
ModelType
|
Model type. |
required |
fewshot_examples
|
Sequence[BaseModel]
|
Few-shot examples. |
()
|
Source code in sieves/tasks/predictive/summarization/bridges.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
extract(docs)
Extract all values from doc instances that are to be injected into the prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Sequence[Doc]
|
Docs to extract values from. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[dict[str, Any]]
|
All values from doc instances that are to be injected into the prompts as a sequence. |
Source code in sieves/tasks/predictive/bridges.py
178 179 180 181 182 183 184 | |
SummarizationBridge
Bases: Bridge[_BridgePromptSignature, _BridgeResult, ModelWrapperInferenceMode], ABC
Abstract base class for summarization bridges.
Source code in sieves/tasks/predictive/summarization/bridges.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
inference_mode
abstractmethod
property
Return inference mode.
Returns:
| Type | Description |
|---|---|
ModelWrapperInferenceMode
|
Inference mode. |
model_settings
property
Return model settings.
Returns:
| Type | Description |
|---|---|
ModelSettings
|
Model settings. |
model_type
property
Return model type.
Returns:
| Type | Description |
|---|---|
ModelType
|
Model type. |
prompt_signature
property
Create output signature.
E.g.: Signature in DSPy, Pydantic objects in outlines, JSON schema in jsonformers.
This is model type-specific.
Returns:
| Type | Description |
|---|---|
type[TaskPromptSignature] | TaskPromptSignature
|
Output signature object. This can be an instance (e.g. a regex string) or a class (e.g. a Pydantic class). |
prompt_template
property
Return prompt template.
Chains _prompt_instructions, _prompt_example_xml and _prompt_conclusion.
Note: different model have different expectations as to how a prompt should look like. E.g. outlines supports the Jinja 2 templating format for insertion of values and few-shot examples, whereas DSPy integrates these things in a different value in the workflow and hence expects the prompt not to include these things. Mind model-specific expectations when creating a prompt template.
Returns:
| Type | Description |
|---|---|
str
|
Prompt template as string. None if not used by model wrapper. |
__init__(task_id, prompt_instructions, n_words, overwrite, model_settings, prompt_signature, model_type, fewshot_examples=())
Initialize summarization bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
Task ID. |
required |
prompt_instructions
|
str | None
|
Custom prompt instructions. If None, default instructions are used. |
required |
n_words
|
int
|
Maximum number of words for summary. |
required |
overwrite
|
bool
|
Whether to overwrite original text with summary. |
required |
model_settings
|
ModelSettings
|
Settings for structured generation. |
required |
prompt_signature
|
type[BaseModel]
|
Unified Pydantic prompt signature. |
required |
model_type
|
ModelType
|
Model type. |
required |
fewshot_examples
|
Sequence[BaseModel]
|
Few-shot examples. |
()
|
Source code in sieves/tasks/predictive/summarization/bridges.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
consolidate(results, docs_offsets)
abstractmethod
Consolidate results for document chunks into document results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Sequence[TaskResult]
|
Results per document chunk. |
required |
docs_offsets
|
list[tuple[int, int]]
|
Chunk offsets per document. Chunks per document can be obtained with |
required |
Returns:
| Type | Description |
|---|---|
Sequence[TaskResult]
|
Results per document as a sequence. |
Source code in sieves/tasks/predictive/bridges.py
195 196 197 198 199 200 201 202 203 | |
extract(docs)
Extract all values from doc instances that are to be injected into the prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Sequence[Doc]
|
Docs to extract values from. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[dict[str, Any]]
|
All values from doc instances that are to be injected into the prompts as a sequence. |
Source code in sieves/tasks/predictive/bridges.py
178 179 180 181 182 183 184 | |
integrate(results, docs)
abstractmethod
Integrate results into Doc instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Sequence[TaskResult]
|
Results from prompt executable. |
required |
docs
|
list[Doc]
|
Doc instances to update. |
required |
Returns:
| Type | Description |
|---|---|
list[Doc]
|
Updated doc instances as a list. |
Source code in sieves/tasks/predictive/bridges.py
186 187 188 189 190 191 192 193 | |
Schemas for summarization task.
FewshotExample
Bases: FewshotExample
Example for summarization few-shot prompting.
Attributes: text: Input text. n_words: Guideline for summary length. summary: Summary of text. score: Confidence score.
Source code in sieves/tasks/predictive/schemas/summarization.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
input_fields
property
Return input fields.
Returns:
| Type | Description |
|---|---|
Sequence[str]
|
Input fields. |
target_fields
property
Return target fields.
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Target fields. |
from_dspy(example)
classmethod
Convert from dspy.Example.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
example
|
Example
|
Example as |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Example as |
Source code in sieves/tasks/predictive/schemas/core.py
63 64 65 66 67 68 69 70 | |
to_dspy()
Convert to dspy.Example.
Returns:
| Type | Description |
|---|---|
Example
|
Example as |
Source code in sieves/tasks/predictive/schemas/core.py
56 57 58 59 60 61 | |
Result
Bases: BaseModel
Result of a summarization task. Contains the generated summary and a confidence score.
Attributes: summary: Summary of text. score: Confidence score.
Source code in sieves/tasks/predictive/schemas/summarization.py
46 47 48 49 50 51 52 53 54 55 56 57 | |