-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `Function` object encapsulates a prompt template, a (`transformers`) model name and an output structure. It can then simply be called with a prompt throughout an application.
- Loading branch information
Showing
3 changed files
with
71 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
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,50 @@ | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING, Callable, Optional, Union | ||
|
||
from outlines import generate, models | ||
|
||
if TYPE_CHECKING: | ||
from outlines.generate.api import SequenceGenerator | ||
|
||
|
||
@dataclass | ||
class Function: | ||
"""Represents an Outlines function. | ||
Functions are a convenient way to encapsulate a prompt template, a language | ||
model and a Pydantic model that define the output structure. Once defined, | ||
the function can be called with arguments that will be used to render the | ||
prompt template. | ||
""" | ||
|
||
prompt_template: Callable | ||
model_name: str | ||
schema: Union[str, Callable, object] | ||
generator: Optional["SequenceGenerator"] = 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, **kwargs): | ||
"""Call the function. | ||
.. warning:: | ||
This currently does not support batching. | ||
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, **kwargs) | ||
return self.generator(prompt) |
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,20 @@ | ||
from pydantic import BaseModel | ||
|
||
import outlines | ||
from outlines.function import Function | ||
|
||
|
||
def test_function_basic(): | ||
@outlines.prompt | ||
def test_template(text: str): | ||
"""{{ text }}""" | ||
|
||
class Foo(BaseModel): | ||
id: int | ||
|
||
fn = Function(test_template, "hf-internal-testing/tiny-random-GPTJForCausalLM", Foo) | ||
|
||
assert fn.generator is None | ||
|
||
result = fn("test") | ||
assert isinstance(result, BaseModel) |