forked from Azure-Samples/cognitive-services-qnamaker-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace-knowledge-base.cs
113 lines (103 loc) · 4.04 KB
/
replace-knowledge-base.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
// NOTE: Install the Newtonsoft.Json NuGet package.
using Newtonsoft.Json;
namespace QnAMaker
{
class Program
{
// Represents the various elements used to create HTTP request URIs
// for QnA Maker operations.
static string host = "https://westus.api.cognitive.microsoft.com";
static string service = "/qnamaker/v4.0";
static string method = "/knowledgebases/";
// NOTE: Replace this with a valid subscription key.
static string key = "ADD KEY HERE";
// NOTE: Replace this with a valid knowledge base ID.
static string kb = "ADD ID HERE";
/// <summary>
/// Defines the data source used to replace the knowledge base.
/// This schema replaces your entire knowledge base with one QnA pair.
/// </summary>
static string new_kb = @"
{
'qnaList': [
{
'id': 0,
'answer': 'You can use our REST APIs to manage your Knowledge Base.
See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/knowledgebases_publish',
'source': 'Custom Editorial',
'questions': [
'How do I programmatically replace my Knowledge Base?'
],
'metadata': [
{
'name': 'category',
'value': 'api'
}
]
}
]
}";
/// <summary>
/// Formats and indents JSON for display.
/// </summary>
/// <param name="s">The JSON to format and indent.</param>
/// <returns>A string containing formatted and indented JSON.</returns>
static string PrettyPrint(string s)
{
return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(s), Formatting.Indented);
}
/// <summary>
/// Asynchronously sends a PUT HTTP request.
/// </summary>
/// <param name="uri">The URI of the HTTP request.</param>
/// <param name="body">The body of the HTTP request.</param>
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}(QnAMaker.Program.Response)"/>
/// object that represents the HTTP response."</returns>
async static Task<string> Put(string uri, String body)
{
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Put;
request.RequestUri = new Uri(uri);
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return "{'result' : 'Success.'}";
}
else
{
return await response.Content.ReadAsStringAsync();
}
}
}
/// <summary>
/// Replaces a knowledge base.
/// </summary>
async static void ReplaceKB()
{
var uri = host + service + method + kb;
Console.WriteLine("Calling " + uri + ".");
var response = await Put(uri, new_kb);
Console.WriteLine(PrettyPrint(response));
Console.WriteLine("Press any key to continue.");
}
static void Main(string[] args)
{
// Invoke the ReplaceKB() method to replace a knowledge base.
ReplaceKB();
// The console waits for a key to be pressed before closing.
Console.ReadLine();
}
}
}