ai-prompter 0.2.2__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.2 → ai_prompter-0.2.3}/PKG-INFO +1 -1
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/notebooks/prompter_usage.ipynb +34 -13
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/pyproject.toml +1 -1
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/src/ai_prompter/__init__.py +29 -1
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/uv.lock +1 -1
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/.env.sample +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/.github/workflows/publish.yml +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/.gitignore +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/.python-version +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/LICENSE +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/Makefile +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/README.md +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/notebooks/prompts/article.jinja +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/notebooks/prompts/inner.jinja +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/notebooks/prompts/outer.jinja +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/src/ai_prompter/py.typed +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/tests/prompts/greet.jinja +0 -0
- {ai_prompter-0.2.2 → ai_prompter-0.2.3}/tests/test_prompter.py +0 -0
@@ -16,7 +16,7 @@
|
|
16
16
|
},
|
17
17
|
{
|
18
18
|
"cell_type": "code",
|
19
|
-
"execution_count":
|
19
|
+
"execution_count": 1,
|
20
20
|
"metadata": {},
|
21
21
|
"outputs": [],
|
22
22
|
"source": [
|
@@ -36,7 +36,7 @@
|
|
36
36
|
},
|
37
37
|
{
|
38
38
|
"cell_type": "code",
|
39
|
-
"execution_count":
|
39
|
+
"execution_count": 2,
|
40
40
|
"metadata": {},
|
41
41
|
"outputs": [
|
42
42
|
{
|
@@ -45,7 +45,7 @@
|
|
45
45
|
"'Write an article about AI.'"
|
46
46
|
]
|
47
47
|
},
|
48
|
-
"execution_count":
|
48
|
+
"execution_count": 2,
|
49
49
|
"metadata": {},
|
50
50
|
"output_type": "execute_result"
|
51
51
|
}
|
@@ -71,7 +71,28 @@
|
|
71
71
|
},
|
72
72
|
{
|
73
73
|
"cell_type": "code",
|
74
|
-
"execution_count":
|
74
|
+
"execution_count": 3,
|
75
|
+
"metadata": {},
|
76
|
+
"outputs": [
|
77
|
+
{
|
78
|
+
"data": {
|
79
|
+
"text/plain": [
|
80
|
+
"'/Users/luisnovo/dev/projetos/ai-prompter/notebooks/prompts/article.jinja'"
|
81
|
+
]
|
82
|
+
},
|
83
|
+
"execution_count": 3,
|
84
|
+
"metadata": {},
|
85
|
+
"output_type": "execute_result"
|
86
|
+
}
|
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,
|
75
96
|
"metadata": {},
|
76
97
|
"outputs": [
|
77
98
|
{
|
@@ -80,7 +101,7 @@
|
|
80
101
|
"'Write an article about AI.'"
|
81
102
|
]
|
82
103
|
},
|
83
|
-
"execution_count":
|
104
|
+
"execution_count": 4,
|
84
105
|
"metadata": {},
|
85
106
|
"output_type": "execute_result"
|
86
107
|
}
|
@@ -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, Callable
|
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,6 +36,8 @@ 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,
|
@@ -69,6 +71,8 @@ class Prompter:
|
|
69
71
|
self.parser = parser
|
70
72
|
self.template: Template | None = None
|
71
73
|
self.model = model or os.getenv("OPENAI_MODEL", "gpt-4-turbo")
|
74
|
+
self.text_templates = {}
|
75
|
+
self.prompt_folders = []
|
72
76
|
self._setup_template(template_text, prompt_dir)
|
73
77
|
|
74
78
|
def _setup_template(
|
@@ -100,9 +104,11 @@ class Prompter:
|
|
100
104
|
prompt_dirs.append(prompt_path_default)
|
101
105
|
env = Environment(loader=FileSystemLoader(prompt_dirs))
|
102
106
|
self.template = env.get_template(f"{self.prompt_template}.jinja")
|
107
|
+
self.prompt_folders = prompt_dirs
|
103
108
|
else:
|
104
109
|
self.template_text = template_text
|
105
110
|
self.template = Template(template_text)
|
111
|
+
self.text_templates[self.prompt_template] = template_text
|
106
112
|
|
107
113
|
def to_langchain(self):
|
108
114
|
# Support for both text-based and file-based templates with LangChain
|
@@ -187,6 +193,28 @@ class Prompter:
|
|
187
193
|
"Either prompt_template with a valid template or template_text must be provided for LangChain conversion"
|
188
194
|
)
|
189
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
|
+
|
190
218
|
@classmethod
|
191
219
|
def from_text(
|
192
220
|
cls, text: str, model: Optional[Union[str, Any]] = None
|
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
|
File without changes
|