Bedrock invoke
bedrock_invoke
BedrockInvoke
BedrockInvoke(model_id, endpoint_name=None, region=None, bedrock_boto3_client=None, max_attempts=3, generated_text_jmespath='choices[0].message.content', generated_token_count_jmespath='usage.completion_tokens', input_text_jmespath='messages[].content[].text', input_token_count_jmespath='usage.prompt_tokens')
Bases: BedrockInvokeBase[InvokeModelResponseTypeDef]
LLMeter Endpoint for Amazon Bedrock InvokeModel API (non-streaming)
The default ..._jmespath parameters assume your target model uses an OpenAI ChatCompletions-like API, which is true for many (but not all) Bedrock models. You'll need to override these if targeting a model with different request/response format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
The identifier for the model to use |
required |
endpoint_name
|
str | None
|
Name of the endpoint. Defaults to None. |
None
|
region
|
str | None
|
AWS region to use. Defaults to bedrock_boto3_client's, or configured from AWS CLI. |
None
|
bedrock_boto3_client
|
Any
|
Optional pre-configured boto3 client, otherwise one will be created. |
None
|
max_attempts
|
int
|
Maximum number of retry attempts. Defaults to 3. |
3
|
generated_text_jmespath
|
str
|
JMESPath query to extract generated text from model response. |
'choices[0].message.content'
|
generated_token_count_jmespath
|
str | None
|
JMESPath query to extract generated token count from model response. |
'usage.completion_tokens'
|
input_text_jmespath
|
str
|
JMESPath query to extract input text from the model request payload. |
'messages[].content[].text'
|
input_token_count_jmespath
|
str | None
|
JMESPath query to extract input token count from the response. |
'usage.prompt_tokens'
|
Source code in llmeter/endpoints/bedrock_invoke.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
invoke
invoke(payload)
Invoke the Bedrock InvokeModel API with the given payload.
Source code in llmeter/endpoints/bedrock_invoke.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
process_raw_response
process_raw_response(raw_response, start_t, response)
Parse the response from a Bedrock InvokeModel API call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_response
|
Raw response from the Bedrock API. |
required | |
start_t
|
float
|
The timestamp when the request was initiated. |
required |
response
|
InvocationResponse
|
LLMeter InvocationResponse object on which results will be saved (in-place) |
required |
Source code in llmeter/endpoints/bedrock_invoke.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
BedrockInvokeBase
BedrockInvokeBase(model_id, generated_text_jmespath, input_text_jmespath, generated_token_count_jmespath=None, input_token_count_jmespath=None, endpoint_name=None, region=None, bedrock_boto3_client=None, max_attempts=3)
Bases: Endpoint[TBedrockInvokeResponse], Generic[TBedrockInvokeResponse]
Source code in llmeter/endpoints/bedrock_invoke.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
create_payload
staticmethod
create_payload(user_message, max_tokens=256, **kwargs)
Create a payload, assuming your target Bedrock model supports ChatCompletions-like API
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_message
|
str | list[str]
|
The user's message or a sequence of messages. |
required |
max_tokens
|
int | None
|
The maximum number of tokens to generate. Defaults to 256. |
256
|
**kwargs
|
Any
|
Additional keyword arguments to include in the payload. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The formatted payload for the Bedrock API request. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If user_message is not a string or list of strings |
ValueError
|
If max_tokens is not a positive integer |
Source code in llmeter/endpoints/bedrock_invoke.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
BedrockInvokeStream
BedrockInvokeStream(model_id, endpoint_name=None, region=None, bedrock_boto3_client=None, max_attempts=3, generated_text_jmespath='choices[0].delta.content', generated_token_count_jmespath='"amazon-bedrock-invocationMetrics".outputTokenCount', input_text_jmespath='messages[].content[].text', input_token_count_jmespath='"amazon-bedrock-invocationMetrics".inputTokenCount')
Bases: BedrockInvokeBase[InvokeModelWithResponseStreamResponseTypeDef]
LLMeter Endpoint for Amazon Bedrock InvokeModelWithResponseStream API
The default ..._jmespath parameters assume your target model uses an OpenAI ChatCompletions-like streaming API, which is true for many (but not all) Bedrock models. You'll need to override these if targeting a model with different request/response format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
The identifier for the model to use |
required |
endpoint_name
|
str | None
|
Name of the endpoint. Defaults to None. |
None
|
region
|
str | None
|
AWS region to use. Defaults to bedrock_boto3_client's, or configured from AWS CLI. |
None
|
bedrock_boto3_client
|
Any
|
Optional pre-configured boto3 client, otherwise one will be created. |
None
|
max_attempts
|
int
|
Maximum number of retry attempts. Defaults to 3. |
3
|
generated_text_jmespath
|
str
|
JMESPath query to extract incremental text from a chunk of the model response. |
'choices[0].delta.content'
|
generated_token_count_jmespath
|
str | None
|
JMESPath query to extract generated token count from a chunk of model response. |
'"amazon-bedrock-invocationMetrics".outputTokenCount'
|
input_text_jmespath
|
str
|
JMESPath query to extract input text from the model request payload. |
'messages[].content[].text'
|
input_token_count_jmespath
|
str | None
|
JMESPath query to extract input token count from a chunk of the model response. |
'"amazon-bedrock-invocationMetrics".inputTokenCount'
|
Source code in llmeter/endpoints/bedrock_invoke.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
process_raw_response
process_raw_response(raw_response, start_t, response)
Parse the streaming response from Bedrock InvokeModelWithResponseStream API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_response
|
The raw response from the Bedrock API. |
required | |
start_t
|
float
|
The timestamp when the request was initiated. |
required |
Returns:
| Type | Description |
|---|---|
None
|
InvocationResponse with the generated text and metadata. |
Source code in llmeter/endpoints/bedrock_invoke.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |