How to handle the chunk data with libsoup?

Hi all,

OpenAI stream API makes HTTP chunk response more popular, many Apps have chunked support or have been implementing it.

The good news is libsoup have been supportted the chunked response, but there is no example on the internet, could anybody share an example for us?

Libsoup 3.0 HTTP 1.1 Chunked response - Platform - GNOME Discourse

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const response = await openai.chat.completions
    .create({
      model: "gpt-4",
      messages: [{ role: "user", content: "Say this is a test!" }],
      stream: true,
    })
    .asResponse();

  for await (const chunk of response.body) {
    console.log(chunk.toString());
    console.log("-".repeat(10));
  }
}

main();

“wrote-chunk” is emitted when a chunck was sent to inform the app to provide another chunkc of data. To send another chunk, just append the new block of bytes (GBytes or guint8*) to SoupMessageBody of your SoupServerMessage…you can use soup_message_body_append_bytes (body, chunk);

when there is no more byte to send you should call (inside your handler) soup_message_body_complete to inform libsoup about that…libsoup will stop emitting “wrote-chunck” again and will transision to “finished” and “disconnected”

RECEIVING PART

you just need to handle “got-chunck” and “got-body”.
check if this is the first reception, open the store (file, database etc)
if it the case on every “got-chunk” serialize the received bytes to the store on “got-body”, finalize the job and close the store that all

to send in chuncked-encoding from the client side use soup_message_set_request_body with a length of -1;

hope this will help you.

Thanks for your response awfully.

The SoupMessage of client side seems that doesn’t export any SoupMessageBody interface.

There is an API but behind the scene. What is important is set the enconding to SOUP_ENCODING_CHUNKED on the headers and define the “content-length” to -1 and finally connecto the relevant signal “got-" or "wrote-” “finished” “disconnected” etc

It is very simple to use soup_message_set_request_body (I use libsoup 3.4.2).

Maybe you are right, but I’m not got it yet, would you mind translate the demo code with libsoup SoupMessage?

Tips: the OpenAI api could use placeholder symbol to code them.

Thanks awfully again.

I see the version of the code using curl on openai…will send you the code later