swarmauri_prompt_j2prompttemplate 0.7.1.dev2__tar.gz → 0.7.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/PKG-INFO +5 -4
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/README.md +2 -2
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/pyproject.toml +3 -2
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/swarmauri_prompt_j2prompttemplate/J2PromptTemplate.py +73 -16
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/swarmauri_prompt_j2prompttemplate/__init__.py +2 -2
- {swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/LICENSE +0 -0
{swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/PKG-INFO
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: swarmauri_prompt_j2prompttemplate
|
3
|
-
Version: 0.7.
|
3
|
+
Version: 0.7.2
|
4
4
|
Summary: Jinja2 Prompt Template for Swarmauri.
|
5
5
|
License: Apache-2.0
|
6
6
|
Author: Jacob Stewart
|
@@ -10,7 +10,8 @@ Classifier: License :: OSI Approved :: Apache Software License
|
|
10
10
|
Classifier: Programming Language :: Python :: 3.10
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
12
12
|
Classifier: Programming Language :: Python :: 3.12
|
13
|
-
Requires-Dist:
|
13
|
+
Requires-Dist: inflect (>=7.0.0)
|
14
|
+
Requires-Dist: jinja2 (>=3.1.6)
|
14
15
|
Requires-Dist: pydantic (>=1.8.2)
|
15
16
|
Requires-Dist: swarmauri_base
|
16
17
|
Requires-Dist: swarmauri_core
|
@@ -21,8 +22,8 @@ Description-Content-Type: text/markdown
|
|
21
22
|
<p align="center">
|
22
23
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
23
24
|
<img src="https://img.shields.io/pypi/dm/swarmauri_tool_j2prompttemplate" alt="PyPI - Downloads"/></a>
|
24
|
-
<a href="https://github.com/swarmauri/swarmauri-sdk/
|
25
|
-
<img src="https://hits.
|
25
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_prompt_j2prompttemplate/">
|
26
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_prompt_j2prompttemplate.svg"/></a>
|
26
27
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
27
28
|
<img src="https://img.shields.io/pypi/pyversions/swarmauri_tool_j2prompttemplate" alt="PyPI - Python Version"/></a>
|
28
29
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
{swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/README.md
RENAMED
@@ -3,8 +3,8 @@
|
|
3
3
|
<p align="center">
|
4
4
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
5
5
|
<img src="https://img.shields.io/pypi/dm/swarmauri_tool_j2prompttemplate" alt="PyPI - Downloads"/></a>
|
6
|
-
<a href="https://github.com/swarmauri/swarmauri-sdk/
|
7
|
-
<img src="https://hits.
|
6
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_prompt_j2prompttemplate/">
|
7
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_prompt_j2prompttemplate.svg"/></a>
|
8
8
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
9
9
|
<img src="https://img.shields.io/pypi/pyversions/swarmauri_tool_j2prompttemplate" alt="PyPI - Python Version"/></a>
|
10
10
|
<a href="https://pypi.org/project/swarmauri_tool_j2prompttemplate/">
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[project]
|
2
2
|
name = "swarmauri_prompt_j2prompttemplate"
|
3
|
-
version = "0.7.
|
3
|
+
version = "0.7.2"
|
4
4
|
description = "Jinja2 Prompt Template for Swarmauri."
|
5
5
|
license = "Apache-2.0"
|
6
6
|
readme = "README.md"
|
@@ -14,7 +14,8 @@ classifiers = [
|
|
14
14
|
]
|
15
15
|
authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
|
16
16
|
dependencies = [
|
17
|
-
"
|
17
|
+
"inflect>=7.0.0",
|
18
|
+
"jinja2>=3.1.6",
|
18
19
|
"pydantic>=1.8.2",
|
19
20
|
"swarmauri_core",
|
20
21
|
"swarmauri_base",
|
@@ -1,8 +1,8 @@
|
|
1
|
-
from typing import Dict, List, Union, Optional, Literal
|
2
|
-
from pydantic import ConfigDict, FilePath
|
3
|
-
from jinja2 import Environment, FileSystemLoader, Template
|
4
1
|
import os
|
2
|
+
from typing import Any, Callable, Dict, List, Literal, Optional, Union
|
5
3
|
|
4
|
+
from jinja2 import Environment, FileSystemLoader, Template
|
5
|
+
from pydantic import ConfigDict, FilePath
|
6
6
|
from swarmauri_base.ComponentBase import ComponentBase
|
7
7
|
from swarmauri_base.prompt_templates.PromptTemplateBase import PromptTemplateBase
|
8
8
|
|
@@ -15,16 +15,22 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
15
15
|
The `template` attribute supports either a literal string representing the template content
|
16
16
|
or a Pydantic FilePath. When a FilePath is provided, the template is loaded using
|
17
17
|
`env.get_template()` and stored in `template`.
|
18
|
+
|
19
|
+
Features:
|
20
|
+
- Support for multiple template directories with fallback mechanism
|
21
|
+
- Built-in filters: split_whitespace, make_singular, make_plural
|
22
|
+
- Template caching for performance
|
18
23
|
"""
|
19
24
|
|
20
25
|
# The template attribute may be a literal string (template content),
|
21
26
|
# a FilePath (when provided as input), or a compiled Jinja2 Template (when loaded from file).
|
22
27
|
template: Union[str, FilePath, Template] = ""
|
23
|
-
variables: Dict[str, Union[str, int, float]] = {}
|
28
|
+
variables: Dict[str, Union[str, int, float, Any]] = {}
|
24
29
|
# Optional templates_dir attribute (can be a single path or a list of paths)
|
25
30
|
templates_dir: Optional[Union[str, List[str]]] = None
|
31
|
+
# Whether to enable code generation specific features like linguistic filters
|
26
32
|
|
27
|
-
model_config = ConfigDict(arbitrary_types_allowed=True)
|
33
|
+
model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
|
28
34
|
type: Literal["J2PromptTemplate"] = "J2PromptTemplate"
|
29
35
|
|
30
36
|
def get_env(self) -> Environment:
|
@@ -34,7 +40,7 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
34
40
|
If `templates_dir` is provided, a FileSystemLoader is created with that directory (or directories).
|
35
41
|
Otherwise, no loader is set.
|
36
42
|
|
37
|
-
The custom
|
43
|
+
The custom filters are added before returning the environment.
|
38
44
|
"""
|
39
45
|
if self.templates_dir:
|
40
46
|
if isinstance(self.templates_dir, str):
|
@@ -44,7 +50,12 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
44
50
|
else:
|
45
51
|
loader = None
|
46
52
|
env = Environment(loader=loader, autoescape=False)
|
53
|
+
|
54
|
+
# Add basic filters
|
47
55
|
env.filters["split"] = self.split_whitespace
|
56
|
+
env.filters["make_singular"] = self.make_singular
|
57
|
+
env.filters["make_plural"] = self.make_plural
|
58
|
+
|
48
59
|
return env
|
49
60
|
|
50
61
|
def set_template(self, template: Union[str, FilePath]) -> None:
|
@@ -55,16 +66,10 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
55
66
|
- If it is a FilePath, the template is loaded via an environment using `get_template()`.
|
56
67
|
"""
|
57
68
|
if type(template) is str:
|
58
|
-
self.
|
69
|
+
self.template = template
|
59
70
|
else:
|
60
71
|
self._set_template_from_filepath(template)
|
61
72
|
|
62
|
-
def _set_template_from_str(self, template_str: str) -> None:
|
63
|
-
"""
|
64
|
-
Sets the template from a literal string.
|
65
|
-
"""
|
66
|
-
self.template = template_str
|
67
|
-
|
68
73
|
def _set_template_from_filepath(self, template_path: FilePath) -> None:
|
69
74
|
"""
|
70
75
|
Loads the template from a file specified by a FilePath.
|
@@ -131,9 +136,12 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
131
136
|
loader=FileSystemLoader([directory]), autoescape=False
|
132
137
|
)
|
133
138
|
fallback_env.filters["split"] = self.split_whitespace
|
139
|
+
fallback_env.filters["make_singular"] = self.make_singular
|
140
|
+
fallback_env.filters["make_plural"] = self.make_plural
|
141
|
+
|
134
142
|
self.template = fallback_env.get_template(template_name)
|
135
143
|
|
136
|
-
def generate_prompt(self, variables: Dict[str,
|
144
|
+
def generate_prompt(self, variables: Dict[str, Any] = None) -> str:
|
137
145
|
"""
|
138
146
|
Generates a prompt by rendering the current template with the provided variables.
|
139
147
|
|
@@ -143,14 +151,17 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
143
151
|
variables = variables if variables else self.variables
|
144
152
|
return self.fill(variables)
|
145
153
|
|
146
|
-
def __call__(self, variables: Optional[Dict[str,
|
154
|
+
def __call__(self, variables: Optional[Dict[str, Any]] = None) -> str:
|
147
155
|
"""
|
148
156
|
Allows the instance to be called directly to generate a prompt.
|
149
157
|
"""
|
150
158
|
variables = variables if variables else self.variables
|
151
159
|
return self.fill(variables)
|
152
160
|
|
153
|
-
def fill(self, variables: Dict[str,
|
161
|
+
def fill(self, variables: Dict[str, Any] = None) -> str:
|
162
|
+
"""
|
163
|
+
Renders the template with the provided variables.
|
164
|
+
"""
|
154
165
|
variables = variables or self.variables
|
155
166
|
env = self.get_env()
|
156
167
|
if isinstance(self.template, Template):
|
@@ -170,3 +181,49 @@ class J2PromptTemplate(PromptTemplateBase):
|
|
170
181
|
return value.split(delimiter)
|
171
182
|
else:
|
172
183
|
return value.split()
|
184
|
+
|
185
|
+
@staticmethod
|
186
|
+
def make_singular(word: str):
|
187
|
+
"""
|
188
|
+
Converts a plural word to singular form.
|
189
|
+
Requires inflect library to be installed.
|
190
|
+
"""
|
191
|
+
try:
|
192
|
+
import inflect
|
193
|
+
|
194
|
+
# Initialize the engine
|
195
|
+
p = inflect.engine()
|
196
|
+
# Return the singular form of the verb
|
197
|
+
return p.singular_noun(word) if p.singular_noun(word) else word
|
198
|
+
except ImportError:
|
199
|
+
# Return the original if inflect is not available
|
200
|
+
return word
|
201
|
+
|
202
|
+
@staticmethod
|
203
|
+
def make_plural(word: str) -> str:
|
204
|
+
"""
|
205
|
+
Converts a singular word to its plural form.
|
206
|
+
Requires inflect library to be installed.
|
207
|
+
"""
|
208
|
+
try:
|
209
|
+
import inflect
|
210
|
+
|
211
|
+
p = inflect.engine()
|
212
|
+
return p.plural(word) or word
|
213
|
+
except ImportError:
|
214
|
+
return word
|
215
|
+
|
216
|
+
def add_filter(self, name: str, filter_func: Callable) -> None:
|
217
|
+
"""
|
218
|
+
Adds a custom filter to the Jinja2 environment.
|
219
|
+
|
220
|
+
Parameters:
|
221
|
+
name: The name of the filter (used in templates)
|
222
|
+
filter_func: The function to be called when the filter is used
|
223
|
+
"""
|
224
|
+
env = self.get_env()
|
225
|
+
env.filters[name] = filter_func
|
226
|
+
|
227
|
+
|
228
|
+
# Create a singleton instance for peagen usage with code generation mode enabled
|
229
|
+
j2pt = J2PromptTemplate()
|
{swarmauri_prompt_j2prompttemplate-0.7.1.dev2 → swarmauri_prompt_j2prompttemplate-0.7.2}/LICENSE
RENAMED
File without changes
|