Skip to content

Commit b764a63

Browse files
authored
Make System Prompt templates a Multi-Selector (#404)
1 parent ce7c22a commit b764a63

File tree

89 files changed

+1093
-605
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+1093
-605
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageVersion Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="2.0.0" />
2121
<PackageVersion Include="MongoDB.Driver" Version="3.4.0" />
2222
<PackageVersion Include="NuGet.Versioning" Version="7.3.0" />
23-
<PackageVersion Include="OllamaSharp" Version="5.4.24" />
23+
<PackageVersion Include="OllamaSharp" Version="5.4.25" />
2424
<PackageVersion Include="SSH.NET" Version="2025.1.0" />
2525
<PackageVersion Include="ZString" Version="2.6.0" />
2626
<PackageVersion Include="YesSql.Abstractions" Version="5.4.7" />

src/Abstractions/CrestApps.OrchardCore.AI.Abstractions/Models/AICompletionContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ public class AICompletionContext
66
{
77
public string ConnectionName { get; set; }
88

9-
public bool UserMarkdownInResponse { get; set; } = true;
10-
119
public bool DisableTools { get; set; }
1210

1311
public string SystemMessage { get; set; }

src/Common/CrestApps.Support/StreamExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System.IO;
2-
31
namespace CrestApps.Support;
2+
43
public static class StreamExtensions
54
{
65
public static byte[] ReadAllBytes(this Stream instream)

src/Core/CrestApps.OrchardCore.AI.Chat.Core/Hubs/AIChatHub.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ private static async Task<string> GetGeneratedTitleAsync(IServiceProvider servic
399399
c.TopP = 1;
400400
c.Temperature = 0;
401401
c.MaxTokens = 64; // 64 token to generate about 255 characters.
402-
c.UserMarkdownInResponse = false;
403402

404403
// Avoid using tools or any data sources when generating title to reduce the used tokens.
405404
c.DataSourceId = null;
@@ -621,7 +620,6 @@ private static async Task ProcessGeneratedPromptAsync(ChannelWriter<CompletionPa
621620

622621
var completionContext = await completionContextBuilder.BuildAsync(profile, c =>
623622
{
624-
c.UserMarkdownInResponse = true;
625623
});
626624

627625
var builder = ZString.CreateStringBuilder();
@@ -667,7 +665,6 @@ private static async Task ProcessUtilityAsync(ChannelWriter<CompletionPartialMes
667665

668666
var completionContext = await completionContextBuilder.BuildAsync(profile, c =>
669667
{
670-
c.UserMarkdownInResponse = true;
671668
});
672669

673670
var references = new Dictionary<string, AICompletionReference>();
@@ -701,16 +698,16 @@ private static object CreateSessionPayload(AIChatSession chatSession, AIProfile
701698
},
702699
chatSession.Documents,
703700
Messages = prompts.Select(message => new AIChatResponseMessageDetailed
704-
{
705-
Id = message.ItemId,
706-
Role = message.Role.Value,
707-
IsGeneratedPrompt = message.IsGeneratedPrompt,
708-
Title = message.Title,
709-
Content = message.Content,
710-
UserRating = message.UserRating,
711-
References = message.References,
712-
Appearance = message.As<AssistantMessageAppearance>(),
713-
})
701+
{
702+
Id = message.ItemId,
703+
Role = message.Role.Value,
704+
IsGeneratedPrompt = message.IsGeneratedPrompt,
705+
Title = message.Title,
706+
Content = message.Content,
707+
UserRating = message.UserRating,
708+
References = message.References,
709+
Appearance = message.As<AssistantMessageAppearance>(),
710+
})
714711
};
715712

716713
private static string BuildTitleUserPrompt(AIProfile profile, string userPrompt)

src/Core/CrestApps.OrchardCore.AI.Chat.Interactions.Core/Models/TabularBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public string GetContent()
5656
}
5757

5858
var allRows = new List<string>(1 + (DataRows?.Count ?? 0)) { HeaderRow };
59-
59+
6060
if (DataRows is not null)
6161
{
6262
allRows.AddRange(DataRows);

src/Core/CrestApps.OrchardCore.AI.Chat.Interactions.Core/Services/TabularBatchProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ private async Task<TabularBatchResult> ProcessSingleBatchAsync(
266266
FrequencyPenalty = sourceContext.FrequencyPenalty,
267267
PresencePenalty = sourceContext.PresencePenalty,
268268
MaxTokens = sourceContext.MaxTokens,
269-
UserMarkdownInResponse = false, // We want structured output
270269
DisableTools = true, // Disable tools for batch processing
271270
};
272271

src/Core/CrestApps.OrchardCore.AI.Core/Handlers/CompletionContextOrchestrationHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public async Task BuildingAsync(OrchestrationContextBuildingContext context)
2222
var completionContext = await _completionContextBuilder.BuildAsync(context.Resource);
2323

2424
context.Context.CompletionContext = completionContext;
25-
context.Context.CompletionContext.UserMarkdownInResponse = true;
2625

2726
// Propagate DisableTools from the completion context.
2827
context.Context.DisableTools = context.Context.CompletionContext.DisableTools;

src/Core/CrestApps.OrchardCore.AI.Core/Models/PromptTemplateMetadata.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ namespace CrestApps.OrchardCore.AI.Core.Models;
33
public sealed class PromptTemplateMetadata
44
{
55
/// <summary>
6-
/// Gets or sets the identifier of the selected AI template.
6+
/// Gets or sets the ordered list of selected prompt templates.
77
/// </summary>
8-
public string TemplateId { get; set; }
8+
public List<PromptTemplateSelectionEntry> Templates { get; set; } = [];
99

10-
/// <summary>
11-
/// Gets or sets the template parameters as key-value pairs.
12-
/// </summary>
13-
public Dictionary<string, object> Parameters { get; set; }
10+
public void SetSelections(IEnumerable<PromptTemplateSelectionEntry> selections)
11+
{
12+
Templates = selections?
13+
.Where(selection => !string.IsNullOrWhiteSpace(selection.TemplateId))
14+
.Select(selection => new PromptTemplateSelectionEntry
15+
{
16+
TemplateId = selection.TemplateId,
17+
Parameters = selection.Parameters is { Count: > 0 }
18+
? new Dictionary<string, object>(selection.Parameters, StringComparer.OrdinalIgnoreCase)
19+
: null,
20+
})
21+
.ToList() ?? [];
22+
}
1423
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CrestApps.OrchardCore.AI.Core.Models;
2+
3+
public sealed class PromptTemplateSelectionEntry
4+
{
5+
public string TemplateId { get; set; }
6+
7+
public Dictionary<string, object> Parameters { get; set; }
8+
}

src/Core/CrestApps.OrchardCore.AI.Core/Services/AICompletionServiceBase.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,4 @@ protected virtual Task<AIDeployment> GetDeploymentAsync(AICompletionContext cont
100100
{
101101
return Task.FromResult<AIDeployment>(null);
102102
}
103-
104-
protected async Task<string> GetSystemMessageAsync(AICompletionContext context)
105-
{
106-
var systemMessage = string.Empty;
107-
108-
if (!string.IsNullOrEmpty(context.SystemMessage))
109-
{
110-
systemMessage = context.SystemMessage;
111-
}
112-
113-
if (context.UserMarkdownInResponse)
114-
{
115-
var markdownInstruction = await AITemplateService.RenderAsync(AITemplateIds.UseMarkdownSyntax);
116-
systemMessage += Environment.NewLine + markdownInstruction;
117-
}
118-
119-
return systemMessage;
120-
}
121103
}

0 commit comments

Comments
 (0)