|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using NewRelic.Agent.Api; |
| 7 | +using NewRelic.Agent.Extensions.Llm; |
| 8 | +using NUnit.Framework; |
| 9 | +using Telerik.JustMock; |
| 10 | + |
| 11 | +namespace Agent.Extensions.Tests.Llm |
| 12 | +{ |
| 13 | + // When creating tests, make sure to not use duplicate model name since CreateModelIdSupportabilityMetricsForXXXX only creates the metric once. |
| 14 | + [TestFixture] |
| 15 | + public class SupportabilityHelpersTests |
| 16 | + { |
| 17 | + private IAgent _agent; |
| 18 | + |
| 19 | + [SetUp] |
| 20 | + public void Setup() |
| 21 | + { |
| 22 | + _agent = Mock.Create<IAgent>(); |
| 23 | + } |
| 24 | + |
| 25 | + [TestCase("anthropic.claude-3-sonnet-20240229-v1:0", "anthropic", "claude-3")] |
| 26 | + [TestCase("us.anthropic.claude-3-sonnet-20240229-v1:0", "anthropic", "claude-3")] |
| 27 | + [TestCase("apac.anthropic.claude-3-sonnet-20240229-v1:0", "anthropic", "claude-3")] |
| 28 | + [TestCase("meta.llama3-2-3b-instruct-v1:0", "meta", "llama3")] |
| 29 | + [TestCase("us.meta.llama3-2-3b-instruct-v1:0", "meta", "llama3")] |
| 30 | + [TestCase("amazon.nova-lite-v1:0", "amazon", "nova-lite")] |
| 31 | + [TestCase("eu.amazon.nova-lite-v1:0", "amazon", "nova-lite")] |
| 32 | + [TestCase("amazon.titan-embed-text-v1", "amazon", "titan-embed")] |
| 33 | + [TestCase("us.amazon.titan-embed-text-v1", "amazon", "titan-embed")] |
| 34 | + [TestCase("ai21.jamba-1-5-large-v1:0", "ai21", "jamba")] |
| 35 | + [TestCase("apac.ai21.jamba-1-5-large-v1:0", "ai21", "jamba")] |
| 36 | + [TestCase("writer-palmyra-med-70b-32k", "writer", "palmyra")] |
| 37 | + public void Bedrock_ModelFormatsTests(string fullModel, string vendor, string model) |
| 38 | + { |
| 39 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 40 | + var expectedMetric = $"Supportability/DotNet/LLM/{vendor}/{model}"; |
| 41 | + var actualMetric = string.Empty; |
| 42 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 43 | + .DoInstead((string m, long c) => actualMetric = $"Supportability/{m}"); |
| 44 | + |
| 45 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 46 | + |
| 47 | + Assert.That(actualMetric == expectedMetric, $"Model: '{fullModel}', Actual: '{actualMetric}', Expected: '{expectedMetric}'"); |
| 48 | + } |
| 49 | + |
| 50 | + [TestCase("o1", "openai", "o1")] |
| 51 | + [TestCase("o3-mini", "openai", "o3-mini")] |
| 52 | + [TestCase("gpt-4o-2024-11-20", "openai", "gpt-4o")] |
| 53 | + public void OpenAi_ModelFormatsTests(string fullModel, string vendor, string model) |
| 54 | + { |
| 55 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 56 | + var expectedMetric = $"Supportability/DotNet/LLM/{vendor}/{model}"; |
| 57 | + var actualMetric = string.Empty; |
| 58 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 59 | + .DoInstead((string m, long c) => actualMetric = $"Supportability/{m}"); |
| 60 | + |
| 61 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 62 | + |
| 63 | + Assert.That(actualMetric == expectedMetric, $"Model: '{fullModel}', Actual: '{actualMetric}', Expected: '{expectedMetric}'"); |
| 64 | + } |
| 65 | + |
| 66 | + [TestCase("bedrock", "")] |
| 67 | + [TestCase("bedrock", "bedrock.bad.model.more.than.four.sections")] |
| 68 | + [TestCase("openai", "")] |
| 69 | + public void BadModel_NoMetricTest(string source, string model) |
| 70 | + { |
| 71 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 72 | + var actualMetric = string.Empty; |
| 73 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 74 | + .DoInstead((string m, long c) => actualMetric = $"Supportability/{m}"); // Will not get called |
| 75 | + |
| 76 | + // Model is always stored so we want to check different values |
| 77 | + if (source == "bedrock") |
| 78 | + { |
| 79 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(model, _agent); |
| 80 | + } |
| 81 | + else if (source == "openai") |
| 82 | + { |
| 83 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(model, _agent); |
| 84 | + } |
| 85 | + |
| 86 | + Assert.That(actualMetric == string.Empty); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void Bedrock_DuplicateModels_OnlyOneMetricTest() |
| 91 | + { |
| 92 | + var fullModel = "luma.ray-v2:0"; |
| 93 | + |
| 94 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 95 | + var expectedMetric = $"Supportability/DotNet/LLM/luma/ray"; |
| 96 | + var actualMetrics = new List<string>(); |
| 97 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 98 | + .DoInstead((string m, long c) => actualMetrics.Add($"Supportability/{m}")); |
| 99 | + |
| 100 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 101 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 102 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 103 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 104 | + |
| 105 | + Assert.That(actualMetrics.Count == 1); |
| 106 | + Assert.That(actualMetrics[0] == expectedMetric, $"Model: '{fullModel}', Actual: '{actualMetrics[0]}', Expected: '{expectedMetric}'"); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void OpenAi_DuplicateModels_OnlyOneMetricTest() |
| 111 | + { |
| 112 | + var fullModel = "gpt-4.5"; |
| 113 | + |
| 114 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 115 | + var expectedMetric = $"Supportability/DotNet/LLM/openai/gpt-4.5"; |
| 116 | + var actualMetrics = new List<string>(); |
| 117 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 118 | + .DoInstead((string m, long c) => actualMetrics.Add($"Supportability/{m}")); |
| 119 | + |
| 120 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 121 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 122 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 123 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 124 | + |
| 125 | + Assert.That(actualMetrics.Count == 1); |
| 126 | + Assert.That(actualMetrics[0] == expectedMetric, $"Model: '{fullModel}', Actual: '{actualMetrics[0]}', Expected: '{expectedMetric}'"); |
| 127 | + } |
| 128 | + |
| 129 | + [Test] |
| 130 | + public void Bedrock_Exception_Test() |
| 131 | + { |
| 132 | + var fullModel = "meta.llama3-1-70b-instruct-v1:0"; |
| 133 | + var exception = new Exception("Test exception"); |
| 134 | + var expectedExceptionMessage = $"Error creating model supportability metric for {fullModel}: {exception.Message}"; |
| 135 | + |
| 136 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 137 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 138 | + .Throws(exception); |
| 139 | + |
| 140 | + var exceptionMessage = string.Empty; |
| 141 | + Mock.Arrange(() => _agent.Logger.Finest(Arg.AnyString)) |
| 142 | + .DoInstead((string m) => exceptionMessage = m); |
| 143 | + |
| 144 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForBedrock(fullModel, _agent); |
| 145 | + |
| 146 | + Assert.That(exceptionMessage == expectedExceptionMessage, message: exceptionMessage); |
| 147 | + } |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void OpenAi_Exception_Test() |
| 151 | + { |
| 152 | + var fullModel = "chatgpt-4o"; |
| 153 | + var exception = new Exception("Test exception"); |
| 154 | + var expectedExceptionMessage = $"Error creating model supportability metric for {fullModel}: {exception.Message}"; |
| 155 | + |
| 156 | + // Supportability/DotNet/LLM/{vendor}/{model} |
| 157 | + Mock.Arrange(() => _agent.RecordSupportabilityMetric(Arg.AnyString, Arg.AnyLong)) |
| 158 | + .Throws(exception); |
| 159 | + |
| 160 | + var exceptionMessage = string.Empty; |
| 161 | + Mock.Arrange(() => _agent.Logger.Finest(Arg.AnyString)) |
| 162 | + .DoInstead((string m) => exceptionMessage = m); |
| 163 | + |
| 164 | + SupportabilityHelpers.CreateModelIdSupportabilityMetricsForOpenAi(fullModel, _agent); |
| 165 | + |
| 166 | + Assert.That(exceptionMessage == expectedExceptionMessage, message: exceptionMessage); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments