applications
Encapsulate a prompt template and an output type into a reusable object.
Application
Application is a class that encapsulates a prompt template and an output type. It can be called to generate a response by providing a model, the values to be substituted in the template in a dictionary and optional inference parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
Union[Template, Callable]
|
A callable that takes arguments and returns a prompt string. |
required |
output_type
|
Any
|
The expected output type of the generated response. |
None
|
Examples:
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from outlines import models, Application
from outlines.types import JsonType
from outlines.templates import Template
class OutputModel(BaseModel):
result: int
model = models.from_transformers(
AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"),
AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
)
template_string = "What is 2 times {{ num }}?"
template = Template.from_string(template_string)
application = Application(template, JsonType(OutputModel))
result = application(model, {"num": 3}, max_new_tokens=20)
print(result) # Expected output: { "result" : 6 }
Source code in outlines/applications.py
__call__(model, template_vars, **inference_kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
The model to use to generate the response. |
required |
template_vars
|
Dict[str, Any]
|
The variables to be substituted in the template. |
required |
**inference_kwargs
|
Additional keyword arguments to pass to the model. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The generated response. |
Source code in outlines/applications.py
__init__(template, output_type=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
Union[Template, Callable]
|
The template to use to build the prompt. |
required |
output_type
|
Optional[Any]
|
The output type provided to the generator. |
None
|