LoginSignup
0
2

ChatGPTみたいに文字をぬるぬる出す方法。

Posted at

概要

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)

全体のソースはこちら

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2