diff --git a/outlines/text/application.py b/outlines/text/application.py new file mode 100644 index 000000000..dcafaa144 --- /dev/null +++ b/outlines/text/application.py @@ -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