-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathTextClassifier.cs
More file actions
52 lines (44 loc) · 1.85 KB
/
TextClassifier.cs
File metadata and controls
52 lines (44 loc) · 1.85 KB
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
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.TypeChat.Classification;
/// <summary>
/// A Text classifier is a JsonTranslator that translates the user's request into a TextClassification
/// The language model is provided with a set of classes to choose from
/// This is very useful for common routing problems: to categorize/bucket a request
/// A text class is a {ClassName, Description} pair
/// </summary>
public class TextClassifier : JsonTranslator<TextClassification>
{
private readonly TextClasses _classes;
/// <summary>
/// Create a classifier that will make classification decisions using the given language model
/// </summary>
/// <param name="languageModel">model to use</param>
public TextClassifier(ILanguageModel languageModel)
: this(languageModel, new TextClasses())
{
}
/// <summary>
/// Create a classifier that will make classification decisions using the given language model
/// </summary>
/// <param name="languageModel">model to use</param>
/// <param name="classes">Classes to classify into</param>
public TextClassifier(ILanguageModel languageModel, TextClasses classes)
: base(languageModel, new TypeValidator<TextClassification>(classes.Vocabs))
{
if (classes is null)
{
throw new ArgumentNullException(nameof(classes));
}
_classes = classes;
}
/// <summary>
/// Text classes used by this classifier
/// </summary>
public TextClasses Classes => _classes;
protected override Prompt CreateRequestPrompt(Prompt request, IList<IPromptSection> preamble)
{
string classes = Json.Stringify(_classes);
string fullRequest = $"Classify \"{request}\" using the following classification table:\n{classes}\n";
return base.CreateRequestPrompt(fullRequest, preamble);
}
}