forked from microsoft/typechat.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptBuilder.cs
172 lines (154 loc) · 4.88 KB
/
PromptBuilder.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.TypeChat;
/// <summary>
/// Prompts have a maximum length. Prompt lengths are be limited by model capacity or policy
/// PromptBuilder builds prompts consisting of multiple prompt sections in a way that the prompt
/// length does not exceed a given maximum
/// </summary>
public class PromptBuilder
{
Prompt _prompt;
int _currentLength;
int _maxLength;
Func<string, int, string>? _substring;
/// <summary>
/// Create a builder to create prompts whose length does not exceed maxLength characters
/// </summary>
/// <param name="maxLength">maximum length</param>
public PromptBuilder(int maxLength)
: this(maxLength, Substring)
{
}
/// <summary>
/// Create a builder to create prompts whose length does not exceed maxLength characters
/// If a full prompt section is too long, inovokes a substringExtractor callback to extract a
/// suitable substring, if any. Substring extractors could do so at sentence boundaries, paragraph
/// boundaries and so on.
/// </summary>
/// <param name="maxLength">Prompt will not exceed this maxLengthin characters</param>
/// <param name="substringExtractor">optional extractor</param>
public PromptBuilder(int maxLength, Func<string, int, string>? substringExtractor = null)
{
_prompt = new Prompt();
_maxLength = maxLength;
_substring = substringExtractor;
}
/// <summary>
/// The prompt being built
/// </summary>
public Prompt Prompt => _prompt;
/// <summary>
/// Current length of the prompt in characters
/// </summary>
public int Length => _currentLength;
/// <summary>
/// Maximum allowed prompt length
/// </summary>
public int MaxLength
{
get => _maxLength;
set
{
if (value < _currentLength)
{
throw new ArgumentException($"CurrentLength: {_currentLength} exceeds {value}");
}
_maxLength = value;
}
}
/// <summary>
/// Add a prompt section if the total length of the prompt will not exceed limits
/// </summary>
/// <param name="text">text to add</param>
/// <returns>true if added, false if not</returns>
public bool Add(string text)
{
return Add(new PromptSection(text));
}
/// <summary>
/// Add a prompt section if the total length of the prompt will not exceed limits
/// </summary>
/// <param name="section">section to add</param>
/// <returns>true if added, false if not</returns>
public bool Add(IPromptSection section)
{
if (section is null)
{
throw new ArgumentNullException(nameof(section));
}
string text = section.GetText();
if (string.IsNullOrEmpty(text))
{
return true;
}
int lengthAvailable = _maxLength - _currentLength;
if (text.Length <= lengthAvailable)
{
_prompt.Append(section);
_currentLength += text.Length;
return true;
}
if (_substring is not null)
{
text = _substring(text, lengthAvailable);
_prompt.Append(section.Source, text);
return true;
}
return false;
}
/// <summary>
/// Add a collection of prompt sections - until the MaxLength - is hit
/// </summary>
/// <param name="sections">sections to add</param>
/// <returns>true if all sections were added</returns>
public bool AddRange(IEnumerable<IPromptSection> sections)
{
ArgumentVerify.ThrowIfNull(sections, nameof(sections));
foreach (var section in sections)
{
if (!Add(section))
{
return false;
}
}
return true;
}
/// <summary>
/// Add collection of prompt sections - until the MaxLength - is hit
/// </summary>
/// <param name="sections"></param>
/// <returns></returns>
public async Task<bool> AddRangeAsync(IAsyncEnumerable<IPromptSection> sections)
{
ArgumentVerify.ThrowIfNull(sections, nameof(sections));
await foreach (var section in sections.ConfigureAwait(false))
{
if (!Add(section))
{
return false;
}
}
return true;
}
/// <summary>
/// Clear the builder
/// </summary>
public void Clear()
{
_prompt.Clear();
_currentLength = 0;
}
/// <summary>
/// Reverse the order of sections in the builder, in the given range
/// </summary>
/// <param name="startAt"></param>
/// <param name="count"></param>
public void Reverse(int startAt, int count)
{
Reverse(startAt, count);
}
static string Substring(string text, int length)
{
return text.Substring(0, length);
}
}