diff --git a/README.md b/README.md index 698aee8..5d8de09 100755 --- a/README.md +++ b/README.md @@ -94,22 +94,15 @@ To build the application: 2. Launch the Discovery tooling ![](readme_images/discovery_tooling.png) -3. Create a new data collection, name it whatever you like, and select the default configuration. +3. Create a new data collection, name it whatever you like, and select the default configuration. The default configuration now uses Natural Language Understanding.

- After you're done, there should be a new private collection in the UI
+4. Download and unzip the [manualdocs.zip](src/main/resources/manualdocs.zip) in this repo to reveal a set of JSON documents -4. (Optional) [Set up the custom configuration](custom_config/config_instructions.md) in order to enrich specific Discovery fields and improve results - -5. On the collection tooling interface, click "Switch" on the Configuration line and select your new configuration - - - -6. Download and unzip the [manualdocs.zip](src/main/resources/manualdocs.zip) in this repo to reveal a set of JSON documents - -7. In the tooling interface, drag and drop (or browse and select) all of the JSON files into the "Add data to this collection" box +5. In the tooling interface, drag and drop (or browse and select) all of the JSON files into the "Add data to this collection" box - This may take a few minutes -- you will see a notification when the process is finished diff --git a/build.gradle b/build.gradle index ce67a0d..04f22db 100644 --- a/build.gradle +++ b/build.gradle @@ -49,8 +49,8 @@ dependencies { compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1' compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.3.2' compile group: 'org.jsoup', name: 'jsoup', version: '1.9.2' - compile group: 'com.ibm.watson.developer_cloud', name: 'conversation', version:'3.8.0' - compile group: 'com.ibm.watson.developer_cloud', name: 'discovery', version:'3.8.0' + compile group: 'com.ibm.watson.developer_cloud', name: 'conversation', version:'4.2.1' + compile group: 'com.ibm.watson.developer_cloud', name: 'discovery', version:'4.2.1' testCompile 'com.sun.jersey:jersey-client:1.19.1' testCompile 'org.mockito:mockito-core:1.+' testCompile 'org.powermock:powermock-module-junit4:1.6.5' diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryClient.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryClient.java index 0844904..7784271 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryClient.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryClient.java @@ -14,21 +14,22 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.ibm.watson.apis.conversation_with_discovery.payload.DocumentPayload; import com.ibm.watson.apis.conversation_with_discovery.utils.Constants; import com.ibm.watson.apis.conversation_with_discovery.utils.Messages; -import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryResponse; +import com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse; +import com.ibm.watson.developer_cloud.discovery.v1.model.QueryResult; +import com.ibm.watson.developer_cloud.util.GsonSingleton; /** * DiscoveryClient. @@ -40,32 +41,40 @@ public class DiscoveryClient { private static final int SNIPPET_LENGTH = 150; private Pattern pattern = Pattern.compile("((.+?)

){1,2}"); - + /** - * This method uses the Query object to send the user's query (the input param) to the discovery service. + * This method uses the Query object to send the user's query (the + * input param) to the discovery service. * - * @param input The user's query to be sent to the discovery service - * @return A list of DocumentPayload objects, each representing a single document the discovery service believes is a - * possible answer to the user's query - * @throws Exception the exception + * @param input + * The user's query to be sent to the discovery service + * @return A list of DocumentPayload objects, each representing a single + * document the discovery service believes is a possible answer to the + * user's query + * @throws Exception + * the exception */ public List getDocuments(String input) throws Exception { DiscoveryQuery discoveryQuery = new DiscoveryQuery(); QueryResponse output = discoveryQuery.query(input); - List> results = output.getResults(); - String jsonRes = new Gson().toJson(results); + List results = output.getResults(); + + String jsonRes = GsonSingleton.getGson().toJson(results); JsonElement jelement = new JsonParser().parse(jsonRes); return createPayload(jelement); } /** - * Helper Method to include highlighting information along with the Discovery response so the final payload - * includes id,title,body,sourceUrl as json key value pairs. + * Helper Method to include highlighting information along with the Discovery + * response so the final payload includes id,title,body,sourceUrl as json key + * value pairs. * - * @param resultsElement the results element - * @return A list of DocumentPayload objects, each representing a single document the discovery service believes is a - * possible answer to the user's query + * @param resultsElement + * the results element + * @return A list of DocumentPayload objects, each representing a single + * document the discovery service believes is a possible answer to the + * user's query */ private List createPayload(JsonElement resultsElement) { logger.info(Messages.getString("Service.CREATING_DISCOVERY_PAYLOAD")); @@ -75,13 +84,13 @@ private List createPayload(JsonElement resultsElement) { if (jarray.size() > 0) { for (int i = 0; (i < jarray.size()) && (i < Constants.DISCOVERY_MAX_SEARCH_RESULTS_TO_SHOW); i++) { DocumentPayload documentPayload = new DocumentPayload(); - String id = jarray.get(i).getAsJsonObject().get(Constants.DISCOVERY_FIELD_ID).toString().replaceAll("\"", ""); + JsonObject jObj = jarray.get(i).getAsJsonObject(); + + String id = jObj.get(Constants.DISCOVERY_FIELD_ID).toString().replaceAll("\"", ""); documentPayload.setId(id); - documentPayload.setTitle( - jarray.get(i).getAsJsonObject().get(Constants.DISCOVERY_FIELD_TITLE).toString().replaceAll("\"", "")); - if (jarray.get(i).getAsJsonObject().get(Constants.DISCOVERY_FIELD_BODY) != null) { - String body = jarray.get(i).getAsJsonObject().get(Constants.DISCOVERY_FIELD_BODY).toString().replaceAll("\"", - ""); + documentPayload.setTitle(jObj.get(Constants.DISCOVERY_FIELD_TITLE).toString().replaceAll("\"", "")); + if (jObj.get(Constants.DISCOVERY_FIELD_BODY) != null) { + String body = jObj.get(Constants.DISCOVERY_FIELD_BODY).toString().replaceAll("\"", ""); // This method limits the response text in this sample // app to two paragraphs. @@ -104,6 +113,7 @@ private List createPayload(JsonElement resultsElement) { } else { documentPayload.setConfidence("0.0"); } + logger.info("documentPayloada: " + documentPayload); payload.add(i, documentPayload); } } else { @@ -122,7 +132,8 @@ private List createPayload(JsonElement resultsElement) { /** * get first SNIPPET_LENGTH characters of body response. * - * @param body discovery response + * @param body + * discovery response * @return */ private String getSniplet(String body) { @@ -138,8 +149,9 @@ private String getSniplet(String body) { } /** - * This method limits the response text in this sample app to two paragraphs. For your own application, you can - * comment out the method to allow the full text to be returned. + * This method limits the response text in this sample app to two paragraphs. + * For your own application, you can comment out the method to allow the full + * text to be returned. * * @param body * @return string diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryQuery.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryQuery.java index 233c487..aa99797 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryQuery.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/discovery/DiscoveryQuery.java @@ -19,14 +19,14 @@ import com.ibm.watson.apis.conversation_with_discovery.utils.Constants; import com.ibm.watson.developer_cloud.discovery.v1.Discovery; -import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryRequest; -import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryResponse; +import com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions; +import com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse; /** * The Class DiscoveryQuery. */ public class DiscoveryQuery { - + private static final Logger logger = LogManager.getLogger(DiscoveryQuery.class.getName()); private String collectionId; @@ -57,18 +57,20 @@ public DiscoveryQuery() { } /** - * Use the Watson Developer Cloud SDK to send the user's query to the discovery service. + * Use the Watson Developer Cloud SDK to send the user's query to the + * discovery service. * - * @param userQuery The user's query to be sent to the discovery service + * @param userQuery + * The user's query to be sent to the discovery service * @return The query responses obtained from the discovery service - * @throws Exception the exception + * @throws Exception + * the exception */ public QueryResponse query(String userQuery) throws Exception { - QueryRequest.Builder queryBuilder = new QueryRequest.Builder(environmentId, collectionId); - + StringBuilder sb = new StringBuilder(); - - if(queryFields == null || queryFields.length() == 0 || queryFields.equalsIgnoreCase("none")) { + + if (queryFields == null || queryFields.length() == 0 || queryFields.equalsIgnoreCase("none")) { sb.append(userQuery); } else { StringTokenizer st = new StringTokenizer(queryFields, ","); @@ -81,12 +83,9 @@ public QueryResponse query(String userQuery) throws Exception { } } } - - logger.info("Query: " + sb.toString()); - queryBuilder.query(sb.toString()); - QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute(); + QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId).query(sb.toString()).build(); - return queryResponse; + return discovery.query(queryOptions).execute(); } } diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/AppServletContextListener.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/AppServletContextListener.java index 69ce652..2aea894 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/AppServletContextListener.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/AppServletContextListener.java @@ -21,12 +21,14 @@ import org.apache.logging.log4j.Logger; import com.google.gson.JsonObject; +import com.ibm.watson.apis.conversation_with_discovery.utils.Messages; /** - * The listener interface for receiving servletContext events. The class that is interested in processing a - * servletContext event implements this interface, and the object created with that class is registered with a component - * using the component's addServletContextListener method. When the servletContext event occurs, that - * object's appropriate method is invoked. + * The listener interface for receiving servletContext events. The class that is + * interested in processing a servletContext event implements this interface, + * and the object created with that class is registered with a component using + * the component's addServletContextListener method. When the + * servletContext event occurs, that object's appropriate method is invoked. * * @see ServletContextEvent */ @@ -39,17 +41,19 @@ public class AppServletContextListener implements javax.servlet.ServletContextLi /* * (non-Javadoc) * - * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) + * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet. + * ServletContextEvent) */ @Override - public void contextDestroyed(ServletContextEvent arg0) { + public void contextDestroyed(ServletContextEvent arg0) { logger.info("Destroying ServletContextListener"); } /* * (non-Javadoc) * - * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) + * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet. + * ServletContextEvent) */ @Override public void contextInitialized(ServletContextEvent arg0) { @@ -71,10 +75,12 @@ public JsonObject getJsonConfig() { /* * (non-Javadoc) * - * @see java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEvent) + * @see java.beans.PropertyChangeListener#propertyChange(java.beans. + * PropertyChangeEvent) */ @Override public void propertyChange(PropertyChangeEvent evt) { config = (JsonObject) evt.getNewValue(); + logger.info(config.toString()); } } diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/SetupThread.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/SetupThread.java index f84b0fd..b6d4bb6 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/SetupThread.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/listener/SetupThread.java @@ -23,15 +23,17 @@ import com.google.gson.JsonObject; import com.ibm.watson.apis.conversation_with_discovery.utils.Constants; import com.ibm.watson.apis.conversation_with_discovery.utils.Messages; -import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; -import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest; +import com.ibm.watson.developer_cloud.conversation.v1.Conversation; +import com.ibm.watson.developer_cloud.conversation.v1.model.InputData; +import com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions; import com.ibm.watson.developer_cloud.discovery.v1.Discovery; -import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryRequest; +import com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions; import com.ibm.watson.developer_cloud.service.exception.UnauthorizedException; /** - * This class is forked when the application is accessed for the first time. It tests if the conversation and discovery - * service associated with the app (either bound on bluemix or specified in server.env) + * This class is forked when the application is accessed for the first time. It + * tests if the conversation and discovery service associated with the app + * (either bound on bluemix or specified in server.env) */ public class SetupThread extends Thread { @@ -43,8 +45,8 @@ public class SetupThread extends Thread { public JsonObject config = new JsonObject(); /** - * Method to update the listeners about any property changes. This is used by the UI to inform user what the status of - * the setup. + * Method to update the listeners about any property changes. This is used by + * the UI to inform user what the status of the setup. * * @param object * @param config @@ -56,14 +58,20 @@ private void notifyListeners(Object object, JsonObject config) { } /** - * This method is used to update the JSON Config Object that will be sent back to the UI. + * This method is used to update the JSON Config Object that will be sent back + * to the UI. * * @param config - * @param setupStep The step at which the setup is - * @param setupState The state of the setup(accepts ready/not_ready) - * @param setupPhase The current phase of the setup(this can be that the services is being setup or if any other - * phase of the application is being setup) - * @param setupMessage The message that you want to be shown in the UI. + * @param setupStep + * The step at which the setup is + * @param setupState + * The state of the setup(accepts ready/not_ready) + * @param setupPhase + * The current phase of the setup(this can be that the services is + * being setup or if any other phase of the application is being + * setup) + * @param setupMessage + * The message that you want to be shown in the UI. */ private void updateConfigObject(String setupStep, String setupState, String setupPhase, String setupMessage) { config.addProperty(Constants.SETUP_STEP, setupStep); // $NON-NLS-1$ @@ -76,7 +84,8 @@ private void updateConfigObject(String setupStep, String setupState, String setu /** * Method to add a listener. * - * @param newListener PropertyChangeListener + * @param newListener + * PropertyChangeListener */ public void addChangeListener(PropertyChangeListener newListener) { listener.add(newListener); @@ -117,9 +126,9 @@ public void run() { discovery.setEndPoint(Constants.DISCOVERY_URL); discovery.setUsernameAndPassword(userName, password); - QueryRequest.Builder queryBuilder = new QueryRequest.Builder(environmentId, collectionId); - queryBuilder.query("searchText:car tire pressure"); - discovery.query(queryBuilder.build()).execute(); + QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId) + .query("searchText:car tire pressure").build(); + discovery.query(queryOptions).execute(); // test conversation credentials @@ -139,10 +148,14 @@ public void run() { status = "Conversation "; - ConversationService service = new ConversationService(Constants.CONVERSATION_VERSION); + Conversation service = new Conversation(Constants.CONVERSATION_VERSION); service.setUsernameAndPassword(userName, password); - MessageRequest newMessage = new MessageRequest.Builder().inputText("hi").context(null).build(); - service.message(workspaceId, newMessage).execute(); + + InputData input = new InputData.Builder("Hi").build(); + + MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build(); + + service.message(options).execute(); updateConfigObject("3", Constants.READY, Messages.getString("SetupThread.EMPTY"), Messages.getString("SetupThread.EMPTY")); diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/payload/DocumentPayload.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/payload/DocumentPayload.java index 569b45c..2d22aa0 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/payload/DocumentPayload.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/payload/DocumentPayload.java @@ -13,8 +13,9 @@ package com.ibm.watson.apis.conversation_with_discovery.payload; /** - * A Payload object which describes a single result returned by the Discovery Service. The service is provided - * with a user query, and returns a list of 'hits' for the query. Each hit is called a 'document'. The document contains + * A Payload object which describes a single result returned by the Discovery + * Service. The service is provided with a user query, and returns a list of + * 'hits' for the query. Each hit is called a 'document'. The document contains * various metadata which describes the data the service retrieved. */ public class DocumentPayload { @@ -29,8 +30,9 @@ public class DocumentPayload { private String title; /** - * Returns the body of the search result. The search result is comprised of several pieces of metadata which when - * combined form a 'document'. This method returns the body of the search result. + * Returns the body of the search result. The search result is comprised of + * several pieces of metadata which when combined form a 'document'. This + * method returns the body of the search result. * * @return a string representing the body of the search result. */ @@ -48,17 +50,20 @@ public String getBodySnippet() { } /** - * Returns the confidence associated with the search result. The higher the confidence the more likely it is that the - * search result contains data relevant to the search query. + * Returns the confidence associated with the search result. The higher the + * confidence the more likely it is that the search result contains data + * relevant to the search query. * - * @return a value representing the confidence the service has that the result is a match. + * @return a value representing the confidence the service has that the result + * is a match. */ public String getConfidence() { return confidence; } /** - * Returns the text in the search result which is to be highlighted, indicating a match to the search term. + * Returns the text in the search result which is to be highlighted, + * indicating a match to the search term. * * @return a string which is to be highlighted */ @@ -85,8 +90,9 @@ public String getSourceUrl() { } /** - * Returns a String which represents the title of the document. This may be null, depending - * on how the service was configured. + * Returns a String which represents the title of + * the document. This may be null, depending on how the service was + * configured. * * @return a string which represents the title of the document. */ @@ -97,7 +103,8 @@ public String getTitle() { /** * Sets the body of the document. * - * @param body the actual text contained in the body of the document. + * @param body + * the actual text contained in the body of the document. */ public void setBody(String body) { this.body = body; @@ -106,7 +113,8 @@ public void setBody(String body) { /** * Sets the body snippet. * - * @param bodySnippet the new body snippet + * @param bodySnippet + * the new body snippet */ public void setBodySnippet(String bodySnippet) { this.bodySnippet = bodySnippet; @@ -115,7 +123,8 @@ public void setBodySnippet(String bodySnippet) { /** * Sets the confidence that the system has in the search result. * - * @param confidence confidence values for the answer + * @param confidence + * confidence values for the answer */ public void setConfidence(String confidence) { this.confidence = confidence; @@ -124,7 +133,8 @@ public void setConfidence(String confidence) { /** * Sets the text which is to be highlighted. * - * @param highlight text to be highlighted. + * @param highlight + * text to be highlighted. */ public void setHighlight(String highlight) { this.highlight = highlight; @@ -133,7 +143,8 @@ public void setHighlight(String highlight) { /** * Sets the document id. * - * @param id a string. + * @param id + * a string. */ public void setId(String id) { this.id = id; @@ -142,7 +153,8 @@ public void setId(String id) { /** * Sets the source document url. * - * @param url a string + * @param url + * a string */ public void setSourceUrl(String url) { sourceUrl = url; @@ -151,7 +163,8 @@ public void setSourceUrl(String url) { /** * Sets the title of the document. May be null. * - * @param title the title of the document. + * @param title + * the title of the document. */ public void setTitle(String title) { this.title = title; diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResource.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResource.java index bee40a8..9aa01cd 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResource.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResource.java @@ -20,7 +20,6 @@ import java.net.MalformedURLException; import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.POST; @@ -38,8 +37,10 @@ import com.ibm.watson.apis.conversation_with_discovery.payload.DocumentPayload; import com.ibm.watson.apis.conversation_with_discovery.utils.Constants; import com.ibm.watson.apis.conversation_with_discovery.utils.Messages; -import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; -import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest; +import com.ibm.watson.developer_cloud.conversation.v1.Conversation; +import com.ibm.watson.developer_cloud.conversation.v1.model.Context; +import com.ibm.watson.developer_cloud.conversation.v1.model.InputData; +import com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions; import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse; import com.ibm.watson.developer_cloud.service.exception.UnauthorizedException; import com.ibm.watson.developer_cloud.util.GsonSingleton; @@ -49,19 +50,18 @@ */ @Path("conversation/api/v1/workspaces") public class ProxyResource { - private static String API_VERSION; private static final String ERROR = "error"; private static final Logger logger = LogManager.getLogger(ProxyResource.class.getName()); - + private DiscoveryClient discoveryClient = new DiscoveryClient(); - + private String password = System.getenv("CONVERSATION_PASSWORD"); - + private String url; - + private String username = System.getenv("CONVERSATION_USERNAME"); - private MessageRequest buildMessageFromPayload(InputStream body) { + private MessageOptions buildMessageFromPayload(InputStream body, String workspaceId) { StringBuilder sbuilder = null; BufferedReader reader = null; try { @@ -75,7 +75,14 @@ private MessageRequest buildMessageFromPayload(InputStream body) { sbuilder.append("\n"); } } - return GsonSingleton.getGson().fromJson(sbuilder.toString(), MessageRequest.class); + + MessageResponse response = GsonSingleton.getGson().fromJson(sbuilder.toString(), MessageResponse.class); + Context context = response.getContext(); + String intent = response.getInput().getText(); + InputData input = new InputData.Builder(intent).build(); + MessageOptions options = new MessageOptions.Builder(workspaceId).context(context).input(input).build(); + + return options; } catch (IOException e) { logger.error(Messages.getString("ProxyResource.JSON_READ"), e); } finally { @@ -91,23 +98,27 @@ private MessageRequest buildMessageFromPayload(InputStream body) { } /** - * This method is responsible for sending the query the user types into the UI to the Watson services. The code - * demonstrates how the conversation service is called, how the response is evaluated, and how the response is then - * sent to the discovery service if necessary. + * This method is responsible for sending the query the user types into the UI + * to the Watson services. The code demonstrates how the conversation service + * is called, how the response is evaluated, and how the response is then sent + * to the discovery service if necessary. * - * @param request The full query the user asked of Watson - * @param id The ID of the conversational workspace - * @return The response from Watson. The response will always contain the conversation service's response. If the - * intent confidence is high or the intent is out_of_scope, the response will also contain information from - * the discovery service + * @param request + * The full query the user asked of Watson + * @param id + * The ID of the conversational workspace + * @return The response from Watson. The response will always contain the + * conversation service's response. If the intent confidence is high + * or the intent is out_of_scope, the response will also contain + * information from the discovery service */ - private MessageResponse getWatsonResponse(MessageRequest request, String id) throws Exception { + private MessageResponse getWatsonResponse(MessageOptions options) throws Exception { // Configure the Watson Developer Cloud SDK to make a call to the // appropriate conversation service. - ConversationService service = - new ConversationService(API_VERSION != null ? API_VERSION : Constants.CONVERSATION_VERSION); + Conversation service = new Conversation(Constants.CONVERSATION_VERSION); + if ((username != null) || (password != null)) { service.setUsernameAndPassword(username, password); } @@ -116,7 +127,7 @@ private MessageResponse getWatsonResponse(MessageRequest request, String id) thr // Use the previously configured service object to make a call to the // conversational service - MessageResponse response = service.message(id, request).execute(); + MessageResponse response = service.message(options).execute(); // Determine if conversation's response is sufficient to answer the // user's question or if we @@ -124,7 +135,7 @@ private MessageResponse getWatsonResponse(MessageRequest request, String id) thr if (response.getOutput().containsKey("action") && (response.getOutput().get("action").toString().indexOf("call_discovery") != -1)) { - String query = response.getInputText(); + String query = response.getInput().getText(); // Extract the user's original query from the conversational // response @@ -139,18 +150,13 @@ private MessageResponse getWatsonResponse(MessageRequest request, String id) thr // and rank answers to the user in the main UI. The JSON // response section of the UI will // show information from the calls to both services. - Map output = response.getOutput(); - if (output == null) { - output = new HashMap(); - response.setOutput(output); - } // Send the user's question to the discovery service List docs = discoveryClient.getDocuments(query); // Append the discovery answers to the output object that will // be sent to the UI - output.put("CEPayload", docs); + response.put("DiscoveryPayload", docs); } } @@ -160,27 +166,29 @@ private MessageResponse getWatsonResponse(MessageRequest request, String id) thr /** * Post message. * - * @param id the id - * @param body the body + * @param id + * the id + * @param body + * the body * @return the response */ @POST @Path("{id}/message") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Response postMessage(@PathParam("id") String id, InputStream body) { + public Response postMessage(@PathParam("id") String workspaceId, InputStream body) { HashMap errorsOutput = new HashMap(); - MessageRequest request = buildMessageFromPayload(body); + MessageOptions options = buildMessageFromPayload(body, workspaceId); - if (request == null) { + if (options == null) { throw new IllegalArgumentException(Messages.getString("ProxyResource.NO_REQUEST")); } MessageResponse response = null; try { - response = getWatsonResponse(request, id); + response = getWatsonResponse(options); } catch (Exception e) { if (e instanceof UnauthorizedException) { @@ -200,22 +208,16 @@ public Response postMessage(@PathParam("id") String id, InputStream body) { } return Response.ok(new Gson().toJson(response, MessageResponse.class)).type(MediaType.APPLICATION_JSON).build(); } - - /** - * Sets the conversation API version. - * - * @param version the new conversation API version - */ - public static void setConversationAPIVersion(String version) { - API_VERSION = version; - } /** * Sets the credentials. * - * @param username the username - * @param password the password - * @param url the url + * @param username + * the username + * @param password + * the password + * @param url + * the url */ public void setCredentials(String username, String password, String url) { this.username = username; diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResource.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResource.java index 5e83e4d..6a47080 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResource.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResource.java @@ -30,8 +30,9 @@ import com.ibm.watson.apis.conversation_with_discovery.utils.Messages; /** - * The SetupResource is used to fetch the configuration file with the WorkspaceId from the env file and the setup - * configuration stage that the application is currently in. The API endpoint points to '/rest/setup' + * The SetupResource is used to fetch the configuration file with the + * WorkspaceId from the env file and the setup configuration stage that the + * application is currently in. The API endpoint points to '/rest/setup' * */ @Path("setup") @@ -62,7 +63,7 @@ public Response getConfig() { logger.debug(Messages.getString("SetupResource.CONFIG_STATUS") + config); config.addProperty(Constants.WORKSPACE_ID, workspaceId); - + if (config.has(Constants.SETUP_STEP) && (config.get(Constants.SETUP_STEP).getAsInt() == 3) && config.get(Constants.SETUP_STATE).getAsString().equalsIgnoreCase(Constants.READY)) { if (StringUtils.isBlank(workspaceId)) { diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Constants.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Constants.java index 2455357..a03d039 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Constants.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Constants.java @@ -12,7 +12,8 @@ */ package com.ibm.watson.apis.conversation_with_discovery.utils; -import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; +import com.ibm.watson.developer_cloud.conversation.v1.Conversation; +import com.ibm.watson.developer_cloud.discovery.v1.Discovery; /** * The Class Constants. @@ -22,11 +23,12 @@ public class Constants { private Constants() { } + /** The Constant CONVERSATION_URL. */ public static final String CONVERSATION_URL = "https://gateway.watsonplatform.net/conversation/api"; - + /** The Constant CONVERSATION_VERSION. */ - public static final String CONVERSATION_VERSION = ConversationService.VERSION_DATE_2016_09_20; + public static final String CONVERSATION_VERSION = "2017-05-26"; /** The Constant DISCOVERY_FIELD_BODY. */ public static final String DISCOVERY_FIELD_BODY = "contentHtml"; @@ -50,7 +52,7 @@ private Constants() { public static final String DISCOVERY_URL = "https://gateway.watsonplatform.net/discovery/api/"; /** The Constant DISCOVERY_VERSION. */ - public static final String DISCOVERY_VERSION = "2016-12-15"; + public static final String DISCOVERY_VERSION = "2017-09-01"; /** The Constant NOT_READY. */ public static final String NOT_READY = "not_ready"; diff --git a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Messages.java b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Messages.java index 1b70357..3d52d11 100644 --- a/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Messages.java +++ b/src/main/java/com/ibm/watson/apis/conversation_with_discovery/utils/Messages.java @@ -31,7 +31,8 @@ public class Messages { /** * Gets the string. * - * @param key the key + * @param key + * the key * @return the string */ public static String getString(String key) { @@ -42,5 +43,6 @@ public static String getString(String key) { } } - private Messages() { } + private Messages() { + } } diff --git a/src/main/resources/server.env b/src/main/resources/server.env index 736498a..3958df1 100644 --- a/src/main/resources/server.env +++ b/src/main/resources/server.env @@ -13,6 +13,7 @@ DISCOVERY_ENVIRONMENT_ID= # Set the following value to the Discovery enrichment field names # separated by commas, or set it to 'none' to query without enrichments -# See examples below: + +# Use Natural language Processing (NLP) DISCOVERY_QUERY_FIELDS=none -# DISCOVERY_QUERY_FIELDS=searchText,enrichedText +# DISCOVERY_QUERY_FIELDS=searchText.concepts.text,enrichedText.concepts.text diff --git a/src/main/webapp/ts/app.component.ts b/src/main/webapp/ts/app.component.ts index 03029b1..3e492f2 100644 --- a/src/main/webapp/ts/app.component.ts +++ b/src/main/webapp/ts/app.component.ts @@ -338,8 +338,8 @@ export class AppComponent { responseText = data1.error; data1 = this.langData.NResponse; } else if (data1.output) { - if (data1.output.CEPayload && data1.output.CEPayload.length > 0) { - ce = data1.output.CEPayload; + if (data1.DiscoveryPayload && data1.DiscoveryPayload.length > 0) { + ce = data1.DiscoveryPayload; responseText = this.langData.Great; if (ce.length === 1 && ce[0].title === 'No results found') { diff --git a/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResourceTest.java b/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResourceTest.java index 8d35a68..4231ece 100644 --- a/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResourceTest.java +++ b/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/ProxyResourceTest.java @@ -33,7 +33,7 @@ import org.junit.Test; import com.google.gson.Gson; -import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; +import com.ibm.watson.developer_cloud.conversation.v1.model.InputData; import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest; import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse; import com.ibm.watson.developer_cloud.http.HttpHeaders; @@ -66,7 +66,8 @@ public class ProxyResourceTest { /** * Sets the up. * - * @throws Exception the exception + * @throws Exception + * the exception */ // @Override @Before @@ -78,7 +79,8 @@ public void setUp() throws Exception { /** * Tear down. * - * @throws IOException Signals that an I/O exception has occurred. + * @throws IOException + * Signals that an I/O exception has occurred. */ @After public void tearDown() throws IOException { @@ -88,8 +90,10 @@ public void tearDown() throws IOException { /** * Test send message. * - * @throws IOException Signals that an I/O exception has occurred. - * @throws InterruptedException the 4interrupted exception + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws InterruptedException + * the 4interrupted exception */ @Test public void testSendMessage() throws IOException, InterruptedException { @@ -103,7 +107,10 @@ public void testSendMessage() throws IOException, InterruptedException { server.enqueue(jsonResponse(mockResponse)); - MessageRequest request = new MessageRequest.Builder().inputText(text).build(); + InputData input = new InputData.Builder(text).build(); + MessageRequest request = new MessageRequest(); + request.setInput(input); + String payload = GsonSingleton.getGsonWithoutPrettyPrinting().toJson(request, MessageRequest.class); InputStream inputStream = new ByteArrayInputStream(payload.getBytes("UTF-8")); @@ -113,13 +120,12 @@ public void testSendMessage() throws IOException, InterruptedException { .fromJson(jaxResponse.getEntity().toString(), MessageResponse.class); RecordedRequest mockRequest = server.takeRequest(); - List serviceText = serviceResponse.getText(); - List mockText = serviceResponse.getText(); + List serviceText = serviceResponse.getOutput().getText(); + List mockText = serviceResponse.getOutput().getText(); assertNotNull(serviceText); assertNotNull(mockText); assertTrue(serviceText.containsAll(mockText) && mockText.containsAll(serviceText)); assertEquals(serviceResponse, mockResponse); - assertEquals(serviceResponse.getTextConcatenated(" "), mockResponse.getTextConcatenated(" ")); assertEquals(mockRequest.getMethod(), "POST"); assertNotNull(mockRequest.getHeader(HttpHeaders.AUTHORIZATION)); @@ -128,11 +134,15 @@ public void testSendMessage() throws IOException, InterruptedException { /** * Load fixture. * - * @param the generic type - * @param filename the filename - * @param returnType the return type + * @param + * the generic type + * @param filename + * the filename + * @param returnType + * the return type * @return the t - * @throws FileNotFoundException the file not found exception + * @throws FileNotFoundException + * the file not found exception */ public static T loadFixture(String filename, Class returnType) throws FileNotFoundException { String jsonString = getStringFromInputStream(new FileInputStream(filename)); @@ -142,7 +152,8 @@ public static T loadFixture(String filename, Class returnType) throws Fil /** * Gets the string from input stream. * - * @param is the is + * @param is + * the is * @return the string from input stream */ public static String getStringFromInputStream(InputStream is) { @@ -172,9 +183,11 @@ public static String getStringFromInputStream(InputStream is) { } /** - * Create a MockResponse with JSON content type and the object serialized to JSON as body. + * Create a MockResponse with JSON content type and the object serialized to + * JSON as body. * - * @param body the body + * @param body + * the body * @return the mock response */ protected static MockResponse jsonResponse(Object body) { diff --git a/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResourceTest.java b/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResourceTest.java index e1d65f7..ae946e0 100644 --- a/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResourceTest.java +++ b/src/test/java/com/ibm/watson/apis/conversation_with_discovery/rest/SetupResourceTest.java @@ -60,7 +60,8 @@ public void setUp() { /** * Should return ready state. * - * @throws Exception the exception + * @throws Exception + * the exception */ @Test public void shouldReturnReadyState() throws Exception { @@ -86,7 +87,8 @@ public void shouldReturnReadyState() throws Exception { /** * Should return not ready state. * - * @throws Exception the exception + * @throws Exception + * the exception */ @Test public void shouldReturnNotReadyState() throws Exception { @@ -111,7 +113,8 @@ public void shouldReturnNotReadyState() throws Exception { /** * Should return error state. * - * @throws Exception the exception + * @throws Exception + * the exception */ @Test public void shouldReturnErrorState() throws Exception { @@ -137,7 +140,8 @@ public void shouldReturnErrorState() throws Exception { /** * Should return error workspace id. * - * @throws Exception the exception + * @throws Exception + * the exception */ @Test public void shouldReturnErrorWorkspaceId() throws Exception {