How to Return SSE Data in Python

Recently, I was using python to call the sse interface of gpt and return it to my front end through sse. I encountered a few problems, and I simply recorded them. There was no amount of code, but it took me more than half a day to get it done.

  • how to return sse in python
  • why do the newlines in the sse I return always get lost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import time

from flask import Flask, request
from flask import Response, jsonify

app = Flask(__name__)

import openai
import time
import sys

openai.api_key = ''

@app.route('/stream', methods=["POST"])
def stream():
messages = request.get_json()['messages']

print(messages)
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages,
temperature=0.7,
stream=True # again, we set stream=True
)
def eventStream():
id = 0
for chunk in response:
id += 1
chunk_message = chunk['choices'][0]['delta'] # extract the message
yield 'id: {}\nevent: message\ndata: {}\n\n'.format(id,json.dumps({'data': chunk_message.get('content', '')}))

yield 'id: {}\nevent: close\ndata: {}\n\n'.format(id,json.dumps({'data': chunk_message.get('content', '')}))

return Response(eventStream(), mimetype="text/event-stream")


if __name__ '__main__':
app.run(host='0.0.0.0', port=8889)

There are several key points here, corresponding to the two questions at the beginning:

  • The data type returned by the sse interface should be ‘“text/event-stream”’
  • The returned data should be formatted ‘id: {}\ nevent: message\ ndata: {}\ n\ n’ .format (id, json.dumps ({‘data’: chunk_message ('content ', ‘’)}))`, This is the sse protocol. One newline character represents different data, two newlines represent a piece of data, and the id should not be repeated.
  • data data is best returned with json string, the front end receives it and parses it again, even if your data is originally a string, otherwise you are likely to encounter the problem of missing newlines