Skip to content

Refiner Class

The Refiner class is the core pipeline builder that allows you to chain multiple operations together.

prompt_refiner.refiner.Refiner

Bases: ABC

Base class for all prompt refining operations.

Functions

process abstractmethod

process(text)

Process the input text.

Parameters:

Name Type Description Default
text str

The input text to process

required

Returns:

Type Description
str

The processed text

Source code in src/prompt_refiner/refiner.py
@abstractmethod
def process(self, text: str) -> str:
    """
    Process the input text.

    Args:
        text: The input text to process

    Returns:
        The processed text
    """
    pass

__or__

__or__(other)

Support pipe operator syntax for composing refiners.

Enables LangChain-style pipeline composition: refiner1 | refiner2 | refiner3

Parameters:

Name Type Description Default
other Refiner

The refiner to chain with this refiner

required

Returns:

Type Description
Pipeline

A Pipeline containing both refiners

Example

from prompt_refiner import StripHTML, NormalizeWhitespace pipeline = StripHTML() | NormalizeWhitespace() result = pipeline.run("

hello
")

Returns: "hello"
Source code in src/prompt_refiner/refiner.py
def __or__(self, other: "Refiner") -> "Pipeline":
    """
    Support pipe operator syntax for composing refiners.

    Enables LangChain-style pipeline composition: refiner1 | refiner2 | refiner3

    Args:
        other: The refiner to chain with this refiner

    Returns:
        A Pipeline containing both refiners

    Example:
        >>> from prompt_refiner import StripHTML, NormalizeWhitespace
        >>> pipeline = StripHTML() | NormalizeWhitespace()
        >>> result = pipeline.run("<div>  hello  </div>")
        >>> # Returns: "hello"
    """
    from .pipeline import Pipeline

    return Pipeline().pipe(self).pipe(other)

Usage Examples

from prompt_refiner import StripHTML, NormalizeWhitespace

# Create a pipeline using the pipe operator
pipeline = (
    StripHTML()
    | NormalizeWhitespace()
)

# Execute the pipeline
result = pipeline.run("<p>Hello   World!</p>")
print(result)  # "Hello World!"

Fluent API with .pipe()

The Refiner class supports method chaining with .pipe():

from prompt_refiner import Refiner, StripHTML, NormalizeWhitespace

# Create a pipeline using the fluent API
pipeline = (
    Refiner()
    .pipe(StripHTML())
    .pipe(NormalizeWhitespace())
)

# Execute the pipeline
result = pipeline.run("<p>Hello   World!</p>")
print(result)  # "Hello World!"

Both approaches work identically - choose the one that fits your style.

Pipeline Execution

When you call run(text), the Refiner:

  1. Takes the input text
  2. Passes it through each operation in sequence
  3. Each operation's output becomes the next operation's input
  4. Returns the final processed text
# Pipeline: text → Operation1 → Operation2 → Operation3 → result
result = refiner.run(text)