Skip to content

Commit

Permalink
[chore] remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Feb 12, 2024
1 parent 2944336 commit 5ed3d34
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 250 deletions.
68 changes: 1 addition & 67 deletions R/stream-anthropic.R
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
2 changes: 1 addition & 1 deletion R/stream-chat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
stream_chat <- function(prompt,
service = getOption("gpttools.service"),
r,
r = NULL,
output_id = "streaming",
where = "console") {
switch(service,
Expand Down
38 changes: 1 addition & 37 deletions R/stream-cohere.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
}
}
35 changes: 1 addition & 34 deletions R/stream-ollama.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
}
}
112 changes: 1 addition & 111 deletions R/stream-openai.R
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
# }

0 comments on commit 5ed3d34

Please sign in to comment.