Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Picard committed Feb 11, 2025
1 parent b3e318c commit 2f6394c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
8 changes: 5 additions & 3 deletions outlines/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
class AnthropicTypeAdapter(ModelTypeAdapter):
"""Type adapter for the Anthropic clients.
`AnthropicTypeAdapter` is responsible for preparing the arguments to Anthropic's
`messages.create` methods: the input (prompt and possibly image).
Anthropic does not support defining the output type, so `format_output_type` is not implemented.
`AnthropicTypeAdapter` is responsible for preparing the arguments to
Anthropic's `messages.create` methods: the input (prompt and possibly
image).
Anthropic does not support defining the output type, so
`format_output_type` is not implemented.
"""

Expand Down
39 changes: 22 additions & 17 deletions outlines/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ class ModelTypeAdapter(ABC):
A type adapter instance must be given as a value to the `type_adapter`
attribute when instantiating a model.
The type adapter is responsible for formatting the input and output types
passed to the model to match the specific format expected by the associated model.
passed to the model to match the specific format expected by the
associated model.
"""

@abstractmethod
def format_input(self, model_input):
"""Format the user input to the expected input format of the model.
"""Format the user input to the expected format of the model.
For API-based models, it typically means creating the `messages` argument passed to the client.
For local models, it can mean casting the input from str to list for instance.
This method is also used to validate that the input type provided by the user is supported by the model.
For API-based models, it typically means creating the `messages`
argument passed to the client. For local models, it can mean casting
the input from str to list for instance.
This method is also used to validate that the input type provided by
the user is supported by the model.
"""
...

@abstractmethod
def format_output_type(self, output_type):
"""Format the output type provided by the user to the expected output type format of the model.
"""Format the output type to the expected format of the model.
For API-based models, this typically means creating a `response_format` argument.
For local models, it means formatting the logits processor to create the object type expected by the model.
For API-based models, this typically means creating a `response_format`
argument. For local models, it means formatting the logits processor to
create the object type expected by the model.
"""
...
Expand All @@ -37,10 +41,12 @@ def format_output_type(self, output_type):
class Model(ABC):
"""Base class for all models.
This class defines a shared `__call__` method that can be used to call the model directly.
All models inheriting from this class must define a `type_adapter` attribute of type `ModelTypeAdapter`.
The methods of the `type_adapter` attribute are used in the `generate` method to format
the input and output types received by the model.
This class defines a shared `__call__` method that can be used to call the
model directly.
All models inheriting from this class must define a `type_adapter`
attribute of type `ModelTypeAdapter`. The methods of the `type_adapter`
attribute are used in the `generate` method to format the input and output
types received by the model.
"""

Expand All @@ -50,10 +56,9 @@ class Model(ABC):
def __call__(self, model_input, output_type=None, **inference_kwargs):
"""Call the model.
Users can call the model directly, in which case we will create a generator instance with
the output type provided and call it.
Users can call the model directly, in which case we will create a
generator instance with the output type provided and call it.
Thus, those commands are equivalent:
```python
generator = Generator(model, Foo)
generator("prompt")
Expand All @@ -72,8 +77,8 @@ def __call__(self, model_input, output_type=None, **inference_kwargs):
def generate(self, model_input, output_type=None, **inference_kwargs):
"""Generate a response from the model.
The output_type argument contains a logits processor for local models while it contains
a type (Json, Enum...) for the API-based models.
The output_type argument contains a logits processor for local models
while it contains a type (Json, Enum...) for the API-based models.
"""
...
8 changes: 4 additions & 4 deletions outlines/models/llamacpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def __setstate__(self, state):
class LlamaCppTypeAdapter(ModelTypeAdapter):
"""Type adapter for the `llama-cpp-python` library.
`LlamaCppTypeAdapter` is responsible for preparing the arguments to `llama-cpp-python`'s
`Llama.__call__` method: the input (a string prompt), as well as the logits processor
(an instance of `LogitsProcessorList`).
`LlamaCppTypeAdapter` is responsible for preparing the arguments to
`llama-cpp-python`'s `Llama.__call__` method: the input (a string prompt),
as well as the logits processor (an instance of `LogitsProcessorList`).
"""

Expand All @@ -124,7 +124,7 @@ def format_str_input(self, model_input: str):
return model_input

def format_output_type(self, output_type):
"""Generate the logits processor argument to pass to the model (a `LogitsProcessorList`).
"""Generate the logits processor argument to pass to the model.
Argument
--------
Expand Down
4 changes: 2 additions & 2 deletions outlines/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class OpenAITypeAdapter(ModelTypeAdapter):
"""Type adapter for the OpenAI clients.
`OpenAITypeAdapter` is responsible for preparing the arguments to OpenAI's
`completions.create` methods: the input (prompt and possibly image), as well
as the output type (only JSON).
`completions.create` methods: the input (prompt and possibly image), as
well as the output type (only JSON).
"""

Expand Down

0 comments on commit 2f6394c

Please sign in to comment.