概要
ChatGPTのAPIを利用して、文字をぬるぬる出すチャットアプリ(サンプルコード)を実装しました。
(websocketを使っています。)
環境
Windows11 Flask 2.3.2 Python 3.9.6 JavaScript
メイン部分はこんな感じ
Python Flask
#返答
response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
                {"role": "system", "content": "あなたは###です。"},
                {"role": "user", "content": message}
            ],
        stream=True   
    )
    for chunk in response:
        #文字列送信
        emit('message', {'type': 'text', 'data': chunk["choices"][0]["delta"].get("content", "")}, json=True)
全体のソースはこちら