-
Notifications
You must be signed in to change notification settings - Fork 409
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
feat: Added new API method withLlmCustomAttributes
to run a function in a LLM context
#2437
Merged
Merged
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
2361240
feat: Set LLM events custom attributes
MikeVaz 4dd3be8
fix: Debug statements
MikeVaz 55da778
fix: Example
MikeVaz 720be07
fix: Add guards
MikeVaz 878d6b5
fix: Add unit test
MikeVaz de8b498
feat: WithLlmCustomAttributes
MikeVaz dc0261a
feat: Merge parent context into children
RyanKadri 07cbcab
Merge branch 'newrelic:main' into setLlmCustomAttributes
MikeVaz 08927ec
feat: Option 4
MikeVaz 8b0cd84
fix: Unnecessary test
MikeVaz 7ab7bcd
fix: Test name
MikeVaz 0b2f2d6
fix: Integration tests
MikeVaz f75dabf
fix: Remove extra npm scripts
MikeVaz 19a9e80
fix: Remove extra npm scripts
MikeVaz 24a13bd
fix: PR feedback
MikeVaz af4aafa
fix: PR feedback
MikeVaz d428aaa
fix: PR feedback
MikeVaz d2264ad
fix: Unit test and pr feedback
MikeVaz 807f0cf
Merge branch 'newrelic:main' into setLlmCustomAttributes
MikeVaz ef488da
fix: PR feedback
MikeVaz bcc9e15
fix: Typo
MikeVaz cef4289
fix: PR feedback
MikeVaz 87588e3
fix: PR feedback
MikeVaz d0101f6
fix: Unit test
MikeVaz dbd7282
Merge branch 'newrelic:main' into setLlmCustomAttributes
MikeVaz e7531d7
fix: Apply solution 1
MikeVaz 308bdeb
Update lib/util/llm-utils.js
MikeVaz a32f932
fix: PR feedback
MikeVaz ba516b0
Merge branch 'newrelic:main' into setLlmCustomAttributes
MikeVaz 6e629f9
fix: PR feedback
MikeVaz a8cbb8a
Merge branch 'newrelic:main' into setLlmCustomAttributes
MikeVaz 6d3bd2d
fix: PR feedback
MikeVaz 1f2d240
Update lib/instrumentation/openai.js
MikeVaz 8cc326f
Update lib/instrumentation/langchain/common.js
MikeVaz cab2f74
Update lib/instrumentation/aws-sdk/v3/bedrock.js
MikeVaz 67a35de
fix: Improve test coverage
MikeVaz d8353ff
Merge branch 'setLlmCustomAttributes' of https://github.com/MikeVaz/n…
MikeVaz cb423d6
fix: More unit test and PR feedback
MikeVaz 21e4b5f
chore: Addressed code review feedback
bizob2828 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 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 |
---|---|---|
|
@@ -1548,7 +1548,7 @@ | |
* @param {string} params.traceId Identifier for the feedback event. | ||
* Obtained from {@link getTraceMetadata}. | ||
* @param {string} params.category A tag for the event. | ||
* @param {string} params.rating A indicator of how useful the message was. | ||
* @param {string} [params.message] The message that triggered the event. | ||
* @param {object} [params.metadata] Additional key-value pairs to associate | ||
* with the recorded event. | ||
|
@@ -1902,4 +1902,47 @@ | |
transaction.ignoreApdex = true | ||
} | ||
|
||
/** | ||
Check warning on line 1905 in api.js
|
||
* Runs a function synchronously within a provided LLM custom attributes context and returns its return value. | ||
* | ||
* An example of setting a custom attribute: | ||
* | ||
* newrelic.withLlmCustomAttributes({'llm.someAttribute': 'someVallue'}, () => { | ||
MikeVaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* return; | ||
* }) | ||
* @param {Object} context LLM custom attributes context | ||
* @param {Function} callback synchronous function called within the context | ||
bizob2828 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
API.prototype.withLlmCustomAttributes = function withLlmCustomAttributes(context, callback) { | ||
const metric = this.agent.metrics.getOrCreateMetric( | ||
NAMES.SUPPORTABILITY.API + '/withLlmCustomAttributes' | ||
) | ||
metric.incrementCallCount() | ||
|
||
const transaction = this.agent.tracer.getTransaction() | ||
if (!transaction) { | ||
logger.warn('withLlmCustomAttributes must be called within the scope of a transaction.') | ||
return callback?.() | ||
bizob2828 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
for (const key in context) { | ||
if (Object.hasOwn(context, key)) { | ||
const value = context[key] | ||
if (typeof value === 'object' || typeof value === 'function') { | ||
jsumners-nr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.warn(`Invalid attribute type for ${key}. Skipped.`) | ||
delete context[key] | ||
} else if (key.indexOf('llm.') !== 0) { | ||
logger.warn(`Invalid attribute name ${key}. Renamed to "llm.${key}".`) | ||
delete context[key] | ||
context[`llm.${key}`] = value | ||
} | ||
} | ||
} | ||
jsumners-nr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const parentContext = this.agent._contextManager.getContext() | ||
|
||
const fullContext = parentContext ? Object.assign(parentContext, context || {}) : context | ||
return this.agent._contextManager.runInContext(fullContext, callback, this) | ||
} | ||
|
||
module.exports = API |
This file contains 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 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 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 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 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 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 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 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
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.
I don't think it has to be synchronously. The context manager should properly bind all functions run within the context
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.
I went with how they describe it in the nodejs docs.
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.