Skip to content

Commit

Permalink
Add the Application object
Browse files Browse the repository at this point in the history
  • Loading branch information
rlouf committed Dec 8, 2023
1 parent 430ee4d commit 428ad5e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions outlines/text/application.py
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

0 comments on commit 428ad5e

Please sign in to comment.