swarmauri_prompt_j2prompttemplate 0.7.1.dev2__py3-none-any.whl → 0.7.2.dev14__py3-none-any.whl

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.
@@ -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 'split' filter is added before returning the environment.
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._set_template_from_str(template)
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, str] = None) -> 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, str]] = None) -> 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, str] = None) -> 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()
@@ -1,7 +1,7 @@
1
- from .J2PromptTemplate import J2PromptTemplate
1
+ from .J2PromptTemplate import J2PromptTemplate, j2pt
2
2
 
3
3
 
4
- __all__ = ["J2PromptTemplate"]
4
+ __all__ = ["J2PromptTemplate", "j2pt"]
5
5
 
6
6
  try:
7
7
  # For Python 3.8 and newer
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: swarmauri_prompt_j2prompttemplate
3
- Version: 0.7.1.dev2
3
+ Version: 0.7.2.dev14
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: jinja2 (>=3.0)
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/blob/master/pkgs/standards/swarmauri_tool_j2prompttemplate/README.md">
25
- <img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https://github.com/swarmauri/swarmauri-sdk/pkgs/standards/swarmauri_tool_j2prompttemplate/README.md&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false" alt="GitHub Hits"/></a>
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/">
@@ -0,0 +1,6 @@
1
+ swarmauri_prompt_j2prompttemplate/J2PromptTemplate.py,sha256=d2BFrMxgM98ZGbQtgbrKLZx59ND5CqI2s564GHVz7rg,9082
2
+ swarmauri_prompt_j2prompttemplate/__init__.py,sha256=UOpZnWj00_QRvvIZy9TfXjuQ2hrDDhbeIu-xvzj_ixY,527
3
+ swarmauri_prompt_j2prompttemplate-0.7.2.dev14.dist-info/LICENSE,sha256=djUXOlCxLVszShEpZXshZ7v33G-2qIC_j9KXpWKZSzQ,11359
4
+ swarmauri_prompt_j2prompttemplate-0.7.2.dev14.dist-info/METADATA,sha256=bDus8pqpZG9sOmTmfAD0Metw1oaM59jE1kcP8mSIY9Q,3090
5
+ swarmauri_prompt_j2prompttemplate-0.7.2.dev14.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
6
+ swarmauri_prompt_j2prompttemplate-0.7.2.dev14.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.2
2
+ Generator: poetry-core 2.1.3
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- swarmauri_prompt_j2prompttemplate/J2PromptTemplate.py,sha256=OHwFoW6FWMngHDczlV8pJyD8fx_xtK0jCvemTyhoYOc,7246
2
- swarmauri_prompt_j2prompttemplate/__init__.py,sha256=x84YMbfnymTWk9wsDs1Qrk4fx5ceZx3AaCrZO4jIHXw,513
3
- swarmauri_prompt_j2prompttemplate-0.7.1.dev2.dist-info/LICENSE,sha256=djUXOlCxLVszShEpZXshZ7v33G-2qIC_j9KXpWKZSzQ,11359
4
- swarmauri_prompt_j2prompttemplate-0.7.1.dev2.dist-info/METADATA,sha256=SBMSXNazgBsCgKDSxgE_tVOP3Nvj1U50_qlXMLKFSfw,3193
5
- swarmauri_prompt_j2prompttemplate-0.7.1.dev2.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
6
- swarmauri_prompt_j2prompttemplate-0.7.1.dev2.dist-info/RECORD,,