From d3d01810c46aa3e31f5cb17a733f589a4935f605 Mon Sep 17 00:00:00 2001 From: Hause Lin Date: Sun, 28 Jul 2024 00:22:59 -0400 Subject: [PATCH] Fix resp_process --- R/ollama.R | 5 +++-- R/utils.R | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/R/ollama.R b/R/ollama.R index 9198e2d..ce24cfb 100644 --- a/R/ollama.R +++ b/R/ollama.R @@ -303,7 +303,8 @@ chat <- function(model, messages, output = c("resp", "jsonlist", "raw", "df", "t #' list_models("raw") list_models <- function(output = c("df", "resp", "jsonlist", "raw", "text"), endpoint = "/api/tags", host = NULL) { - if (!output[1] %in% c("df", "resp", "jsonlist", "raw", "text")) { + output <- output[1] + if (!output %in% c("df", "resp", "jsonlist", "raw", "text")) { stop("Invalid output format specified. Supported formats are 'df', 'resp', 'jsonlist', 'raw', 'text'.") } req <- create_request(endpoint, host) @@ -311,7 +312,7 @@ list_models <- function(output = c("df", "resp", "jsonlist", "raw", "text"), end tryCatch( { resp <- httr2::req_perform(req) - return(resp_process(resp = resp, output = output[1])) + return(resp_process(resp = resp, output = output)) }, error = function(e) { stop(e) diff --git a/R/utils.R b/R/utils.R index 0f676d6..c8d4b5a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -61,11 +61,19 @@ resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp", "text return(resp) } + endpoints_without_stream <- c("api/tags", "api/delete") # endpoints that do not stream + # process stream resp separately stream <- FALSE headers <- httr2::resp_headers(resp) transfer_encoding <- headers$`Transfer-Encoding` # if response is chunked, then it was a streamed output if (!is.null(transfer_encoding)) stream <- grepl("chunked", transfer_encoding) + for (endpoint in endpoints_without_stream) { + if (grepl(endpoint, resp$url)) { + stream <- FALSE + break + } + } if (stream) { return(resp_process_stream(resp, output)) }