Skip to content

Commit 9727c5b

Browse files
authored
KnowPro.NET: Answer Generation Part 2 (#1768)
* Hooked up end to end Answer Generation * Anwer Context building * Entity and Topic merging * Relevant Knowledge selection * AnswerTranslator + prompts * Bug fixes
1 parent 0cdd8df commit 9727c5b

Some content is hidden

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

47 files changed

+879
-114
lines changed

dotnet/typeagent/examples/knowProConsole/TestCommands.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ private Command AnswerDef()
387387
{
388388
Command cmd = new("kpTestAnswer")
389389
{
390-
Args.Arg<string>("text")
390+
Args.Arg<string>("query")
391391
};
392392
cmd.TreatUnmatchedTokensAsErrors = false;
393393
cmd.SetAction(this.AnswerAsync);
@@ -399,6 +399,21 @@ private async Task AnswerAsync(ParseResult args, CancellationToken cancellationT
399399
IConversation conversation = EnsureConversation();
400400

401401
NamedArgs namedArgs = new NamedArgs(args);
402+
string? query = namedArgs.Get<string>("query");
403+
if (string.IsNullOrEmpty(query))
404+
{
405+
return;
406+
}
407+
IList<AnswerResponse> answers = await conversation.AnswerQuestionAsync(
408+
query,
409+
null,
410+
null,
411+
null,
412+
null,
413+
cancellationToken
414+
).ConfigureAwait(false);
415+
KnowProWriter.WriteJson(answers);
416+
/*
402417
AnswerContext context = new AnswerContext();
403418
404419
IList<ConcreteEntity> entities = await conversation.SemanticRefs.GetAllEntitiesAsync(cancellationToken);
@@ -414,6 +429,7 @@ private async Task AnswerAsync(ParseResult args, CancellationToken cancellationT
414429
context.Messages = messages.Map((m) => new RelevantMessage(m));
415430
string prompt = context.ToPromptString();
416431
ConsoleWriter.WriteLine(prompt);
432+
*/
417433
}
418434

419435
private IConversation EnsureConversation()

dotnet/typeagent/src/common/Async.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ public static async Task<List<TResult>> MapAsync<T, TResult>(
3131
ArgumentVerify.ThrowIfNull(processor, nameof(processor));
3232

3333
return concurrency <= 1
34-
? await MapSequentialAsync(list, processor, progress, cancellationToken)
35-
: await MapConcurrentAsync(list, concurrency, processor, progress, cancellationToken);
34+
? await MapSequentialAsync(
35+
list,
36+
processor,
37+
progress,
38+
cancellationToken
39+
).ConfigureAwait(false)
40+
: await MapConcurrentAsync(
41+
list,
42+
concurrency,
43+
processor,
44+
progress,
45+
cancellationToken
46+
).ConfigureAwait(false);
3647
}
3748

3849
private static async Task<List<TResult>> MapSequentialAsync<T, TResult>(
@@ -47,7 +58,7 @@ CancellationToken cancellationToken
4758
{
4859
cancellationToken.ThrowIfCancellationRequested();
4960

50-
var result = await processor(list[i], cancellationToken);
61+
var result = await processor(list[i], cancellationToken).ConfigureAwait(false);
5162
results.Add(result);
5263
if (progress is not null)
5364
{
@@ -75,7 +86,7 @@ CancellationToken cancellationToken
7586
var batch = list.Slice(startAt, batchSize);
7687
var tasks = batch.Map<T, Task<TResult>>((t) => processor(t, cancellationToken));
7788

78-
var batchResults = await Task.WhenAll(tasks);
89+
var batchResults = await Task.WhenAll(tasks).ConfigureAwait(false);
7990

8091
results.AddRange(batchResults);
8192
if (progress is not null)

dotnet/typeagent/src/common/Cache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public KeyValueCache(IEqualityComparer<TKey> comparer = null)
180180
public bool Contains(TKey key) => base.ContainsKey(key);
181181

182182
public bool TryGet(TKey key, out TValue value) => base.TryGetValue(key, out value);
183+
184+
public new void Add(TKey key, TValue value)
185+
{
186+
TryAdd(key, value);
187+
}
183188
}
184189

185190
public static class Cache

dotnet/typeagent/src/common/StringExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ namespace TypeAgent.Common;
88

99
public static partial class StringExtensions
1010
{
11+
public static string Trim(this string text, int maxLength)
12+
{
13+
text = text.Trim();
14+
if (maxLength > 0 && text.Length > maxLength)
15+
{
16+
text = text[0..maxLength];
17+
}
18+
return text;
19+
}
20+
1121
/// <summary>
1222
/// Splits an enumerable of strings into chunks, each chunk containing up to maxChunkLength strings and
1323
/// no more than maxCharsPerChunk total characters. Strings longer than maxCharsPerChunk are truncated.
@@ -67,6 +77,7 @@ public static List<string> LowerAndSort(this List<string> list)
6777
public static IList<string> SplitLines(this string text, StringSplitOptions options = default)
6878
=> text.Split(s_lineSplitter, options);
6979

80+
// Split using a Regex
7081
public static IList<string> Split(this string text, Regex regex, StringSplitOptions options = default)
7182
{
7283
ArgumentVerify.ThrowIfNull(regex, nameof(regex));

dotnet/typeagent/src/common/TopNCollection.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@
33

44
namespace TypeAgent.Common;
55

6-
public class TopNCollection<T>
6+
public interface ITopNCollection<T>
7+
{
8+
int Count { get; }
9+
10+
void Add(T item, double score);
11+
void Add(IEnumerable<Scored<T>> items);
12+
List<Scored<T>> ByRankAndClear();
13+
}
14+
15+
public static class TopNCollection
16+
{
17+
public static ITopNCollection<T> Create<T>(int maxMatches)
18+
{
19+
return maxMatches > 0
20+
? new TopNCollection<T>(maxMatches)
21+
: new CollectAllCollection<T>();
22+
}
23+
}
24+
25+
public class TopNCollection<T> : ITopNCollection<T>
726
{
827
private List<Scored<T>>? _items;
928
private int _count; // Actual count, since items always ha a
@@ -170,3 +189,36 @@ private void VerifyNotEmpty()
170189
}
171190
}
172191
}
192+
193+
public class CollectAllCollection<T> : ITopNCollection<T>
194+
{
195+
List<Scored<T>>? _items = null;
196+
197+
public CollectAllCollection()
198+
{
199+
}
200+
201+
public int Count => _items is not null
202+
? _items.Count :
203+
0;
204+
205+
public void Add(T item, double score)
206+
{
207+
_items ??= [];
208+
_items.Add(new Scored<T>(item, score));
209+
}
210+
211+
public void Add(IEnumerable<Scored<T>> items)
212+
{
213+
_items ??= [];
214+
_items.AddRange(items);
215+
}
216+
217+
public List<Scored<T>> ByRankAndClear()
218+
{
219+
var results = _items ?? [];
220+
results.Sort();
221+
_items = null;
222+
return results;
223+
}
224+
}

dotnet/typeagent/src/conversationMemory/Memory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async ValueTask<IList<ConversationSearchResult>> SearchAsync(
4949
}
5050
}
5151

52-
public virtual IList<PromptSection>? GetModelInstructions()
52+
public virtual IList<IPromptSection>? GetModelInstructions()
5353
{
5454
return null;
5555
}

0 commit comments

Comments
 (0)