Skip to content

LLMMap

ingredients

Usage

LLMMap

Bases: MapIngredient

from_args(model=None, few_shot_examples=None, list_options_in_prompt=True, batch_size=DEFAULT_MAP_BATCH_SIZE, k=None) classmethod

Creates a partial class with predefined arguments.

Parameters:

Name Type Description Default
model Optional[Model]

The model to be used. Defaults to None.

None
few_shot_examples Optional[Union[List[dict], List[AnnotatedMapExample]]]

A list of dictionary MapExample few-shot examples. If not specified, will use default_examples.json as default.

None
list_options_in_prompt bool

Whether to list options in the prompt. Defaults to True.

True
batch_size Optional[int]

The batch size for processing. Defaults to 5.

DEFAULT_MAP_BATCH_SIZE
k Optional[int]

Determines number of few-shot examples to use for each ingredient call. Default is None, which will use all few-shot examples on all calls. If specified, will initialize a haystack-based embedding retriever to filter examples.

None

Returns:

Type Description

Type[MapIngredient]: A partial class of MapIngredient with predefined arguments.

Examples:

from blendsql import blend, LLMMap
from blendsql.ingredients.builtin import DEFAULT_MAP_FEW_SHOT

ingredients = {
    LLMMap.from_args(
        few_shot_examples=[
            *DEFAULT_MAP_FEW_SHOT,
            {
                "question": "Is this a sport?",
                "mapping": {
                    "Soccer": "t",
                    "Chair": "f",
                    "Banana": "f",
                    "Golf": "t"
                },
                # Below are optional
                "column_name": "Items",
                "table_name": "Table",
                "example_outputs": ["t", "f"],
                "options": ["t", "f"],
                "output_type": "boolean"
            }
        ],
        # Will fetch `k` most relevant few-shot examples using embedding-based retriever
        k=2,
        # How many inference values to pass to model at once
        batch_size=5,
    )
}
smoothie = blend(
    query=blendsql,
    db=db,
    ingredients=ingredients,
    default_model=model,
)

run(model, question, values, list_options_in_prompt, few_shot_retriever=None, options=None, value_limit=None, example_outputs=None, output_type=None, batch_size=DEFAULT_MAP_BATCH_SIZE, **kwargs)

For each value in a given column, calls a Model and retrieves the output.

Parameters:

Name Type Description Default
question str

The question to map onto the values. Will also be the new column name

required
model Model

The Model (blender) we will make calls to.

required
values List[str]

The list of values to apply question to.

required
value_limit Optional[int]

Optional limit on the number of values to pass to the Model

None
example_outputs Optional[str]

This gives the Model an example of the output we expect.

None
output_type Optional[Union[DataType, str]]

In the absence of example_outputs, give the Model some signal as to what we expect as output.

None
regex

Optional regex to constrain answer generation.

required

Returns:

Type Description
List[Union[float, int, str, bool]]

Iterable[Any] containing the output of the Model for each value.

Description

This type of ingredient applies a function on a given column to create a new column containing the function's output.

In more formal terms, it is a unary scalar function, much like LENGTH or ABS in standard SQLite.

For example, take the following query.

SELECT merchant FROM transactions
    WHERE {{LLMMap('Is this a pizza shop?', 'transactions::merchant')}} = TRUE

LLMMap is one of our builtin MapIngredients. For each of the distinct values in the "merchant" column of the "transactions" table, it will create a column containing the function output.

merchant Is this a pizza shop?
Domino's 1
Safeway 0
Target 0

The temporary table shown above is then combined with the original "transactions" table with an INNER JOIN on the "merchant" column.