|
1 | 1 | /* |
2 | | - * Copyright 2025 New Relic Corporation. All rights reserved. |
| 2 | + * Copyright 2026 New Relic Corporation. All rights reserved. |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | 'use strict' |
7 | | -const LlmEvent = require('./event') |
8 | 7 |
|
9 | | -module.exports = class LlmChatCompletionSummary extends LlmEvent { |
10 | | - constructor({ agent, segment, request = {}, response = {}, withError = false, transaction }) { |
11 | | - super({ agent, segment, request, response, responseAttrs: true, transaction }) |
12 | | - this.error = withError |
| 8 | +const LlmChatCompletionSummary = require('../chat-completion-summary') |
| 9 | +const getUsageTokens = require('./get-usage-tokens') |
| 10 | + |
| 11 | +/** |
| 12 | + * Encapsulates a Google Gen AI LlmChatCompletionSummary. |
| 13 | + */ |
| 14 | +module.exports = class GoogleGenAiLlmChatCompletionSummary extends LlmChatCompletionSummary { |
| 15 | + /** |
| 16 | + * |
| 17 | + * @param {object} params Constructor parameters |
| 18 | + * @param {Agent} params.agent New Relic agent instance |
| 19 | + * @param {TraceSegment} params.segment Current segment |
| 20 | + * @param {Transaction} params.transaction Current and active transaction |
| 21 | + * @param {object} params.request Google Gen AI request object |
| 22 | + * @param {object} params.response Google Gen AI response object |
| 23 | + * @param {boolean} [params.error] Set to `true` if an error occurred |
| 24 | + */ |
| 25 | + constructor({ agent, segment, transaction, request, response, error }) { |
| 26 | + super({ agent, |
| 27 | + segment, |
| 28 | + transaction, |
| 29 | + responseModel: response?.modelVersion, |
| 30 | + requestModel: request?.model, |
| 31 | + finishReason: response?.candidates?.[0]?.finishReason, |
| 32 | + maxTokens: request.config?.maxOutputTokens, |
| 33 | + temperature: request.config?.temperature, |
| 34 | + vendor: 'gemini', |
| 35 | + error }) |
| 36 | + |
13 | 37 | let requestMessagesLength = 0 |
14 | 38 | if (Array.isArray(request?.contents)) { |
15 | 39 | requestMessagesLength = request.contents.length |
16 | 40 | } else if (typeof request?.contents === 'string') { |
17 | 41 | requestMessagesLength = 1 |
18 | 42 | } |
19 | 43 | this['response.number_of_messages'] = requestMessagesLength + (response?.candidates?.length || 0) |
20 | | - this['response.choices.finish_reason'] = response?.candidates?.[0]?.finishReason |
21 | | - this['request.max_tokens'] = request.config?.maxOutputTokens |
22 | | - this['request.temperature'] = request.config?.temperature |
23 | 44 |
|
24 | | - this.timestamp = segment.timer.start |
25 | 45 | this.setTokens(agent, request, response) |
26 | 46 | } |
27 | 47 |
|
@@ -51,7 +71,7 @@ module.exports = class LlmChatCompletionSummary extends LlmEvent { |
51 | 71 | return |
52 | 72 | } |
53 | 73 |
|
54 | | - const tokens = this.getUsageTokens(response) |
| 74 | + const tokens = getUsageTokens(response) |
55 | 75 | this.setTokensInResponse(tokens) |
56 | 76 | } |
57 | 77 | } |
0 commit comments