Skip to content

LLMMap

ingredients

Usage

LLMMap

Bases: MapIngredient

from_args(model=None, few_shot_examples=None, list_options_in_prompt=True, batch_size=None, 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.

None
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 BlendSQL
from blendsql.ingredients.builtin import LLMQA, DEFAULT_QA_FEW_SHOT

ingredients = {
    LLMQA.from_args(
        few_shot_examples=[
            *DEFAULT_QA_FEW_SHOT,
            {
                "question": "Which weighs the most?",
                "context": {
                    {
                        "Animal": ["Dog", "Gorilla", "Hamster"],
                        "Weight": ["20 pounds", "350 lbs", "100 grams"]
                    }
                },
                "answer": "Gorilla",
                # Below are optional
                "options": ["Dog", "Gorilla", "Hamster"]
            }
        ],
        # Will fetch `k` most relevant few-shot examples using embedding-based retriever
        k=2,
        # Lambda to turn the pd.DataFrame to a serialized string
        context_formatter=lambda df: df.to_markdown(
            index=False
        )
    )
}

bsql = BlendSQL(db, ingredients=ingredients)

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.