Skip to content

Commit

Permalink
Updated Watson SDK from 3.8.0 to 4.2.1 for Conversation and Discovery (
Browse files Browse the repository at this point in the history
…watson-developer-cloud#118)

* Updated watson conversation sdk to 4.2.1

* Updated discovery to SDK 4.2.1

* fixed a minor typo

* Minor refactoring and formating
  • Loading branch information
Steve Green authored and germanattanasio committed Feb 14, 2018
1 parent 244a994 commit 2721b6f
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 177 deletions.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<div style="text-align:center;"><img src='readme_images/discovery_collection.png'></div><br>

- After you're done, there should be a new private collection in the UI
<div style="text-align:center;"><img src='readme_images/ford_collection.png'></div>

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

<img src="readme_images/switch_config.png" width="550"></img>

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

<a name="credentials">
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,32 +41,40 @@ public class DiscoveryClient {
private static final int SNIPPET_LENGTH = 150;

private Pattern pattern = Pattern.compile("((.+?)</p>){1,2}");

/**
* This method uses the Query object to send the user's query (the <code>input</code> param) to the discovery service.
* This method uses the Query object to send the user's query (the
* <code>input</code> 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<DocumentPayload> getDocuments(String input) throws Exception {
DiscoveryQuery discoveryQuery = new DiscoveryQuery();
QueryResponse output = discoveryQuery.query(input);
List<Map<String, Object>> results = output.getResults();
String jsonRes = new Gson().toJson(results);
List<QueryResult> 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<DocumentPayload> createPayload(JsonElement resultsElement) {
logger.info(Messages.getString("Service.CREATING_DISCOVERY_PAYLOAD"));
Expand All @@ -75,13 +84,13 @@ private List<DocumentPayload> 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.
Expand All @@ -104,6 +113,7 @@ private List<DocumentPayload> createPayload(JsonElement resultsElement) {
} else {
documentPayload.setConfidence("0.0");
}
logger.info("documentPayloada: " + documentPayload);
payload.add(i, documentPayload);
}
} else {
Expand All @@ -122,7 +132,8 @@ private List<DocumentPayload> createPayload(JsonElement resultsElement) {
/**
* get first <code>SNIPPET_LENGTH</code> characters of body response.
*
* @param body discovery response
* @param body
* discovery response
* @return
*/
private String getSniplet(String body) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, ",");
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>addServletContextListener<code> 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 <code>addServletContextListener<code> method. When the
* servletContext event occurs, that object's appropriate method is invoked.
*
* @see ServletContextEvent
*/
Expand All @@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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$
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand All @@ -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"));
Expand Down
Loading

0 comments on commit 2721b6f

Please sign in to comment.