-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtracing.js
63 lines (49 loc) · 2.27 KB
/
tracing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
'use strict'
const BaseAwsSdkPlugin = require('../../base')
const { parseModelId, extractRequestParams, extractTextAndResponseReason } = require('./utils')
const enabledOperations = ['invokeModel']
class BedrockRuntime extends BaseAwsSdkPlugin {
static get id () { return 'bedrockruntime' }
isEnabled (request) {
const operation = request.operation
if (!enabledOperations.includes(operation)) {
return false
}
return super.isEnabled(request)
}
generateTags (params, operation, response) {
const { modelProvider, modelName } = parseModelId(params.modelId)
const requestParams = extractRequestParams(params, modelProvider)
const textAndResponseReason = extractTextAndResponseReason(response, modelProvider, modelName)
const tags = buildTagsFromParams(requestParams, textAndResponseReason, modelProvider, modelName, operation)
return tags
}
}
function buildTagsFromParams (requestParams, textAndResponseReason, modelProvider, modelName, operation) {
const tags = {}
// add request tags
tags['resource.name'] = operation
tags['aws.bedrock.request.model'] = modelName
tags['aws.bedrock.request.model_provider'] = modelProvider.toLowerCase()
tags['aws.bedrock.request.prompt'] = requestParams.prompt
tags['aws.bedrock.request.temperature'] = requestParams.temperature
tags['aws.bedrock.request.top_p'] = requestParams.topP
tags['aws.bedrock.request.top_k'] = requestParams.topK
tags['aws.bedrock.request.max_tokens'] = requestParams.maxTokens
tags['aws.bedrock.request.stop_sequences'] = requestParams.stopSequences
tags['aws.bedrock.request.input_type'] = requestParams.inputType
tags['aws.bedrock.request.truncate'] = requestParams.truncate
tags['aws.bedrock.request.stream'] = requestParams.stream
tags['aws.bedrock.request.n'] = requestParams.n
// add response tags
if (modelName.includes('embed')) {
tags['aws.bedrock.response.embedding_length'] = textAndResponseReason.message.length
}
if (textAndResponseReason.choiceId) {
tags['aws.bedrock.response.choices.id'] = textAndResponseReason.choiceId
}
tags['aws.bedrock.response.choices.text'] = textAndResponseReason.message
tags['aws.bedrock.response.choices.finish_reason'] = textAndResponseReason.finishReason
return tags
}
module.exports = BedrockRuntime