nvim-neotest / nvim-nio

A library for asynchronous IO in Neovim
MIT License
288 stars 8 forks source link

[Question] get streaming output from nio.process.run #22

Closed Kurama622 closed 1 month ago

Kurama622 commented 1 month ago

I'm trying to use nio.process.run to run a curl command and read the output of curl from stdout.

The code to read stdout is:

while true do
    print("enter loop")
    local chunk = response.stdout.read(1024)
    print("get chunk from response")
end

I added logs before and after it

my test code:

local nio = require("nio")

local messages = {
  { role = "user", content = "hello" },
}

local body = {
  stream = true,
  max_tokens = 512,
  messages = messages,
}

local ACCOUNT = os.getenv("ACCOUNT")
local KEY = os.getenv("KEY")
local MODEL = "@cf/google/gemma-7b-it-lora"

nio.run(function()
  local response = nio.process.run({
    cmd = "curl",
    args = {
      string.format("https://api.cloudflare.com/client/v4/accounts/%s/ai/run/%s", ACCOUNT, MODEL),
      "-N",
      "-X",
      "POST",
      "-H",
      "Content-Type: application/json",
      "-H",
      "Authorization: Bearer " .. KEY,
      "-d",
      vim.fn.json_encode(body),
    },
  })

  while true do
    print("enter loop")
    local chunk = response.stdout.read(1024)
    print("get chunk from response")
    if not chunk then
      print("chunk is nil")
      break
    end
    print(chunk)
  end
end)

my output:

enter loop

Why does my program get stuck at local chunk = response.stdout.read(1024)

This shell command is correct.

curl "https://api.cloudflare.com/client/v4/accounts/${ACCOUNT}/ai/run/@cf/google/gemma-7b-it-lora" \
  -N \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $KEY" \
  -d '{"messages": [{"role": "user", "content": "hello"}], "max_tokens": 512, "stream": true}'

env: