const { ReadableStream } = require('stream/web');
const axios = require('axios');

const data = {
  "key": "",
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {
      "role": "user",
      "content": "code DNS proxy from scratch in Golang",
      "createdAt": Date.now()
    }
  ],
  "temperature": 1,
  "password": ""
};

async function main() {
  const response = await axios({
    method: 'POST',
    url: "https://chat.acytoo.com/api/completions", // Replace with your API endpoint
    data: data, // Use the Readable stream as the request body
    headers: {
      'accept': '*/*',
      'content-type': 'application/json',
    },
    responseType: 'stream'
  });

  const readableStream = new ReadableStream({
    start(controller) {
      response.data.on('data', buff => {
        const resultStr = buff.toString();
        console.log(resultStr);
        controller.enqueue(resultStr)
      });
      response.data.on('end', () => controller.close());
    }
  });

  const reader = readableStream.getReader();

  console.log(await reader.read());

  // You now have a Web Streams API compatible stream
}

main().catch(console.error);

I hope this post was helpful to you.

Leave a reaction if you liked this post!