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

Create a base class for the models #1385

Open
wants to merge 2 commits into
base: v1.0
Choose a base branch
from

Conversation

RobinPicard
Copy link
Contributor

@RobinPicard RobinPicard commented Jan 20, 2025

Updated on 2025-02-11

The objective of this PR is to create a base class all models would inherit from. This base class defines the interface for models and includes a __call__ method that allows models to be called with a prompt and output type directly following the suggestion in #1359 .

Formatting methods for the input and output types are implemented in a separate class inheriting from the base class ModelTypeAdapter. Each model must set a type_adapter attribute in its initialization. The rationale for separating those from the model class is that the accepted types for each model and the formatting logic are quite different concerns compared to the specifics of the model's implementation. Separating them also allows us to have an easier to read documentation of the accepted types for each model.

Another change proposed in this PR is to set a mandatory model_type attribute in each model class. This would allow us not to have to rely on a static list of models through the APIModel and LocalModel classes.

The use of the base model class is implemented for the models that have already been refactored: OpenAI, Gemini, Anthropic and Llamacpp.

@rlouf
Copy link
Member

rlouf commented Jan 20, 2025

That's smart. I am not a big fan of mixins, however, and I wonder if we should instead define at least one base class for all models, and also require that format_input and format_output_type are defined? That's a little too many levels of inheritance to my liking, but we can figure this out later (OpenAIBase does not need to be a class, for instance)

@rlouf rlouf assigned rlouf and RobinPicard and unassigned rlouf Jan 20, 2025
@rlouf rlouf added enhancement impact/user interface Related to improving the user interface labels Jan 20, 2025
@rlouf rlouf added this to the 1.0 milestone Jan 20, 2025
@rlouf rlouf linked an issue Jan 20, 2025 that may be closed by this pull request
@RobinPicard
Copy link
Contributor Author

I agree that mixins aren't great. I like the idea of having a base class for all models as it would allow, on top of avoiding repetitions, to define/document the interface of the models somewhere in a very straightforward manner. If we want to keep the formatting logic outside of the model classes not to make them have too many methods, but not rely on inheritance either, maybe a solution could be to require the models to have a formatter attribute that would inherit from a base ModelFormatter class.

outlines/models/base.py Outdated Show resolved Hide resolved
outlines/models/base.py Outdated Show resolved Hide resolved
@RobinPicard RobinPicard force-pushed the add_call_method_models branch from 4b38b60 to 6627b34 Compare February 11, 2025 21:56
@RobinPicard RobinPicard marked this pull request as ready for review February 11, 2025 22:08
@RobinPicard RobinPicard changed the title Add a __call__ method to models Create a base class for the models Feb 11, 2025
@cpfiffer
Copy link
Contributor

I don't have too much to add from a technical perspective, but I like the single inheritance format. Much easier to extend.

I poked around and it doesn't seem like this has much of an impact on most Outlines users, right? Only people deep into the internals are going to be impacted, and even then minorly.

outlines/models/__init__.py Outdated Show resolved Hide resolved
@@ -134,7 +180,7 @@ def from_pretrained(cls, repo_id, filename, **kwargs):
model = Llama.from_pretrained(repo_id, filename, **kwargs)
return cls(model)

def generate(self, prompt: str, logits_processor, **inference_kwargs) -> str:
def generate(self, model_input, logits_processor, **inference_kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model_input is still a string for now

Copy link
Contributor Author

@RobinPicard RobinPicard Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because I had a mypy error for inconsistent method signature with the method of the parent class. If we want to keep it we would need to remove generate from the base class I think.

@rlouf
Copy link
Member

rlouf commented Feb 12, 2025

A few nitpicks, looks good otherwise. To fix the CI:

  • We need to add datasets as a dependency;
  • We need to skip tests marked as api_call in the regular CI

Copy link
Contributor

@yvan-sraka yvan-sraka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a few superficial comments, nice work!

outlines/generate/__init__.py Outdated Show resolved Hide resolved
outlines/models/base.py Outdated Show resolved Hide resolved
@rlouf rlouf force-pushed the add_call_method_models branch from 6627b34 to 2b65abd Compare February 13, 2025 14:44
@RobinPicard RobinPicard force-pushed the add_call_method_models branch from 2b65abd to 6b15598 Compare February 13, 2025 19:15
@RobinPicard RobinPicard force-pushed the add_call_method_models branch from 6b15598 to 768c524 Compare February 13, 2025 20:15
@RobinPicard RobinPicard requested a review from rlouf February 14, 2025 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement impact/user interface Related to improving the user interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Call models with a prompt and output type directly
4 participants