Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Assistant

Note about Assistant v2

This service is currently in private beta and requires access to use. To learn more and find out how to get access, please read here.

Installation

Maven
<dependency>
  <groupId>com.ibm.watson.developer_cloud</groupId>
  <artifactId>assistant</artifactId>
  <version>6.11.0</version>
</dependency>
Gradle
'com.ibm.watson.developer_cloud:assistant:6.11.0'

Usage

Use the Assistant service to identify intents, entities, and conduct conversations.

Using Assistant v1

// make sure to use the Assistant v1 import!
import com.ibm.watson.developer_cloud.assistant.v1.Assistant;

Assistant service = new Assistant("2018-02-16");
service.setUsernameAndPassword("<username>", "<password>");

InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId)
  .input(input)
  .build();
MessageResponse response = service.message(options).execute();
System.out.println(response);

Maintaining context across messages:

// make sure to use the Assistant v1 import!
import com.ibm.watson.developer_cloud.assistant.v1.Assistant;

Context context = null;

// first message
MessageOptions newMessageOptions = new MessageOptions.Builder()
  .workspaceId("<workspace-id>")
  .input(new InputData.Builder("First message").build())
  .context(context)
  .build();

MessageResponse response = service.message(newMessageOptions).execute();
context = response.getContext();

// second message
newMessageOptions = new MessageOptions.Builder()
  .workspaceId("<workspace-id>")
  .input(new InputData.Builder("Second message").build())
  .context(context) // using context from the first message
  .build();

response = service.message(newMessageOptions).execute();

System.out.println(response);

Using Assistant v2

// make sure to use the Assistant v2 import!
import com.ibm.watson.developer_cloud.assistant.v2.Assistant;

Assistant service = new Assistant("2018-09-20");
service.setUsernameAndPassword("<username>", "<password>");

MessageInput input = new MessageInput.Builder()
  .text("Hi")
  .build();
MessageOptions messageOptions = new MessageOptions.Builder()
  .assistantId("<assistant_id>")
  .sessionId("<session_id>")
  .input(input)
  .build();
MessageResponse messageResponse = service.message(messageOptions).execute();

System.out.println(messageResponse);

Maintaining context across messages:

// make sure to use the Assistant v2 import!
import com.ibm.watson.developer_cloud.assistant.v2.Assistant;

MessageContext context = new MessageContext();

// first message
MessageInput input = new MessageInput.Builder()
  .text("First message")
  .build();
MessageOptions messageOptions = new MessageOptions.Builder()
  .assistantId(assistantId)
  .sessionId(sessionId)
  .input(input)
  .context(context)
  .build();

MessageResponse messageResponse = service.message(messageOptions).execute();
context = messageResponse.getContext();

// second message
input = new MessageInput.Builder()
  .text("Second message")
  .build();
messageOptions = new MessageOptions.Builder()
  .assistantId(assistantId)
  .sessionId(sessionId)
  .input(input)
  .context(context) // using context from first message
  .build();

messageResponse = service.message(messageOptions).execute();

System.out.println(messageResponse);