Skip to content

How to capture the generated streaming data? #104

Open
@chuxinyuan

Description

@chuxinyuan

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语言模型,可以回答你的问题。"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions