Skip to content

Commit

Permalink
feat: propagate streaming to rest of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Feb 5, 2024
1 parent b5c7a3a commit fa05d44
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 20 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(add_roxygen_addin)
export(addin_run_scrape_pkgs)
export(addin_run_select_pkgs)
export(chat)
export(chat_with_context)
export(chat_with_retrieval)
export(collect_dataframes)
Expand Down
1 change: 1 addition & 0 deletions R/embedding.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ create_index <- function(domain,
get_top_matches <- function(index, query_embedding, k = 5) {
k <- min(k, nrow(index))
index |>
tibble::as_tibble() |>
dplyr::mutate(
similarity = purrr::map_dbl(embedding, \(x) {
lsa::cosine(query_embedding, unlist(x))
Expand Down
2 changes: 1 addition & 1 deletion R/gpt-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gpt_chat <- function(instructions,
purrr::map_chr(.f = "content") |>
paste(collapse = "\n\n")

answer <- gptstudio::chat(
answer <- chat(
prompt = simple_prompt,
service = service,
model = model,
Expand Down
43 changes: 34 additions & 9 deletions R/history.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ check_context <- function(context) {
#' @param local Whether to use the local model or not. Default is FALSE.
#' @param embedding_model A model object to use for embedding. Only needed if
#' local is TRUE. Default is NULL.
#' @param stream Whether to stream the response or not. Default is FALSE.
#' @param rv A reactive value to store the response. Default is NULL.
#'
#' @return A list containing the prompt, context, and answer.
#' @export
Expand All @@ -190,7 +192,7 @@ chat_with_context <- function(query,
model = "gpt-4-turbo-preview",
index = NULL,
add_context = TRUE,
check_context = FASLE,
check_context = FALSE,
chat_history = NULL,
history_name = "chat_history",
session_history = NULL,
Expand All @@ -201,7 +203,9 @@ chat_with_context <- function(query,
save_history = TRUE,
overwrite = FALSE,
local = FALSE,
embedding_model = NULL) {
embedding_model = NULL,
stream = FALSE,
rv = NULL) {
arg_match(task, c("Context Only", "Permissive Chat"))

if (rlang::is_true(check_context)) {
Expand Down Expand Up @@ -352,12 +356,33 @@ chat_with_context <- function(query,
cli_alert_info("Service: {service}")
cli_alert_info("Model: {model}")

answer <- gptstudio::chat(
prompt = simple_prompt,
service = service,
model = model,
stream = FALSE
)
cli_alert_info("Stream: {stream}")

if (rlang::is_true(stream)) {
cli_alert_info("Attempting to stream chat.")
if (rlang::is_null(rv)) {
answer <-
stream_chat_openai(
prompt = simple_prompt,
element_callback = create_stream_handler()
)
} else {
stream_chat_shiny(
prompt = simple_prompt,
service = service,
r = rv,
output_id = "streaming"
)
answer <- rv$response
}
} else {
answer <- chat(
prompt = simple_prompt,
service = service,
model = model,
stream = FALSE
)
}

if (save_history) {
purrr::map(prompt, \(x) {
Expand Down Expand Up @@ -406,7 +431,7 @@ is_context_needed <- function(user_prompt,
Respond ONLY with TRUE or FALSE.
\n\n{user_prompt}")

gptstudio::chat(
chat(
prompt = prompt,
service = service,
model = model,
Expand Down
15 changes: 11 additions & 4 deletions R/index.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,17 @@ delete_index <- function() {

combine_index <- function(dir_name) {
dir <- glue('{tools::R_user_dir("gpttools", which = "data")}/{dir_name}')
arrow::open_dataset(
sources = dir,
factory_options = list(selector_ignore_prefixes = "local")
) |>
indices <- list_index(dir_name, full_path = FALSE)
if ("All" %in% indices) {
cli_abort(
c(
"!" = "All.parquet already exists in the directory.",
"-" = "Please remove it with `delete_index` before combining indexes."
)
)
}
list_index(dir_name, full_path = TRUE) |>
arrow::open_dataset() |>
tibble::as_tibble() |>
arrow::write_parquet(sink = glue::glue("{dir}/All.parquet"))
}
1 change: 1 addition & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ Name: Scrape Packages
Description: Scrape packages for use in Chat with Retrieval app
Binding: addin_run_scrape_pkgs
Interactive: true

36 changes: 36 additions & 0 deletions inst/streaming_example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
library(httr2)
library(shiny)
library(bslib)

ui <- fluidPage(
theme = bslib::bs_theme(version = 5),
shinyjs::useShinyjs(),
textInput("message", label = "Ask away", placeholder = "Simple ggplot2, be brief."),
actionButton("send", "Send"),
# div(id = "response"),
bslib::card(
bslib::card_header("Response"),
textOutput(outputId = "response"),
h3("some text")
)
)

server <- function(input, output, session) {
rv <- reactiveValues(response = NULL)
rv$response <- NULL
observeEvent(input$send, {
req(input$message)
shinyjs::show("response")
stream_chat_openai(
prompt = input$message,
element_callback = create_stream_handler_for_shiny(rv, "response")
)
shinyjs::hide("response")
})

output$response2 <- renderText({
rv$response
})
}

shinyApp(ui, server)
110 changes: 110 additions & 0 deletions man/chat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions man/chat_with_context.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/delete_index.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gpttools_index_all_scraped_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/list_index.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/load_index.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa05d44

Please sign in to comment.