लागू करने से जुड़ी सलाह (Dialogflow)

अपनी सेट की गई कार्रवाई में बातचीत डिज़ाइन करने के अच्छे तरीके लागू करने के लिए, ये सलाह देखें.

अलग-अलग वर्शन हो सकते हैं

इसे Dialogflow में "उपयोगकर्ता का कहना है" इनपुट में मैनेज करें. साथ ही, एक से ज़्यादा इंटेंट इस्तेमाल करें, जो एक ही कार्रवाई को मैप कर सकें. यहां हर इंटेंट को "उपयोगकर्ता का कहना है" वाक्यांशों के अलग-अलग सेट से ट्रिगर किया जा सकता है.

मददगार सुझाव दें और ग्रेसफ़ुली फ़ेल करें

कभी-कभी आपकी सेट की गई कार्रवाई को कार्रवाई में शामिल नहीं किया जा सकता. इसकी वजह यह हो सकती है कि उसे कोई इनपुट न मिले या किसी उपयोगकर्ता के इनपुट को समझ न आए. इस वजह से, उसे नो-मैच कहा जाता है. ऐसा होने पर, Assistant सबसे पहले यह पता करने की कोशिश करती है कि उपयोगकर्ता कोई दूसरी कार्रवाई ट्रिगर करना चाहता है या नहीं. अगर Assistant, उपयोगकर्ता के इनपुट का मिलान किसी दूसरी कार्रवाई से नहीं करती है, तो उपयोगकर्ता आपकी सेट की गई कार्रवाई के हिसाब से कार्रवाई जारी रखता है. यह स्थिति किसी भी समय हो सकती है. इसलिए, सबसे अच्छा तरीका यह है कि बातचीत में बिना इनपुट और मैच न होने की स्थितियों को, अलग तरीके से हैंडल किया जाए. फ़ॉलबैक का इस्तेमाल करके, उपयोगकर्ताओं को ट्रैक पर वापस लाने में मदद की जा सकती है.

ऐसा करने के लिए, अपने conv.data ऑब्जेक्ट में fallbackCount वैरिएबल शुरू करें और 0 पर सेट करें. दो फ़ॉलबैक प्रॉम्प्ट का कलेक्शन तैयार करें (साफ़ तौर पर आगे बढ़ते हुए) और बातचीत खत्म करने वाला फ़ाइनल फ़ॉलबैक प्रॉम्प्ट तैयार करें.

इसके बाद, फ़ॉलबैक इंटेंट बनाएं (एजेंट में हर कार्रवाई करने लायक इंटेंट के लिए आम तौर पर एक). इंटेंट हैंडलर में, conv.data ऑब्जेक्ट से फ़ॉलबैक की गिनती करें और इसे बढ़ाएं. साथ ही, अगर यह तीन से कम है, तो 3 के अरे से प्रॉम्प्ट को खींचें. अगर संख्या 4 या उससे ज़्यादा है, तो आखिरी प्रॉम्प्ट का इस्तेमाल करके बातचीत बंद करें. उन सभी इंटेंट के लिए जो फ़ॉलबैक नहीं हैं, फ़ॉलबैक की गिनती को 0 पर रीसेट करें. आम तौर पर, खास इंटेंट के लिए फ़ॉलबैक का टेंप्लेट बनाएं.

Node.js

const GENERAL_FALLBACK = [
   'Sorry, what was that?',
   'I didn\'t quite get that. I can help you find good local restaurants, what do you want to know about?',
];

const LIST_FALLBACK = [
   'Sorry, what was that?',
   'I didn\'t catch that. Could you tell me which one you prefer?',
];

const FINAL_FALLBACK = 'I\'m sorry I\'m having trouble here. Let\'s talk again later.';

const handleFallback = (conv, promptFetch, callback) => {
 conv.data.fallbackCount = parseInt(conv.data.fallbackCount, 10);
 conv.data.fallbackCount++;
 if (conv.data.fallbackCount > 2) {
   conv.close(promptFetch.getFinalFallbackPrompt());
 } else {
   callback();
 }
}
// Intent handlers below
const generalFallback = (conv) => {
  handleFallback = (conv, promptFetch, () => {
    conv.ask(GENERAL_FALLBACK[conv.data.fallbackCount],
      getGeneralNoInputPrompts());
 });
}

const listFallback = (conv) => {
  handleFallback = (conv, promptFetch, () => {
   conv.ask(LIST_FALLBACK[conv.data.fallbackCount],
       getGeneralNoInputPrompts());
 });
}

const nonFallback = (conv) => {
  conv.data.fallbackCount = 0;
  conv.ask('A non-fallback message here');
}

Java

private static final List<String> GENERAL_FALLBACK =
    Arrays.asList(
        "Sorry, what was that?",
        "I didn\'t quite get that. I can tell you all about IO, like date or location, or about the sessions. What do you want to know about?");
private static final List<String> LIST_FALLBACK =
    Arrays.asList(
        "Sorry, what was that?",
        "I didn\'t catch that. Could you tell me which one you liked?");
private static final List<String> FINAL_FALLBACK =
    Arrays.asList("I\'m sorry I\'m having trouble here. Maybe we should try this again later.");

@ForIntent("General Fallback")
public ActionResponse generalFallback(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  int fallbackCount = (Integer) request.getConversationData().get("fallbackCount");
  fallbackCount++;
  request.getConversationData().put("fallbackCount", fallbackCount);
  if (fallbackCount > 2) {
    responseBuilder.add(getRandomPromptFromList(FINAL_FALLBACK)).endConversation();
  } else {
    responseBuilder.add(getRandomPromptFromList(GENERAL_FALLBACK));
  }
  return responseBuilder.build();
}

private String getRandomPromptFromList(List<String> prompts) {
  Random rand = new Random();
  int i = rand.nextInt(prompts.size());
  return prompts.get(i);
}

@ForIntent("List Fallback")
public ActionResponse listFallback(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  int fallbackCount = (Integer) request.getConversationData().get("fallbackCount");
  fallbackCount++;
  request.getConversationData().put("fallbackCount", fallbackCount);
  if (fallbackCount > 2) {
    responseBuilder.add(getRandomPromptFromList(FINAL_FALLBACK)).endConversation();
  } else {
    responseBuilder.add(getRandomPromptFromList(LIST_FALLBACK));
  }
  return responseBuilder.build();
}

@ForIntent("Non Fallback")
public ActionResponse nonFallback(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  request.getConversationData().put("fallbackCount", 0);
  responseBuilder.add("Non Fallback message");
  return responseBuilder.build();
}

किसी भी समय मदद करने के लिए तैयार रहें

कुछ ऐसा इंटेंट बनाएं जो मददगार वाक्यांशों को सुनकर ध्यान दे. जैसे, "मैं क्या कर सकता/सकती हूं?", "तुम मुझे क्या बता सकती हो" या "मदद करो". इस इंटेंट में, कुछ (बारी-बारी से) रिस्पॉन्स ऑफ़र करें, जिससे यह पता चले कि एजेंट क्या कर सकता है और उपयोगकर्ताओं को संभावित कार्रवाई की ओर भेजता है. आम तौर पर, Dialogflow में फ़ॉलो-अप सहायता इंटेंट का इस्तेमाल करें, ताकि अलग-अलग कार्रवाई करने लायक इंटेंट के लिए अलग-अलग मदद बनाई जा सके.

Node.js

const HELP_PROMPTS = [
   'There\'s a lot you might want to know about the local restaurants, and I can tell you all about it, like where it is and what kind of food they have. What do you want to know?',
   'I\'m here to help, so let me know if you need any help figuring out where or what to eat. What do you want to know?',
];

// Intent handler
const help = (conv) => {
 reply(conv, promptFetch.getHelpPrompt(), // fetches random entry from HELP_PROMPTS
     promptFetch.getGeneralNoInputPrompts());
}

Java

private static final List<String> HELP_PROMPTS =
    Arrays.asList(
        "There's a lot you might want to know about IO, and I can tell you all about it, like where it is and what the sessions are. What do you want to know?",
        "IO can be a little overwhelming, so I\'m here to help. Let me know if you need any help figuring out the event, like when it is, or what the sessions are. What do you want to know?");

@ForIntent("Help")
public ActionResponse help(ActionRequest request) {
  return getResponseBuilder(request).add(getRandomPromptFromList(HELP_PROMPTS)).build();
}

उपयोगकर्ताओं को जानकारी फिर से चलाने की अनुमति दें

app.ask(output) के अपने सभी तरीकों को प्रॉक्सी फ़ंक्शन के साथ रैप करें. ऐसा करने से, आउटपुट को conv.data.lastPrompt में जोड़ा जा सकता है. एक दोहराने वाला इंटेंट बनाएं जो उपयोगकर्ता की ओर से दोहराए जाने के संकेतों पर ध्यान देता है, जैसे "क्या?", "फिर से कहो" या "क्या तुम इसे दोहरा सकते हो?". दोहराए जाने वाले प्रीफ़िक्स का ऐसा कलेक्शन बनाएं जिसका इस्तेमाल यह स्वीकार करने के लिए किया जा सके कि उपयोगकर्ता ने किसी चीज़ को दोहराने के लिए कहा है. रिपीट इंटेंट हैंडलर में ask() को, दोहराए जाने वाले प्रीफ़िक्स की स्ट्रिंग और conv.data.lastPrompt वैल्यू के साथ कॉल करें. ध्यान रखें कि अगर अंतिम प्रॉम्प्ट में इस्तेमाल किया गया है, तो आपको एसएसएमएल के किसी भी ओपनिंग टैग को शिफ़्ट करना होगा.

Node.js

const REPEAT_PREFIX = [
    'Sorry, I said ',
    'Let me repeat that. ',
];

const reply = (conv, inputPrompt, noInputPrompts) => {
  conv.data.lastPrompt = inputPrompt;
  conv.data.lastNoInputPrompts = noInputPrompts;
  conv.ask(inputPrompt, noInputPrompts);
}
// Intent handlers
const normalIntent = (conv) => {
  reply(conv, 'Hey this is a question', SOME_NO_INPUT_PROMPTS);
}

const repeat = (conv) => {
  let repeatPrefix = promptFetch.getRepeatPrefix(); // randomly chooses from REPEAT_PREFIX
  // Move SSML start tags over
  if (conv.data.lastPrompt.startsWith(promptFetch.getSSMLPrefix())) {
    conv.data.lastPrompt =
        conv.data.lastPrompt.slice(promptFetch.getSSMLPrefix().length);
    repeatPrefix = promptFetch.getSSMLPrefix() + repeatPrefix;
  }
  conv.ask(repeatPrefix + conv.data.lastPrompt,
      conv.data.lastNoInputPrompts);
}

Java

private final List<String> REPEAT_PREFIX = Arrays.asList("Sorry, I said ", "Let me repeat that.");

private final String SsmlPrefix = "<speak>";

@ForIntent("Normal Intent")
public ActionResponse normalIntent(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  responseBuilder.getConversationData().put("lastPrompt", "Hey this is a question");
  return responseBuilder.build();
}

@ForIntent("repeat")
public ActionResponse repeat(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  String repeatPrefix = getRandomPromptFromList(REPEAT_PREFIX);
  // Move SSML start tags over
  String lastPrompt = (String) responseBuilder.getConversationData().get("lastPrompt");
  if (lastPrompt.startsWith(SsmlPrefix)) {
    String newLastPrompt = lastPrompt.substring(SsmlPrefix.length());
    responseBuilder.getConversationData().put("lastPrompt", newLastPrompt);
    repeatPrefix = SsmlPrefix + repeatPrefix;
  }
  responseBuilder.add(repeatPrefix + lastPrompt);
  return responseBuilder.build();
}

उपयोगकर्ताओं की पसंद के हिसाब से बातचीत करें

आपकी सेट की गई कार्रवाई, उपयोगकर्ताओं से उनकी पसंद के बारे में पूछ सकती है और बाद में इस्तेमाल करने के लिए उन्हें याद रख सकती है. इससे, आने वाले समय में उस उपयोगकर्ता के साथ होने वाली बातचीत को आपके हिसाब से बनाने में मदद मिलती है.

कार्रवाई का यह उदाहरण, उपयोगकर्ताओं को पिन कोड के लिए मौसम की रिपोर्ट देता है. नीचे दिए गए उदाहरण कोड में, उपयोगकर्ता से पूछा गया है कि क्या कार्रवाई में, बाद में की जाने वाली बातचीत के लिए वह अपना पिन कोड याद रखे.

Node.js

app.intent('weather_report', (conv) => {
  let zip = conv.arguments.get('zipcode');
  conv.data.zip = zip;
  conv.ask(getWeatherReport(zip));
  conv.ask(new Confirmation(`Should I remember ${zip} for next time?`));
});

app.intent('remember_zip', (conv, params, confirmation) => {
  if (confirmation) {
    conv.user.storage.zip = conv.data.zip;
    conv.close('Great! See you next time.');
  } else conv.close('Ok, no problem.');
});

Java

@ForIntent("weather_report")
public ActionResponse weatherReport(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  String zip = (String) request.getArgument("location").getStructuredValue().get("zipCode");
  responseBuilder.getConversationData().put("zip", zip);
  responseBuilder.add(getWeatherReport(zip));
  responseBuilder.add(
      new Confirmation().setConfirmationText("Should I remember " + zip + " for next time?"));
  return responseBuilder.build();
}

@ForIntent("remember_zip")
public ActionResponse rememberZip(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  if (request.getUserConfirmation()) {
    responseBuilder.getUserStorage().put("zip", responseBuilder.getConversationData().get("zip"));
    responseBuilder.add("Great! See you next time.").endConversation();
  } else {
    responseBuilder.add("Ok, no problem.").endConversation();
  }
  return responseBuilder.build();
}

पहली बार डायलॉग के दौरान, उपयोगकर्ता से यह पूछने के बाद कि वह किस पिन कोड का इस्तेमाल करेगा, आपके पास अगले सेशन में उस पिन कोड को छोड़कर, उसी पिन कोड का इस्तेमाल करने का विकल्प होता है. आपको अब भी एस्केप रूट देना चाहिए (जैसे कि सुझाव देने वाला चिप, जिससे वे कोई दूसरा पिन कोड चुन सकते हैं), लेकिन आम मामले में बातचीत का मोड़ कम करके, लोगों को बेहतर अनुभव मिलता है.

Node.js

app.intent('weather_report', (conv) => {
  let zip = conv.arguments.get('zipcode');
  if (zip) {
    conv.close(getWeatherReport(zip));
  } else if (conv.user.storage.zip) {
    conv.ask(new SimpleResponse(getWeatherReport(conv.user.storage.zip)));
    conv.ask(new Suggestions('Try another zipcode'));
  } else {
    conv.ask('What\'s your zip code?');
  }
});

app.intent('provide_zip_df', (conv) => {
  conv.user.storage.zip = conv.arguments.get('zipcode');
  conv.close(getWeatherReport(conv.user.storage.zip));
});

Java

public ActionResponse weatherReport2(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  String zip = (String) request.getArgument("location").getStructuredValue().get("zipCode");
  if (zip != null) {
    responseBuilder.add(getWeatherReport(zip)).endConversation();
  } else if ((zip = (String) responseBuilder.getUserStorage().get("zip")) != null) {
    responseBuilder.add(new SimpleResponse().setTextToSpeech(getWeatherReport(zip)));
    responseBuilder.add(new Suggestion().setTitle("Try another zipcode"));
  } else {
    responseBuilder.add("What's your zip code?");
  }
  return responseBuilder.build();
}

लौटने वाले उपयोगकर्ताओं के लिए पसंद के मुताबिक बनाएं

बातचीत के बीच कुछ तालमेल बनाए रखने से, लौटने वाले उपयोगकर्ताओं को ज़्यादा स्वाभाविक अनुभव मिलता है. इस अनुभव को तैयार करने का पहला कदम है, वापस आने वाले उपयोगकर्ताओं का एक अलग तरह से स्वागत करना. जैसे, आप अभिवादन मैसेज पर टैप कर सकते हैं या पिछली बातचीत के आधार पर काम की जानकारी दिखा सकते हैं. ऐसा करने के लिए, AppRequest.User lastSeen प्रॉपर्टी का इस्तेमाल करके पता लगाएं कि उपयोगकर्ता ने आपकी सेट की गई कार्रवाई से पहले इंटरैक्ट किया है या नहीं. अगर lastSeen प्रॉपर्टी को अनुरोध पेलोड में शामिल किया गया है, तो आपके पास सामान्य से अलग ग्रीटिंग मैसेज का इस्तेमाल करने का विकल्प है.

यहां दिया गया कोड, Node.js क्लाइंट लाइब्रेरी का इस्तेमाल करके, last.seen की वैल्यू फ़ेच करता है.

Node.js

// This function is used to handle the welcome intent
// In Dialogflow, the Default Welcome Intent ('input.welcome' action)
// In Actions SDK, the 'actions.intent.MAIN' intent
const welcome = (conv) => {
  if (conv.user.last.seen) {
    conv.ask(`Hey you're back...`);
  } else {
    conv.ask('Welcome to World Cities Trivia!...');
  }
}

Java

// This function is used to handle the welcome intent
// In Dialogflow, the Default Welcome Intent ('input.welcome' action)
// In Actions SDK, the 'actions.intent.MAIN' intent
public ActionResponse welcome(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  if (request.getUser().getLastSeen() != null) {
    responseBuilder.add("Hey you're back...");
  } else {
    responseBuilder.add("Welcome to Number Genie!...");
  }
  return responseBuilder.build();
}

इस वेलकम मैसेज को बेहतर बनाने के लिए, जवाब को lastSeen की असल वैल्यू के हिसाब से बदला जा सकता है. उदाहरण के लिए, जिन उपयोगकर्ताओं का आखिरी इंटरैक्शन मौजूदा इंटरैक्शन से कई महीने पहले हुआ था उन्हें एक दिन पहले कार्रवाई का इस्तेमाल करने वाले लोगों की तुलना में एक अलग वेलकम मैसेज मिल सकता है.

बातचीत में वॉल्यूम कंट्रोल

जिन डिवाइस पर Assistant काम करती है उन पर Assistant, उपयोगकर्ताओं को आपकी बातचीत के दौरान, डिवाइस की आवाज़ कंट्रोल करने की अनुमति देती है. इसके लिए, वे "आवाज़ बढ़ाओ" या "आवाज़ को 50 प्रतिशत पर सेट करो" जैसे निर्देश देती हैं. अगर आपके ऐसे इंटेंट हैं जो मिलते-जुलते ट्रेनिंग वाले वाक्यांशों को हैंडल करते हैं, तो आपके इंटेंट को अहमियत दी जाएगी. हमारा सुझाव है कि जब तक आपकी सेट की गई कार्रवाई को करने की कोई खास वजह न हो, तब तक Assistant को उपयोगकर्ता के इन अनुरोधों का जवाब देने की अनुमति दें.