This article uses the "Talk API" of the AI "A3RT" provided by Recruit to create a talk app, and even though it can be made easier by writing with curl commands, Python, and JavaScript, I dare to make full use of ** Java **. (Suffering?) ~~ This is an article about a leisure person who has left GW.
Please refer to Official Site for AI "A3RT".
This time, let's make a simple talk app using the Talk API.
Since API KEY is required to execute API, issue it at here.
A simple specification that loops this by sending the character string obtained by standard input to the API by POST and obtaining the result. Type "bye" to exit. Since the created environment was an environment with Proxy, the sample code has a description around Proxy, but it is unnecessary if it is an environment without Proxy. Also, since some of the response characters are unicode strings, there is a process to convert them to ordinary strings. Please forgive me because the error handling is loose. : stuck_out_tongue_closed_eyes:
python
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
public class SmallTalkApp {
public static void main(String[] args) {
while (true) {
HttpURLConnection conn = null;
try {
//Standard input
System.out.print("[me]");
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
if (input == null || input.isEmpty()) {
continue;
} else if (input.equals("bye")) {
System.out.println("End");
return;
}
//Transmission data
//Write the issued API KEY
String data = "apikey=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&query=" + input;
//HTTP connection
URL url = new URL("https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk");
//Proxy settings(Only if there is a Proxy in the net environment)
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyhost", 9999));
conn = (HttpURLConnection) url.openConnection(proxy);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
//POST transmission
try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) {
dos.writeBytes(data);
}
//Receive response
int rescode = conn.getResponseCode();
if (rescode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
StringBuilder buf = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
}
System.out.println(convertToOiginal(buf.toString()));
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}
/**
*Convert unicode string to string(u\3042 ⇒ Oh)
*
* @param unicode A string containing a unicode string
* @return string
*/
private static String convertToOiginal(String unicode) {
String tmp = unicode;
while (tmp.indexOf("\\u") > 0) {
String str = tmp.substring(tmp.indexOf("\\u"), tmp.indexOf("\\u") + 6);
int c = Integer.parseInt(str.substring(2), 16);
tmp = tmp.replaceFirst("\\" + str, new String(new int[]{c}, 0, 1));
}
return tmp;
}
}
You can talk (white eyes: rolling_eyes:
[me]thank you for your hard work.
{"status": 0, "message": "ok", "results": [{"perplexity": 0.7773633993386415, "reply": "I don't hate it so much"}]}
[me]Thank you very much.
{"status": 0, "message": "ok", "results": [{"perplexity": 0.07521183094611102, "reply": "Do you do well?"}]}
[me]Yes. Do you do well?
{"status": 0, "message": "ok", "results": [{"perplexity": 1.5817458708187506, "reply": "Is it mine?It's a secret"}]}
[me]Please. please tell me!
{"status": 0, "message": "ok", "results": [{"perplexity": 2.4087016492196223, "reply": "Do you do well?"}]}
[me]I am also a secret
{"status": 0, "message": "ok", "results": [{"perplexity": 0.23161212785866075, "reply": "Do you do well?"}]}
[me]What do you do during your vacation?
{"status": 0, "message": "ok", "results": [{"perplexity": 7.697953651890003, "reply": "You check well?"}]}
[me]
This AI has just been released, so let's look forward to future learning.
Recommended Posts