fenic.core.types.semantic_examples
Module for handling semantic examples in query processing.
This module provides classes and utilities for building, managing, and validating semantic examples used in query processing.
Classes:
-
BaseExampleCollection–Abstract base class for all semantic example collections.
-
ClassifyExample–A single semantic example for classification operations.
-
ClassifyExampleCollection–Collection of text-to-category examples for classification operations.
-
JoinExample–A single semantic example for semantic join operations.
-
JoinExampleCollection–Collection of comparison examples for semantic join operations.
-
MapExample–A single semantic example for semantic mapping operations.
-
MapExampleCollection–Collection of input-output examples for semantic map operations.
-
PredicateExample–A single semantic example for semantic predicate operations.
-
PredicateExampleCollection–Collection of input-to-boolean examples for predicate operations.
BaseExampleCollection
BaseExampleCollection(examples: List[ExampleType] = None)
Bases: ABC, Generic[ExampleType]
Abstract base class for all semantic example collections.
Semantic examples demonstrate the expected input-output relationship for a given task, helping guide language models to produce consistent and accurate responses. Each example consists of inputs and the corresponding expected output.
These examples are particularly valuable for:
- Demonstrating the expected reasoning pattern
- Showing correct output formats
- Handling edge cases through demonstration
- Improving model performance without changing the underlying model
Initialize a collection of semantic examples.
Parameters:
-
examples(List[ExampleType], default:None) –Optional list of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.
Note
The examples list is initialized as empty if no examples are provided. Each example in the provided list will be processed through create_example() to ensure proper formatting and validation.
Methods:
-
create_example–Create an example in the collection.
-
from_pandas–Create a collection from a Pandas DataFrame.
-
from_polars–Create a collection from a Polars DataFrame.
-
to_pandas–Convert the collection to a Pandas DataFrame.
-
to_polars–Convert the collection to a Polars DataFrame.
Source code in src/fenic/core/types/semantic_examples.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
create_example
create_example(example: ExampleType) -> BaseExampleCollection
Create an example in the collection.
example: The semantic example to add. Must be an instance of the collection's example_class.
Returns:
-
BaseExampleCollection–Self for method chaining.
Source code in src/fenic/core/types/semantic_examples.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
from_pandas
classmethod
from_pandas(df: DataFrame) -> BaseExampleCollection
Create a collection from a Pandas DataFrame.
Parameters:
-
df(DataFrame) –The Pandas DataFrame containing example data. The specific column structure requirements depend on the concrete collection type.
Returns:
-
BaseExampleCollection–A new example collection populated with examples from the DataFrame.
Raises:
-
InvalidExampleCollectionError–If the DataFrame's structure doesn't match the expected format for this collection type.
Source code in src/fenic/core/types/semantic_examples.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
from_polars
abstractmethod
classmethod
from_polars(df: DataFrame) -> BaseExampleCollection
Create a collection from a Polars DataFrame.
Parameters:
-
df(DataFrame) –The Polars DataFrame containing example data. The specific column structure requirements depend on the concrete collection type.
Returns:
-
BaseExampleCollection–A new example collection populated with examples from the DataFrame.
Raises:
-
InvalidExampleCollectionError–If the DataFrame's structure doesn't match the expected format for this collection type.
Source code in src/fenic/core/types/semantic_examples.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
to_pandas
to_pandas() -> pd.DataFrame
Convert the collection to a Pandas DataFrame.
Returns:
-
DataFrame–A Pandas DataFrame representing the collection's examples.
-
DataFrame–Returns an empty DataFrame if the collection contains no examples.
Source code in src/fenic/core/types/semantic_examples.py
199 200 201 202 203 204 205 206 207 | |
to_polars
to_polars() -> pl.DataFrame
Convert the collection to a Polars DataFrame.
Returns:
-
DataFrame–A Polars DataFrame representing the collection's examples.
-
DataFrame–Returns an empty DataFrame if the collection contains no examples.
Source code in src/fenic/core/types/semantic_examples.py
189 190 191 192 193 194 195 196 197 | |
ClassifyExample
Bases: BaseModel
A single semantic example for classification operations.
Classify examples demonstrate the classification of an input string into a specific category string, used in a semantic.classify operation.
ClassifyExampleCollection
ClassifyExampleCollection(examples: List[ExampleType] = None)
Bases: BaseExampleCollection[ClassifyExample]
Collection of text-to-category examples for classification operations.
Stores examples showing which category each input text should be assigned to. Each example contains an input string and its corresponding category label.
Methods:
-
from_polars–Create collection from a Polars DataFrame. Must have an 'output' column and an 'input' column.
Source code in src/fenic/core/types/semantic_examples.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
from_polars
classmethod
from_polars(df: DataFrame) -> ClassifyExampleCollection
Create collection from a Polars DataFrame. Must have an 'output' column and an 'input' column.
Source code in src/fenic/core/types/semantic_examples.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
JoinExample
Bases: BaseModel
A single semantic example for semantic join operations.
Join examples demonstrate the evaluation of two input variables across different datasets against a specific condition, used in a semantic.join operation.
JoinExampleCollection
JoinExampleCollection(examples: List[JoinExample] = None)
Bases: BaseExampleCollection[JoinExample]
Collection of comparison examples for semantic join operations.
Stores examples showing which pairs of values should be considered matches for joining data. Each example contains a left value, right value, and boolean output indicating whether they match.
Initialize a collection of semantic join examples.
Parameters:
-
examples(List[JoinExample], default:None) –List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.
Methods:
-
create_example–Create an example in the collection with type validation.
-
from_polars–Create collection from a Polars DataFrame. Must have 'left_on', 'right_on', and 'output' columns.
Source code in src/fenic/core/types/semantic_examples.py
566 567 568 569 570 571 572 573 574 575 | |
create_example
create_example(example: JoinExample) -> JoinExampleCollection
Create an example in the collection with type validation.
Validates that left_on and right_on values have consistent types across examples. The first example establishes the types and cannot have None values. Subsequent examples must have matching types but can have None values.
Parameters:
-
example(JoinExample) –The JoinExample to add.
Returns:
-
JoinExampleCollection–Self for method chaining.
Raises:
-
InvalidExampleCollectionError–If the example type is wrong, if the first example contains None values, or if subsequent examples have type mismatches.
Source code in src/fenic/core/types/semantic_examples.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |
from_polars
classmethod
from_polars(df: DataFrame) -> JoinExampleCollection
Create collection from a Polars DataFrame. Must have 'left_on', 'right_on', and 'output' columns.
Source code in src/fenic/core/types/semantic_examples.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
MapExample
Bases: BaseModel
A single semantic example for semantic mapping operations.
Map examples demonstrate the transformation of input variables to a specific output string or structured model used in a semantic.map operation.
MapExampleCollection
MapExampleCollection(examples: List[MapExample] = None)
Bases: BaseExampleCollection[MapExample]
Collection of input-output examples for semantic map operations.
Stores examples that demonstrate how input data should be transformed into output text or structured data. Each example shows the expected output for a given set of input fields.
Initialize a collection of semantic map examples.
Parameters:
-
examples(List[MapExample], default:None) –List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.
Methods:
-
create_example–Create an example in the collection with output and input type validation.
-
from_polars–Create collection from a Polars DataFrame. Must have an 'output' column and at least one input column.
Source code in src/fenic/core/types/semantic_examples.py
258 259 260 261 262 263 264 265 266 267 | |
create_example
create_example(example: MapExample) -> MapExampleCollection
Create an example in the collection with output and input type validation.
Ensures all examples in the collection have consistent output types (either all strings or all BaseModel instances) and validates that input fields have consistent types across examples.
For input validation: - The first example establishes the schema and cannot have None values - Subsequent examples must have the same fields but can have None values - Non-None values must match the established type for each field
Parameters:
-
example(MapExample) –The MapExample to add.
Returns:
-
MapExampleCollection–Self for method chaining.
Raises:
-
InvalidExampleCollectionError–If the example output type doesn't match the existing examples in the collection, if the first example contains None values, or if subsequent examples have type mismatches.
Source code in src/fenic/core/types/semantic_examples.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
from_polars
classmethod
from_polars(df: DataFrame) -> MapExampleCollection
Create collection from a Polars DataFrame. Must have an 'output' column and at least one input column.
Source code in src/fenic/core/types/semantic_examples.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
PredicateExample
Bases: BaseModel
A single semantic example for semantic predicate operations.
Predicate examples demonstrate the evaluation of input variables against a specific condition, used in a semantic.predicate operation.
PredicateExampleCollection
PredicateExampleCollection(examples: List[PredicateExample] = None)
Bases: BaseExampleCollection[PredicateExample]
Collection of input-to-boolean examples for predicate operations.
Stores examples showing which inputs should evaluate to True or False based on some condition. Each example contains input fields and a boolean output indicating whether the condition holds.
Initialize a collection of semantic predicate examples.
Parameters:
-
examples(List[PredicateExample], default:None) –List of examples to add to the collection. Each example will be processed through create_example() to ensure proper formatting and validation.
Methods:
-
create_example–Create an example in the collection with input type validation.
-
from_polars–Create collection from a Polars DataFrame.
Source code in src/fenic/core/types/semantic_examples.py
463 464 465 466 467 468 469 470 471 472 | |
create_example
create_example(example: PredicateExample) -> PredicateExampleCollection
Create an example in the collection with input type validation.
Validates that input fields have consistent types across examples. The first example establishes the schema and cannot have None values. Subsequent examples must have the same fields but can have None values.
Parameters:
-
example(PredicateExample) –The PredicateExample to add.
Returns:
-
PredicateExampleCollection–Self for method chaining.
Raises:
-
InvalidExampleCollectionError–If the example type is wrong, if the first example contains None values, or if subsequent examples have type mismatches.
Source code in src/fenic/core/types/semantic_examples.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
from_polars
classmethod
from_polars(df: DataFrame) -> PredicateExampleCollection
Create collection from a Polars DataFrame.
Source code in src/fenic/core/types/semantic_examples.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |