llmeter 0.1.0__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.
- llmeter-0.1.0/LICENSE +175 -0
- llmeter-0.1.0/NOTICE +1 -0
- llmeter-0.1.0/PKG-INFO +125 -0
- llmeter-0.1.0/README.md +95 -0
- llmeter-0.1.0/llmeter/__init__.py +0 -0
- llmeter-0.1.0/llmeter/endpoints/__init__.py +13 -0
- llmeter-0.1.0/llmeter/endpoints/base.py +232 -0
- llmeter-0.1.0/llmeter/endpoints/bedrock.py +192 -0
- llmeter-0.1.0/llmeter/endpoints/litellm.py +165 -0
- llmeter-0.1.0/llmeter/endpoints/openai.py +78 -0
- llmeter-0.1.0/llmeter/endpoints/sagemaker.py +316 -0
- llmeter-0.1.0/llmeter/experiments.py +163 -0
- llmeter-0.1.0/llmeter/plotting.py +143 -0
- llmeter-0.1.0/llmeter/prompt_utils.py +190 -0
- llmeter-0.1.0/llmeter/results.py +228 -0
- llmeter-0.1.0/llmeter/runner.py +511 -0
- llmeter-0.1.0/llmeter/tokenizers.py +175 -0
- llmeter-0.1.0/llmeter/utils.py +32 -0
- llmeter-0.1.0/pyproject.toml +65 -0
llmeter-0.1.0/LICENSE
ADDED
|
@@ -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.
|
llmeter-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
llmeter-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: llmeter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight, cross-platform latency and throughput profiler for LLMs
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Keywords: llm,genai,testing,performance
|
|
7
|
+
Author: Amazon Web Services
|
|
8
|
+
Maintainer: llmeter-maintainers
|
|
9
|
+
Maintainer-email: llmeter-maintainers@amazon.com
|
|
10
|
+
Requires-Python: >=3.10,<3.13
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Provides-Extra: all
|
|
17
|
+
Provides-Extra: litellm
|
|
18
|
+
Provides-Extra: openai
|
|
19
|
+
Provides-Extra: plotting
|
|
20
|
+
Requires-Dist: boto3 (>=1.34.129)
|
|
21
|
+
Requires-Dist: fsspec[http,s3] (>=2023.6.0)
|
|
22
|
+
Requires-Dist: jmespath (>=0.7.1,<2.0.0)
|
|
23
|
+
Requires-Dist: litellm (>=1.47.1) ; extra == "all" or extra == "litellm"
|
|
24
|
+
Requires-Dist: openai (>=1.35.1) ; extra == "all" or extra == "openai"
|
|
25
|
+
Requires-Dist: seaborn (>=0.13.2) ; extra == "all" or extra == "plotting"
|
|
26
|
+
Requires-Dist: tqdm (>=4.66.0)
|
|
27
|
+
Requires-Dist: universal-pathlib (>=0.2.1)
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
<div align="center">
|
|
31
|
+
<img alt="LLMeter (Logo)" src="docs/llmeter-logotype-192px.png" height="96px" width="396px"/>
|
|
32
|
+
|
|
33
|
+
**Measuring large language models latency and throughput**
|
|
34
|
+
|
|
35
|
+
[](https://pypi.python.org/pypi/llmeter)
|
|
36
|
+
[](https://pypi.python.org/pypi/llmeter)
|
|
37
|
+
[](https://github.com/astral-sh/ruff)
|
|
38
|
+
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
LLMeter is a pure-python library for simple latency and throughput testing of large language models (LLMs). It's designed to be lightweight to install; straightforward to run standard tests; and versatile to integrate - whether in notebooks, CI/CD, or other workflows.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## :hammer_and_wrench: Installation
|
|
45
|
+
|
|
46
|
+
LLMeter requires `python>=3.10`, please make sure your current version of python is compatible.
|
|
47
|
+
|
|
48
|
+
To install the basic metering functionalities, you can install the minimum package using pip install:
|
|
49
|
+
|
|
50
|
+
```terminal
|
|
51
|
+
pip install llmeter
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
LLMeter also offers extra features that require additional dependencies. Currently these extras include:
|
|
55
|
+
|
|
56
|
+
- **plotting**: Add methods to generate charts and heatmaps to summarize the results
|
|
57
|
+
- **openai**: Enable testing endpoints offered by OpenAI
|
|
58
|
+
- **litellm**: Enable testing a range of different models through [LiteLLM](https://github.com/BerriAI/litellm)
|
|
59
|
+
|
|
60
|
+
You can install one or more of these extra options using pip:
|
|
61
|
+
|
|
62
|
+
```terminal
|
|
63
|
+
pip install llmeter[plotting, openai, litellm]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## :rocket: Quick-start
|
|
67
|
+
|
|
68
|
+
At a high level, you'll start by configuring an LLMeter "Endpoint" for whatever type of LLM you're connecting to:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
# For example with Amazon Bedrock...
|
|
72
|
+
from llmeter.endpoints import BedrockConverse
|
|
73
|
+
endpoint = BedrockConverse(model_id="...")
|
|
74
|
+
|
|
75
|
+
# ...or OpenAI...
|
|
76
|
+
from llmeter.endpoints import OpenAIEndpoint
|
|
77
|
+
endpoint = OpenAIEndpoint(model_id="...", api_key="...")
|
|
78
|
+
|
|
79
|
+
# ...or via LiteLLM...
|
|
80
|
+
from llmeter.endpoints import LiteLLM
|
|
81
|
+
endpoint = LiteLLM("{provider}/{model_id}")
|
|
82
|
+
|
|
83
|
+
# ...and so on
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
You can then run the high-level "experiments" offered by LLMeter:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
# For example a heatmap of latency by input & output token count:
|
|
90
|
+
from llmeter.experiments import LatencyHeatmap
|
|
91
|
+
latency_heatmap = LatencyHeatmap(
|
|
92
|
+
endpoint=endpoint,
|
|
93
|
+
clients=10,
|
|
94
|
+
source_file="examples/MaryShelleyFrankenstein.txt",
|
|
95
|
+
...
|
|
96
|
+
)
|
|
97
|
+
heatmap_results = await latency_heatmap.run()
|
|
98
|
+
latency_heatmap.plot_heatmap()
|
|
99
|
+
|
|
100
|
+
# ...Or testing how throughput varies with concurrent request count:
|
|
101
|
+
from llmeter.experiments import LoadTest
|
|
102
|
+
sweep_test = LoadTest(
|
|
103
|
+
endpoint=endpoint,
|
|
104
|
+
payload={...},
|
|
105
|
+
sequence_of_clients=[1, 5, 20, 50, 100, 500],
|
|
106
|
+
)
|
|
107
|
+
sweep_results = await sweep_test.run()
|
|
108
|
+
sweep_test.plot_sweep_results()
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Alternatively, you can use the low-level `llmeter.runner.Runner` class to run and analyze request
|
|
112
|
+
batches - and build your own custom experiments.
|
|
113
|
+
|
|
114
|
+
For more details, check out our selection of end-to-end code examples in the [examples](examples) folder!
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
## Security
|
|
118
|
+
|
|
119
|
+
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
This project is licensed under the Apache-2.0 License.
|
|
125
|
+
|
llmeter-0.1.0/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img alt="LLMeter (Logo)" src="docs/llmeter-logotype-192px.png" height="96px" width="396px"/>
|
|
3
|
+
|
|
4
|
+
**Measuring large language models latency and throughput**
|
|
5
|
+
|
|
6
|
+
[](https://pypi.python.org/pypi/llmeter)
|
|
7
|
+
[](https://pypi.python.org/pypi/llmeter)
|
|
8
|
+
[](https://github.com/astral-sh/ruff)
|
|
9
|
+
|
|
10
|
+
</div>
|
|
11
|
+
|
|
12
|
+
LLMeter is a pure-python library for simple latency and throughput testing of large language models (LLMs). It's designed to be lightweight to install; straightforward to run standard tests; and versatile to integrate - whether in notebooks, CI/CD, or other workflows.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## :hammer_and_wrench: Installation
|
|
16
|
+
|
|
17
|
+
LLMeter requires `python>=3.10`, please make sure your current version of python is compatible.
|
|
18
|
+
|
|
19
|
+
To install the basic metering functionalities, you can install the minimum package using pip install:
|
|
20
|
+
|
|
21
|
+
```terminal
|
|
22
|
+
pip install llmeter
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
LLMeter also offers extra features that require additional dependencies. Currently these extras include:
|
|
26
|
+
|
|
27
|
+
- **plotting**: Add methods to generate charts and heatmaps to summarize the results
|
|
28
|
+
- **openai**: Enable testing endpoints offered by OpenAI
|
|
29
|
+
- **litellm**: Enable testing a range of different models through [LiteLLM](https://github.com/BerriAI/litellm)
|
|
30
|
+
|
|
31
|
+
You can install one or more of these extra options using pip:
|
|
32
|
+
|
|
33
|
+
```terminal
|
|
34
|
+
pip install llmeter[plotting, openai, litellm]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## :rocket: Quick-start
|
|
38
|
+
|
|
39
|
+
At a high level, you'll start by configuring an LLMeter "Endpoint" for whatever type of LLM you're connecting to:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# For example with Amazon Bedrock...
|
|
43
|
+
from llmeter.endpoints import BedrockConverse
|
|
44
|
+
endpoint = BedrockConverse(model_id="...")
|
|
45
|
+
|
|
46
|
+
# ...or OpenAI...
|
|
47
|
+
from llmeter.endpoints import OpenAIEndpoint
|
|
48
|
+
endpoint = OpenAIEndpoint(model_id="...", api_key="...")
|
|
49
|
+
|
|
50
|
+
# ...or via LiteLLM...
|
|
51
|
+
from llmeter.endpoints import LiteLLM
|
|
52
|
+
endpoint = LiteLLM("{provider}/{model_id}")
|
|
53
|
+
|
|
54
|
+
# ...and so on
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can then run the high-level "experiments" offered by LLMeter:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
# For example a heatmap of latency by input & output token count:
|
|
61
|
+
from llmeter.experiments import LatencyHeatmap
|
|
62
|
+
latency_heatmap = LatencyHeatmap(
|
|
63
|
+
endpoint=endpoint,
|
|
64
|
+
clients=10,
|
|
65
|
+
source_file="examples/MaryShelleyFrankenstein.txt",
|
|
66
|
+
...
|
|
67
|
+
)
|
|
68
|
+
heatmap_results = await latency_heatmap.run()
|
|
69
|
+
latency_heatmap.plot_heatmap()
|
|
70
|
+
|
|
71
|
+
# ...Or testing how throughput varies with concurrent request count:
|
|
72
|
+
from llmeter.experiments import LoadTest
|
|
73
|
+
sweep_test = LoadTest(
|
|
74
|
+
endpoint=endpoint,
|
|
75
|
+
payload={...},
|
|
76
|
+
sequence_of_clients=[1, 5, 20, 50, 100, 500],
|
|
77
|
+
)
|
|
78
|
+
sweep_results = await sweep_test.run()
|
|
79
|
+
sweep_test.plot_sweep_results()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Alternatively, you can use the low-level `llmeter.runner.Runner` class to run and analyze request
|
|
83
|
+
batches - and build your own custom experiments.
|
|
84
|
+
|
|
85
|
+
For more details, check out our selection of end-to-end code examples in the [examples](examples) folder!
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
## Security
|
|
89
|
+
|
|
90
|
+
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
This project is licensed under the Apache-2.0 License.
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
|
|
3
|
+
from .base import Endpoint, InvocationResponse # noqa: F401
|
|
4
|
+
from .bedrock import BedrockConverse, BedrockConverseStream # noqa: F401
|
|
5
|
+
from .sagemaker import SageMakerEndpoint, SageMakerStreamEndpoint # noqa: F401
|
|
6
|
+
|
|
7
|
+
spec = importlib.util.find_spec("openai")
|
|
8
|
+
if spec:
|
|
9
|
+
from .openai import OpenAIEndpoint, OpenAICompletionEndpoint # noqa: F401
|
|
10
|
+
|
|
11
|
+
spec = importlib.util.find_spec("litellm")
|
|
12
|
+
if spec:
|
|
13
|
+
from .litellm import LiteLLM, LiteLLMStreaming # noqa: F401
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from abc import ABC, abstractmethod
|
|
8
|
+
from dataclasses import asdict, dataclass
|
|
9
|
+
from typing import Dict, TypeVar
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from upath import UPath as Path
|
|
13
|
+
|
|
14
|
+
Self = TypeVar(
|
|
15
|
+
"Self", bound="Endpoint"
|
|
16
|
+
) # for python >= 3.11 can be replaced with direct import of `Self`
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class InvocationResponse:
|
|
21
|
+
"""
|
|
22
|
+
A class representing a invocation result.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
response_text (str): The invocation output.
|
|
26
|
+
id (str): A unique identifier for the invocation.
|
|
27
|
+
time_to_last_token (float): The time taken to generate the response in seconds.
|
|
28
|
+
time_to_first_token (float): The time taken to receive the first token of the response in seconds.
|
|
29
|
+
num_tokens_output (Optional[int]): The number of tokens in the response.
|
|
30
|
+
num_tokens_input (Optional[int]): The number of tokens in the invocation payload.
|
|
31
|
+
input_prompt (str): The input prompt used in the invocation.
|
|
32
|
+
time_per_output_token (float): The average time taken to generate each token in the response.
|
|
33
|
+
error (str): Any error that occurred during invocation.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
response_text: str | None
|
|
37
|
+
id: str | None = None
|
|
38
|
+
input_prompt: str | None = None
|
|
39
|
+
time_to_first_token: float | None = None
|
|
40
|
+
time_to_last_token: float | None = None
|
|
41
|
+
num_tokens_input: int | None = None
|
|
42
|
+
num_tokens_output: int | None = None
|
|
43
|
+
time_per_output_token: float | None = None
|
|
44
|
+
error: str | None = None
|
|
45
|
+
|
|
46
|
+
def to_json(self, **kwargs) -> str:
|
|
47
|
+
return json.dumps(self.__dict__, **kwargs)
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def error_output(
|
|
51
|
+
input_prompt: str | None = None, error=None, id: str | None = None
|
|
52
|
+
):
|
|
53
|
+
return InvocationResponse(
|
|
54
|
+
id=id or uuid4().hex,
|
|
55
|
+
response_text=None,
|
|
56
|
+
input_prompt=input_prompt,
|
|
57
|
+
time_to_last_token=None,
|
|
58
|
+
error="invocation failed" if error is None else str(error),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def __repr__(self):
|
|
62
|
+
return self.to_json(default=str)
|
|
63
|
+
|
|
64
|
+
def __str__(self):
|
|
65
|
+
return self.to_json(indent=4, default=str)
|
|
66
|
+
|
|
67
|
+
def to_dict(self):
|
|
68
|
+
return asdict(self)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class Endpoint(ABC):
|
|
72
|
+
"""
|
|
73
|
+
An abstract base class for endpoint implementations.
|
|
74
|
+
|
|
75
|
+
This class defines the basic structure and interface for all endpoint classes.
|
|
76
|
+
It provides abstract methods that must be implemented by subclasses.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
@abstractmethod
|
|
80
|
+
def __init__(
|
|
81
|
+
self,
|
|
82
|
+
endpoint_name: str,
|
|
83
|
+
model_id: str,
|
|
84
|
+
provider: str,
|
|
85
|
+
):
|
|
86
|
+
"""
|
|
87
|
+
Initialize the BaseEndpoint.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
endpoint_name (str): The name of the endpoint.
|
|
91
|
+
model_id (str): The identifier of the model associated with this endpoint.
|
|
92
|
+
provider (str): The provider of the endpoint.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
None
|
|
96
|
+
"""
|
|
97
|
+
self.endpoint_name = endpoint_name
|
|
98
|
+
self.model_id = model_id
|
|
99
|
+
self.provider = provider
|
|
100
|
+
|
|
101
|
+
@abstractmethod
|
|
102
|
+
def invoke(self, payload: Dict) -> InvocationResponse:
|
|
103
|
+
"""
|
|
104
|
+
Invoke the endpoint with the given payload.
|
|
105
|
+
|
|
106
|
+
This method must be implemented by subclasses to define how the endpoint
|
|
107
|
+
is invoked and how the response is processed.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
payload (Dict): The input payload for the model.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
InvocationResponse: An object containing the model's response and associated metrics.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
NotImplementedError: If the method is not implemented by a subclass.
|
|
117
|
+
"""
|
|
118
|
+
raise NotImplementedError
|
|
119
|
+
|
|
120
|
+
@staticmethod
|
|
121
|
+
def create_payload(*args, **kwargs):
|
|
122
|
+
"""
|
|
123
|
+
Create a payload for the endpoint invocation.
|
|
124
|
+
|
|
125
|
+
This static method should be implemented by subclasses to define
|
|
126
|
+
how the payload is created based on the given arguments.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
*args: Variable length argument list.
|
|
130
|
+
**kwargs: Arbitrary keyword arguments.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
NotImplemented: This method returns NotImplemented in the base class.
|
|
134
|
+
"""
|
|
135
|
+
return NotImplemented
|
|
136
|
+
|
|
137
|
+
@classmethod
|
|
138
|
+
def __subclasshook__(cls, C):
|
|
139
|
+
"""
|
|
140
|
+
Determine if a class is considered a subclass of BaseEndpoint.
|
|
141
|
+
|
|
142
|
+
This method is used to implement a custom subclass check. A class
|
|
143
|
+
is considered a subclass of BaseEndpoint if it has an 'invoke' method.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
C: The class to check.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
bool or NotImplemented: True if the class is a subclass, False if it isn't,
|
|
150
|
+
or NotImplemented if the check is inconclusive.
|
|
151
|
+
"""
|
|
152
|
+
if cls is Endpoint:
|
|
153
|
+
if any("invoke" in B.__dict__ for B in C.__mro__):
|
|
154
|
+
return True
|
|
155
|
+
return NotImplemented
|
|
156
|
+
|
|
157
|
+
def save(self, output_path: os.PathLike) -> os.PathLike:
|
|
158
|
+
"""
|
|
159
|
+
Save the endpoint configuration to a JSON file.
|
|
160
|
+
|
|
161
|
+
This method serializes the endpoint's configuration (excluding private attributes)
|
|
162
|
+
to a JSON file at the specified path.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
output_path (str | UPath): The path where the configuration file will be saved.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
None
|
|
169
|
+
"""
|
|
170
|
+
output_path = Path(output_path)
|
|
171
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
172
|
+
with output_path.open("w") as f:
|
|
173
|
+
endpoint_conf = self.to_dict()
|
|
174
|
+
json.dump(endpoint_conf, f, indent=4, default=str)
|
|
175
|
+
return output_path
|
|
176
|
+
|
|
177
|
+
def to_dict(self) -> Dict:
|
|
178
|
+
"""
|
|
179
|
+
Convert the endpoint configuration to a dictionary.
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
Dict: A dictionary representation of the endpoint configuration.
|
|
183
|
+
"""
|
|
184
|
+
endpoint_conf = {k: v for k, v in vars(self).items() if not k.startswith("_")}
|
|
185
|
+
endpoint_conf["endpoint_type"] = self.__class__.__name__
|
|
186
|
+
return endpoint_conf
|
|
187
|
+
|
|
188
|
+
@classmethod
|
|
189
|
+
def load_from_file(cls, input_path: os.PathLike) -> Self:
|
|
190
|
+
"""
|
|
191
|
+
Load an endpoint configuration from a JSON file.
|
|
192
|
+
|
|
193
|
+
This class method reads a JSON file containing an endpoint configuration,
|
|
194
|
+
determines the appropriate endpoint class, and instantiates it with the
|
|
195
|
+
loaded configuration.
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
input_path (str|UPath): The path to the JSON configuration file.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
Endpoint: An instance of the appropriate endpoint class, initialized
|
|
202
|
+
with the configuration from the file.
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
input_path = Path(input_path)
|
|
206
|
+
with input_path.open("r") as f:
|
|
207
|
+
data = json.load(f)
|
|
208
|
+
endpoint_type = data.pop("endpoint_type")
|
|
209
|
+
endpoint_module = importlib.import_module("llmeter.endpoints")
|
|
210
|
+
endpoint_class = getattr(endpoint_module, endpoint_type)
|
|
211
|
+
return endpoint_class(**data)
|
|
212
|
+
|
|
213
|
+
@classmethod
|
|
214
|
+
def load(cls, endpoint_config: Dict) -> Self: # type: ignore
|
|
215
|
+
"""
|
|
216
|
+
Load an endpoint configuration from a dictionary.
|
|
217
|
+
|
|
218
|
+
This class method reads a dictionary containing an endpoint configuration,
|
|
219
|
+
determines the appropriate endpoint class, and instantiates it with the
|
|
220
|
+
loaded configuration.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
data (Dict): A dictionary containing the endpoint configuration.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
Endpoint: An instance of the appropriate endpoint class, initialized
|
|
227
|
+
with the configuration from the dictionary.
|
|
228
|
+
"""
|
|
229
|
+
endpoint_type = endpoint_config.pop("endpoint_type")
|
|
230
|
+
endpoint_module = importlib.import_module("llmeter.endpoints")
|
|
231
|
+
endpoint_class = getattr(endpoint_module, endpoint_type)
|
|
232
|
+
return endpoint_class(**endpoint_config)
|