Templated provides a painless experience for developers looking to build semantic templated functions. With Templated, you can easily define a function with a Jinja2 template string, and use it to generate outputs with dynamic data passed as function arguments.
Templated can be installed via pip:
pip install templated
Here is an example of how to use Templated:
from templated import Function
f = Function("Hello, {{name}}!")
result = f(name="world")
print(result)
The output of the above code will be:
Hello, world!
In this example, we have defined a function f
with a Jinja2 template string "Hello, {{name}}!"
. We then called this function with an argument name="world"
, which filled in the {{name}}
placeholder in the template, resulting in the output "Hello, world!"
.
In Templated, you can also define stateful template arguments. Here is an example:
f = Function("{{greeting}}, {{name}}!")
f(greeting="Hello")
f(name="world")
# Output: "Hello, world!"
f(greeting="Goodbye")
f()
# Output: "Goodbye, world!"
In this example, we have defined a function f
with a Jinja2 template string "{{greeting}}, {{name}}!"
. We then called this function with an argument greeting="Hello"
, which filled in the {{greeting}}
placeholder in the template. We then called the function again with an argument name="world"
, which filled in the {{name}}
placeholder in the template, resulting in the output "Hello, world!"
.
We then called the function again with a different value for the greeting
argument, "Goodbye"
. We did not pass a value for the name
argument, so it retained its previous value of "world"
. This resulted in the output "Goodbye, world!"
.
In Templated, you can also use custom language models for generating output from templates. Here is an example:
from langchain.chat_models import ChatOpenAI
from templated._utils.chat2vanilla_lm import Chat2VanillaLM
from templated.function import Function
LLMChatOpenAI = Chat2VanillaLM(ChatOpenAI)
f = Function("Hello, {{name}}!", llm=LLMChatOpenAI)
result = f(name="world")
print(result)
In this example, we have defined a custom language model LLMChatOpenAI
that wraps a ChatOpenAI
language model. We then defined a function f
with a Jinja2 template string "Hello, {{name}}!"
, and passed our custom language model as the llm
argument to the Function
constructor.
We welcome contributions to Templated! Please refer to the contributing guidelines for more information.
poetry run py.test --doctest-modules
Templated is licensed under our Modified MIT License.