使用ConvetFrom-JSON时curl的PowerShell 7流输出

问题描述 投票:0回答:1

我使用此命令通过 WebUI API 获取 Ollama 补全,使用curl:

curl -sd '{"model": "code-companion:latest", "prompt": "Why is the sky blue?"}' http://127.0.0.1:11434/api/generate | ConvertFrom-Json | ForEach-Object {Write-Host -NoNewline $_.response}

虽然这效果很好,但它不会流式传输输出,而是立即显示整个响应。 如果我不使用

ConvertFrom-Json
,那么curl输出确实是流式传输的。

如何使

ConvertFrom-Json
流式传输输出?

windows powershell curl streaming
1个回答
0
投票

到目前为止,看起来

ConvertFrom-Json
无论如何都会缓冲输出。

我找到了一个解决方法,使用正则表达式过滤 JSON 输出,使用

-match
操作。这似乎流式传输中间输出(仍然有一些缓冲,但流式传输而不是一次显示所有内容):

curl -sd $data $url | % { 
    if ($_ -match '"response":\s*"(.+?)"') {Write-Host -NoNewline $Matches[1]} 
}

我使用了

if
语句,否则
-match
会不断输出
True

© www.soinside.com 2019 - 2024. All rights reserved.