PALCrossEncoder¶
- class hana_ml.text.pal_cross_encoder.PALCrossEncoder(model_version=None, max_token_num=None)¶
PALCrossEncoder processes paired inputs, like (query, document) pairs, together to determine their relationship or similarity, outputting their relevance scores. Unlike bi-encoders, which encode inputs separately into vectors, cross-encoders analyze the interaction between the input pair simultaneously, allowing for precise semantic understanding and high-accuracy results, particularly in re-ranking tasks within information retrieval.
- Parameters
- model_versionstr, optional
Specifies the version of SAP PAL cross-encoder model to use.
Currently the only valid version is 'SAP_CER.20250701'.
Defaults to None (currently uses 'SAP_CER.20250701' by default).
- max_token_numint, optional
The maximum number of tokens allowed for the input of the cross-encoder model.
The default value and value range depend on the model.
- Attributes
- stats_DataFrame
A DataFrame containing additional messages related to the resulting scores.
Methods
predict(data, key, content[, thread_number, ...])Calculate relevance scores for paired inputs using a cross-encoder model.
Examples
Suppose you have a HANA DataFrame df with ID column 'ID', content columns 'QUERY' and 'DOC'. To compute relevance scores for pairs in 'QUERY' and 'DOC' with the same IDs, we can create a PALCrossEncoder instance and call its predict() method:
>>> from hana_ml.text.pal_cross_encoder import PALCrossEncoder >>> ce_model = PALCrossEncoder(model_version='SAP_CER.20250701') >>> result = ce_model.predict(data=df, key='ID', content=['QUERY', 'DOC'], return_table=True) >>> print(result.collect())
- predict(data, key, content, thread_number=None, batch_size=None, max_token_num=None, return_table=False)¶
Calculate relevance scores for paired inputs using a cross-encoder model.
- Parameters
- data: DataFrame
Input data containing the paired inputs for relevance score calculation.
- key: str
Name of the key (ID) column.
- content: list/tuple of str
Names of the two columns that contain the pairs for relevance score calculation.
- thread_number: int, optional
Specifies the number of HTTP connections that are established simultaneously to the backend cross-encoder service. The valid range is from 1 to 10.
Defaults to 6.
- batch_size: int, optional
Specifies the number of pairs grouped into a single request before sending to the cross-encoder service.
The value range is from 1 to 50.
Defaults to 10.
- max_token_num: int, optional
The maximum number of tokens allowed for the input of the cross-encoder model.
If
max_token_numis None, the value in self.max_token_num will be used.Defaults to None.
- return_tablebool, optional
Specifies whether to return the DataFrame of the result or not.
If set to False, a numpy array of relevance scores shall be returned instead.
Defaults to False.
- Returns
- DataFrame or numpy.ndarray
DataFrame or numpy.ndarray containing the relevance scores.
When
return_tableis True, the returned DataFrame includes anEXT_MSGcolumn. This column is currently always NULL and is reserved for possible future additional messages related to the resulting scores.