זיהוי כוונות באמצעות פלט אודיו

בדרך כלל, אפליקציות צריכות בוט כדי להשיב למשתמש הקצה. ‫Dialogflow: אפליקציות צריכות לעיתים קרובות בוט שיענה למשתמש הקצה. ב-Dialogflow אפשר להשתמש ב-Cloud Text-to-Speech, שמופעל על ידי DeepMind WaveNet, כדי ליצור תגובות דיבור מהנציג. ההמרה הזו של תשובות טקסטואליות של Intent לאודיו נקראת פלט אודיו, סינתזת דיבור,

אתם יכולים להשתמש באודיו גם לקלט וגם לפלט כשאתם מזהים כוונה. זה קורה בדרך כלל כשמפתחים אפליקציות שמתקשרות עם המשתמשים באמצעות ממשק אודיו בלבד.

רשימת השפות הנתמכות מופיעה בעמודה TTS בדף Languages.

לפני שמתחילים

התכונה הזו רלוונטית רק כשמשתמשים ב-API לאינטראקציות עם משתמשי קצה. אם אתם משתמשים בשילוב, אתם יכולים לדלג על המדריך הזה.

לפני שמתחילים, צריך לעשות את הפעולות האלה:

יצירת סוכן

  1. נכנסים למסוף Dialogflow ES.
  2. אם מופיעה בקשה, נכנסים למסוף. מידע נוסף זמין בסקירה הכללית על מסוף Dialogflow.
  3. בתפריט הצד, מרחיבים את הקטע סוכני טעינה.
  4. לוחצים על יצירת נציג חדש.
  5. מזינים את שם הנציג, שפת ברירת המחדל ואזור הזמן שמוגדר כברירת מחדל.
  6. נכנסים לפרויקט קיים. כדי ליצור פרויקט במסוף Dialogflow, בוחרים באפשרות Create a new Google project.
  7. לוחצים על יצירה.

זיהוי כוונה

כדי לזהות כוונה, צריך להפעיל את ה-method‏ detectIntent בסוג Sessions.

REST

1. הכנת תוכן האודיו

מורידים את קובץ ה-input_audio לדוגמה book-a-room.wav, שבו נאמר "book a room". בדוגמה הזו, קובץ האודיו צריך להיות מקודד ב-base64 כדי שאפשר יהיה לספק אותו בבקשת ה-JSON שבהמשך.

דוגמה ל-Linux:

wget https://cloud.google.com/dialogflow/es/docs/data/book-a-room.wav
base64 -w 0 book-a-room.wav > book-a-room.b64

דוגמאות לפלטפורמות אחרות מופיעות במאמר הטמעת אודיו בקידוד Base64 במסמכי התיעוד של Cloud Speech API.

2. שליחת בקשה לזיהוי כוונה

מפעילים את השיטה detectIntent בסוג Sessions ומציינים אודיו בקידוד Base64.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט ב-Google Cloud
  • ‫SESSION_ID: מזהה סשן
  • BASE64_AUDIO: תוכן ה-base64 מקובץ הפלט שלמעלה

ה-method של ה-HTTP וכתובת ה-URL:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent/sessions/SESSION_ID:detectIntent

תוכן בקשת JSON:

{
  "queryInput": {
    "audioConfig": {
      "languageCode": "en-US"
    }
  },
  "outputAudioConfig" : {
    "audioEncoding": "OUTPUT_AUDIO_ENCODING_LINEAR_16"
  },
  "inputAudio": "BASE64_AUDIO"
}

כדי לשלוח את הבקשה צריך להרחיב אחת מהאפשרויות הבאות:

אתם אמורים לקבל תגובת JSON שדומה לזו:

{
  "responseId": "b7405848-2a3a-4e26-b9c6-c4cf9c9a22ee",
  "queryResult": {
    "queryText": "book a room",
    "speechRecognitionConfidence": 0.8616504,
    "action": "room.reservation",
    "parameters": {
      "time": "",
      "date": "",
      "duration": "",
      "guests": "",
      "location": ""
    },
    "fulfillmentText": "I can help with that. Where would you like to reserve a room?",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "I can help with that. Where would you like to reserve a room?"
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/PROJECT_ID/agent/intents/e8f6a63e-73da-4a1a-8bfc-857183f71228",
      "displayName": "room.reservation"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {},
    "languageCode": "en-us"
  },
  "outputAudio": "UklGRs6vAgBXQVZFZm10IBAAAAABAAEAwF0AAIC7AA..."
}

שימו לב שהערך של השדה queryResult.action הוא room.reservation, והשדה outputAudio מכיל מחרוזת אודיו גדולה ב-base64.

3. השמעת פלט האודיו

מעתיקים את הטקסט מהשדה outputAudio ושומרים אותו בקובץ בשם output_audio.b64. צריך להמיר את הקובץ הזה לאודיו.

דוגמה ל-Linux:

base64 -d output_audio.b64 > output_audio.wav

דוגמאות לפלטפורמות אחרות מופיעות במאמר פענוח תוכן אודיו בקידוד Base64 במאמרי העזרה של ה-API של Text-to-Speech.

עכשיו אפשר להפעיל את קובץ האודיו output_audio.wav ולשמוע שהוא תואם לטקסט בשדה queryResult.fulfillmentMessages[1].text.text[0] שלמעלה. הרכיב השני של fulfillmentMessages נבחר כי הוא התגובה בטקסט לפלטפורמה שמוגדרת כברירת מחדל.

Java

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.v2.DetectIntentRequest;
import com.google.cloud.dialogflow.v2.DetectIntentResponse;
import com.google.cloud.dialogflow.v2.OutputAudioConfig;
import com.google.cloud.dialogflow.v2.OutputAudioEncoding;
import com.google.cloud.dialogflow.v2.QueryInput;
import com.google.cloud.dialogflow.v2.QueryResult;
import com.google.cloud.dialogflow.v2.SessionName;
import com.google.cloud.dialogflow.v2.SessionsClient;
import com.google.cloud.dialogflow.v2.TextInput;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class DetectIntentWithTextToSpeechResponse {

  public static Map<String, QueryResult> detectIntentWithTexttoSpeech(
      String projectId, List<String> texts, String sessionId, String languageCode)
      throws IOException, ApiException {
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    try (SessionsClient sessionsClient = SessionsClient.create()) {
      // Set the session name using the sessionId (UUID) and projectID (my-project-id)
      SessionName session = SessionName.of(projectId, sessionId);
      System.out.println("Session Path: " + session.toString());

      // Detect intents for each text input
      for (String text : texts) {
        // Set the text (hello) and language code (en-US) for the query
        TextInput.Builder textInput =
            TextInput.newBuilder().setText(text).setLanguageCode(languageCode);

        // Build the query with the TextInput
        QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();

        //
        OutputAudioEncoding audioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16;
        int sampleRateHertz = 16000;
        OutputAudioConfig outputAudioConfig =
            OutputAudioConfig.newBuilder()
                .setAudioEncoding(audioEncoding)
                .setSampleRateHertz(sampleRateHertz)
                .build();

        DetectIntentRequest dr =
            DetectIntentRequest.newBuilder()
                .setQueryInput(queryInput)
                .setOutputAudioConfig(outputAudioConfig)
                .setSession(session.toString())
                .build();

        // Performs the detect intent request
        DetectIntentResponse response = sessionsClient.detectIntent(dr);

        // Display the query result
        QueryResult queryResult = response.getQueryResult();

        System.out.println("====================");
        System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
        System.out.format(
            "Detected Intent: %s (confidence: %f)\n",
            queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
        System.out.format(
            "Fulfillment Text: '%s'\n",
            queryResult.getFulfillmentMessagesCount() > 0
                ? queryResult.getFulfillmentMessages(0).getText()
                : "Triggered Default Fallback Intent");

        queryResults.put(text, queryResult);
      }
    }
    return queryResults;
  }
}

Node.js

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

// Imports the Dialogflow client library
const dialogflow = require('@google-cloud/dialogflow').v2;

// Instantiate a DialogFlow client.
const sessionClient = new dialogflow.SessionsClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
// const sessionId = `user specific ID of session, e.g. 12345`;
// const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`;
// const languageCode = 'BCP-47 language code, e.g. en-US';
// const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`;

// Define session path
const sessionPath = sessionClient.projectAgentSessionPath(
  projectId,
  sessionId
);
const fs = require('fs');
const util = require('util');

async function detectIntentwithTTSResponse() {
  // The audio query request
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: query,
        languageCode: languageCode,
      },
    },
    outputAudioConfig: {
      audioEncoding: 'OUTPUT_AUDIO_ENCODING_LINEAR_16',
    },
  };
  sessionClient.detectIntent(request).then(responses => {
    console.log('Detected intent:');
    const audioFile = responses[0].outputAudio;
    util.promisify(fs.writeFile)(outputFile, audioFile, 'binary');
    console.log(`Audio content written to file: ${outputFile}`);
  });
}
detectIntentwithTTSResponse();

Python

כדי לבצע אימות ב-Dialogflow CX, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

def detect_intent_with_texttospeech_response(
    project_id, session_id, texts, language_code
):
    """Returns the result of detect intent with texts as inputs and includes
    the response in an audio format.

    Using the same `session_id` between requests allows continuation
    of the conversation."""
    from google.cloud import dialogflow

    session_client = dialogflow.SessionsClient()

    session_path = session_client.session_path(project_id, session_id)
    print("Session path: {}\n".format(session_path))

    for text in texts:
        text_input = dialogflow.TextInput(text=text, language_code=language_code)

        query_input = dialogflow.QueryInput(text=text_input)

        # Set the query parameters with sentiment analysis
        output_audio_config = dialogflow.OutputAudioConfig(
            audio_encoding=dialogflow.OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16
        )

        request = dialogflow.DetectIntentRequest(
            session=session_path,
            query_input=query_input,
            output_audio_config=output_audio_config,
        )
        response = session_client.detect_intent(request=request)

        print("=" * 20)
        print("Query text: {}".format(response.query_result.query_text))
        print(
            "Detected intent: {} (confidence: {})\n".format(
                response.query_result.intent.display_name,
                response.query_result.intent_detection_confidence,
            )
        )
        print("Fulfillment text: {}\n".format(response.query_result.fulfillment_text))
        # The response's audio_content is binary.
        with open("output.wav", "wb") as out:
            out.write(response.output_audio)
            print('Audio content written to file "output.wav"')

מידע נוסף על שדות התגובה הרלוונטיים זמין בקטע תגובות לזיהוי כוונה.

תגובות לזיהוי כוונה

התגובה לבקשה לזיהוי כוונה היא מסוג DetectIntentResponse.

התוכן של השדה DetectIntentResponse.queryResult.fulfillmentMessages נשלט על ידי עיבוד רגיל של זיהוי כוונה.

השדה DetectIntentResponse.outputAudio מאוכלס באודיו על סמך הערכים של תשובות הטקסט של פלטפורמת ברירת המחדל שנמצאות בשדה DetectIntentResponse.queryResult.fulfillmentMessages:

  • אם יש כמה תגובות טקסט שמוגדרות כברירת מחדל, הן משורשרות כשנוצר האודיו.
  • אם אין תשובות טקסט שמוגדרות כברירת מחדל בפלטפורמה, תוכן האודיו שנוצר ריק.

השדה DetectIntentResponse.outputAudioConfig מאוכלס בהגדרות האודיו שמשמשות ליצירת פלט האודיו.

זיהוי כוונה מתוך סטרימינג

כשמזהים כוונה מסטרימינג, שולחים בקשות שדומות לדוגמה שלא משתמשת בפלט אודיו: זיהוי כוונה מסטרימינג. עם זאת, אתם מספקים לבקשה את השדה OutputAudioConfig. השדות output_audio ו-output_audio_config מאוכלסים בתגובת הסטרימינג הסופית שמתקבלת משרת Dialogflow API. למידע נוסף, ראו StreamingDetectIntentRequest ו-StreamingDetectIntentResponse.

הגדרות הדיבור של הסוכן

אתם יכולים לשלוט בהיבטים שונים של המרת טקסט לדיבור. הגדרות הדיבור של הנציג

שימוש בסימולטור של Dialogflow

אתם יכולים לתקשר עם הנציג ולקבל תשובות קוליות באמצעות הסימולטור של Dialogflow:

  1. מפעילים המרה אוטומטית של טקסט לדיבור (TTS) לפי ההוראות במאמר הגדרות הדיבור של הנציג.
  2. מקלידים או אומרים "book a room" (הזמנת חדר) בסימולטור.
  3. מעיינים בקטע פלט אודיו בסימולטור.