0

I am trying to use a script I found on the internet to extend the maximum time for a webhook request through Google Dialogflow (max 5 seconds to timeout). I need to extend the time because I make an API call to openai and it sometimes takes longer than 5 seconds. My idea was to start the 2 functions in parallel. The broadbridge_webhook_results() function is there to extend the time by triggering a followupEventInput at Dialogflow after 3,5 seconds, so a new call comes through Dialogflow and the 5 seconds start from new. This goes apparently up to 2 times. In the meantime the API call should be made towards openai. As soon as the API call was successful, the answer should be sent back to Dialogflow. Unfortunately, I am currently not getting anywhere and I think that the thread functionality was set up or understood incorrectly by me.

The following code I have so far:

import os
import openai
import time
import backoff
from datetime import datetime, timedelta
from flask import Flask, request, render_template
from threading import Thread
import asyncio

app = Flask(__name__)

conversation_History = ""
user_Input = ""
reply=''
answer = ""

@app.route('/') 
def Default(): 
    return render_template('index.html')

@backoff.on_exception(backoff.expo, openai.error.RateLimitError)
def ask(question):
  global conversation_History
  global answer
  global reply
  openai.api_key = os.getenv("gtp_Secret_Key")  
  #start_sequence = "\nAI:"
  #restart_sequence = "\nHuman: "
  response = openai.Completion.create(
    model="text-davinci-003",
    prompt="I am a chatbot from OpenAI. I'm happy to answer your questions.\nHuman:" + conversation_History + " "+ question +"\nAI: ",    
    temperature=0.9,
    max_tokens=500,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0.6,
    stop=[" Human:", " AI:"]
    )
  conversation_History = conversation_History + question + "\nAI" + answer + "\nHuman:"
  answer = response.choices[0].text  

def broadbridge_webhook_results():
  global answer
  
  now = datetime.now()
  current_time = now.strftime("%H:%M:%S")
  print("Current Time =", current_time)

  extended_time = now + timedelta(seconds=3)
  print("extended Time =", extended_time.time())

  req = request.get_json(force=True)

  action = req.get('queryResult').get('action')
  reply=''

  if action=='input.unknown' or action=='input.welcome':
    time.sleep(3.5)

    if now<=extended_time and not len(answer) == 0:
      reply={
              "fulfillmentText": answer,
              "source": "webhookdata"
            }

    reply={      
      "followupEventInput": {        
        "name": "extent_webhook_deadline",        
        "languageCode": "en-US"
        }
      }

  if action=='followupevent':
    print("enter into first followup event")
    time.sleep(3.5)

    if now<=extended_time and not len(answer) == 0:
      reply={
        "fulfillmentText": answer,
        "source": "webhookdata"
      }

    reply={      
      "followupEventInput": {        
        "name": "extent_webhook_deadline_2",        
        "languageCode": "en-US"
        }
      }
    
  if action=='followupevent_2':
    print("enter into second followup event")
    time.sleep(3.5)

    reply={
      "fulfillmentText": answer,
      "source": "webhookdata"
    }
        
    print("Final time of execution:=>", now.strftime("%H:%M:%S"))

@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
  global answer
  global reply

  answer=""
  req = request.get_json(silent=True, force=True)
  user_Input = req.get('queryResult').get('queryText')
  Thread(target=broadbridge_webhook_results()).start()
  Thread(target=ask(user_Input)).start()
  
  return reply

#conversation_History = conversation_History + user_Input + "\nAI" + answer + "\nHuman:"
#if now<=extended_time and not len(answer) == 0:
  
if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8080)

1 Answers1

0

Dialogflow supports increasing the webhook execution time limit up to 30 seconds by increasing the webhook execution time limit. This is done through the "extend_webhook_deadline" setting in the Dialogflow agent configuration file.

To enable this setting you will need to do the following:

  1. Access the Dialogflow Console
  2. Select the agent for which you want to enable the setting
  3. Click "Settings" on the left sidebar
  4. Click "Export and Import"
  5. Select "Export as ZIP"
  6. Locate the "agent.json" file inside the downloaded ZIP file
  7. Open the "agent.json" file with a text editor
  8. Locate a "greeting" section
  9. Add the following line "extend_webhook_deadline": true
  10. Save the changes and import the "agent.json" file back into Dialogflow

Bear in mind that by enabling this setting, Dialogflow will charge an additional fee to increase the duration of the webhook.