autointent.Ranker#

class autointent.Ranker(cross_encoder_config, classifier_head=None)#

Cross-encoder for Natural Language Inference (NLI).

This class uses sentence_transformers.cross_encoder.CrossEncoder model to extract features. It can use either the model’s classifier or a custom trained sklearn.linear_model.LogisticRegressionCV to rank documents using similarity scores to the query.

Parameters:
config: autointent.configs.CrossEncoderConfig#
cross_encoder: sentence_transformers.CrossEncoder#
fit(utterances, labels)#

Construct training samples and train the logistic regression classifier.

Parameters:
  • utterances (list[str]) – List of utterances (texts)

  • labels (autointent.custom_types.ListOfLabels) – Intent class labels corresponding to the utterances

Return type:

None

predict(pairs)#

Predict probabilities of two utterances having the same intent label.

Parameters:

pairs (list[tuple[str, str]]) – List of text pairs to classify

Returns:

Array of probabilities

Raises:

ValueError – If classifier is not trained yet

Return type:

numpy.typing.NDArray[Any]

rank(query, query_docs, top_k=None)#

Rank documents according to meaning closeness to the query.

Parameters:
  • query (str) – Reference document

  • query_docs (list[str]) – List of documents to rank

  • top_k (int | None) – Number of documents to return

Returns:

List of dictionaries containing ranked items with scores

Return type:

list[dict[str, Any]]

save(path)#

Save the model and classifier to disk.

Parameters:

path (str) – Directory path to save the model and classifier

Return type:

None

classmethod load(path, override_config=None)#

Load the model and classifier from disk.

Parameters:
Returns:

Initialized Ranker instance

Return type:

Ranker

clear_ram()#

Clear model from RAM and GPU memory.

Return type:

None