Skip to content
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

fix: catch exceptions thrown by template interpolation #43

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pkgs/sdk/server-ai/src/LdAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,28 @@ public ILdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig de
}


var prompt =
parsed.Prompt?.Select(m => new LdAiConfig.Message(InterpolateTemplate(m.Content, mergedVariables), m.Role));
var prompt = new List<LdAiConfig.Message>();

if (parsed.Prompt != null)
{
for (var i = 0; i < parsed.Prompt.Count; i++)
{
try
{
var content = InterpolateTemplate(parsed.Prompt[i].Content, mergedVariables);
prompt.Add(new LdAiConfig.Message(content, parsed.Prompt[i].Role));
}
catch (Exception ex)
{
_logger.Error(
$"AI model config prompt has malformed message at index {i}: {ex.Message} (returning default config, which will not contain interpolated prompt messages)");
return new LdAiConfigTracker(_client, key, defaultValue, context);
}
}
}

return new LdAiConfigTracker(_client, key, new LdAiConfig(parsed.Meta?.Enabled ?? false, prompt, parsed.Meta, parsed.Model), context);

}

/// <summary>
Expand Down Expand Up @@ -137,7 +155,8 @@ private AiConfig ParseConfig(LdValue value, string key)
}
catch (JsonException e)
{
_logger.Error("Unable to parse AI model config for key {0}: {1}", key, e.Message);
_logger.Error(
$"Unable to parse AI model config for key {key}: {e.Message} (returning default config, which will not contain interpolated prompt messages)");
return null;
}
}
Expand Down
32 changes: 32 additions & 0 deletions pkgs/sdk/server-ai/test/InterpolationTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -117,6 +118,37 @@ public void TestInterpolationWithArraySectionWorks()
Assert.Equal("hello world ! ", result);
}

[Fact]
public void TestInterpolationMalformed()
{
var mockClient = new Mock<ILaunchDarklyClient>();
var mockLogger = new Mock<ILogger>();

const string configJson = """
{
"_ldMeta": {"versionKey": "1", "enabled": true},
"model": {},
"prompt": [
{
"content": "This is a {{ malformed }]} prompt",
"role": "System"
}
]
}
""";


mockClient.Setup(x =>
x.JsonVariation("foo", It.IsAny<Context>(), It.IsAny<LdValue>())).Returns(LdValue.Parse(configJson));

mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);

mockLogger.Setup(x => x.Error(It.IsAny<string>()));

var client = new LdAiClient(mockClient.Object);
var tracker = client.ModelConfig("foo", Context.New("key"), LdAiConfig.Disabled);
Assert.False(tracker.Config.Enabled);
}

[Fact]
public void TestInterpolationWithBasicContext()
Expand Down
Loading