Advertisement
Guest User

Untitled

a guest
Mar 27th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function handleResponseAsStream(clientResponse, apiResponse) {
  2.   const reader = apiResponse.body.getReader();
  3.   const nextDecoder = new TextDecoder();
  4.  
  5.   clientResponse.write("data: " + JSON.stringify(createBeginChunk()) + "\n\n");
  6.  
  7.   new ReadableStream({
  8.     start(controller) {
  9.       return pump();
  10.       function pump() {
  11.         return reader.read().then(({ done, value }) => {
  12.           const textData = nextDecoder.decode(value);
  13.  
  14.           clientResponse.write(
  15.             "data: " + JSON.stringify(createMessageChunk(textData)) + "\n\n"
  16.           );
  17.  
  18.           // When no more data needs to be consumed, close the stream
  19.           if (done) {
  20.             clientResponse.write(
  21.               "data: " + JSON.stringify(createEndChunk()) + "\n\n"
  22.             );
  23.             clientResponse.end();
  24.             controller.close();
  25.             return;
  26.           }
  27.           // Enqueue the next data chunk into our target stream
  28.           controller.enqueue(value);
  29.           return pump();
  30.         });
  31.       }
  32.     },
  33.   });
  34. }
  35.  
  36. const createBeginChunk = () => ({
  37.   id: "chatcmpl-123",
  38.   object: "chat.completion.chunk",
  39.   created: getCurrentDate(),
  40.   model: "gpt-4",
  41.   system_fingerprint: "",
  42.   choices: [
  43.     {
  44.       index: 0,
  45.       delta: { role: "assistant", content: "" },
  46.       logprobs: null,
  47.       finish_reason: null,
  48.     },
  49.   ],
  50. });
  51.  
  52. const createMessageChunk = (text) => ({
  53.   id: "chatcmpl-123",
  54.   object: "chat.completion.chunk",
  55.   created: getCurrentDate(),
  56.   model: "gpt-4",
  57.   system_fingerprint: "",
  58.   choices: [
  59.     {
  60.       index: 0,
  61.       delta: { content: text },
  62.       logprobs: null,
  63.       finish_reason: null,
  64.     },
  65.   ],
  66. });
  67.  
  68. const createEndChunk = () => ({
  69.   id: "chatcmpl-123",
  70.   object: "chat.completion.chunk",
  71.   created: getCurrentDate(),
  72.   model: "gpt-4",
  73.   system_fingerprint: "",
  74.   choices: [{ index: 0, delta: {}, logprobs: null, finish_reason: "stop" }],
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement