Files
spark/src/DataStreamSuite/SharedStreams/NL/NLPromptConfig.cs
T
John Colvin ada7d4bed1 refactor: Load IDataStreamFactory(s) with dependency injection
So we can create ViewStreams that depend on other services, config, etc.
Previously, the PrimitiveManager relied purely on static construction of
types through reflection.
2025-11-14 14:52:07 -05:00

78 lines
2.6 KiB
C#

namespace bts.SilverPine;
using Spark.Core.Json;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class NLPromptConfig {
static NLPromptConfig? instance;
public static NLPromptConfig Instance =>
instance ?? throw new InvalidOperationException(
"SetGlobalConfig must be called to initialize config static singleton");
public List<FewShotPrompt> prompts { get; } = new(0);
public static void SetGlobalConfig(NLPromptConfig cfg) {
// This should only be called once
Log.isnull(instance);
instance = Log.notnull(cfg);
}
public static NLPromptConfig Default() => new();
// Represents a pair of a natural language query and its dsl
public class FewShotExample {
public string? natural_language { get; set; }
public string? dsl { get; set; }
}
public class FewShotPrompt {
public string? category { get; set; }
public string? description { get; set; }
public bool IsEnabled { get; set; } = true;
// MultiLineDescription is simply here for convenience. It is functionally redundant, as it
// serves the same purpose as 'description'. If 'description' is too long, you can break it
// up into an array of strings and assign it to MultiLineDescription instead.
public List<string>? MultiLineDescription { get; set; }
// the actual prompt given by the user/CME
public string? prompt { get; set; }
public List<FewShotExample> examples { get; } = new(0);
public override string ToString() {
var desc = description ?? string.Concat(MultiLineDescription.empty_if_null());
string rv = $"System: {desc}\n";
int ex_no = 0;
foreach (FewShotExample ex in examples ?? new(0)) {
rv += $"{++ex_no}. Natural Language: \"{ex.natural_language}\"" + "\n";
rv += $" DSL: {ex.dsl}\n";
}
return rv;
}
// Reads normal strings and arrays of lines
public class FewShotPromptLinesConverter : JsonConverter<string> {
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.ReadString(out var str)) {
return str;
}
if (reader.ReadArray(out var array)) {
return string.Join('\n', array.Select(v => v?.AsString()));
}
throw new JsonException("Failed to parse FewShotPrompt lines");
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) =>
writer.WriteStringValue(value);
}
}
}