Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

@app.route("/bing", methods=['GET','POST']) async def bing(): text2 =…

  1. User

    @app.route("/bing", methods=['GET','POST']) async def bing():

    text2 = request.form.get('text')
    print("test1")
    bot = Chatbot(cookiePath='c:/Users/mozit/cookies.json')
    res = await bot.ask(prompt=text2, conversation_style=ConversationStyle.creative,
                       wss_link="wss://sydney.bing.com/sydney/ChatHub")
    print("test1")
    resulta = res['items']['messages'][1]['text']
    await bot.close()
    return jsonify({"response": res})
    
    
    

    if name == "main":

    app.run(host="0.0.0.0",port=3712)#,debug=True)
    

    실행하면 TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine. 오류원인의 원인

  2. Assistant

    오류 메시지를 보면 핸들러 함수에서 코루틴(coroutine)을 반환했다고 나옵니다. 이것은 async def bing() 함수에서 await 키워드를 사용하지 않고, 실행을 곧바로 반환했기 때문입니다. Flask에서 비동기적으로 처리하기 위해서 이벤트 루프를 가져와서 주어진 코루틴 객체를 직접 실행해 볼 수 있지만, 가장 쉬운 해결책은 Flask를 Quart로 대체하는 것입니다.

    Quart는 Flask와 거의 동일한 API를 제공하는 비동기적인 프레임워크입니다. 이 작업을 위해 일단 모듈을 설치해야 합니다.

    pip install quart
    

    그 후, 아래와 같이 코드를 수정하십시오.

    from quart import Quart, jsonify, request
    
    app = Quart(__name__)
    
    @app.route("/bing", methods=['GET','POST'])
    async def bing():
        text2 = request.form['text']
        print("test1")
        bot = Chatbot(cookiePath='c:/Users/mozit/cookies.json')
        res = await bot.ask(prompt=text2, conversation_style=ConversationStyle.creative,
                           wss_link="wss://sydney.bing.com/sydney/ChatHub")
        print("test1")
        resulta = res['items']['messages'][1]['text']
        await bot.close()
        return jsonify({"response": res})
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=3712)  # ,debug=True)
    

    위 코드에서는 from flask import 대신 `from quart import 구문을 사용하였습니다. 이외의 코드는 동일하게 사용할 수 있습니다.

    이제 애플리케이션이 에러 없이 실행되어야 합니다.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.