This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreplace-knowledge-base.java
161 lines (138 loc) · 5.35 KB
/
replace-knowledge-base.java
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
import java.io.*;
import java.lang.reflect.Type;
import java.net.*;
import java.util.*;
import javax.net.ssl.HttpsURLConnection;
/*
* Gson: https://github.com/google/gson
* Maven info:
* <dependency>
* <groupId>com.google.code.gson</groupId>
* <artifactId>gson</artifactId>
* <version>2.8.1</version>
* </dependency>
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
/* NOTE: To compile and run this code:
1. Save this file as ReplaceKB.java.
2. Run:
javac ReplaceKB.java -cp .;gson-2.8.1.jar -encoding UTF-8
3. Run:
java -cp .;gson-2.8.1.jar ReplaceKB
*/
public class ReplaceKB {
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace this with a valid subscription key.
static String subscriptionKey = "ADD KEY HERE";
// Replace this with a valid knowledge base ID.
static String kb = "ADD ID HERE";
// 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/";
// We'll serialize these classes into JSON for our request to the server.
// For the JSON request schema, see:
// https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/knowledgebases_publish
public static class Request {
Question[] qnaList;
}
public static class Question {
Integer id;
String answer;
String source;
String[] questions;
Metadata[] metadata;
}
public static class Metadata {
String name;
String value;
}
/**
* Formats and indents JSON for display.
* @param json_text The raw JSON input
* @return String A string containing formatted and indented JSON.
*/
public static String PrettyPrint (String json_text) {
JsonParser parser = new JsonParser();
JsonElement json = parser.parse(json_text);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(json);
}
/**
* Sends an HTTP PUT request.
* @param url The string URL request
* @param content The content of the request
* @return The response result
*/
public static String Put (URL url, String content) throws Exception {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", content.length() + "");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
byte[] encoded_content = content.getBytes("UTF-8");
wr.write(encoded_content, 0, encoded_content.length);
wr.flush();
wr.close();
if (connection.getResponseCode() == 204)
{
return "{'result' : 'Success.'}";
}
else {
StringBuilder response = new StringBuilder ();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
return response.toString();
}
}
/**
* Sends the request to replace the knowledge base.
*/
public static String ReplaceKB (String kb, Request req) throws Exception {
URL url = new URL(host + service + method + kb);
System.out.println ("Calling " + url.toString() + ".");
String content = new Gson().toJson(req);
return Put(url, content);
}
/**
* Compiles the details of a request.
* @return The Request object that represents the details of the request.
*/
public static Request GetRequest () {
Request req = new Request ();
Question q = new Question();
q.id = 0;
q.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/5ac266295b4ccd1554da7600";
q.source = "Custom Editorial";
q.questions = new String[]{"How do I programmatically update my Knowledge Base?"};
Metadata md = new Metadata();
md.name = "category";
md.value = "api";
q.metadata = new Metadata[]{md};
req.qnaList = new Question[]{q};
return req;
}
public static void main(String[] args) {
try {
String response = ReplaceKB (kb, GetRequest ());
System.out.println (PrettyPrint (response));
}
catch (Exception e) {
System.out.println (e.getCause().getMessage());
}
}
}