Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Models do not receive prompt_type or task_name #1491

Closed
ArthurSBianchessi opened this issue Nov 23, 2024 · 6 comments
Closed

Custom Models do not receive prompt_type or task_name #1491

ArthurSBianchessi opened this issue Nov 23, 2024 · 6 comments

Comments

@ArthurSBianchessi
Copy link

Specifically SentenceTransformerWrapper (from mteb/models/sentence_transformer_wrapper.py) does not forward these variables to Custom Model encode.

@ArthurSBianchessi ArthurSBianchessi changed the title MTEB prompt_type and task_name do not work with Custom Models Custom Models do not receive prompt_type or task_name Nov 23, 2024
@Samoed
Copy link
Collaborator

Samoed commented Nov 23, 2024

Can you provide code to check?

@ArthurSBianchessi
Copy link
Author

ArthurSBianchessi commented Nov 24, 2024

Sure.
The code in the README.md raises error due to missing task_name

from mteb.encoder_interface import PromptType
import mteb
from mteb import MTEB
import numpy as np

class CustomModel:
    def encode(
        self,
        sentences: list[str],
        task_name: str,
        prompt_type: PromptType | None = None,
        **kwargs,
    ) -> np.ndarray:
        """Encodes the given sentences using the encoder.
        
        Args:
            sentences: The sentences to encode.
            task_name: The name of the task.
            prompt_type: The prompt type to use.
            **kwargs: Additional arguments to pass to the encoder.
            
        Returns:
            The encoded sentences.
        """
        pass

model = CustomModel()
tasks = [mteb.get_task("Banking77Classification") ]            # README doesnt turn the tasks into a list, else it raises an error if get_task is used 
evaluation = MTEB(tasks=tasks)        
evaluation.run(model)

It raises:

TypeError: CustomModel.encode() missing 1 required positional argument: 'task_name'

Printing the inputs of encode with:

class CustomModel:
    def encode(self, sentences, *inputs, **kwargs):
        print(f'inputs: {inputs}')
        print(f'kwargs: {kwargs}')

outputs:

inputs: ()
kwargs: {'prompt_name': None, 'batch_size': 32}

There might be some other variable that is not being forwarded.

@Samoed
Copy link
Collaborator

Samoed commented Nov 24, 2024

This is correct because task_name is a positional argument and is passed correctly to CustomModel. It would be better to update the example in the README. The CustomModel should function similarly to SentenceTransformerWrapper. You can find more implementations of custom models in the models directory.

@ArthurSBianchessi
Copy link
Author

ArthurSBianchessi commented Nov 24, 2024

Right now neither task_name nor prompt_type is passed to CustomModels, I did create a pull request, but sent task name as a keyword argument because any encode method that expects taskname as a positional argument does not work, and any other method that currently does not break in mteb amd does not receive *args will break (I expect it to be most models). In my understanding, CustomModel.encode should receive task_name either as a positional or as a keyword argument, but currently it does not receive either.
(In my code it prints (), since no other positional argument are passed. Removing *inputs does not break the code, which would happen if task_name was passed as a positional argument)

This is what I understand that was the expected behavior, sorry if I am misunderstanding something.

@Samoed
Copy link
Collaborator

Samoed commented Nov 24, 2024

You need to inherit custom model from Wrapper class like:

import mteb
from mteb.encoder_interface import PromptType
import numpy as np
from mteb.models.wrapper import Wrapper

class CustomModel(Wrapper):
    def encode(
        self,
        sentences: list[str],
        task_name: str,
        prompt_type: PromptType | None = None,
        **kwargs,
    ) -> np.ndarray:
        ...

I'll update Readme to make it more relevant. Thank you for report!

@ArthurSBianchessi
Copy link
Author

Ok, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants