From 5ed3d34fbc061206bdc5c95aeadafb1f250b87e2 Mon Sep 17 00:00:00 2001 From: James Wade Date: Mon, 12 Feb 2024 09:22:55 -0500 Subject: [PATCH] [chore] remove unused code --- R/stream-anthropic.R | 68 +------------------------- R/stream-chat.R | 2 +- R/stream-cohere.R | 38 +-------------- R/stream-ollama.R | 35 +------------- R/stream-openai.R | 112 +------------------------------------------ 5 files changed, 5 insertions(+), 250 deletions(-) diff --git a/R/stream-anthropic.R b/R/stream-anthropic.R index e43bfa6..1a7ddb4 100644 --- a/R/stream-anthropic.R +++ b/R/stream-anthropic.R @@ -1,71 +1,5 @@ -create_stream_handler_anthropic <- function() { - env <- rlang::env() - - function(x) { - x <- rawToChar(x) - - pattern <- "\\{\"type\":\"completion\",.*\"log_id\":\"compl_[^\"]*\"\\}" - - if (rlang::is_null(env$resp)) { - env$resp <- x - } else { - env$resp <- paste0(env$resp, x) - } - if (stringr::str_detect(env$resp, pattern)) { - parsed <- stringr::str_extract(env$resp, pattern) |> - jsonlite::fromJSON() |> - purrr::pluck("completion") - - env$full_resp <- paste0(env$full_resp, parsed) - - cat(parsed) - - # Use shinyjs to update a div with the response - # shinyjs::html(output_id, env$full_resp) - # r$response <- env$full_resp - - env$resp <- stringr::str_split(env$resp, pattern) - env$resp <- env$resp[[1]][[length(env$resp[[1]])]] - } - TRUE - } -} - -create_stream_handler_anthropic_for_shiny <- function(r, output_id) { - env <- rlang::env() - - function(x) { - x <- rawToChar(x) - - # cat(x) - - pattern <- "\\{\"type\":\"completion\",.*\"log_id\":\"compl_[^\"]*\"\\}" - - if (rlang::is_null(env$resp)) { - env$resp <- x - } else { - env$resp <- paste0(env$resp, x) - } - if (stringr::str_detect(env$resp, pattern)) { - parsed <- stringr::str_extract(env$resp, pattern) |> - jsonlite::fromJSON() |> - purrr::pluck("completion") - - env$full_resp <- paste0(env$full_resp, parsed) - - # Use shinyjs to update a div with the response - shinyjs::html(output_id, env$full_resp) - r$response <- env$full_resp - - env$resp <- stringr::str_split(env$resp, pattern) - env$resp <- env$resp[[1]][[length(env$resp[[1]])]] - } - TRUE - } -} - stream_chat_anthropic <- function(prompt, - element_callback = create_stream_handler_anthropic(), + element_callback = create_handler("anthropic"), model = "claude-2", key = Sys.getenv("ANTHROPIC_API_KEY")) { request_body <- list( diff --git a/R/stream-chat.R b/R/stream-chat.R index 8de8a34..e8311a4 100644 --- a/R/stream-chat.R +++ b/R/stream-chat.R @@ -1,6 +1,6 @@ stream_chat <- function(prompt, service = getOption("gpttools.service"), - r, + r = NULL, output_id = "streaming", where = "console") { switch(service, diff --git a/R/stream-cohere.R b/R/stream-cohere.R index a789964..c905a1b 100644 --- a/R/stream-cohere.R +++ b/R/stream-cohere.R @@ -1,6 +1,6 @@ stream_chat_cohere <- function(prompt, model = getOption("gpttools.model", "command"), - element_callback = create_stream_handler_cohere(), + element_callback = create_handler("cohere"), key = Sys.getenv("COHERE_API_KEY")) { request_body <- list( message = prompt, @@ -31,39 +31,3 @@ stream_chat_cohere <- function(prompt, )) } } - -create_stream_handler_cohere <- function() { - env <- rlang::env() - - function(x) { - x <- rawToChar(x) - # cat(x) - - pattern <- - '\\{"is_finished":false,"event_type":"text-generation","text":".*"\\}' - - if (rlang::is_null(env$resp)) { - env$resp <- x - } else { - env$resp <- paste0(env$resp, x) - } - if (stringr::str_detect(env$resp, pattern)) { - parsed <- stringr::str_extract(env$resp, pattern) |> - jsonlite::fromJSON() |> - purrr::pluck("text") - - env$full_resp <- paste0(env$full_resp, parsed) - - rstudioapi::setGhostText(env$full_resp) - # cat(parsed) - - # # Uncomment and customize if you need to update UI components in a Shiny app: - # shinyjs::html(output_id, env$full_resp) - # r$response <- env$full_resp - - env$resp <- stringr::str_split(env$resp, pattern) - env$resp <- env$resp[[1]][[length(env$resp[[1]])]] - } - TRUE - } -} diff --git a/R/stream-ollama.R b/R/stream-ollama.R index c812c88..ffb16d0 100644 --- a/R/stream-ollama.R +++ b/R/stream-ollama.R @@ -1,6 +1,6 @@ stream_chat_ollama <- function(prompt, model = getOption("gpttools.model"), - element_callback = create_stream_handler_ollama()) { + element_callback = create_handler("ollama")) { body <- list( model = model, prompt = prompt, @@ -53,36 +53,3 @@ ollama_is_available <- function(verbose = FALSE) { invisible(check_value) } - -create_stream_handler_ollama <- function() { - env <- rlang::env() - - function(x) { - x <- rawToChar(x) - - pattern <- '\\{"model":.*"done":false\\}' - - if (rlang::is_null(env$resp)) { - env$resp <- x - } else { - env$resp <- paste0(env$resp, x) - } - if (stringr::str_detect(env$resp, pattern)) { - parsed <- stringr::str_extract(env$resp, pattern) |> - jsonlite::fromJSON() |> - purrr::pluck("response") - - env$full_resp <- paste0(env$full_resp, parsed) - - cat(parsed) - - # # Uncomment and customize if you need to update UI components in a Shiny app: - # shinyjs::html(output_id, env$full_resp) - # r$response <- env$full_resp - - env$resp <- stringr::str_split(env$resp, pattern) - env$resp <- env$resp[[1]][[length(env$resp[[1]])]] - } - TRUE - } -} diff --git a/R/stream-openai.R b/R/stream-openai.R index 4010ad3..3d4a47c 100644 --- a/R/stream-openai.R +++ b/R/stream-openai.R @@ -1,5 +1,5 @@ stream_chat_openai <- function(prompt = NULL, - element_callback = create_stream_handler_openai(), + element_callback = create_openai("openai"), model = getOption("gpttools.model", "gpt-4-turbo-preview"), openai_api_key = Sys.getenv("OPENAI_API_KEY"), shiny = FALSE) { @@ -30,113 +30,3 @@ stream_chat_openai <- function(prompt = NULL, invisible(response) } - - -create_stream_handler_openai <- function() { - # Create an environment to hold the state - env <- rlang::env() - env$resp <- NULL - - function(x) { - pattern <- '\\{"id":.*?\\}\\]\\}' - x <- rawToChar(x) - if (rlang::is_null(env$resp)) { - env$resp <- x - } else { - env$resp <- paste0(env$resp, x) - } - - if (stringr::str_detect(env$resp, pattern)) { - parsed <- stringr::str_extract(env$resp, pattern) |> - jsonlite::fromJSON() |> - purrr::pluck("choices") |> - purrr::pluck("delta") |> - purrr::pluck("content") - - cat(parsed) - - # Reset resp after processing - env$resp <- stringr::str_split(env$resp, pattern) - - env$resp <- env$resp[[1]][[length(env$resp[[1]])]] - } - TRUE - } -} - -# create_handler_for_shiny <- function(r, output_id = "streaming") { -# env <- rlang::env() -# env$resp <- NULL -# env$full_resp <- NULL -# -# function(x) { -# pattern <- '\\{"id":.*?\\}\\]\\}' -# x <- rawToChar(x) -# -# if (rlang::is_null(env$resp)) { -# env$resp <- x -# } else { -# env$resp <- paste0(env$resp, x) -# } -# -# if (stringr::str_detect(env$resp, pattern)) { -# parsed <- stringr::str_extract(env$resp, pattern) |> -# jsonlite::fromJSON() |> -# purrr::pluck("choices", "delta", "content") -# -# env$full_resp <- paste0(env$full_resp, parsed) -# -# # Use shinyjs to update a div with the response -# shinyjs::html(output_id, -# shiny::markdown(paste("**Assistant**", env$full_resp, sep = "\n\n"))) -# r$response <- env$full_resp -# -# env$resp <- stringr::str_split(env$resp, pattern) -# env$resp <- env$resp[[1]][[length(env$resp[[1]])]] -# } -# TRUE -# } -# } - - -# response <- -# stream_chat_completion( -# message = "Tell me a story about a magical backpack.", -# element_callback = function(content) cat(content, "\n") -# ) -# -# result <- -# callr::r(\() { -# stream_chat_completion( -# message = "Tell me a story about a magical backpack.", -# element_callback = function(content) cat(content, "\n") -# ) -# }) -# -# Define a wrapper function to call in the subprocess -# subprocess_function <- function(stream_chat_completion, create_stream_handler, message, element_callback) { -# environment(stream_chat_completion) <- environment() -# environment(create_stream_handler) <- environment() -# stream_chat_completion( -# message = message, -# element_callback = element_callback -# ) -# } -# -# # Call the subprocess with the wrapper function, passing the necessary objects and arguments -# result <- callr::r_bg( -# func = subprocess_function, -# args = list( -# stream_chat_completion = stream_chat_completion, -# create_stream_handler = create_stream_handler, -# message = "Tell me a story about a magical backpack.", -# element_callback = function(content) cat(content, "\n") -# ) -# ) -# -# output <- NULL -# while (rlang::is_true(result$is_alive())) { -# cat(result$read_output()) -# output <- paste0(output, result$read_output()) -# Sys.sleep(0.1) -# }