-
Notifications
You must be signed in to change notification settings - Fork 419
refactor: Google Gen AI LLM event refactor #3748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amychisholm03
merged 2 commits into
newrelic:main
from
amychisholm03:google-genai-llm-refactor
Feb 17, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,34 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| const LlmEvent = require('./event') | ||
| const LlmEmbedding = require('../embedding') | ||
|
|
||
| class LlmEmbedding extends LlmEvent { | ||
| constructor({ agent, segment, request = {}, response = {}, withError = false, transaction }) { | ||
| super({ agent, segment, request, response, responseAttrs: true, transaction }) | ||
| this.error = withError | ||
|
|
||
| if (agent.config.ai_monitoring.record_content.enabled === true) { | ||
| this.input = request.contents?.toString() | ||
| } | ||
| /** | ||
| * Encapsulates a Google Gen AI LlmEmbedding. | ||
| */ | ||
| module.exports = class GoogleGenAiLlmEmbedding extends LlmEmbedding { | ||
| /** | ||
| * | ||
| * @param {object} params Constructor params | ||
| * @param {Agent} params.agent New Relic agent instance | ||
| * @param {TraceSegment} params.segment Current segment | ||
| * @param {Transaction} params.transaction Current and active transaction | ||
| * @param {object} params.request Google Gen AI request object | ||
| * @param {object} params.response Google Gen AI response object | ||
| * @param {boolean} [params.error] Set to true if an error occurred | ||
| */ | ||
| constructor({ agent, segment, transaction, request = {}, response = {}, error }) { | ||
| super({ agent, | ||
| segment, | ||
| transaction, | ||
| requestInput: request?.contents, | ||
| requestModel: request?.model, | ||
| responseModel: response?.modelVersion, | ||
| vendor: 'gemini', | ||
| error }) | ||
| } | ||
| } | ||
|
|
||
| module.exports = LlmEmbedding |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| module.exports = getUsageTokens | ||
|
|
||
| /** | ||
| * Grabs the prompt, completion, and total token count from the | ||
| * given response object. | ||
| * @param {object} response Google Gen AI response object | ||
| * @returns {object} { promptTokens, completionTokens, totalTokens } | ||
| */ | ||
| function getUsageTokens(response) { | ||
| const { usageMetadata } = response | ||
| if (!usageMetadata) { | ||
| return { promptTokens: 0, completionTokens: 0, totalTokens: 0 } | ||
| } | ||
| return { | ||
| promptTokens: Number(usageMetadata.promptTokenCount), | ||
| completionTokens: Number(usageMetadata.candidatesTokenCount), | ||
| totalTokens: Number(usageMetadata.totalTokenCount) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,10 @@ | ||
| /* | ||
| * Copyright 2025 New Relic Corporation. All rights reserved. | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| const LlmChatCompletionSummary = require('./chat-completion-summary') | ||
| const LlmChatCompletionMessage = require('./chat-completion-message') | ||
| const LlmEmbedding = require('./embedding') | ||
| const LlmErrorMessage = require('../error-message') | ||
|
|
||
| module.exports = { | ||
| LlmChatCompletionMessage, | ||
| LlmChatCompletionSummary, | ||
| LlmEmbedding, | ||
| LlmErrorMessage | ||
| LlmChatCompletionMessage: require('./chat-completion-message'), | ||
| LlmChatCompletionSummary: require('./chat-completion-summary'), | ||
| LlmEmbedding: require('./embedding') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,15 +27,38 @@ class GoogleGenAIGenerateContentSubscriber extends AiMonitoringChatSubscriber { | |
| }) | ||
| } | ||
|
|
||
| getMessages({ request, response }) { | ||
| // Only take the first response message and append to input messages | ||
| /** | ||
| * Gets the request/input and response messages from the | ||
| * Google Gen AI request and response objects. | ||
| * @param {object} params function parameters | ||
| * @param {object} params.request Google Gen AI request object | ||
| * @param {object} params.response Google Gen AI response object | ||
| * @returns {object[]} an array of messages like { content, role } | ||
| */ | ||
| getMessages({ request, response = {} }) { | ||
| // request.contents can be a string or an array of strings | ||
| // response.candidates is an array of candidates (choices); we only take the first one | ||
| const inputMessages = Array.isArray(request.contents) ? request.contents : [request.contents] | ||
| const responseMessage = response?.candidates?.[0]?.content | ||
| return responseMessage !== undefined ? [...inputMessages, responseMessage] : inputMessages | ||
| const contents = Array.isArray(request.contents) ? request.contents : [request.contents] | ||
| const messages = contents.map((item) => { | ||
| return { content: item, role: 'user' } | ||
| }) | ||
| const responseContent = response.text ?? response.candidates?.[0]?.content?.parts?.[0]?.text | ||
| if (responseContent) { | ||
| // Do not push an empty response (likely from an error) | ||
| messages.push({ content: responseContent, role: 'assistant' }) | ||
| } | ||
| return messages | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Google Gen AI LlmChatCompletionSummary instance. | ||
| * | ||
| * @param {object} params to function | ||
| * @param {Context} params.ctx active context | ||
| * @param {object} params.request request made to method | ||
| * @param {object} params.response response from method | ||
| * @param {object} [params.err] error object if present | ||
| * @returns {object} a llm completion summary instance for Google Gen AI | ||
| */ | ||
| createCompletionSummary({ ctx, request, response = {}, err }) { | ||
| const { transaction, segment } = ctx | ||
| return new LlmChatCompletionSummary({ | ||
|
|
@@ -44,21 +67,26 @@ class GoogleGenAIGenerateContentSubscriber extends AiMonitoringChatSubscriber { | |
| transaction, | ||
| request, | ||
| response, | ||
| withError: !!err | ||
| error: !!err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }) | ||
| } | ||
|
|
||
| createCompletionMessage({ ctx, request, response, index, completionId, message }) { | ||
| const { segment, transaction } = ctx | ||
|
|
||
| const isResponse = message?.content === response?.text | ||
|
|
||
| return new LlmChatCompletionMessage({ | ||
| agent: this.agent, | ||
| segment, | ||
| transaction, | ||
| request, | ||
| response, | ||
| index, | ||
| sequence: index, | ||
| completionId, | ||
| message | ||
| content: message.content, | ||
| role: message.role, | ||
| isResponse | ||
| }) | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err is an object, updated doc block to reflect