Skip to content

Commit

Permalink
Add AIM docs for Java agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjkeller committed May 17, 2024
1 parent 7e7b1a7 commit 1eeb3d5
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ metaDescription: 'Compatibility and requirements for AI monitoring'
freshnessValidatedDate: never
---

AI monitoring allows agents to recognize and capture AI data. AI monitoring has different library compatibility requirements depending on what language you used for your AI-powered app.
AI monitoring allows agents to recognize and capture AI data. AI monitoring has different library compatibility requirements depending on what language you used for your AI-powered app.

When you disable distributed tracing or enable high security mode, the agent will not capture AI data.

<Callout variant="important">
You should not enable AI monitoring if you're a [FedRAMP customer](/docs/security/security-privacy/compliance/certificates-standards-regulations/fedramp-moderate), because AI and AI-based technologies are not currently FedRAMP authorized.
</Callout>

## Compatible AI libraries [#compatibility]
## Compatible AI libraries [#compatibility]

AI monitoring is compatible with these agent versions and AI libraries:

Expand All @@ -37,6 +37,14 @@ AI monitoring is compatible with these agent versions and AI libraries:
* [AWS SDK for Go v2](https://github.com/aws/aws-sdk-go-v2) versions 1.6.0 and above
</td>
</tr>
<tr>
<td>
[Java version 8.12.0 and above](/docs/apm/agents/java-agent/getting-started/compatibility-requirements-java-agent/#digital-intelligence-platform)
</td>
<td>
* [AWS SDK for Java v2 Bedrock Runtime Client](https://github.com/aws/aws-sdk-java-v2/) versions 2.20.157 and above
</td>
</tr>
<tr>
<td>
[Node.js version 11.13.0 and above](/docs/apm/agents/nodejs-agent/getting-started/compatibility-requirements-nodejs-agent/#digital-intelligence-platform)
Expand All @@ -53,7 +61,7 @@ AI monitoring is compatible with these agent versions and AI libraries:
</td>
<td>
* [OpenAI](https://pypi.org/project/openai/) library versions 0.28.0 and above.

* [Boto3 AWS SDK for Python](https://pypi.org/project/boto3/) versions 1.28.57 and above.
* [LangChain](https://pypi.org/project/langchain/) versions 0.1.0 and above.
</td>
Expand All @@ -73,4 +81,4 @@ AI monitoring is compatible with these agent versions and AI libraries:

* You can get started by [installing AI monitoring](/install/ai-monitoring).
* Explore our AI monitoring UI to see how we can help you [improve your AI-powered app](/docs/ai-monitoring/view-ai-data).
* Learn how to maintain data compliancy by [setting up drop filters](/docs/ai-monitoring/drop-sensitive-data).
* Learn how to maintain data compliancy by [setting up drop filters](/docs/ai-monitoring/drop-sensitive-data).
29 changes: 28 additions & 1 deletion src/content/docs/ai-monitoring/customize-agent-ai-monitoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Update default agent behavior for AI monitoring at these agent configuration doc
* [`ai_monitoring.record_content.enabled`](/docs/apm/agents/go-agent/configuration/go-agent-configuration/#ai-monitoring-record-content)
* [`ConfigCustomInsightsEventsMaxSamplesStored`](/docs/apm/agents/go-agent/configuration/go-agent-configuration/#env-var-table)
</Collapser>
<Collapser
<Collapser
id="dotnet-config"
title=".NET configurations"
>
Expand All @@ -29,6 +29,15 @@ Update default agent behavior for AI monitoring at these agent configuration doc
* [`customEvents.maximumSamplesStored`](/docs/apm/agents/net-agent/configuration/net-agent-configuration/#customevents-maximumSamplesStored)
* [`spanEvents.maximumSamplesStored`](/docs/apm/agents/net-agent/configuration/net-agent-configuration/#span-max-samples-stored)
</Collapser>
<Collapser
id="java-config"
title="Java configurations"
>
* [`ai_monitoring.enabled`](/docs/apm/agents/java-agent/configuration/java-agent-configuration-config-file/#ai-monitoring-enabled)
* [`ai_monitoring.record_content.enabled`](/docs/apm/agents/java-agent/configuration/java-agent-configuration-config-file/#ai-monitoring-record-content)
* [`custom_insights_events.max_samples_stored`](/docs/apm/agents/java-agent/configuration/java-agent-configuration-config-file/#cie-max_samples_stored)
* [`span_events.max_samples_stored`](/docs/apm/agents/java-agent/configuration/java-agent-configuration-config-file/#cfg-span-events-max-samples-stored)
</Collapser>
<Collapser
id="nodejs-config"
title="Node.js configurations"
Expand Down Expand Up @@ -85,6 +94,13 @@ Refer to the docs below for examples to set up counting tokens locally:
>
Refer to our API docs for [`SetLlmTokenCountingCallback`](/docs/apm/agents/net-agent/net-agent-api/setllmtokencountingcallback-net-agent-api)

</Collapser>
<Collapser
id="java-token-method"
title="Java token count method"
>
Refer to our API docs for [`setLlmTokenCountCallback`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/AiMonitoring.html#setLlmTokenCountCallback)

</Collapser>
<Collapser
id="nodejs-token-method"
Expand Down Expand Up @@ -126,6 +142,17 @@ There are two methods you need to implement to forward this kind of information:
* [`GetTraceMetadata`](https://pkg.go.dev/github.com/newrelic/go-agent/v3/newrelic#Application.getTraceMetadata)
* [`RecordLLMFeedbackEvent`](https://pkg.go.dev/github.com/newrelic/go-agent/v3/newrelic#Application.RecordLLMFeedbackEvent)

</Collapser>
<Collapser
id="java-feedback-methods"
title="Java feedback methods"
>

Refer to the Java API docs for:

* [`TraceMetadata.getTraceId()`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/TraceMetadata.html#getTraceId)
* [`recordLlmFeedbackEvent`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/AiMonitoring.html#recordLlmFeedbackEvent)

</Collapser>
<Collapser
id="nodejs-feedback-methods"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
title: AI monitoring APIs
tags:
- Agents
- Java agent
- API guides
metaDescription: 'For information about customizing New Relic''s Java agent for AI monitoring.'
freshnessValidatedDate: never
redirects:
- /docs/apm/agents/java-agent/api-guides/java-ai-monitoring-apis
- /docs/apm/agents/java-agent/api-guides/java-ai-monitoring
---

When you've instrumented your app for AI monitoring, the New Relic Java agent automatically collects many AI metrics, but also provides APIs for collecting information on token count and user feedback.

<Callout variant="tip">
AI monitoring APIs are available in Java agent version 8.12.0 and higher.
</Callout>

## Recording token count [#token-count]
By default, New Relic will attempt to calculate token usage based on the LLM message content or prompt that was reported.

If `ai_monitoring.record_content.enabled=false` is set, or the content could not be captured for other reasons, then it will not be possible for New Relic to calculate token usage. In this scenario, you can set a callback for calculating `token_count` attributes for `LlmEmbedding` and `LlmChatCompletionMessage` events, and then pass that information to New Relic using the [`setLlmTokenCountCallback(LlmTokenCountCallback llmTokenCountCallback)`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/AiMonitoring.html#setLlmTokenCountCallback) API.

This API should be called only once to set a callback for use with all LLM token calculations. If it is called multiple times, each new callback will replace the old one.

The callback will be called with an implementation of `LlmTokenCountCallback` as its input argument and must return an integer representing the number of tokens used for that particular prompt, completion message, or embedding. Values less than or equal to 0 will not be attached to an event.

An implementation of `LlmTokenCountCallback` must override the `calculateLlmTokenCount(String model, String content)` method to calculate a token count based on a given LLM model name and the LLM message content or prompt:
```java
class MyTokenCountCallback implements LlmTokenCountCallback {
@Override
public int calculateLlmTokenCount(String model, String content) {
// Implement custom token calculating logic here based on the LLM model and content.
// Return an integer representing the calculated token count.
return 0;
}
}
```

To register the callback, create an instance of the `LlmTokenCountCallback` implementation and pass it to the `setLlmTokenCountCallback` API:
```java
LlmTokenCountCallback myTokenCountCallback = new MyTokenCountCallback();
NewRelic.getAgent().getAiMonitoring.setLlmTokenCountCallback(myTokenCountCallback);
```

## Recording user feedback [#user-feedback]

AI monitoring can correlate trace IDs between a generated message from your LLM models and the message feedback from an end user using the [`recordLlmFeedbackEvent(Map<String, Object> llmFeedbackEventAttributes)`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/AiMonitoring.html#recordLlmFeedbackEvent) API. This API takes a map as an argument that should be constructed using the `LlmFeedbackEventAttributes.Builder` class, which takes the following parameters:

* `trace_id` (required) - ID of the trace where the chat completion(s) related to the feedback occurred
* `rating` (required) - Rating provided by an end user (ex: 'Good', 'Bad', 1, 2, 5, 8, 10)
* `category` (optional) - Category of the feedback as provided by the end user (ex: “informative”, “inaccurate”)
* `message` (optional) - Freeform text feedback from an end user
* `metadata` (optional) - Set of key-value pairs to store any other desired data to submit with the feedback event

If the user feedback is to be recorded on a different thread, or even in a completely different service, from where the LLM prompt/response occurred, then the trace ID will need to be acquired from the originating thread or service and propagated to where the user feedback event will be recorded. The [`TraceMetadata.getTraceId()`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/TraceMetadata.html#getTraceId) API can be used to acquire the trace ID for the currently executing transaction.
```java
String traceId = NewRelic.getAgent().getTraceMetadata().getTraceId();
```

Below is a complete example of how you could record an LLM feedback event:
```java
String traceId = ... // acquired directly from New Relic API or retrieved from some propagation mechanism

Map<String, String> metadata = new HashMap<>();
metadata.put("interestingKey", "interestingVal");

LlmFeedbackEventAttributes.Builder llmFeedbackEvenAttrBuilder = new LlmFeedbackEventAttributes.Builder(traceId, ratingString);

Map<String, Object> llmFeedbackEventAttributes = llmFeedbackEvenAttrBuilder
.category("General")
.message("Ok")
.metadata(metadata)
.build();

NewRelic.getAgent().getAiMonitoring().recordLlmFeedbackEvent(llmFeedbackEventAttributes);
```

## Adding custom LLM attributes [#custom-llm-attributes]

Any custom attributes added using the [`NewRelic.addCustomParameter(...)`](https://newrelic.github.io/java-agent-api/javadoc/com/newrelic/api/agent/NewRelic.html#addCustomParameter(java.lang.String,boolean)) API that are prefixed with `llm.` will automatically be copied to `LlmEvent`s. For custom attributes added by the `addCustomParameters` API to be added to `LlmEvent`s the API calls must occur before invoking the Bedrock SDK.

One optional custom attribute with special meaning is `llm.conversation_id`, which can be used to group LLM messages into specific conversations in APM.

## LLM event limits [#llm-event-limits]

AI Monitoring instrumentation for supported SDKs will generate the following events to drive the APM experience.

* `LlmEmbedding`: An event that captures data specific to the creation of an embedding.
* `LlmChatCompletionSummary`: An event that captures high-level data about the creation of a chat completion including request, response, and call information.
* `LlmChatCompletionMessage`: An event that corresponds to each message (sent and received) from a chat completion call including those created by the user, assistant, and the system.

These events are sent as custom events and contribute towards the `custom_insights_events.max_samples_stored` limit. Because of this, it is recommended to increase `custom_insights_events.max_samples_stored` to the maximum value of `100,000` to minimize the likelihood of sampling occurring.

```yaml
custom_insights_events:
max_samples_stored: 100000
```
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,79 @@ In addition to overriding configuration settings, the agent recognizes these sys
</Collapser>
</CollapserGroup>

## AI monitoring [#ai-monitoring]

This section details the Java agent configuration options for [AI monitoring](/docs/ai-monitoring/intro-to-ai-monitoring). This feature can be configured in the `ai_monitoring` yaml config file stanza, with `newrelic.config.ai_monitoring.` prefixed system properties, or with `NEW_RELIC_AI_MONITORING_` prefixed environment variables.

<Callout variant="important">
If distributed tracing is disabled or high security mode is enabled, AI monitoring will not collect AI data.
</Callout>

<CollapserGroup>
<Collapser
id="ai-monitoring-enabled"
title="enabled"
>
<table>
<tbody>
<tr>
<th>
Type
</th>

<td>
Boolean
</td>
</tr>

<tr>
<th>
Default
</th>

<td>
`false`
</td>
</tr>
</tbody>
</table>

When set to `true`, enables AI monitoring.
</Collapser>
<Collapser
id="ai-monitoring-record-content"
title="record_content.enabled"
>

<table>
<tbody>
<tr>
<th>
Type
</th>

<td>
Boolean
</td>
</tr>

<tr>
<th>
Default
</th>

<td>
`true`
</td>
</tr>
</tbody>
</table>

If set to `false`, agent will omit input and output content (like text strings from prompts and responses) captured in LLM events. This is an optional security setting if you don’t want to record sensitive data sent to and received from your LLMs.

</Collapser>
</CollapserGroup>

## Attributes [#attributes]

To set these options, use the `attributes` section. To [override](#System_Properties) them, use a `newrelic.config.attributes` prefixed system property.
Expand Down Expand Up @@ -2026,7 +2099,7 @@ common: &default_settings​
</tbody>
</table>

Customize the precentage of free heap memory below which the circuit breaker should trip. When the percentage of free heap memory is less than `memory_threshold`, and the CPU time spent doing garbage collection is greater than `gc_cpu_threshold`, the circuit breaker trips. In order to make the circuit breaker less likely to trip, decrease `memory_threshold` and/or increase `gc_cpu_threshold`. Adjust these values as needed, based on your application's operating performance and behavior.
Customize the percentage of free heap memory below which the circuit breaker should trip. When the percentage of free heap memory is less than `memory_threshold`, and the CPU time spent doing garbage collection is greater than `gc_cpu_threshold`, the circuit breaker trips. In order to make the circuit breaker less likely to trip, decrease `memory_threshold` and/or increase `gc_cpu_threshold`. Adjust these values as needed, based on your application's operating performance and behavior.
</Collapser>

<Collapser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,24 @@ The Java agent integrates with other New Relic products to give you end-to-end v
</th>

<th>
Integration
Capability
</th>
</tr>
</thead>

<tbody>
<tr>
<td>
[AI monitoring](/docs/ai-monitoring/intro-to-ai-monitoring)
</td>

<td>
If you have version 8.12.0 or higher of Java agent, you can collect AI data from certain AI libraries and frameworks:

* [AWS SDK for Java v2 Bedrock Runtime Client](https://github.com/aws/aws-sdk-java-v2/) versions 2.20.157 and above
</td>
</tr>

<tr>
<td>
[Browser monitoring](/docs/browser/new-relic-browser/getting-started/introduction-new-relic-browser)
Expand Down

0 comments on commit 1eeb3d5

Please sign in to comment.