Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DialogFlow tests and update to canonical sample format. #1210

Merged
merged 4 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 4 additions & 201 deletions dialogflow/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ To get started, [download][maven-download] and [install][maven-install] it.
variable:

```bash
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
```
* To run tests, set GOOGLE_CLOUD_PROJECT to your PROJECT_ID:
```
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
```
```
mvn clean verify
```
* Set PROJECT_ID in pom.xml to your Google Cloud Project Id.
* Set SESSION_ID in pom.xml to a session name of your choice. (Defaults to SESSION_ID)
* Set CONTEXT_ID in pom.xml to a context name of your choice. (Defaults to CONTEXT_ID)
Expand All @@ -49,201 +43,10 @@ To get started, [download][maven-download] and [install][maven-install] it.
[dialogflow-api]: https://console.cloud.google.com/apis/library/dialogflow.googleapis.com
[auth]: https://cloud.google.com/docs/authentication/getting-started

## Run the sample
## Run the Tests

To build the sample, we use Maven.
To verify the API's are enabled, run the unit tests via
```bash
mvn clean package
```

### Set up the Agent
Import the sample agent (RoomReservation.zip) from the resources directory to your
[Dialogflow Project][dialogflow-import] using the following guide:
[Versioning with Import/Export][import-export-versioning]

[dialogflow-import]: https://console.dialogflow.com/api-client/#/editAgent/
[import-export-versioning]: https://dialogflow.com/docs/best-practices/import-export-for-versions

## Samples

### Detect Intent Texts
DialogFlow API Detect Intent sample with text inputs.

```
mvn exec:java -DDetectIntentText
```

### Detect Intent Audio
DialogFlow API Detect Intent sample with audio files. Returns the result of detect intent with an
audio file as input.

Note: Execute the following commands in order to yield reasonable outputs.
```
mvn exec:java -DDetectIntentAudioBookARoom
```
```
mvn exec:java -DDetectIntentAudioMountainView
```
```
mvn exec:java -DDetectIntentAudioToday
```
```
mvn exec:java -DDetectIntentAudio230PM
```
```
mvn exec:java -DDetectIntentAudioHalfAnHour
```
```
mvn exec:java -DDetectIntentAudioTwoPeople
```

### Detect Intent Stream
DialogFlow API Detect Intent sample with audio files processes as an audio stream.

```
mvn exec:java -DDetectIntentStreamBookARoom
```
```
mvn exec:java -DDetectIntentStreamMountainView
```

### Detect Intent With Model Selection
DialogFlow API Detect Intent sample with model selection
```
mvn exec:java -DDetectIntentWithModelSelection -Dexec.args='--projectId PROJECT_ID --audioFilePath resources/book_a_room.wav --sessionId SESSION_ID'
```

### Detect Intent With Sentiment Analysis
DialogFlow API Detect Intent sample with sentiment analysis
```
mvn exec:java -DDetectIntentWithSentimentAnalysis -Dexec.args="--projectId PROJECT_ID -sessionId SESSION_ID 'hello'"
```

### Detect Intent With Text-to-Speech
DialogFlow API Detect Intent sample with Text-to-Speech
```
mvn exec:java -DetectIntentTTSResponse
```

### Detect Intent Knowledge
DialogFlow API Detect Intent sample with querying knowledge connector.
```
mvn exec:java -DDetectIntentKnowledge -Dexec.args="--projectId PROJECT_ID --knowledgeBaseId KNOWLEDGE_BASE_ID -sessionId SESSION_ID 'Where can I find pricing information?'"
```

### Context Management
DialogFlow API Context sample.

Lists contexts
```
mvn exec:java -DContextManagementList
```
Create an entity type
```
mvn exec:java -DContextManagementCreate
```
Delete entity type
```
mvn exec:java -DContextManagementDelete
```

### Entity Management
DialogFlow API Entity sample.

List entities
```
mvn exec:java -DEntityManagementList
```
Create an entity
```
mvn exec:java -DEntityManagementCreate
```
Delete entity
```
mvn exec:java -DEntityManagementDelete
```

### Entity Type Management
DialogFlow API EntityType sample.

List entity types
```
mvn exec:java -DEntityTypeManagementList
```
Create an entity type
```
mvn exec:java -DEntityTypeManagementCreate
```
Delete entity type
```
mvn exec:java -DEntityTypeManagementDelete
```

### Intent Management
DialogFlow API Intent sample.

List intents
```
mvn exec:java -DIntentManagementList
```
Create an intent
```
mvn exec:java -DIntentManagementCreate
```
Delete intent
```
mvn exec:java -DIntentManagementDelete
```

### Session Entity Type Management
DialogFlow API SessionEntityType sample.

List session entity types
```
mvn exec:java -DSessionEntityTypeManagementList
```
Create session entity type
```
mvn exec:java -DSessionEntityTypeManagementCreate
```
Delete session entity type
```
mvn exec:java -DSessionEntityTypeManagementDelete
```

### Knowledge Base Management
DialogFlow API KnowledgeBaseManagement sample

List knowledge base
```
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='list --projectId PROJECT_ID'
```
Create knowledge base
```
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='create DISPLAY_NAME --projectId PROJECT_ID'
```
Delete knowledge base
```
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='delete KNOWLEDGE_BASE_ID --projectId PROJECT_ID'
```

### Document Management
DialogFlow API DocumentManagement sample

List documents
```
mvn exec:java -DDocumentManagement -Dexec.args='list --projectId PROJECT_ID --knowledgeBaseId KNOWLEDGE_BASE_ID'
```
Create doucment
```
mvn exec:java -DDocumentManagement -Dexec.args='create KNOWLEDGE_BASE_ID --projectId PROJECT_ID
--displayName DISPLAY_NAME
--mimeType text/html
--knowledgeType FAQ'
--contentUri https://cloud.google.com/storage/docs/faq'
```
Delete doucment
```
mvn exec:java -DDocumentManagement -Dexec.args='delete KNOWLEDGE_BASE_ID --projectId PROJECT_ID --documentId DOCUMENT_ID
mvn clean verify
```
5 changes: 0 additions & 5 deletions dialogflow/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
<artifactId>google-cloud-dialogflow</artifactId>
<version>0.55.1-alpha</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
<version>0.8.1</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,38 @@
package com.example.dialogflow;

// Imports the Google Cloud client library

import com.google.cloud.dialogflow.v2.Context;
import com.google.cloud.dialogflow.v2.ContextName;
import com.google.cloud.dialogflow.v2.ContextsClient;
import com.google.cloud.dialogflow.v2.SessionName;
import com.google.common.collect.Lists;
import com.google.protobuf.Value;

import java.util.List;
import java.util.Map.Entry;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;


/**
* DialogFlow API Context sample.
*/
public class ContextManagement {

// [START dialogflow_list_contexts]

/**
* Lists contexts
*
* @param sessionId Identifier of the DetectIntent session.
* @param projectId Project/Agent Id.
* @return List of Contexts found.
*/
public static void listContexts(String sessionId, String projectId) throws Exception {
public static List<Context> listContexts(String sessionId, String projectId) throws Exception {
List<Context> contexts = Lists.newArrayList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we are aiming for the new format, the context tags need to be moved inside of the function and examples of the arguments need to be provided in comments to give the user additional context:

public static List<Context> listContexts(String sessionId, String projectId) throws Exception {
// [START dialogflow_list_contexts]
// String sessionId = "my-session-id"
// String projectId = "my-project-id"

List<Context> contexts = Lists.newArrayList();


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your take on the return statement being inside vs outside the tags?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say leave the return outside.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that sounds reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the perspective of having these regions be copy-pastable by developers, though, I would think the function tag and return statement would be useful so that they could just plop the function into their code directly. They would basically have to write a function wrapper around the code anyway regardless

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying, but I think the presumption is that they can copy paste the code into an existing code and get it working. Even if they copy the function as a whole they still have to create a class around it, so I don't think including the function signature really make's a significant impact.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey every keystroke counts :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we should make sure this gets decided in the new rubric. (To have a solid answer going forward)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'd prefer to leave it as-is for now though for consistency.

// Instantiates a client
try (ContextsClient contextsClient = ContextsClient.create()) {
// Set the session name using the sessionId (UUID) and projectId (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);

// Performs the list contexts request
System.out.format("Contexts for session %s:\n", session.toString());
for (Context context : contextsClient.listContexts(session).iterateAll()) {
System.out.format("Context name: %s\n", context.getName());
System.out.format("Lifespan Count: %d\n", context.getLifespanCount());
Expand All @@ -61,20 +58,29 @@ public static void listContexts(String sessionId, String projectId) throws Excep
System.out.format("\t%s: %s\n", entry.getKey(), entry.getValue());
}
}

contexts.add(context);
}
}
return contexts;
}
// [END dialogflow_list_contexts]

// [START dialogflow_create_context]

/**
* Create an entity type with the given display name
* @param contextId The Id of the context.
* @param sessionId Identifier of the DetectIntent session.
*
* @param contextId The Id of the context.
* @param sessionId Identifier of the DetectIntent session.
* @param lifespanCount The lifespan count of the context.
* @param projectId Project/Agent Id.
* @param projectId Project/Agent Id.
* @return The new Context.
*/
public static void createContext(String contextId, String sessionId, String projectId,
public static Context createContext(
String contextId,
String sessionId,
String projectId,
int lifespanCount) throws Exception {
// Instantiates a client
try (ContextsClient contextsClient = ContextsClient.create()) {
Expand All @@ -97,13 +103,17 @@ public static void createContext(String contextId, String sessionId, String proj
// Performs the create context request
Context response = contextsClient.createContext(session, context);
System.out.format("Context created: %s\n", response);

return response;
}
}
// [END dialogflow_create_context]

// [START dialogflow_delete_context]

/**
* Delete entity type with the given entity type name
*
* @param contextId The Id of the context.
* @param sessionId Identifier of the DetectIntent session.
* @param projectId Project/Agent Id.
Expand All @@ -119,59 +129,4 @@ public static void deleteContext(String contextId, String sessionId, String proj
}
}
// [END dialogflow_delete_context]

public static void main(String[] args) throws Exception {
ArgumentParser parser =
ArgumentParsers.newFor("ContextManagement")
.build()
.defaultHelp(true)
.description("Create / List / Delete a context.");

Subparsers subparsers = parser.addSubparsers().dest("command").title("Commands");

Subparser listParser = subparsers.addParser("list")
.help("mvn exec:java -DContextManagement -Dexec.args='list --sessionId SESSION_ID "
+ "--projectId PROJECT_ID'");
listParser.addArgument("--sessionId")
.help("Identifier of the DetectIntent session").required(true);
listParser.addArgument("--projectId").help("Project/Agent Id").required(true);

Subparser createParser = subparsers.addParser("create")
.help("mvn exec:java -DContextManagement -Dexec.args='create --sessionId SESSION_ID "
+ "--projectId PROJECT_ID --contextId CONTEXT_ID'");
createParser.addArgument("--sessionId")
.help("Identifier of the DetectIntent session").required(true);
createParser.addArgument("--projectId").help("Project/Agent Id").required(true);
createParser.addArgument("--contextId")
.help("The Id of the context")
.required(true);
createParser.addArgument("--lifespanCount")
.help("The lifespan count of the context (Default: 1)").setDefault(1);

Subparser deleteParser = subparsers.addParser("delete")
.help("mvn exec:java -DContextManagement -Dexec.args='delete --sessionId SESSION_ID "
+ "--projectId PROJECT_ID --contextId CONTEXT_ID'");
deleteParser.addArgument("--sessionId")
.help("Identifier of the DetectIntent session").required(true);
deleteParser.addArgument("--projectId").help("Project/Agent Id").required(true);
deleteParser.addArgument("--contextId")
.help("The Id of the context")
.required(true);

try {
Namespace namespace = parser.parseArgs(args);

if (namespace.get("command").equals("list")) {
listContexts(namespace.get("sessionId"), namespace.get("projectId"));
} else if (namespace.get("command").equals("create")) {
createContext(namespace.get("contextId"), namespace.get("sessionId"),
namespace.get("projectId"), namespace.get("lifespanCount"));
} else if (namespace.get("command").equals("delete")) {
deleteContext(namespace.get("contextId"), namespace.get("sessionId"),
namespace.get("projectId"));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
Loading