ChatGPT-3 延长gpt API调用的对话流webhook截止时间

yquaqz18  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(374)

我正在尝试使用在Internet上找到的脚本,通过Google Dialogflow延长webhook请求的最长时间(最多5秒超时)。我需要延长时间,因为我对openai进行API调用,有时需要超过5秒。我的想法是并行启动这两个函数。broadbridge_webhook_results()函数用于通过在3.5秒后触发对话流中的followupEventInput来延长时间,所以一个新的调用通过对话流来,5秒从new开始。2这显然是2次。3同时API调用应该是对openai的。一旦API调用成功,答案应该被发送回对话流。不幸的是,我目前没有任何进展,我认为线程功能的设置或理解是错误的。
下面的代码我已经:

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)
qncylg1j

qncylg1j1#

对话流支持通过增加webhook执行时间限制来将webhook执行时间限制增加到30秒。这是通过对话流代理配置文件中的“extend_webhook_deadline”设置来完成的。
要启用此设置,您需要执行以下操作:
1.访问对话流控制台
1.选择要为其启用设置的代理
1.单击左侧边栏上的“设置
1.点击“导出和导入”
1.选择“导出为ZIP”
1.在下载的ZIP文件中找到“agent.json”文件
1.使用文本编辑器打开“agent.json”文件
1.找到“问候语”部分
1.添加以下行“extend_webhook_deadline”:真
1.保存更改并将“agent.json”文件导入回对话流
请记住,启用此设置后,对话流将收取额外费用以延长webhook的持续时间。

相关问题