Skip to content

Programs

Programs

A Program is the base class used for dynamically formatting prompts and returning unconstrained/constrained generation results. At it's core, a Program should be a callable that takes a BlendSQL model as a named arg, along with any number of other positional or keyword arguments. It should then return a Tuple[str, str], containing the (response, prompt) pair from the internal program logic.

Examples:

import pandas as pd
import outlines
from typing import Tuple

from blendsql.models import Model
from blendsql._program import Program

class SummaryProgram(Program):
    def __call__(self, model: Model, context: pd.DataFrame) -> Tuple[str, str]:
        prompt = f"Summarize the following table. {context.to_string()}"
        # Below we follow the outlines pattern for unconstrained text generation
        # https://github.com/outlines-dev/outlines
        generator = outlines.generate.text(model.model_obj)
        response: str = generator(prompt)
        # Finally, return (response, prompt) tuple
        # Returning the prompt here allows the underlying BlendSQL classes to track token usage
        return (response, prompt)
We could also write the same Program as a function:
def summary_program(model: Model, context: pd.DataFrame) -> Tuple[str, str]:
    ...

Source code in blendsql/_program.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Program:
    """A Program is the base class used for dynamically formatting prompts
    and returning unconstrained/constrained generation results.
    At it's core, a Program should be a callable that takes a BlendSQL model as a named arg,
    along with any number of other positional or keyword arguments.
    It should then return a Tuple[str, str], containing the (response, prompt) pair from the
    internal program logic.

    Examples:
        ```python
        import pandas as pd
        import outlines
        from typing import Tuple

        from blendsql.models import Model
        from blendsql._program import Program

        class SummaryProgram(Program):
            def __call__(self, model: Model, context: pd.DataFrame) -> Tuple[str, str]:
                prompt = f"Summarize the following table. {context.to_string()}"
                # Below we follow the outlines pattern for unconstrained text generation
                # https://github.com/outlines-dev/outlines
                generator = outlines.generate.text(model.model_obj)
                response: str = generator(prompt)
                # Finally, return (response, prompt) tuple
                # Returning the prompt here allows the underlying BlendSQL classes to track token usage
                return (response, prompt)
        ```
        We could also write the same `Program` as a function:
        ```python
        def summary_program(model: Model, context: pd.DataFrame) -> Tuple[str, str]:
            ...
        ```
    """

    def __new__(
        self,
        model: Model,
        **kwargs,
    ):
        return self.__call__(self, model, **kwargs)

    @abstractmethod
    def __call__(
        self, model: Model, *args, **kwargs
    ) -> Tuple[Union[str, List[str]], str]:
        """Logic for formatting prompt and calling the underlying model.
        Should return tuple of (response, prompt).
        """
        ...