lbarthelet
(Luc Barthelet)
1
Has anyone implemented a connection receiving server-events?
As described in Using server-sent events - Web APIs | MDN
That’s what is being used by chatGPT to update the response. But is it used by many servers to update browser during in long compute.
It would be nice to be able to use with OpenAILink` …
lbarthelet
(Luc Barthelet)
2
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"}]