marvin.fns.cast

Cast is a function that uses a language model to analyze the input data and transform it into the specified target type, maintaining as much semantic meaning as possible.

Constants

PROMPT

PROMPT = "'\nYou are an expert data converter that always maintains as much semantic\nmeaning as possible. You use inference or deduction whenever necessary to\nunderstand and transform the input data. Examine the provided `data`, text,\nor information and transform it into a single entity of the requested type.\n\n- When providing integers, do not write out any decimals at all\n- Use deduction where appropriate e.g. "3 dollars fifty cents" should be\n    converted to 3.5 when casting to a float\n- Preserve as much of the original meaning and structure as possible while\n    conforming to the target type\n- When providing a string response, do not return JSON or a quoted string\n    unless they provided instructions requiring it. If you do return JSON, it\n    must be valid and parseable including double quotes.\n- When converting to bool, treat "truthy" values as true'"

T

T = TypeVar('T')

Functions

cast

def cast(data: Any, target: TargetType[T] | None = None, instructions: str | None = None, agent: Agent | None = None, thread: Thread | str | None = None, context: dict[str, Any] | None = None) -> T

Transforms input data into a specific type using a language model.

This function uses a language model to analyze the input data and transform it into the specified target type, maintaining as much semantic meaning as possible.

Args: data: The input data to transform. Can be any type. target: The type to transform the data into. Defaults to str. instructions: Optional additional instructions to guide the transformation. Used to provide specific guidance about how to interpret or transform the data. agent: Optional custom agent to use for transformation. If not provided, the default agent will be used. thread: Optional thread for maintaining conversation context. Can be either a Thread object or a string thread ID. context: Optional dictionary of additional context to include in the task.

Returns: The transformed data of type T.

Examples:

from marvin import cast
cast(123, str) # '123'

cast("three point five", float, instructions="Convert words to numbers") # 3.5

cast("yes", bool) # True

cast_async

def cast_async(data: Any, target: TargetType[T] | None = None, instructions: str | None = None, agent: Agent | None = None, thread: Thread | str | None = None, context: dict[str, Any] | None = None) -> T

Asynchronously transforms input data into a specific type using a language model.

This function uses a language model to analyze the input data and transform it into the specified target type, maintaining as much semantic meaning as possible.

Args: data: The input data to transform. Can be any type. target: The type to transform the data into. Defaults to str. instructions: Optional additional instructions to guide the transformation. Used to provide specific guidance about how to interpret or transform the data. agent: Optional custom agent to use for transformation. If not provided, the default agent will be used. thread: Optional thread for maintaining conversation context. Can be either a Thread object or a string thread ID. context: Optional dictionary of additional context to include in the task.

Returns: The transformed data of type T.

Examples:

from marvin import cast_async
await cast_async("whats the answer to it all?, int) # 42

await cast_async("three point five", float, instructions="Convert words to numbers") # 3.5

await cast_async("yes", bool) # True

Parent Module: fns