[go: up one dir, main page]

Skip to content

daft.functions.jev_choice#

jev_choice #

jev_choice(row: Expression, question: str, options: list[str], *, model: str | None = None, api_key: str | None = None, base_url: str | None = None, timeout: float = 60.0) -> Expression

The most probable of options as the answer to question for each row.

Parameters:

Name Type Description Default
row Expression

The value to classify. Pass to_struct(...) to classify on several columns.

required
question str

A plain-English question, e.g. "which team should handle this?".

required
options list[str]

Between 1 and 255 distinct candidate answers; duplicates are dropped.

required
model str | None

The Jev model. Defaults to TYPESAFE_DEFAULT_MODEL, else jev-latest.

None
api_key str | None

TypeSafe API key. Defaults to TYPESAFE_API_KEY.

None
base_url str | None

API root, e.g. to route through a proxy. Defaults to TYPESAFE_BASE_URL, else https://api.typesafe.ai.

None
timeout float

Seconds allowed for each request, which carries up to 25 rows. Defaults to 60.

60.0

Returns:

Name Type Description
Expression Expression

A String drawn from options, or null where row is null.

Examples:

1
2
3
4
5
6
7
>>> import daft
>>> from daft.functions import jev_choice
>>> df = daft.from_pydict({"ticket": ["I was charged twice", "The app crashes on login"]})
>>> teams = ["billing", "technical", "security", "sales"]
>>> df.with_column(
...     "team", jev_choice(df["ticket"], "which team should handle this?", teams)
... ).show()
Source code in daft/functions/jev_.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def jev_choice(
    row: Expression,
    question: str,
    options: list[str],
    *,
    model: str | None = None,
    api_key: str | None = None,
    base_url: str | None = None,
    timeout: float = 60.0,
) -> Expression:
    """The most probable of `options` as the answer to `question` for each row.

    Args:
        row (Expression): The value to classify. Pass `to_struct(...)` to classify on several columns.
        question (str): A plain-English question, e.g. "which team should handle this?".
        options (list[str]): Between 1 and 255 distinct candidate answers; duplicates are dropped.
        model (str | None): The Jev model. Defaults to `TYPESAFE_DEFAULT_MODEL`, else `jev-latest`.
        api_key (str | None): TypeSafe API key. Defaults to `TYPESAFE_API_KEY`.
        base_url (str | None): API root, e.g. to route through a proxy. Defaults to `TYPESAFE_BASE_URL`, else `https://api.typesafe.ai`.
        timeout (float): Seconds allowed for each request, which carries up to 25 rows. Defaults to 60.

    Returns:
        Expression: A String drawn from `options`, or null where `row` is null.

    Examples:
        >>> import daft
        >>> from daft.functions import jev_choice
        >>> df = daft.from_pydict({"ticket": ["I was charged twice", "The app crashes on login"]})
        >>> teams = ["billing", "technical", "security", "sales"]
        >>> df.with_column(
        ...     "team", jev_choice(df["ticket"], "which team should handle this?", teams)
        ... ).show()  # doctest: +SKIP
    """
    options = list(dict.fromkeys(options))
    if not 1 <= len(options) <= 255:
        raise ValueError(f"jev_choice requires between 1 and 255 options, got {len(options)}")
    return _client(api_key, model, base_url, timeout).choice(serialize(row, format="json"), question, options)