Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/copilot #76

Merged
merged 10 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ repos:
# - tidyr
# - tokenizers
# - urltools
- id: use-tidy-description
# - id: use-tidy-description
# args: [--warn_only]
- id: lintr
args: [--warn_only]
- id: readme-rmd-rendered
Expand Down
18 changes: 11 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gpttools
Title: Extensions and Tools for gptstudio
Version: 0.0.8.9009
Version: 0.0.8.9010
Authors@R:
person("James", "Wade", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-9740-1905"))
Expand Down Expand Up @@ -39,15 +39,20 @@ Imports:
xml2,
yaml
Suggests:
bslib,
bsicons,
bslib,
covr,
furrr,
future,
htmltools,
httr,
knitr,
later,
mockr,
pak,
pdftools,
precommit,
reprex,
reticulate,
rmarkdown,
roxygen2,
Expand All @@ -61,13 +66,12 @@ Suggests:
tuneR,
uuid,
waiter,
withr,
future,
furrr,
reprex
Remotes: michelnivard/gptstudio
withr
VignetteBuilder:
knitr
Remotes:
michelnivard/gptstudio,
rstudio/rstudioapi
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Major features and improvements

* Added a GitHub Copilot-like code suggestion adding called "Copilot."
* Added support for more AI services: anthropic, huggingface, google ai studio, and ollama (local models) (in #0042b93, #f3c64c2).
* Implemented a fully local option for AI models (in #231f2c8, #482157f).
* Users can now use local embeddings as an option within the package (in #0042b93).
Expand Down
132 changes: 98 additions & 34 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#' `gptstudio_response_process()` for further processing. Defaults to `FALSE`.
#' Refer to `gptstudio_response_process()` for more details.,
#' @param where A character string indicating the location or environment where
#' the chat is taking place. Options are `c("", "source", and "shiny")`. The
#' the chat is taking place. Options are `c("console", "source", and "shiny")`. The
#' default is `""`, which means the chat is taking place in the R console.
#' @param ... Reserved for future use.
#'
Expand Down Expand Up @@ -85,7 +85,7 @@ chat <- function(prompt,
task = NULL,
custom_prompt = NULL,
process_response = FALSE,
where = "",
where = "console",
...) {
if (rlang::is_false(stream) || service %in% c("google", "azure_openai", "huggingface")) {
response <-
Expand All @@ -110,38 +110,102 @@ chat <- function(prompt,
response$response
}
} else {
if (where != "") where <- paste0("_", where)
callback <- glue::glue("create_stream_handler_{service}{where}") |>
get()
switch(service,
"openai" = {
response <- stream_chat_openai(
prompt = prompt,
element_callback = callback(),
model = model
)
},
"anthropic" = {
response <- stream_chat_anthropic(
prompt = prompt,
element_callback = callback(),
model = model
)
},
"perplexity" = {
response <- stream_chat_perplexity(
prompt = prompt,
element_callback = callback(),
model = model
)
},
"cohere" = {
response <- stream_chat_cohere(
prompt = prompt,
element_callback = callback(),
model = model
)
}
stream_chat(
prompt = prompt,
service = service,
r = NULL,
output_id = NULL,
where = where
)
}
}

#' Ghost Chat
#'
#' @inheritParams chat
#'
ghost_chat <- function(service = getOption("gpttools.service", "openai"),
stream = TRUE,
where = "source") {
context <- get_cursor_context()

instructions <- glue::glue(
"You are an expert coding assistant that provides brief code suggestions
directly into the files as code. Your response will go directly into an
.{context$file_ext} file. You response should only contain code or code
comments. Do not add freetext.
You are given context above and below the current cursor position.

Here is an example:

library(tidyverse)

p1 <-
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
geom_smooth(method = 'lm') +
labs(title = 'MPG vs. Weight', x = 'Miles per Gallon', y = 'Weight') +
[[start here]]

ggsave(\"myplot.png\", p1)

Your reponse begins at the placeholder [[start_here]].

Here is the context:

{context$above}
{context$below}"
)

stream_chat(
prompt = instructions,
service = service,
r = NULL,
output_id = NULL,
where = where
)
}

get_cursor_context <- function(context_lines = 20,
placeholder = "[[start_here]]") {
doc <- rstudioapi::getActiveDocumentContext()
cursor_line <- doc$selection[[1]]$range$start[1]
cursor_pos <- doc$selection[[1]]$range$end
start_line <- max(1, cursor_line - context_lines)
end_line <- min(length(doc$content), cursor_line + context_lines)

original_str <- doc$contents[cursor_line]
doc$contents[cursor_line] <-
stringr::str_c(
stringr::str_sub(original_str, end = cursor_pos[2] - 1),
placeholder,
stringr::str_sub(original_str, start = cursor_pos[2])
)

context_above <- if (start_line < cursor_line) {
doc$content[(start_line):(cursor_line)] |>
paste0(collapse = "\n")
} else {
character(0)
}

context_below <- if (end_line > cursor_line) {
doc$content[(cursor_line + 1):end_line] |>
paste0(collapse = "\n")
} else {
character(0)
}

if (doc$path == "") {
file_ext <- "R"
} else {
file_ext <- doc$path |> tools::file_ext()
}

list(
above = context_above,
below = context_below,
cursor = cursor_pos,
file_ext = file_ext
)
}
7 changes: 4 additions & 3 deletions R/history.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,15 @@ chat_with_context <- function(query,
answer <-
stream_chat_openai(
prompt = simple_prompt,
element_callback = create_stream_handler()
element_callback = create_handler(service)
)
} else {
stream_chat_shiny(
stream_chat(
prompt = simple_prompt,
service = service,
r = rv,
output_id = "streaming"
output_id = "streaming",
where = "shiny"
)
answer <- rv$response
}
Expand Down
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
45 changes: 33 additions & 12 deletions R/stream-chat.R
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
stream_chat_shiny <- function(prompt, service, r, output_id) {
stream_chat <- function(prompt,
service = getOption("gpttools.service"),
r = NULL,
output_id = "streaming",
where = "console") {
switch(service,
"openai" = {
response <- stream_chat_openai(
prompt = prompt,
element_callback = create_handler_for_shiny("openai", r, output_id)
element_callback = create_handler("openai", r, output_id, where)
)
},
"anthropic" = {
response <- stream_chat_anthropic(
prompt = prompt,
element_callback = create_handler_for_shiny("anthropic", r, output_id)
element_callback = create_handler("anthropic", r, output_id, where)
)
},
"perplexity" = {
response <- stream_chat_perplexity(
prompt = prompt,
element_callback = create_handler_for_shiny("perplexity", r, output_id)
element_callback = create_handler("perplexity", r, output_id, where)
)
},
"cohere" = {
response <- stream_chat_cohere(
prompt = prompt,
element_callback = create_handler_for_shiny("cohere", r, output_id)
element_callback = create_handler("cohere", r, output_id, where)
)
},
"ollama" = {
response <- stream_chat_ollama(
prompt = prompt,
element_callback = create_handler_for_shiny("ollama", r, output_id)
element_callback = create_handler("ollama", r, output_id, where)
)
}
)
}

create_handler_for_shiny <- function(service = "openai", r, output_id = "streaming") {
create_handler <- function(service = "openai",
r,
output_id = "streaming",
where = "console") {
env <- rlang::env()
env$resp <- NULL
env$full_resp <- NULL
Expand Down Expand Up @@ -62,11 +69,25 @@ create_handler_for_shiny <- function(service = "openai", r, output_id = "streami
jsonlite::fromJSON() |>
purrr::pluck(!!!new_pluck)
env$full_resp <- paste0(env$full_resp, parsed)
shinyjs::html(
output_id,
shiny::markdown(paste("**Assistant**", env$full_resp, sep = "\n\n"))
)
r$response <- env$full_resp

if (where == "shiny") {
shinyjs::html(
output_id,
shiny::markdown(paste("**Assistant**", env$full_resp, sep = "\n\n"))
)

r$response <- env$full_resp
} else if (where == "console") {
cat(parsed)
} else if (where == "source") {
rlang::check_installed("pak")
rlang::check_installed("rstudioapi",
version = "0.15.0.9",
action = \(pkg, ...) pak::pak("rstudio/rstudioapi")
)
rstudioapi::setGhostText(env$full_resp)
}

env$resp <- stringr::str_split(env$resp, pattern)
env$resp <- env$resp[[1]][[length(env$resp[[1]])]]
}
Expand Down
Loading
Loading