No project description provided
Project description
Templated
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.
Installation
Templated can be installed via pip:
pip install templated
Usage
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!"
.
Stateful Template Arguments
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!"
.
Custom Language Models
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.
Contributing
We welcome contributions to Templated! Please refer to the contributing guidelines for more information.
Tests
poetry run py.test --doctest-modules
License
Templated is licensed under our Modified MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file templated-0.1.2.tar.gz
.
File metadata
- Download URL: templated-0.1.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01aabd7d99d6e22bd4214ba15b05e6f7bc2ab9723bcdedb8210fc1dead38f963 |
|
MD5 | 876d8035d64aec49c39a6118a5a1d284 |
|
BLAKE2b-256 | 727db7ffb10d22f5e75777f2052e22ecda34cbb53f579f3ace0cc116c53414df |
File details
Details for the file templated-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: templated-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99b301f25d85585c869ea9647bdd1531b0a007edcf0999d37c1605fd4cc3ede9 |
|
MD5 | 8087d0d25bdd84170d2b1193d1ae23eb |
|
BLAKE2b-256 | 5b2154b0a47c19cb999b830f17c4826d540cad3b2a17a043a18390316dab51a1 |