ai-prompter 0.2.1__tar.gz → 0.2.3__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.
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/PKG-INFO +1 -1
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/notebooks/prompter_usage.ipynb +30 -9
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/pyproject.toml +1 -1
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/src/ai_prompter/__init__.py +48 -8
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/tests/test_prompter.py +6 -1
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/uv.lock +1 -1
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/.env.sample +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/.github/workflows/publish.yml +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/.gitignore +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/.python-version +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/LICENSE +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/Makefile +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/README.md +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/notebooks/prompts/article.jinja +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/notebooks/prompts/inner.jinja +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/notebooks/prompts/outer.jinja +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/src/ai_prompter/py.typed +0 -0
- {ai_prompter-0.2.1 → ai_prompter-0.2.3}/tests/prompts/greet.jinja +0 -0
@@ -77,7 +77,7 @@
|
|
77
77
|
{
|
78
78
|
"data": {
|
79
79
|
"text/plain": [
|
80
|
-
"'
|
80
|
+
"'/Users/luisnovo/dev/projetos/ai-prompter/notebooks/prompts/article.jinja'"
|
81
81
|
]
|
82
82
|
},
|
83
83
|
"execution_count": 3,
|
@@ -85,6 +85,27 @@
|
|
85
85
|
"output_type": "execute_result"
|
86
86
|
}
|
87
87
|
],
|
88
|
+
"source": [
|
89
|
+
"prompter = Prompter(prompt_template=\"article\") #will look for article.jinja in the prompts directory\n",
|
90
|
+
"prompter.template_location(\"article\")"
|
91
|
+
]
|
92
|
+
},
|
93
|
+
{
|
94
|
+
"cell_type": "code",
|
95
|
+
"execution_count": 4,
|
96
|
+
"metadata": {},
|
97
|
+
"outputs": [
|
98
|
+
{
|
99
|
+
"data": {
|
100
|
+
"text/plain": [
|
101
|
+
"'Write an article about AI.'"
|
102
|
+
]
|
103
|
+
},
|
104
|
+
"execution_count": 4,
|
105
|
+
"metadata": {},
|
106
|
+
"output_type": "execute_result"
|
107
|
+
}
|
108
|
+
],
|
88
109
|
"source": [
|
89
110
|
"prompter = Prompter(prompt_template=\"article\") #will look for article.jinja in the prompts directory\n",
|
90
111
|
"\n",
|
@@ -103,7 +124,7 @@
|
|
103
124
|
},
|
104
125
|
{
|
105
126
|
"cell_type": "code",
|
106
|
-
"execution_count":
|
127
|
+
"execution_count": 5,
|
107
128
|
"metadata": {},
|
108
129
|
"outputs": [
|
109
130
|
{
|
@@ -112,7 +133,7 @@
|
|
112
133
|
"'This is the outer file \\n\\nThis is the inner file\\n\\nValue: a\\n\\n\\n You selected A\\n\\n\\nThis is the end of the outer file'"
|
113
134
|
]
|
114
135
|
},
|
115
|
-
"execution_count":
|
136
|
+
"execution_count": 5,
|
116
137
|
"metadata": {},
|
117
138
|
"output_type": "execute_result"
|
118
139
|
}
|
@@ -136,7 +157,7 @@
|
|
136
157
|
},
|
137
158
|
{
|
138
159
|
"cell_type": "code",
|
139
|
-
"execution_count":
|
160
|
+
"execution_count": 6,
|
140
161
|
"metadata": {},
|
141
162
|
"outputs": [
|
142
163
|
{
|
@@ -145,7 +166,7 @@
|
|
145
166
|
"ChatPromptTemplate(input_variables=['topic'], input_types={}, partial_variables={}, messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['topic'], input_types={}, partial_variables={}, template='Write an article about {{topic}}.', template_format='jinja2'), additional_kwargs={})])"
|
146
167
|
]
|
147
168
|
},
|
148
|
-
"execution_count":
|
169
|
+
"execution_count": 6,
|
149
170
|
"metadata": {},
|
150
171
|
"output_type": "execute_result"
|
151
172
|
}
|
@@ -162,7 +183,7 @@
|
|
162
183
|
},
|
163
184
|
{
|
164
185
|
"cell_type": "code",
|
165
|
-
"execution_count":
|
186
|
+
"execution_count": 7,
|
166
187
|
"metadata": {},
|
167
188
|
"outputs": [
|
168
189
|
{
|
@@ -171,7 +192,7 @@
|
|
171
192
|
"ChatPromptTemplate(input_variables=['type'], input_types={}, partial_variables={}, messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['type'], input_types={}, partial_variables={}, template=\"This is the outer file \\n\\nThis is the inner file\\n\\nValue: {{ type }}\\n\\n{% if type == 'a' %}\\n You selected A\\n{% else %}\\n You didn't select A\\n{% endif %}\\n\\n\\nThis is the end of the outer file\\n\", template_format='jinja2'), additional_kwargs={})])"
|
172
193
|
]
|
173
194
|
},
|
174
|
-
"execution_count":
|
195
|
+
"execution_count": 7,
|
175
196
|
"metadata": {},
|
176
197
|
"output_type": "execute_result"
|
177
198
|
}
|
@@ -184,7 +205,7 @@
|
|
184
205
|
},
|
185
206
|
{
|
186
207
|
"cell_type": "code",
|
187
|
-
"execution_count":
|
208
|
+
"execution_count": 8,
|
188
209
|
"metadata": {},
|
189
210
|
"outputs": [
|
190
211
|
{
|
@@ -193,7 +214,7 @@
|
|
193
214
|
"ChatPromptValue(messages=[HumanMessage(content='This is the outer file \\n\\nThis is the inner file\\n\\nValue: a\\n\\n\\n You selected A\\n\\n\\n\\nThis is the end of the outer file', additional_kwargs={}, response_metadata={})])"
|
194
215
|
]
|
195
216
|
},
|
196
|
-
"execution_count":
|
217
|
+
"execution_count": 8,
|
197
218
|
"metadata": {},
|
198
219
|
"output_type": "execute_result"
|
199
220
|
}
|
@@ -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
|
@@ -17,10 +17,15 @@ def test_raw_text_template():
|
|
17
17
|
|
18
18
|
|
19
19
|
def test_missing_both_raises():
|
20
|
-
with pytest.raises(ValueError):
|
20
|
+
with pytest.raises(ValueError, match="Either prompt_template or template_text must be provided"):
|
21
21
|
Prompter()
|
22
22
|
|
23
23
|
|
24
|
+
def test_both_parameters_raises():
|
25
|
+
with pytest.raises(ValueError, match="Cannot provide both"):
|
26
|
+
Prompter(template_text="Hello", prompt_template="template")
|
27
|
+
|
28
|
+
|
24
29
|
def test_base_model_data():
|
25
30
|
class DataModel(BaseModel):
|
26
31
|
foo: str
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|