Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Kurama622 opened this issue Aug 10, 2024 · 0 comments
Closed

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

Kurama622 opened this issue Aug 10, 2024 · 0 comments

Comments

@Kurama622
Copy link

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:

  • nvim version
NVIM v0.10.1
Build type: Release
LuaJIT 2.1.1720049189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant