-
Notifications
You must be signed in to change notification settings - Fork 395
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
Conversation
mvazhenin seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
withLlmCustomAttributes
api.js
Outdated
@@ -1902,4 +1902,47 @@ API.prototype.ignoreApdex = function ignoreApdex() { | |||
transaction.ignoreApdex = true | |||
} | |||
|
|||
/** | |||
* Runs a function synchronously within a provided LLM custom attributes context and returns its return value. |
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.
Runs a function synchronously within a context and returns its return value. The store is not accessible outside of the callback function. The store is accessible to any asynchronous operations created within the callback.
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.
The store is accessible to any asynchronous operations created within the callback.
@bizob2828 @jsumners-nr I have updated my PR to address most of the feedback. I also got an idea why langchain was failing. Basically it would record events but only when virtual_llm = true and is_response = true events has the same context. The rest would somehow lose the context. Do you have ideas? |
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.
With the latest changes, I am not seeing any test failures. What does a test look like that generates the issue you are seeing?
It is not failing since I am filtering events const responses = events.filter((event) => {
const [, chainEvent] = event
return chainEvent.virtual_llm === true && chainEvent.is_response === true
}) Removing filter would make test to fail |
PR feedback Co-authored-by: James Sumners <jsumners@newrelic.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2437 +/- ##
==========================================
+ Coverage 96.20% 97.15% +0.94%
==========================================
Files 288 289 +1
Lines 45305 45415 +110
==========================================
+ Hits 43586 44123 +537
+ Misses 1719 1292 -427
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
feedback from the last round
Co-authored-by: Bob Evans <robert.evans25@gmail.com>
Co-authored-by: Bob Evans <robert.evans25@gmail.com>
Co-authored-by: Bob Evans <robert.evans25@gmail.com>
…ode-newrelic into setLlmCustomAttributes
withLlmCustomAttributes
withLlmCustomAttributes
to run a function in a LLM context
Hi team. Here is our AIM's humble effort to implement adding LLM custom attributes
Description
During GA for the AIM product, custom attributes on the transaction level are prefixed with
llm.
were set up such that they would be added to all LLM events generated in that transaction from that point onwards, however, there has been demand for a more granular version of this.This would be intended to support use cases such as A/B testing individual parts of flows (for example, the first call out to the LLM in a transaction might go to flow A, and the resulting second call in the transaction might go down flow B based on output from a tool).
Solution highlight
withLlmCustomAttributes() method
setLlmCustomAttributesCallback
can be provided but removed from this PR for now.this.agent._contextManager.runInContext
.transaction._llmContextManager = new AsyncLocalStorage();
.withLlmCustomAttributes
added to the APIs plusrecordEvent
method updated foropenai
,bedrock
andlangchain
instrumentations.How to Test
Related Issues
this.agent._contextManager.setContext({....})
. A solutions could be to go with a dedicated isolated context managertransaction._llmContextManager = new AsyncLocalStorage();
.Problem
Consider we have a flow like below
Solution 1 (currently implemented)
We created an isolated context manager so it would never collide with agent's one.
Solution 2
Alternatively we can refactor agent's context manager to store parent context in a WeakMap
Now we can traverse related contexts to aggregate all llm context attributes attached along the way.