cognition.language.classification

Selecting from amongst enumerated values based upon natural language input

Attributes

DEFAULT_SCHEMA_FIELD_NAME

Default field name for response schema

Classes

EmpiricalConfidence

Fractional confidence from

EnumClassifier

Function to classify an utterance (with confidence)

Functions

enum_schema(enum_type[, field_name])

Produces a validation schema for

Module Contents

cognition.language.classification.DEFAULT_SCHEMA_FIELD_NAME: str = 'value'

Default field name for response schema

class cognition.language.classification.EmpiricalConfidence

Fractional confidence from a probabilistic process

selected: int

Number of positive outcomes

trials: int

Number of opportunities

__float__()
Return type:

float

cognition.language.classification.enum_schema(enum_type, field_name=DEFAULT_SCHEMA_FIELD_NAME)

Produces a validation schema for producing an optional value from amongst a supplied enumerated type

Parameters:
  • enum_type (type[enum.Enum]) – type representing options

  • field_name (str) – schema field name for the enumerated type

Returns:

resulting schema

Return type:

type[pydantic.BaseModel]

class cognition.language.classification.EnumClassifier[T: enum.Enum](enum_type, task_desc)

Function to classify an utterance (with confidence) with respect to an enumerated type.

Parameters:
  • enum_type (type[T]) – type representing options

  • task_desc (str | None) – textual description of the task

prompt(utterance)

Produces the prompt for a supplied utterance

Parameters:

utterance (str) – user input

Returns:

resulting classifier llm prompt

Return type:

str

__call__(utterance, llm, num_trials=3, timeout_secs=10)

Classifies the utterance (with confidence) based upon a timeout budget.

Parameters:
  • utterance (str) – text to classify

  • llm (pydantic_ai.models.Model) – textual model to utilize

  • num_trials (int) – number of classifications to perform & aggregate

  • timeout_secs (int)

Timeout_secs:

time given per LLM call

Returns:

most common classification with confidence

Return type:

tuple[T | None, EmpiricalConfidence]

classmethod classify(utterance, enum_type, llm, task_desc, num_trials=3, timeout_secs=10)

One-off instantiation and calling of a classifier

Parameters:
  • utterance (str) – text to classify

  • enum_type (type[T]) – type representing options

  • llm (pydantic_ai.models.Model) – textual model to utilize

  • task_desc (str | None) – textual description of the task

  • num_trials (int) – number of classifications to perform & aggregate

  • timeout_secs (int)

Timeout_secs:

time given per LLM call

Returns:

most common classification with confidence

Return type:

tuple[T | None, EmpiricalConfidence]