Hello, I am currently trying to make a chatbot using the chatGPT API.
The form returns differently each time I ask openai for an answer.
for example
- Plain as a line answer (this is very optimal).
- {‘prompt’: content, ‘question’: content, ‘context’: content}
- {‘completion’:{‘chunks’:[{‘text’:‘Hello?’,‘index’:0}],…,‘id’:…
At first, like number 1), the answer came out very well. Then, suddenly, a strange answer comes out like 2&3. If the format is constant, it will be tuned accordingly, but it is impossible to predict the irregularity of the answer based on the rule.
Why? The code is nothing special.
result = openai.ChatCompletion.create(
model=model_name,
stream = True,
messages=[
{"role" : "user", "content": str(msg)}
] )
return result
for event in response:
if event["choices"][0]["delta"].get("role") == "assistant":
yield '{"event":"start",\n"data":"stream"}\n\n'
if event["choices"][0]["delta"].get("content") is not None:
response_message = event["choices"][0]["delta"]["content"]
json_data = json.dumps({"text": response_message, "sender": "assistant"}, ensure_ascii=False)
yield '{"event" : "message",\n"data":' + json_data +"\n}\n\n"
yield '{"event":"end",\n "data":"stream",\n "docs":'+ str(json_docs) + '\n}\n'