Chatbot HTTP Server

51 views
Skip to first unread message

Andrew

unread,
Nov 24, 2016, 1:09:11 AM11/24/16
to opencog
Not sure if this is the right approach but I thought it might be of use to someone. Just need to startup the relex server first

(use-modules (web server) (web request) (web response) (web uri) (opencog) (opencog nlp) (opencog nlp chatbot) (opencog nlp relex2logic) (ice-9 pretty-print) (ice-9 iconv))

(define (process-chat user body)

    (define result (process-query "user1" (bytevector->string body "iso-8859-1") ))
    (pretty-print result)
    (if (unspecified? result)
        (set! result "unspecified")
    )
    (if (null? result)
        (set! result "null")
    )
    (if (list? result)
        (set! result (car result))
    )

    (string-append "{result:" result "}")
)

(define (my-handler request body)
  (trace-msg "--------------- request -------------------\n")
  (pretty-print  (bytevector->string body "iso-8859-1") )
  (display (request-content-type request))
    (values '((content-type . (application/json)))
        (process-chat "user1" body)
    )
)

(run-server my-handler 'http '(#:port 8093))

Apil Tamang

unread,
Nov 24, 2016, 7:57:54 AM11/24/16
to opencog
Hi Andrew,
I'm sure this will be helpful at some point in the future. Can you please also mention the context of use...  ?

Andrew

unread,
Nov 24, 2016, 4:28:21 PM11/24/16
to opencog
Sure using it as a API to Java. Here is the client side

public String sendMessage(String question, String user) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
        String url = "http://localhost:8093";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("content-type", "text/plain");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        //String body = "{user:user1,question:gh threw the ball}";
        String body = question;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(body);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        //System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
       
        System.out.println("response:" + response);
       
        String result = response.toString();
        if(result.contains(".")) {
            result = result.replace(".", "");
        }
        result = result.trim();
  
        return response.toString();
    }

Linas Vepstas

unread,
Nov 25, 2016, 8:26:09 AM11/25/16
to opencog
I really like short programs that do useful things.

I apologize that the current chatbot cannot say anything interesting or useful, just right now. But we're working on that.

--linas

--
You received this message because you are subscribed to the Google Groups "opencog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opencog+unsubscribe@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at https://groups.google.com/group/opencog.
To view this discussion on the web visit https://groups.google.com/d/msgid/opencog/9eb1affb-7a79-44be-854f-c532c7b80ccf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages