-
Notifications
You must be signed in to change notification settings - Fork 27
/
Extensions.cs
57 lines (53 loc) · 1.89 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.TypeChat.Dialog;
internal static class SerializationEx
{
public static string Stringify(this object obj)
{
if (obj is string str)
{
return str;
}
if (obj is ITextSerializable textSerializable)
{
return textSerializable.Stringify();
}
return Json.Stringify(obj, false);
}
/// <summary>
/// Add messages in priority order to the prompt
/// Will keep adding messages until the prompt runs out of room
/// </summary>
/// <param name="builder">builder used to build prompt</param>
/// <param name="context">message history to add</param>
/// <returns></returns>
public static bool AddHistory(this PromptBuilder builder, IEnumerable<IPromptSection> context)
{
int contextStartAt = builder.Prompt.Count;
bool retVal = builder.AddRange(context);
int contextEndAt = builder.Prompt.Count;
if (contextStartAt < contextEndAt)
{
builder.Prompt.Reverse(contextStartAt, contextEndAt - contextStartAt);
}
return retVal;
}
/// <summary>
/// Add messages in priority order to the prompt
/// Will keep adding messages until the prompt runs out of room
/// </summary>
/// <param name="builder"></param>
/// <param name="context"></param>
/// <returns></returns>
public static async Task<bool> AddHistoryAsync(this PromptBuilder builder, IAsyncEnumerable<IPromptSection> context)
{
int contextStartAt = builder.Prompt.Count;
bool retVal = await builder.AddRangeAsync(context).ConfigureAwait(false);
int contextEndAt = builder.Prompt.Count;
if (contextStartAt < contextEndAt)
{
builder.Prompt.Reverse(contextStartAt, contextEndAt - contextStartAt);
}
return retVal;
}
}