I tried to make a talk application in Java using AI "A3RT"

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.

Introduction

Please refer to Official Site for AI "A3RT".

API provided

This time, let's make a simple talk app using the Talk API.

Environment / preparation

Since API KEY is required to execute API, issue it at here.

Sample code

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;
    }
}

result

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]

in conclusion

This AI has just been released, so let's look forward to future learning.

Recommended Posts

I tried to make a talk application in Java using AI "A3RT"
I tried to make a login function in Java
I tried to make a client of RESAS-API in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Java beginner tried to make a simple web application using Spring Boot
I tried to create a Clova skill in Java
I tried using JWT in Java
I tried to make an application in 3 months from inexperienced
I tried to modernize a Java EE application with OpenShift.
I just wanted to make a Reactive Property in Java
I tried to convert a string to a LocalDate type in Java
I tried using Dapr in Java to facilitate microservice development
I tried to implement a buggy web application in Kotlin
I tried using Elasticsearch API in Java
I tried to build a simple application using Dockder + Rails Scaffold
[Java] I tried to make a maze by the digging method ♪
I tried to make a parent class of a value object in Ruby
I tried to make Basic authentication with Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
[iOS] I tried to make a processing application like Instagram with Swift
I tried to implement deep learning in Java
I tried to build a Firebase application development environment with Docker in 2020
I tried to output multiplication table in Java
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried to make a sample program using the problem of database specialist in Domain Driven Design
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
[Small story] I tried to make the java ArrayList a little more convenient
I tried using a database connection in Android development
I tried to implement Firebase push notification in Java
I tried using Google Cloud Vision API in Java
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried using Log4j2 on a Java EE server
How to convert A to a and a to A using AND and OR in Java
I tried using an extended for statement in Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried scraping a stock chart using Java (Jsoup)
I tried to find out what changed in Java 9
I tried using Java REPL
Make a rhombus using Java
I tried metaprogramming in Java
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I stumbled when I tried using neo4j in the jenv environment, so make a note
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried to create a java8 development environment with Chocolatey
Let's make a calculator application in Java ~ Display the application window
I want to find a relative path in a situation using Path
[Beginner] I made a program to sell cakes in Java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried to make Java Optional and guard clause coexist
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I made a Dockerfile to start Glassfish 5 using Oracle Java