/
QuestionAnsweringClient.cs
275 lines (244 loc) · 17.9 KB
/
QuestionAnsweringClient.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Azure.AI.Language.QuestionAnswering.Models;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.AI.Language.QuestionAnswering
{
/// <summary>
/// The <see cref="QuestionAnsweringClient"/> allows you to ask questions of a custom or built-in knowledge base.
/// </summary>
public class QuestionAnsweringClient
{
internal const string AuthorizationHeader = "Ocp-Apim-Subscription-Key";
private readonly QuestionAnsweringKnowledgeBaseRestClient _knowledgebaseRestClient;
private readonly QuestionAnsweringTextRestClient _textRestClient;
/// <summary>
/// Initializes a new instance of the <see cref="QuestionAnsweringClient"/> class.
/// </summary>
/// <param name="endpoint">The Question Answering endpoint on which to operate.</param>
/// <param name="credential">An <see cref="AzureKeyCredential"/> used to authenticate requests to the <paramref name="endpoint"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="endpoint"/> or <paramref name="credential"/> is null.</exception>
public QuestionAnsweringClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuestionAnsweringClient"/> class.
/// </summary>
/// <param name="endpoint">The Question Answering endpoint on which to operate.</param>
/// <param name="credential">An <see cref="AzureKeyCredential"/> used to authenticate requests to the <paramref name="endpoint"/>.</param>
/// <param name="options">Optional <see cref="QuestionAnsweringClientOptions"/> to customize requests sent to the endpoint.</param>
/// <exception cref="ArgumentNullException"><paramref name="endpoint"/> or <paramref name="credential"/> is null.</exception>
public QuestionAnsweringClient(Uri endpoint, AzureKeyCredential credential, QuestionAnsweringClientOptions options)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
Argument.AssertNotNull(credential, nameof(credential));
Endpoint = endpoint;
Options = options ?? new QuestionAnsweringClientOptions();
Diagnostics = new ClientDiagnostics(Options);
Pipeline = HttpPipelineBuilder.Build(
Options,
new AzureKeyCredentialPolicy(credential, AuthorizationHeader));
_knowledgebaseRestClient = new(Diagnostics, Pipeline, Endpoint, Options.Version);
_textRestClient = new(Diagnostics, Pipeline, Endpoint, Options.Version);
}
/// <summary>
/// Protected constructor to allow mocking.
/// </summary>
protected QuestionAnsweringClient()
{
}
/// <summary>
/// Get the service endpoint for this client.
/// </summary>
public virtual Uri Endpoint { get; }
/// <summary>
/// Gets the <see cref="QuestionAnsweringClientOptions"/> for this client.
/// </summary>
private protected virtual QuestionAnsweringClientOptions Options { get; }
/// <summary>
/// Gets the <see cref="ClientDiagnostics"/> for this client.
/// </summary>
private protected virtual ClientDiagnostics Diagnostics { get; }
/// <summary>
/// Gets the <see cref="HttpPipeline"/> for this client.
/// </summary>
private protected virtual HttpPipeline Pipeline { get; }
/// <summary>Answers the specified question using your knowledge base.</summary>
/// <param name="projectName">The name of the project to use.</param>
/// <param name="deploymentName">The deployment name of the project to use, such as "test" or "prod".</param>
/// <param name="question">The question to answer.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <exception cref="ArgumentException"><paramref name="question"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectName"/>, <paramref name="deploymentName"/>, or <paramref name="question"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Task<Response<KnowledgeBaseAnswers>> QueryKnowledgeBaseAsync(string projectName, string deploymentName, string question, CancellationToken cancellationToken = default) =>
QueryKnowledgeBaseAsync(new QueryKnowledgeBaseOptions(projectName, deploymentName, question), cancellationToken);
/// <summary>Answers the specified question using your knowledge base.</summary>
/// <param name="options">
/// An <see cref="QueryKnowledgeBaseOptions"/> containing the <see cref="QueryKnowledgeBaseOptions.ProjectName"/>,
/// <see cref="QueryKnowledgeBaseOptions.DeploymentName"/>, <see cref="QueryKnowledgeBaseOptions.Question"/>, and other options to answer a question.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual async Task<Response<KnowledgeBaseAnswers>> QueryKnowledgeBaseAsync(QueryKnowledgeBaseOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(options, nameof(options));
using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryKnowledgeBase)}");
scope.AddAttribute("projectName", options.ProjectName);
scope.AddAttribute("deploymentName", options.DeploymentName);
scope.Start();
try
{
return await _knowledgebaseRestClient.QueryAsync(options.ProjectName, options.DeploymentName, options, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>Answers the specified question using your knowledge base.</summary>
/// <param name="projectName">The name of the project to use.</param>
/// <param name="deploymentName">The deployment name of the project to use, such as "test" or "prod".</param>
/// <param name="question">The question to answer.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <exception cref="ArgumentException"><paramref name="question"/> is an empty string.</exception>
/// <exception cref="ArgumentNullException"><paramref name="projectName"/>, <paramref name="deploymentName"/>, or <paramref name="question"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Response<KnowledgeBaseAnswers> QueryKnowledgeBase(string projectName, string deploymentName, string question, CancellationToken cancellationToken = default) =>
QueryKnowledgeBase(new QueryKnowledgeBaseOptions(projectName, deploymentName, question), cancellationToken);
/// <summary>Answers the specified question using your knowledge base.</summary>
/// <param name="options">
/// An <see cref="QueryKnowledgeBaseOptions"/> containing the <see cref="QueryKnowledgeBaseOptions.ProjectName"/>,
/// <see cref="QueryKnowledgeBaseOptions.DeploymentName"/>, <see cref="QueryKnowledgeBaseOptions.Question"/>, and other options to answer a question.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Response<KnowledgeBaseAnswers> QueryKnowledgeBase(QueryKnowledgeBaseOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(options, nameof(options));
using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryKnowledgeBase)}");
scope.AddAttribute("projectName", options.ProjectName);
scope.AddAttribute("deploymentName", options.DeploymentName);
scope.Start();
try
{
return _knowledgebaseRestClient.Query(options.ProjectName, options.DeploymentName, options, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary>
/// <param name="question">The question to answer.</param>
/// <param name="records">The text records to query.</param>
/// <param name="language">
/// The language of the text records. This is the BCP-47 representation of a language.
/// For example, use "en" for English, "es" for Spanish, etc.
/// If not set, uses <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> as the default.
/// If <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> is not set, the service default, "en" for English, is used.
/// </param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Task<Response<TextAnswers>> QueryTextAsync(string question, IEnumerable<string> records, string language = default, CancellationToken cancellationToken = default) =>
QueryTextAsync(QueryTextOptions.From(question, records, language ?? Options.DefaultLanguage), cancellationToken);
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary>
/// <param name="question">The question to answer.</param>
/// <param name="records">The text records to query.</param>
/// <param name="language">
/// The language of the text records. This is the BCP-47 representation of a language.
/// For example, use "en" for English, "es" for Spanish, etc.
/// If not set, uses <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> as the default.
/// If <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> is not set, the service default, "en" for English, is used.
/// </param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Response<TextAnswers> QueryText(string question, IEnumerable<string> records, string language = default, CancellationToken cancellationToken = default) =>
QueryText(QueryTextOptions.From(question, records, language ?? Options.DefaultLanguage), cancellationToken);
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary>
/// <param name="question">The question to answer.</param>
/// <param name="records">A collection of <see cref="TextRecord"/> to query.</param>
/// <param name="language">
/// The language of the text records. This is the BCP-47 representation of a language.
/// For example, use "en" for English, "es" for Spanish, etc.
/// If not set, uses <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> as the default.
/// If <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> is not set, the service default, "en" for English, is used.
/// </param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Task<Response<TextAnswers>> QueryTextAsync(string question, IEnumerable<TextRecord> records, string language = default, CancellationToken cancellationToken = default) =>
QueryTextAsync(QueryTextOptions.From(question, records, language ?? Options.DefaultLanguage), cancellationToken);
/// <summary>Answers the specified question using the text <paramref name="records"/>.</summary>
/// <param name="question">The question to answer.</param>
/// <param name="records">A collection of <see cref="TextRecord"/> to query.</param>
/// <param name="language">
/// The language of the text records. This is the BCP-47 representation of a language.
/// For example, use "en" for English, "es" for Spanish, etc.
/// If not set, uses <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> as the default.
/// If <see cref="QuestionAnsweringClientOptions.DefaultLanguage"/> is not set, the service default, "en" for English, is used.
/// </param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <paramref name="question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="question"/> or <paramref name="records"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Response<TextAnswers> QueryText(string question, IEnumerable<TextRecord> records, string language = default, CancellationToken cancellationToken = default) =>
QueryText(QueryTextOptions.From(question, records, language ?? Options.DefaultLanguage), cancellationToken);
/// <summary>Answers the specified question using the provided text in the body.</summary>
/// <param name="options">The question to answer.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <see cref="QueryTextOptions.Question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual async Task<Response<TextAnswers>> QueryTextAsync(QueryTextOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(options, nameof(options));
using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryText)}");
scope.Start();
try
{
options = options.Clone(Options.DefaultLanguage);
return await _textRestClient.QueryAsync(options, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>Answers the specified question using the provided text in the body.</summary>
/// <param name="options">The question to answer.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the request.</param>
/// <returns><see cref="TextAnswers"/> containing answers to the <see cref="QueryTextOptions.Question"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">The service returned an error. The exception contains details of the service error.</exception>
public virtual Response<TextAnswers> QueryText(QueryTextOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(options, nameof(options));
using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(QuestionAnsweringClient)}.{nameof(QueryText)}");
scope.Start();
try
{
options = options.Clone(Options.DefaultLanguage);
return _textRestClient.Query(options, cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
}
}