環境 Python 3.10 Streamlit 1.21.0 openai 0.27.4 Colab (Jupyter) Colabなどを用いてChatGPTのstream=Trueにした時にストリームで表示するにはいくつか方法が考えられますが、print(end='')などで連結しながら出力するのが手軽だと思います。 import openai completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "昔々あるところに"}], stream=True, ) for chunk in completion: next = chunk['choices'][0]['delta'].get('content', '') print(next,
![StreamlitでChatGPTのStreamを表示する - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/a65ca676020674bd25a6c3529d1cd8f9a6d25faa/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9U3RyZWFtbGl0JUUzJTgxJUE3Q2hhdEdQVCVFMyU4MSVBRVN0cmVhbSVFMyU4MiU5MiVFOCVBMSVBOCVFNyVBNCVCQSVFMyU4MSU5OSVFMyU4MiU4QiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZ0eHQtY29sb3I9JTIzMUUyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTU2JnM9NzhlY2YyYzNjMGNmYTdmOWIwZjUwNzhkYmZmYzIzYjY%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDBzdXp1a2lfc2gmdHh0LWNvbG9yPSUyMzFFMjEyMSZ0eHQtZm9udD1IaXJhZ2lubyUyMFNhbnMlMjBXNiZ0eHQtc2l6ZT0zNiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZzPTU1ZDRkMTI0ODhlYTJiMDAzOGJmNzRlOWIzNjQzZDBl%26blend-x%3D142%26blend-y%3D436%26blend-mode%3Dnormal%26txt64%3DaW4g5qCq5byP5Lya56S-44Ko44Kk44OB44O844Og44Op44Kk44OV44OH44K244Kk44Oz%26txt-width%3D770%26txt-clip%3Dend%252Cellipsis%26txt-color%3D%25231E2121%26txt-font%3DHiragino%2520Sans%2520W6%26txt-size%3D36%26txt-x%3D156%26txt-y%3D536%26s%3D023dd35d49f881d3ced688d151eeca57)