-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from dataclasses import dataclass | ||
from typing import Callable, Optional | ||
|
||
from outlines import generate, models | ||
|
||
|
||
@dataclass | ||
class Application: | ||
"""Represents an Outlines application. | ||
Applications are a convenient way to encapsulate a prompt template, a model | ||
and a Pydantic model that define the output structure. Once defined, the | ||
application can be called with arguments that will be used to render the | ||
prompt template. | ||
""" | ||
|
||
prompt_template: Callable | ||
model_name: str | ||
schema: object | ||
generator: Optional[object] = None | ||
|
||
def init_generator(self): | ||
"""Load the model and initialize the generator.""" | ||
model = models.transformers(self.model_name) | ||
self.generator = generate.json(model, self.schema) | ||
|
||
def __call__(self, *args): | ||
"""Call the application. | ||
Parameters | ||
---------- | ||
args | ||
Values to pass to the prompt template as positional arguments. | ||
kwargs | ||
Values to pass to the prompt template as keyword arguments. | ||
""" | ||
if self.generator is None: | ||
self.init_generator() | ||
|
||
prompt = self.prompt_template(*args) | ||
return self.generator(prompt) | ||
|
||
|
||
App = Application |