I was able to make this work using URLSubmit
body = <|
"model" -> "gpt-3.5-turbo",
"messages" -> {<|"role" -> "user",
"content" -> "Can you give me a short description of Etiopia?"|>},
"n" -> 1,
"temperature" -> 0.2,
"stream" -> True
|>;
bodyRule =
If[body === None,
Nothing,
"Body" -> ExportByteArray[body, "JSON"]
];
request = HTTPRequest[
<|
"Scheme" -> "https",
"Domain" -> "api.openai.com",
"Method" -> If[body === None, "GET", "POST"],
"ContentType" -> "application/json",
"Path" -> {"v1", "chat", "completions"},
bodyRule,
"Headers" -> {
"Authorization" -> "Bearer " <> apiKey
}
|>
];
receiveBodyChunk[partialResponse_] :=
Module[{chunks, chunkText, validChunks},
chunkText = ImportString[partialResponse["BodyChunk"], "Text"];
chunks = StringSplit[chunkText, "\n"];
validChunks =
ImportString[StringDrop[#, 5], "JSON"] & /@
Select[ chunks, StringLength[#] > 5 && # != "data: [DONE]" &];
chunkText = Cases[validChunks, ("content" -> l_String) :> l, -1];
BodyString = BodyString <> (StringJoin @@ chunkText)
];
BodyString = "";
Print[Dynamic[BodyString]];
response2 =
URLSubmit[request,
HandlerFunctions -> <|"BodyChunkReceived" -> receiveBodyChunk|>,
HandlerFunctionsKeys -> {"BodyChunk"}]