ai-prompter 0.2.1__py3-none-any.whl → 0.2.3__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.
- ai_prompter/__init__.py +48 -8
- {ai_prompter-0.2.1.dist-info → ai_prompter-0.2.3.dist-info}/METADATA +1 -1
- ai_prompter-0.2.3.dist-info/RECORD +6 -0
- ai_prompter-0.2.1.dist-info/RECORD +0 -6
- {ai_prompter-0.2.1.dist-info → ai_prompter-0.2.3.dist-info}/WHEEL +0 -0
- {ai_prompter-0.2.1.dist-info → ai_prompter-0.2.3.dist-info}/licenses/LICENSE +0 -0
ai_prompter/__init__.py
CHANGED
@@ -5,7 +5,7 @@ A prompt management module using Jinja to generate complex prompts with simple t
|
|
5
5
|
import os
|
6
6
|
from dataclasses import dataclass
|
7
7
|
from datetime import datetime
|
8
|
-
from typing import Any, Dict, Optional, Union
|
8
|
+
from typing import Any, Dict, List, Optional, Union, Callable
|
9
9
|
|
10
10
|
from jinja2 import Environment, FileSystemLoader, Template
|
11
11
|
from pydantic import BaseModel
|
@@ -36,27 +36,43 @@ class Prompter:
|
|
36
36
|
template: Optional[Union[str, Template]] = None
|
37
37
|
template_text: Optional[str] = None
|
38
38
|
parser: Optional[Any] = None
|
39
|
+
text_templates: Optional[Dict[str, str]] = None
|
40
|
+
prompt_folders: Optional[List[str]] = None
|
39
41
|
|
40
42
|
def __init__(
|
41
43
|
self,
|
42
|
-
prompt_template:
|
43
|
-
model:
|
44
|
-
|
45
|
-
prompt_dir:
|
44
|
+
prompt_template: str | None = None,
|
45
|
+
model: str | Any | None = None,
|
46
|
+
prompt_variation: str = "default",
|
47
|
+
prompt_dir: str | None = None,
|
48
|
+
template_text: str | None = None,
|
49
|
+
parser: Callable[[str], dict[str, Any]] | None = None,
|
50
|
+
*args,
|
51
|
+
**kwargs,
|
46
52
|
) -> None:
|
47
53
|
"""Initialize the Prompter with a template name, model, and optional custom directory.
|
48
54
|
|
49
55
|
Args:
|
50
56
|
prompt_template (str, optional): The name of the prompt template (without .jinja extension).
|
51
57
|
model (Union[str, Any], optional): The model to use for generation.
|
52
|
-
|
58
|
+
prompt_variation (str, optional): The variation of the prompt template.
|
53
59
|
prompt_dir (str, optional): Custom directory to search for templates.
|
60
|
+
template_text (str, optional): The raw text of the template.
|
61
|
+
parser (Callable[[str], dict[str, Any]], optional): The parser to use for generation.
|
54
62
|
"""
|
63
|
+
if template_text is not None and prompt_template is not None:
|
64
|
+
raise ValueError(
|
65
|
+
"Cannot provide both template_text and prompt_template. Choose one or the other."
|
66
|
+
)
|
55
67
|
self.prompt_template = prompt_template
|
56
|
-
self.
|
68
|
+
self.prompt_variation = prompt_variation
|
69
|
+
self.prompt_dir = prompt_dir
|
57
70
|
self.template_text = template_text
|
71
|
+
self.parser = parser
|
72
|
+
self.template: Template | None = None
|
58
73
|
self.model = model or os.getenv("OPENAI_MODEL", "gpt-4-turbo")
|
59
|
-
self.
|
74
|
+
self.text_templates = {}
|
75
|
+
self.prompt_folders = []
|
60
76
|
self._setup_template(template_text, prompt_dir)
|
61
77
|
|
62
78
|
def _setup_template(
|
@@ -88,9 +104,11 @@ class Prompter:
|
|
88
104
|
prompt_dirs.append(prompt_path_default)
|
89
105
|
env = Environment(loader=FileSystemLoader(prompt_dirs))
|
90
106
|
self.template = env.get_template(f"{self.prompt_template}.jinja")
|
107
|
+
self.prompt_folders = prompt_dirs
|
91
108
|
else:
|
92
109
|
self.template_text = template_text
|
93
110
|
self.template = Template(template_text)
|
111
|
+
self.text_templates[self.prompt_template] = template_text
|
94
112
|
|
95
113
|
def to_langchain(self):
|
96
114
|
# Support for both text-based and file-based templates with LangChain
|
@@ -175,6 +193,28 @@ class Prompter:
|
|
175
193
|
"Either prompt_template with a valid template or template_text must be provided for LangChain conversion"
|
176
194
|
)
|
177
195
|
|
196
|
+
def template_location(self, template_name: str) -> str:
|
197
|
+
"""
|
198
|
+
Returns the location of the template used for the given template name.
|
199
|
+
If the template is a text template (not a file), returns 'text'.
|
200
|
+
If the template is not found, returns 'not found'.
|
201
|
+
|
202
|
+
Args:
|
203
|
+
template_name (str): The name of the template to check.
|
204
|
+
|
205
|
+
Returns:
|
206
|
+
str: The file path of the template, or 'text' if it's a text template, or 'not found' if the template doesn't exist.
|
207
|
+
"""
|
208
|
+
if template_name in self.text_templates:
|
209
|
+
return 'text'
|
210
|
+
|
211
|
+
for folder in self.prompt_folders:
|
212
|
+
template_file = os.path.join(folder, f"{template_name}.jinja")
|
213
|
+
if os.path.exists(template_file):
|
214
|
+
return template_file
|
215
|
+
|
216
|
+
return 'not found'
|
217
|
+
|
178
218
|
@classmethod
|
179
219
|
def from_text(
|
180
220
|
cls, text: str, model: Optional[Union[str, Any]] = None
|
@@ -0,0 +1,6 @@
|
|
1
|
+
ai_prompter/__init__.py,sha256=l5PTvboNK5uUSVoAJbNuntFc3iUlVCmGsThA8Qy3WI4,11894
|
2
|
+
ai_prompter/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
3
|
+
ai_prompter-0.2.3.dist-info/METADATA,sha256=o2E-K4xbBm69yJOxg6OFeqiIPUQFmwcXtDusefavdWA,6950
|
4
|
+
ai_prompter-0.2.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
ai_prompter-0.2.3.dist-info/licenses/LICENSE,sha256=cS0_fa_8BoP0PvVG8D19pn_HDJrG96hd4PyEm9nkRo8,1066
|
6
|
+
ai_prompter-0.2.3.dist-info/RECORD,,
|
@@ -1,6 +0,0 @@
|
|
1
|
-
ai_prompter/__init__.py,sha256=ehtuhOKP0ze3sW1cYO4OyNhoB8CY926TmkPqvzz4ilA,10124
|
2
|
-
ai_prompter/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
3
|
-
ai_prompter-0.2.1.dist-info/METADATA,sha256=c4Wh6qs7JVcBwWf1FCe2MZtsw5HjygYuXyJzhuKFTUc,6950
|
4
|
-
ai_prompter-0.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
ai_prompter-0.2.1.dist-info/licenses/LICENSE,sha256=cS0_fa_8BoP0PvVG8D19pn_HDJrG96hd4PyEm9nkRo8,1066
|
6
|
-
ai_prompter-0.2.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|