Skip to content

Commit

Permalink
Fix resp_process
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent b506423 commit d3d0181
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,16 @@ 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)
req <- httr2::req_method(req, "GET")
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)
Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit d3d0181

Please sign in to comment.