Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to Implement Partial Responses in Dialogflow CX While Waiting for a Datastore Query?

Hi everyone, I’m working on a Dialogflow CX agent that uses a datastore handler to fetch answers for user queries. The datastore query takes around 3-4 seconds to process, and I’d like to display a partial response (e.g., "Please wait while we process your request...") to users while the query is being processed (Not sure is there a simple way to achieve this behaviour).

Here’s what I’ve tried so far:

Using a webhook and enable partial response:
I created a webhook (and enable the partial response check) that sends the user question to the agent with the datastore and waits for the response. However, the webhook doesn’t seem to handle getting the responses properly. It sends the question to the agent but doesn’t get the response. 

Here’s part of my webhook code:

 
 

 

// Initialize Dialogflow Sessions Client
const client = new SessionsClient({
apiEndpoint: 'global-dialogflow.googleapis.com', // Use this for global agents
});

functions.http('dialogflowWebhook', async (req, res) => {
  console.log('Dialogflow Request Body:', JSON.stringify(req.body, null, 2));

  try {
    const parameters = req.body.sessionInfo?.parameters || {};
    const userQuestion = parameters.user_question || 'No question provided';
	const sessionId = req.body.sessionInfo?.session.split('/').pop(); // Extract Session ID from the session path
	// Construct the session path for a global agent
	const sessionPath = client.projectLocationAgentSessionPath(
	projectId,
	location, 
	agentId,
	sessionId
	);

	// Build the query input
	const request = {
	session: sessionPath,
	queryInput: {
	text: {
	text: userQuestion,
	},
	languageCode: 'en-US', 
	},
    pageSize: 1,
	};

    console.log('Extracted user question:', userQuestion);
	
	// Send the query to Dialogflow
	const [response] = await client.detectIntent(request);
	...
	

 

Solved Solved
0 1 159
1 ACCEPTED SOLUTION

Hi @r2carrillo,

Welcome to the Google Cloud Community!

Your webhook triggers the Datastore query but fails to handle Dialogflow CX's final response, preventing you from delivering the complete answer to your user. You can send the ‘userQuestion’ to Dialogflow but can't retrieve your corresponding response.

Here are the potential ways that might help with your use case:

  • Initial Webhook Response: When your Dialogflow CX agent activates your webhook, promptly send a partial response with responseType: 'STREAMING_RESPONSE' to let the user know their query is being processed (e.g., "Please wait while we search...") and ensure this is returned without delay or awaiting any datastore operations.
  • Datastore Indexing: Make sure that your Datastore is properly indexed based on your ‘userQuestion’ field. Without proper indexing, your queries will still be slow, even if you use the partial response approach.
  • Project and Agent Configuration: Ensure you properly configure your projectId, location, and agentId using your environment variables or settings, as this is a frequent cause of errors.
  • Permissions: Verify that the service account for your Cloud Functions possesses the required permissions to interact with Datastore.
  • Error Handling: You may want to implement a robust error-handling mechanism using your try...catch blocks to gracefully manage any issues that arise during your Datastore query or while constructing your final response. 

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

View solution in original post

1 REPLY 1

Hi @r2carrillo,

Welcome to the Google Cloud Community!

Your webhook triggers the Datastore query but fails to handle Dialogflow CX's final response, preventing you from delivering the complete answer to your user. You can send the ‘userQuestion’ to Dialogflow but can't retrieve your corresponding response.

Here are the potential ways that might help with your use case:

  • Initial Webhook Response: When your Dialogflow CX agent activates your webhook, promptly send a partial response with responseType: 'STREAMING_RESPONSE' to let the user know their query is being processed (e.g., "Please wait while we search...") and ensure this is returned without delay or awaiting any datastore operations.
  • Datastore Indexing: Make sure that your Datastore is properly indexed based on your ‘userQuestion’ field. Without proper indexing, your queries will still be slow, even if you use the partial response approach.
  • Project and Agent Configuration: Ensure you properly configure your projectId, location, and agentId using your environment variables or settings, as this is a frequent cause of errors.
  • Permissions: Verify that the service account for your Cloud Functions possesses the required permissions to interact with Datastore.
  • Error Handling: You may want to implement a robust error-handling mechanism using your try...catch blocks to gracefully manage any issues that arise during your Datastore query or while constructing your final response. 

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.