developed a JSON contract for charts and injected it into the main system prompt

This commit is contained in:
Nils Kruthoff 2026-08-04 14:49:55 +02:00
parent f54deb287a
commit 191be98031
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C

View File

@ -14,6 +14,34 @@ public sealed record ChatThread
{
private static readonly ILogger<ChatThread> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ChatThread>();
private const string CHART_OUTPUT_INSTRUCTIONS = """
When a chart is useful or explicitly requested, you may include one or more complete chart blocks in your normal response. Use exactly this fenced JSON format:
```aistudio-chart
{
"schema_version": 1,
"type": "bar",
"title": "Chart title",
"caption": "Contextual interpretation of the chart",
"data": {
"categories": ["A", "B"],
"series": [
{
"name": "Series",
"values": [1, 2]
}
]
}
}
```
- Supported types are `bar`, `stacked_bar`, `line`, `pie`, and `donut`.
- Every series needs exactly one finite numeric value per category.
- Pie and donut charts need exactly one series and non-negative values.
- Add a concise caption that correctly contextualizes the chart and may explain the chart's main finding.
- Use only the fields shown above.
- Keep other explanatory text outside the chart block.
""";
/// <summary>
/// The unique identifier of the chat thread.
/// </summary>
@ -56,6 +84,12 @@ public sealed record ChatThread
/// </summary>
public bool IncludeDateTime { get; set; } = false;
/// <summary>
/// Indicates whether the model may emit locally rendered chart blocks.
/// False by default so internal structured model requests remain unchanged.
/// </summary>
public bool AllowChartOutput { get; set; } = false;
/// <summary>
/// The data source options for this chat thread.
/// </summary>
@ -198,9 +232,8 @@ public sealed record ChatThread
}
LOGGER.LogInformation(logMessage);
if(!this.IncludeDateTime)
return systemPromptText;
if(this.IncludeDateTime)
{
//
// Prepend the current date and time to the system prompt:
//
@ -211,13 +244,23 @@ public sealed record ChatThread
$"Today is {nowUtc:dddd, MMMM d, yyyy h:mm tt} (UTC) and {nowLocal:dddd, MMMM d, yyyy h:mm tt} (local time)."
);
return $"""
systemPromptText = $"""
{currentDateTime}
{systemPromptText}
""";
}
if (!this.AllowChartOutput)
return systemPromptText;
return $"""
{systemPromptText}
{CHART_OUTPUT_INSTRUCTIONS}
""";
}
/// <summary>
/// Removes a content block from this chat thread.
/// </summary>