|
關(guān)于這個(gè)例子的完整介紹,請(qǐng)參考公眾號(hào) “汪子熙”的兩篇文章: 
SAP C/4HANA與人工智能和增強(qiáng)現(xiàn)實(shí)(AR)技術(shù)結(jié)合的又一個(gè)創(chuàng)新案例 和使用Recast.AI創(chuàng)建具有人工智能的聊天機(jī)器人: 
本文介紹如何用Java代碼同recast.AI網(wǎng)站上創(chuàng)建好的模型交互。 我創(chuàng)建了一個(gè)名為get-product-infomation的機(jī)器學(xué)習(xí)模型,用"Add an expression"下面的這么多句子去喂這個(gè)模型: 
一會(huì)測(cè)試時(shí),我會(huì)用這個(gè)句子進(jìn)行測(cè)試 " I am looking for some materials", 所以先記下來(lái)。 
如果任意輸入一句話,recast.AI識(shí)別出來(lái)意圖為get-product-infomation, 我希望AI自動(dòng)返回一些句子,這些句子定義在recast.AI模型的Actions標(biāo)簽頁(yè)下面: 
比如這個(gè)Actions模型的意思是,從Sure, what type of product are you going to produce?和Cool, what products do you want to produce?里隨機(jī)挑選一句返回。 下圖右半部份是recast.AI的測(cè)試控制臺(tái)。 
下面是用Java代碼方式消費(fèi)這個(gè)人工智能模型的例子: public class RecastAIService {private final static String RECAST_AI_URL = "https://api./build/v1/dialog";private final static String DEVELOPER_TOKEN = "Token feb6b413a1a8cf8efdd53f48ba1d4";public Answer dialog(final String content, final String conversationId) throws ClientProtocolException, IOException{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost postRequest = new HttpPost(RECAST_AI_URL);
postRequest.addHeader("Authorization", DEVELOPER_TOKEN);
postRequest.addHeader("Content-Type", "application/json");
String body = "{"message": {"content":""
+ content
+ "","type":"text"}, "conversation_id": ""
+ conversationId
+""}";
HttpEntity entity = new StringEntity(body);
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);if(response.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(response.getEntity());
JSONObject resultJsonObj = JSON.parseObject(result);
JSONObject results = (JSONObject) resultJsonObj.get("results");
JSONArray messages = results.getJSONArray("messages");
JSONObject nlp = (JSONObject) results.get("nlp");
JSONArray intents = nlp.getJSONArray("intents");
Answer answer = new Answer();if (null != messages && messages.size() > 0){
JSONObject messageJson = messages.getJSONObject(0);
answer.setContent(messageJson.getString("content"));
}if (null != intents && intents.size() > 0){
JSONObject intentJson = intents.getJSONObject(0);
answer.setIntent(intentJson.getString("slug"));
}return answer;
}
logger.debug("Failed to access recastai. The response code is" + response.getStatusLine().getStatusCode());return null;
}
測(cè)試代碼: 
傳入I am looking for some materials,recast.AI解析出這個(gè)句子的意圖有99%的可能性是get-product-information: 
Java代碼返回的句子也確實(shí)是recast.AI模型里維護(hù)的回復(fù)之一: 
要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙
|