-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1837 from newrelic/aws-bedrock-llm-support
Aws bedrock llm support
- Loading branch information
Showing
68 changed files
with
8,071 additions
and
7 deletions.
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
21 changes: 21 additions & 0 deletions
21
agent-bridge/src/main/java/com/newrelic/agent/bridge/NoOpAiMonitoring.java
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.newrelic.agent.bridge; | ||
|
||
import com.newrelic.api.agent.AiMonitoring; | ||
import com.newrelic.api.agent.LlmTokenCountCallback; | ||
|
||
import java.util.Map; | ||
|
||
public class NoOpAiMonitoring implements AiMonitoring { | ||
|
||
static final AiMonitoring INSTANCE = new NoOpAiMonitoring(); | ||
|
||
private NoOpAiMonitoring() {} | ||
|
||
@Override | ||
public void recordLlmFeedbackEvent(Map<String, Object> llmFeedbackEventAttributes) { | ||
} | ||
|
||
@Override | ||
public void setLlmTokenCountCallback(LlmTokenCountCallback llmTokenCountCallback) { | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
agent-bridge/src/main/java/com/newrelic/agent/bridge/aimonitoring/AiMonitoringUtils.java
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.newrelic.agent.bridge.aimonitoring; | ||
|
||
import com.newrelic.api.agent.Config; | ||
import com.newrelic.api.agent.NewRelic; | ||
|
||
import java.util.logging.Level; | ||
|
||
public class AiMonitoringUtils { | ||
// Enabled defaults | ||
private static final boolean AI_MONITORING_ENABLED_DEFAULT = false; | ||
private static final boolean AI_MONITORING_STREAMING_ENABLED_DEFAULT = true; | ||
private static final boolean AI_MONITORING_RECORD_CONTENT_ENABLED_DEFAULT = true; | ||
private static final boolean HIGH_SECURITY_ENABLED_DEFAULT = false; | ||
|
||
/** | ||
* Check if ai_monitoring features are enabled. | ||
* Indicates whether LLM instrumentation will be registered. If this is set to False, no metrics, events, or spans are to be sent. | ||
* | ||
* @return true if AI monitoring is enabled, else false | ||
*/ | ||
public static boolean isAiMonitoringEnabled() { | ||
Config config = NewRelic.getAgent().getConfig(); | ||
Boolean aimEnabled = config.getValue("ai_monitoring.enabled", AI_MONITORING_ENABLED_DEFAULT); | ||
Boolean highSecurity = config.getValue("high_security", HIGH_SECURITY_ENABLED_DEFAULT); | ||
|
||
if (highSecurity || !aimEnabled) { | ||
aimEnabled = false; | ||
String disabledReason = highSecurity ? "High Security Mode." : "agent config."; | ||
NewRelic.getAgent().getLogger().log(Level.FINE, "AIM: AI Monitoring is disabled due to " + disabledReason); | ||
NewRelic.incrementCounter("Supportability/Java/ML/Disabled"); | ||
} else { | ||
NewRelic.incrementCounter("Supportability/Java/ML/Enabled"); | ||
} | ||
|
||
return aimEnabled; | ||
} | ||
|
||
/** | ||
* Check if ai_monitoring.streaming features are enabled. | ||
* | ||
* @return true if streaming is enabled, else false | ||
*/ | ||
public static boolean isAiMonitoringStreamingEnabled() { | ||
Boolean enabled = NewRelic.getAgent().getConfig().getValue("ai_monitoring.streaming.enabled", AI_MONITORING_STREAMING_ENABLED_DEFAULT); | ||
|
||
if (enabled) { | ||
NewRelic.incrementCounter("Supportability/Java/ML/Streaming/Enabled"); | ||
} else { | ||
NewRelic.incrementCounter("Supportability/Java/ML/Streaming/Disabled"); | ||
} | ||
|
||
return enabled; | ||
} | ||
|
||
/** | ||
* Check if the input and output content should be added to LLM events. | ||
* | ||
* @return true if adding content is enabled, else false | ||
*/ | ||
public static boolean isAiMonitoringRecordContentEnabled() { | ||
Boolean enabled = NewRelic.getAgent().getConfig().getValue("ai_monitoring.record_content.enabled", AI_MONITORING_RECORD_CONTENT_ENABLED_DEFAULT); | ||
|
||
if (enabled) { | ||
NewRelic.incrementCounter("Supportability/Java/ML/RecordContent/Enabled"); | ||
} else { | ||
NewRelic.incrementCounter("Supportability/Java/ML/RecordContent/Disabled"); | ||
} | ||
|
||
return enabled; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...dge/src/main/java/com/newrelic/agent/bridge/aimonitoring/LlmTokenCountCallbackHolder.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.newrelic.agent.bridge.aimonitoring; | ||
|
||
import com.newrelic.api.agent.LlmTokenCountCallback; | ||
|
||
/** | ||
* A thread-safe holder for an instance of {@link LlmTokenCountCallback}. | ||
* This class provides methods for setting and retrieving the callback instance. | ||
*/ | ||
public class LlmTokenCountCallbackHolder { | ||
|
||
private static volatile LlmTokenCountCallback llmTokenCountCallback = null; | ||
|
||
/** | ||
* Sets the {@link LlmTokenCountCallback} instance to be stored. | ||
* | ||
* @param newLlmTokenCountCallback the callback instance | ||
*/ | ||
public static void setLlmTokenCountCallback(LlmTokenCountCallback newLlmTokenCountCallback) { | ||
llmTokenCountCallback = newLlmTokenCountCallback; | ||
} | ||
|
||
/** | ||
* Retrieves the stored {@link LlmTokenCountCallback} instance. | ||
* | ||
* @return stored callback instance | ||
*/ | ||
public static LlmTokenCountCallback getLlmTokenCountCallback() { | ||
return llmTokenCountCallback; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
agent-model/src/main/java/com/newrelic/agent/model/LlmCustomInsightsEvent.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.newrelic.agent.model; | ||
|
||
/** | ||
* Represents an internal subtype of a CustomInsightsEvent that is sent to the | ||
* custom_event_data collector endpoint but potentially subject to different | ||
* validation rules and agent configuration. | ||
*/ | ||
public class LlmCustomInsightsEvent { | ||
// LLM event types | ||
private static final String LLM_EMBEDDING = "LlmEmbedding"; | ||
private static final String LLM_CHAT_COMPLETION_SUMMARY = "LlmChatCompletionSummary"; | ||
private static final String LLM_CHAT_COMPLETION_MESSAGE = "LlmChatCompletionMessage"; | ||
|
||
/** | ||
* Determines if a CustomInsightsEvent should be treated as a LlmEvent | ||
* | ||
* @param eventType type of the current event | ||
* @return true if eventType is an LlmEvent, else false | ||
*/ | ||
public static boolean isLlmEvent(String eventType) { | ||
return eventType.equals(LLM_EMBEDDING) || eventType.equals(LLM_CHAT_COMPLETION_MESSAGE) || eventType.equals(LLM_CHAT_COMPLETION_SUMMARY); | ||
} | ||
} |
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.