rstudio/websocket

How to capture the generated streaming data?

chuxinyuan opened this issue · 0 comments

code like this

  ws = websocket::WebSocket$new(url, autoConnect = FALSE)
  ws$onOpen(function(event) {
    cat("Connection opened.\n")
    ws$send(request_json)
  })
  
  ws$onMessage(function(event) {
    msg = jsonlite::fromJSON(event$data)
    if (
      !is.null(msg$header$message) && 
      msg$header$message == "Success" &&
      msg$payload$choices$status != 2
    ) {
      tmp = msg$payload$choices$text[, "content"]
    } else {
      tmp = ""
      ws$close()
    }
    print(tmp)
  })

result like this

Connection opened.
[1] "\n"
[1] "\n我是一个"
[1] "AI语言模型,"
[1] "可以回答你的问题。"
[1] ""

I expect the result like this

[1] "我是一个AI语言模型,可以回答你的问题。"