marvin.utilities.jinja
Module for Jinja utilities.
BaseEnvironment
¶
BaseEnvironment provides a configurable environment for rendering Jinja templates.
This class encapsulates a Jinja environment with customizable global functions and template settings, allowing for flexible template rendering.
Attributes:
Name | Type | Description |
---|---|---|
environment |
Environment
|
The Jinja environment for template rendering. |
globals |
dict[str, Any]
|
A dictionary of global functions and variables available in templates. |
Example
Basic Usage of BaseEnvironment
render
¶
Renders a given template str
or BaseTemplate
with provided context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template |
Union[str, Template]
|
The template to be rendered. |
required |
**kwargs |
Any
|
Context variables to be passed to the template. |
{}
|
Returns:
Type | Description |
---|---|
str
|
The rendered template as a string. |
Transcript
¶
Transcript is a model representing a conversation involving multiple roles.
Attributes:
Name | Type | Description |
---|---|---|
content |
str
|
The content of the transcript. |
roles |
dict[str, str]
|
The roles involved in the transcript. |
environment |
BaseEnvironment
|
The jinja environment to use for rendering the transcript. |
Example
Basic Usage of Transcript:
from marvin.utilities.jinja import Transcript
transcript = Transcript(
content="system: Hello, there! user: Hello, yourself!",
roles=["system", "user"],
)
print(transcript.render_to_messages())
# [
# BaseMessage(content='system: Hello, there!', role='system'),
# BaseMessage(content='Hello, yourself!', role='user')
# ]
split_text_by_tokens
¶
Splits a given text by a list of tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The text to be split. split_tokens: The tokens to split the text |
required |
by. |
only_on_newline
|
If True, only match tokens that are either |
required |
Returns:
Type | Description |
---|---|
list[tuple[str, str]]
|
A list of tuples containing the token and the text following it. |
Example
Basic Usage of split_text_by_tokens
```python from
marvin.utilities.jinja import split_text_by_tokens
text = "Hello, World!" split_tokens = ["Hello", "World"] pairs = split_text_by_tokens(text, split_tokens) print(pairs) # Output: [("Hello", ", "), ("World", "!")] ```