nova-prompt-optimizer 1.0.48__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.
Files changed (40) hide show
  1. nova_prompt_optimizer-1.0.48/LICENSE +175 -0
  2. nova_prompt_optimizer-1.0.48/NOTICE +1 -0
  3. nova_prompt_optimizer-1.0.48/PKG-INFO +356 -0
  4. nova_prompt_optimizer-1.0.48/README.md +329 -0
  5. nova_prompt_optimizer-1.0.48/pyproject.toml +30 -0
  6. nova_prompt_optimizer-1.0.48/requirements.txt +10 -0
  7. nova_prompt_optimizer-1.0.48/setup.cfg +4 -0
  8. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/__init__.py +17 -0
  9. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/__version__.py +14 -0
  10. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/__init__.py +0 -0
  11. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/evaluation/__init__.py +140 -0
  12. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/inference/__init__.py +182 -0
  13. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/inference/adapter.py +109 -0
  14. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/inference/bedrock_converse.py +115 -0
  15. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/inference/inference_constants.py +22 -0
  16. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/input_adapters/__init__.py +0 -0
  17. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/input_adapters/dataset_adapter.py +192 -0
  18. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/input_adapters/metric_adapter.py +39 -0
  19. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/input_adapters/prompt_adapter.py +361 -0
  20. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/__init__.py +17 -0
  21. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/adapter.py +47 -0
  22. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/miprov2/__init__.py +0 -0
  23. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/miprov2/custom_adapters/custom_chat_adapter.py +115 -0
  24. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/miprov2/custom_lm/rate_limited_lm.py +46 -0
  25. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/miprov2/miprov2_optimizer.py +468 -0
  26. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_meta_prompter/__init__.py +14 -0
  27. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_meta_prompter/nova_mp_optimizer.py +163 -0
  28. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_meta_prompter/nova_prompt_template.py +127 -0
  29. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_prompt_optimizer/__init__.py +14 -0
  30. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_prompt_optimizer/nova_grounded_proposer.py +87 -0
  31. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/core/optimizers/nova_prompt_optimizer/nova_prompt_optimizer.py +108 -0
  32. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/py.typed +0 -0
  33. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/util/__init__.py +0 -0
  34. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/util/logging_utils.py +86 -0
  35. nova_prompt_optimizer-1.0.48/src/amzn_nova_prompt_optimizer/util/rate_limiter.py +63 -0
  36. nova_prompt_optimizer-1.0.48/src/nova_prompt_optimizer.egg-info/PKG-INFO +356 -0
  37. nova_prompt_optimizer-1.0.48/src/nova_prompt_optimizer.egg-info/SOURCES.txt +38 -0
  38. nova_prompt_optimizer-1.0.48/src/nova_prompt_optimizer.egg-info/dependency_links.txt +1 -0
  39. nova_prompt_optimizer-1.0.48/src/nova_prompt_optimizer.egg-info/requires.txt +16 -0
  40. nova_prompt_optimizer-1.0.48/src/nova_prompt_optimizer.egg-info/top_level.txt +1 -0
@@ -0,0 +1,175 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
@@ -0,0 +1 @@
1
+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
@@ -0,0 +1,356 @@
1
+ Metadata-Version: 2.4
2
+ Name: nova-prompt-optimizer
3
+ Version: 1.0.48
4
+ Summary: A Python SDK for optimizing prompts for Nova.
5
+ Author-email: Amazon <nova-prompt-optimizer@amazon.com>
6
+ License-Expression: Apache-2.0
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ License-File: NOTICE
11
+ Requires-Dist: jinja2
12
+ Requires-Dist: boto3
13
+ Requires-Dist: botocore
14
+ Requires-Dist: boto3-stubs
15
+ Requires-Dist: dspy
16
+ Requires-Dist: numpy==2.3.1
17
+ Requires-Dist: virtualenv==20.31.2
18
+ Requires-Dist: urllib3==2.5.0
19
+ Requires-Dist: setuptools==80.9.0
20
+ Requires-Dist: h11==0.16.0
21
+ Provides-Extra: dev
22
+ Requires-Dist: build; extra == "dev"
23
+ Requires-Dist: setuptools; extra == "dev"
24
+ Requires-Dist: twine; extra == "dev"
25
+ Requires-Dist: wheel; extra == "dev"
26
+ Dynamic: license-file
27
+
28
+ # Nova Prompt Optimizer
29
+
30
+ A Python SDK for optimizing prompts for Nova.
31
+
32
+ ## 📚 Table of contents
33
+ * [Installation](#installation)
34
+ * [Pre-Requisites](#pre-requisites)
35
+ * [Quick Start: Facility Support Analyzer Dataset](#-quick-start)
36
+ * [Core Concepts](#core-concepts)
37
+ * [Input Adapters](#input-adapters)
38
+ * [1. Prompt Adapter](#1-prompt-adapter)
39
+ * [2. Inference Adapter](#2-inference-adapter)
40
+ * [3. Dataset Adapter](#3-dataset-adapter)
41
+ * [4. Metric Adapter](#4-metric-adapter)
42
+ * [5. Optimization Adapter](#5-optimization-adapter)
43
+ * [Optimizers](#optimizers)
44
+ * [NovaPromptOptimizer](#novapromptoptimizer)
45
+ * [Evaluator](#evaluator)
46
+ * [Optimization Recommendations](#optimization-recommendations)
47
+ * [Preview Status](#-preview-status)
48
+ * [Acknowledgements](#acknowledgements)
49
+
50
+ ## Installation
51
+
52
+ Install the library using
53
+ ```sh
54
+ pip install amzn-nova-prompt-optimizer
55
+ ```
56
+
57
+
58
+ ## Pre-Requisites
59
+
60
+ #### Setup your AWS Access Keys:
61
+
62
+ To execute the SDK, you will need AWS credentials configured. Take a look at the [AWS CLI configuration documentation](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html#config-settings-and-precedence) for details on the various ways to configure credentials.
63
+ An easy way to try out the SDK is to populate the following environment variables with your AWS API credentials.
64
+ Take a look at this guide for [Authenticating with short-term credentials for the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-authentication-short-term.html)
65
+
66
+ ```commandline
67
+ export AWS_ACCESS_KEY_ID="..."
68
+ export AWS_SECRET_ACCESS_KEY="..."
69
+ ```
70
+
71
+ #### To enable Nova model access:
72
+ 1. Go to the Amazon Bedrock Model Access page
73
+ 2. Click "Manage model access"
74
+ 3. Choose Amazon as provider and Nova models
75
+ 4. Click "Request access"
76
+ 5. Wait for approval (instant in most cases)
77
+
78
+
79
+ ## 🏁 Quick Start
80
+ ### Facility Support Analyzer Dataset
81
+ The Facility Support Analyzer dataset consists of emails that are to be classified based on category, urgency and sentiment.
82
+
83
+ Please see the [samples](samples/facility-support-analyzer/) folder for example notebooks of how to optimize a prompt in the scenario where a [user prompt template is to be optimized](samples/facility-support-analyzer/user_prompt_only) and the scenario where a [user and system prompt is to be optimized together](samples/facility-support-analyzer/system_and_user_prompt)
84
+
85
+
86
+ ## Core Concepts
87
+
88
+ ### Input Adapters
89
+ We break each component of Prompt Optimization into Adapters providing a modular approach to Prompt Optimization.
90
+
91
+ ![adapters](docs/adapters.png)
92
+
93
+ ### 1. Prompt Adapter
94
+
95
+ **Responsibility:** Ability to load prompts from different formats and store them in the standardized format (JSON)
96
+
97
+
98
+ **Sample Prompt Adapter Initialization**
99
+
100
+ ```python
101
+ from amzn_nova_prompt_optimizer.core.input_adapters.prompt_adapter import TextPromptAdapter
102
+
103
+ prompt_adapter = TextPromptAdapter()
104
+
105
+ prompt_adapter.set_system_prompt(file_path="prompt/sys_prompt.txt", variables={"foo"})
106
+
107
+ prompt_adapter.set_user_prompt(content="You are a .....", variables={"bar"})
108
+
109
+ prompt_adapter.adapt()
110
+ ```
111
+
112
+ **Supported Prompt Adapters:** `TextPromptAdapter`
113
+
114
+ Learn More about the Prompt Adapter [here](docs/PromptAdapter.md)
115
+
116
+ ### 2. Inference Adapter
117
+ **Responsibility:** Ability to call an inference backend for the models e.g. Bedrock, etc.
118
+
119
+ **Sample use of Inference Adapter**
120
+
121
+ ```python
122
+ from amzn_nova_prompt_optimizer.core.inference.adapter import BedrockInferenceAdapter
123
+
124
+ inference_adapter = BedrockInferenceAdapter(region_name="us-east-1")
125
+ ```
126
+
127
+ You can pass `rate_limit` into constructor of InferenceAdapter to limit the max TPS of bedrock call to avoid throttle. Default to 2 if not set.
128
+
129
+ ```python
130
+ from amzn_nova_prompt_optimizer.core.inference.adapter import BedrockInferenceAdapter
131
+
132
+ inference_adapter = BedrockInferenceAdapter(region_name="us-east-1", rate_limit=10) # Max 10 TPS
133
+ ```
134
+
135
+ **Supported Inference Adapters:** `BedrockInferenceAdapter`
136
+
137
+ **Core Functions**
138
+
139
+ Call the model using the parameters
140
+ ```python
141
+ # Call the model with the passed parametrs
142
+ inference_output = inference_adapter.call_model(model_id: str, system_prompt: str, messages: List[Dict[str, str]], inf_config: Dict[str, Any])
143
+ ```
144
+
145
+ The Inference Adapter accepts the `system_prompt` as a string.
146
+
147
+ The input to the model as a list of User/Assistant turns (messages). e.g. `[{"user": "foo"}, {"assistant": "bar"}, {"user": "What comes next?"}]`
148
+
149
+ ### 3. Dataset Adapter
150
+
151
+ **Responsibility:** Ability to read/write datasets from different formats. Uses an intermediary standardized format when communicating with other adapters.
152
+ It can also read list of JSON object. It can also create Train/Test splits (with stratify capability if set).
153
+
154
+ **Requirements:** Currently, you can only provide a singleton set as output column.
155
+
156
+ **Sample Dataset Adapter Initialization**
157
+ ```python
158
+ # Example Usage
159
+ from amzn_nova_prompt_optimizer.core.input_adapters.dataset_adapter import JSONDatasetAdapter
160
+
161
+ input_columns = {"input"}
162
+ output_columns = {"answer"}
163
+
164
+ dataset_adapter = JSONDatasetAdapter(input_columns, output_columns)
165
+
166
+ # Adapt
167
+ dataset_adapter.adapt(data_source="sample_data.jsonl")
168
+
169
+ # Split
170
+ train, test = dataset_adapter.split(0.5)
171
+ ```
172
+
173
+ **Supported Dataset Adapters:** `JSONDatasetAdapter`, `CSVDatasetAdapter`
174
+
175
+ Learn More about the Dataset Adapter [here](docs/DatasetAdapter.md)
176
+
177
+ ### 4. Metric Adapter
178
+ **Responsibility:** Ability to load custom metrics and apply them on inference output and ground truth
179
+
180
+ **Metric Adapter Class**
181
+ ```python
182
+ class MetricAdapter():
183
+ def apply(self, y_pred: Any, y_true: Any) -> float:
184
+ pass
185
+
186
+ def batch_apply(self, y_preds: List[Any], y_trues: List[Any]) -> float:
187
+ pass
188
+ ```
189
+
190
+ **Sample Custom Metric Adapter Initialization**
191
+ Let's create a Custom metric adapter that parses the inference output for the answer between `<answer> </answer>` tags and then performs an exact match metric.
192
+
193
+ ```python
194
+ from amzn_nova_prompt_optimizer.core.input_adapters.metric_adapter import MetricAdapter
195
+ from typing import List, Any, Dict
196
+ import re
197
+ import json
198
+
199
+ class CustomMetric(MetricAdapter):
200
+ def _parse_answer(self, model_output):
201
+ # Parse Answer between tags
202
+ match = re.search(r"<answer>(.*?)</answer>", model_output)
203
+ if not match:
204
+ return "Choice not found"
205
+ return match.group(1).lower().strip()
206
+
207
+ def _calculate_metrics(self, y_pred: Any, y_true: Any) -> Dict:
208
+ # Peform Exact Match
209
+ pred = self._parse_answer(y_pred)
210
+ ground_truth = self._parse_answer(y_true)
211
+ return int(pred == ground_truth)
212
+
213
+ def apply(self, y_pred: Any, y_true: Any):
214
+ # Apply to one row of the dataset
215
+ return self._calculate_metrics(y_pred, y_true)
216
+
217
+ def batch_apply(self, y_preds: List[Any], y_trues: List[Any]):
218
+ # Apply to the whole dataset
219
+ evals = []
220
+ for y_pred, y_true in zip(y_preds, y_trues):
221
+ evals.append(self.apply(y_pred, y_true))
222
+ return sum(evals)/len(evals)
223
+
224
+ metric_adapter = CustomMetric()
225
+ ```
226
+
227
+ **Core Functions**
228
+ Apply the metric on a prediction and ground_truth one row at a time
229
+ ```python
230
+ y_pred = "The question asks ...... <answer>3</answer>"
231
+ y_true = "<answer>3</answer>"
232
+
233
+ # Apply the metric on a prediction and ground_truth
234
+ score = metric_adapter.apply(y_pred, y_true)
235
+
236
+ # score = 1
237
+ ```
238
+
239
+ Apply the metric on a list of prediction and ground_truth i.e. for the dataset
240
+ ```python
241
+ y_preds = ["The question asks ...... <answer>3</answer>", "The question asks ...... <answer>5</answer>"]
242
+ y_trues = ["<answer>3</answer>", "<answer>4</answer>"]
243
+
244
+ # Apply the metric on a list of prediction and ground_truth
245
+ aggregeate_score = metric_adapter.batch_apply(y_preds, y_trues)
246
+
247
+ # aggregeate_score = 0.5
248
+ ```
249
+
250
+
251
+ ### 5. Optimization Adapter
252
+ **Responsibility:** Load Optimizer, Prompt Adapter, and Optionally Dataset Adapter, Metric Adapter, and Inference Adapter. Perform Optimization and ability to create a Prompt Adapter with the Optimized Prompt.
253
+
254
+ Sample Optimization Initialization
255
+ ```python
256
+ from amzn_nova_prompt_optimizer.core.optimizers import NovaPromptOptimizer
257
+
258
+ nova_prompt_optimizer = NovaPromptOptimizer(prompt_adapter=prompt_adapter, inference_adapter=inference_adapter, dataset_adapter=train_dataset_adapter, metric_adapter=metric_adapter)
259
+
260
+ optimized_prompt_adapter = nova_prompt_optimizer.optimize(mode="lite")
261
+ ```
262
+
263
+ We can take a look more deeply into the optimizers in the next section.
264
+
265
+
266
+ ## Optimizers
267
+
268
+ ### NovaPromptOptimizer
269
+
270
+ NovaPromptOptimizer is a combination of Meta Prompting using the Nova Guide on prompting and DSPy's MIPROv2 Optimizer using Nova Prompting Tips.
271
+ NovaPromptOptimizer first runs a meta prompter to identify system instructions and user template from the prompt adapter.
272
+ Then MIPROv2 is run on top of this to optimize system instructions and identify few-shot samples that need to be added.
273
+ The few shot samples are added as `converse` format so they are added as User/Assistant turns.
274
+
275
+ **Requirements:** NovaPromptOptimizer requires Prompt Adapter, Dataset Adapter, Metric Adapter and Inference Adapter.
276
+
277
+ **Optimization Example**
278
+ ```python
279
+ from amzn_nova_prompt_optimizer.core.optimizers import NovaPromptOptimizer
280
+
281
+ nova_prompt_optimizer = NovaPromptOptimizer(prompt_adapter=prompt_adapter, inference_adapter=inference_adapter, dataset_adapter=train_dataset_adapter, metric_adapter=metric_adapter)
282
+
283
+ optimized_prompt_adapter = nova_prompt_optimizer.optimize(mode="lite")
284
+ ```
285
+ NovaPromptOptimizer uses Premier for Meta Prompting and then uses MIPROv2 with 20 candidates and 50 trials with Premier as Prompting model and task model dependent on the mode it's set at.
286
+ You can specify enable_json_fallback=False to disable the behavior that MIPROv2 will [fallback to use JSONAdapter to parse LM model output](https://github.com/stanfordnlp/dspy/blob/main/dspy/adapters/chat_adapter.py#L44-L51). This will force MIPROv2 use structured output (pydantic model) to parse LM output.
287
+
288
+ You could also define a custom mode and pass your own parameter values to NovaPromptOptimizer
289
+
290
+ ```python
291
+ from amzn_nova_prompt_optimizer.core.optimizers import NovaPromptOptimizer
292
+
293
+ nova_prompt_optimizer = NovaPromptOptimizer(prompt_adapter=prompt_adapter, inference_adapter=inference_adapter, dataset_adapter=train_dataset_adapter, metric_adapter=metric_adapter)
294
+
295
+ optimized_prompt_adapter = nova_prompt_optimizer.optimize(mode="custom", custom_params={"task_model_id": "us.amazon.nova-pro-v1:0",
296
+ "num_candidates": 10,
297
+ "num_trials": 15,
298
+ "max_bootstrapped_demos": 5,
299
+ "max_labeled_demos": 0
300
+ })
301
+ ```
302
+
303
+ Learn More about the Optimizers [here](docs/Optimizers.md)
304
+
305
+ ## Evaluator
306
+ The SDK also provides a way to baseline prompts and provide evaluation scores.
307
+ The evaluator has the `aggregate_score` and `scores` function.
308
+
309
+ **Initialization Example**
310
+ ```python
311
+ from amzn_nova_prompt_optimizer.core.evaluation import Evaluator
312
+
313
+ evaluator = Evaluator(nova_mp_optimized_prompt_adapter, test_dataset_adapter, metric_adapter, inference_adapter)
314
+
315
+ nova_mp_score = evaluator.aggregate_score(model_id="us.amazon.nova-lite-v1:0")
316
+ ```
317
+ **Core Functions**
318
+
319
+ Runs Batch evaluation on the dataset using the batch_apply function of the metric
320
+ ```python
321
+ # Uses Batch Apply
322
+ aggregate_score = evaluator.aggregate_score(model_id)
323
+ ```
324
+
325
+ Runs evaluation on the dataset a row at a time and returns the eval results as a whole.
326
+ ```python
327
+ # Uses Apply metric. Returns a list of scores.
328
+ scores = evaluator.score(model_id)
329
+ ```
330
+
331
+ Save the eval results.
332
+ ```python
333
+ # Save the eval results
334
+ evaluator.save("eval_results.jsonl")
335
+ ```
336
+
337
+
338
+ **Note: You may come across the below warning. This is when prompt variables are missing from the prompt, the inference runner under the evaluator appends them to the end of the prompt for continuity**
339
+ ```python
340
+ WARNING amzn_nova_prompt_optimizer.core.inference: Warn: Prompt Variables not found in User Prompt, injecting them at the end of the prompt
341
+ ```
342
+
343
+ ## Optimization Recommendations
344
+ 1. Provide representative real-world evaluation sets and split them into training and testing sets. Ensure dataset is balanced on output label when splitting train and test sets.
345
+ 2. For evaluation sets, the ground truth column should be as close to the inference output as possible. e.g. If the inference output is {"answer": "POSITIVE"} ground truth should also be in the same format {"answer": "POSITIVE"}
346
+ 3. For NovaPromptOptimizer, choose the mode (mode= "premier" | ""pro" | "lite" | "micro") based on your Nova Model of choice. By default, we use "pro".
347
+ 4. The `apply` function of the evaluation metric should return a numerical value between 0 and 1 for NovaPromptOptimizer or MIPROv2.
348
+
349
+ ## ⚠️ Preview Status
350
+
351
+ NovaPromptOptimizer is currently in public preview. During this period:
352
+ - SDK functionality might change as we support more use cases.
353
+ - We welcome feedback and contributions
354
+
355
+ ## Acknowledgements
356
+ * Special acknowledgment to [DSPy](https://github.com/stanfordnlp/dspy) – your innovations continue to inspire us.