콘텐츠로 이동

45. /api/chat 경로로 스트림 API 연습

Video

준비 중

Note

test.py

test.py
from flask import Flask, render_template, Response

app = Flask(__name__)

@app.route("/")
def index():
    title = "OpenAI API Agent School" 
    return render_template('index.html', title=title)

@app.route("/api/hello")
def hello_api():
    return "Hello, World!"

@app.route("/api/chat")
def chat_api():
    text = "Hello, World!"
    def generate():
        import time
        for char in text:
            yield char
            time.sleep(1)
    return Response(generate(), mimetype='text/event-stream')

if __name__ == "__main__":
    app.run(host="0.0.0.0")

Resources