Skip to content

Commit

Permalink
first pass at gpt chat
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Jan 1, 2023
1 parent c3b26e4 commit e46e3e1
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 15 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ LazyData: true
Imports:
assertthat,
cli,
fs,
glue,
jsonlite,
magrittr,
miniUI,
purrr,
readr,
rlang,
rstudioapi (>= 0.12),
shiny,
skimr,
stringr,
usethis
RoxygenNote: 7.2.3
Suggests:
covr,
httr,
mockr,
testthat (>= 3.0.0),
quarto,
uuid,
withr
Config/testthat/edition: 3
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export(collect_dataframes)
export(comAddin)
export(create_completion2)
export(create_edit2)
export(create_gpt_chat)
export(gpt_addin)
export(gpt_create)
export(gpt_edit)
export(gpt_insert)
export(openai_chat)
export(prep_data_prompt)
export(run_gpt_freeform)
export(run_specify_model)
Expand Down
3 changes: 2 additions & 1 deletion R/avAddin.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' active voice Addin
#'
#' Call this function as a Rstudio addin to ask GPT to change selected text into the active voice
#' Call this function as a Rstudio addin to ask GPT to change selected text
#' into the active voice
#'
#' @export
avAddin <- function() {
Expand Down
79 changes: 79 additions & 0 deletions R/chat_gpt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Create a GPT Q&A File
#'
#' This function creates a GPT Q&A file for use with OpenAI's GPT models.
#'
#' @param path The path of the file to be created. Defaults to "gpt_q_and_a.qmd"
#'
#' @export
#'
create_gpt_chat <- function(path = "gpt_q_and_a.qmd") {
rlang::check_installed("quarto")
if (is.null(quarto::quarto_path())) {
cli::cli_abort(c(
"x" = "Quarto is not installed.",
"i" = "Visit {.url https://quarto.org/docs/get-started/} to install."
))
}

confirm_create <-
usethis::ui_yeah("Would you like to create the GPT Q&A file in {getwd()}?")

if (confirm_create) {
if (fs::file_exists(path)) {
cli::cli_alert_warning("File already exists at {path}.")
cli::cli_alert_info("Opening that file instead.")
} else {
qna_file <- system.file("templates/gpt_q_and_a.qmd",
package = "gptstudio"
)
fs::file_copy(qna_file, new_path = fs::path(getwd(), path))
}
usethis::edit_file(path)
}
}



#' Generate an answer to a question using OpenAI
#'
#' @export
#'
openai_chat <- function() {
active_doc <- rstudioapi::getSourceEditorContext()
if (fs::path_ext(active_doc$path) != "qmd") {
cli::cli_inform("Path: {active_doc$path}")
cli::cli_abort("Quarto Q&A document is not selected. Please try again.")
}
content <- readr::read_lines(active_doc$path)
first_question <- which(stringr::str_starts(content, "Q"))[1]
if (rlang::is_empty(first_question)) {
cli::cli_abort("First question not found.")
} else {
trimmed_content <- content[first_question:length(content)]
}

if (!rlang::is_empty(trimmed_content)) {
cli::cli_alert_info("Querying GPT for an answer...")
response <-
openai_create_completion(
model = "text-davinci-003",
prompt = stringr::str_c(trimmed_content, collapse = "\n"),
max_tokens = 500,
temperature = 0.5,
openai_api_key = Sys.getenv("OPENAI_API_KEY"),
openai_organization = NULL
)
answer <- response$choice$text
} else {
cli::cli_abort("Question no found in the document. Please try again.")
}

if (!rlang::is_empty(answer)) {
cli::cli_inform("Answer: {answer}")
improved_text <- stringr::str_c(answer, "\n\nQuestion: ", collapse = "")
id <- active_doc$id
rstudioapi::insertText(improved_text, id = id)
} else {
cli::cli_abort("No answer returned or API query failed. Please try again.")
}
}
13 changes: 10 additions & 3 deletions R/document_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ collect_column_types <- function(data) {
#' Summarize a data frame using one of three methods.
#'
#' @param data A data frame
#' @param method A character vector specifying the method to use for summarizing the data.
#' Must be one of "skimr", "skimr_lite", "column_types", or "summary". Default is "skimr".
#' @param method A character vector specifying the method to use for
#' summarizing the data. Must be one of "skimr", "skimr_lite", "column_types",
#' or "summary". Default is "skimr".
#'
#' @return Summarized data according to specified method
summarize_data <- function(data, method = c("skimr", "skimr_lite", "column_types", "summary")) {
summarize_data <- function(data,
method = c(
"skimr",
"skimr_lite",
"column_types",
"summary"
)) {
assertthat::assert_that(is.data.frame(data))

rlang::arg_match(method)
Expand Down
6 changes: 3 additions & 3 deletions R/gpt_queries.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ warn_about_openai_pkg <- function() {
current release of the openai package but it is not installed.
Please install it with: `install.packages(\"openai\")`"
rlang::warn(message,
.frequency = "regularly",
.frequency_id = "openai_pkg",
use_cli_format = TRUE
.frequency = "regularly",
.frequency_id = "openai_pkg",
use_cli_format = TRUE
)
}
}
3 changes: 2 additions & 1 deletion R/sandgAddin.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' spelling and grammar Addin
#'
#' Call this function as a Rstudio addin to ask GPT to improve spelling and grammar of selected text.
#' Call this function as a Rstudio addin to ask GPT to improve spelling and
#' grammar of selected text.
#'
#' @export
sandgAddin <- function() {
Expand Down
3 changes: 2 additions & 1 deletion R/wpAddin.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' write/code from prompt Addin
#'
#' Call this function as a Rstudio addin to ask GPT to write text or code from a descriptive prompt
#' Call this function as a Rstudio addin to ask GPT to write text or code from
#' a descriptive prompt
#'
#' @export
wpAddin <- function() {
Expand Down
10 changes: 10 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ Name: Specify model editor
Description: Uses OpenAI's GPT-3.5 specify a model in R with user input
Binding: specify_model_addin
Interactive: true

Name: GPT Chat
Description: Uses OpenAI's GPT-3.5 for Q & A
Binding: openai_chat
Interactive: true

Name: Start GPT Chat
Description: Create file to start GPT Q & A session
Binding: create_gpt_chat
Interactive: true
10 changes: 10 additions & 0 deletions inst/templates/gpt_q_and_a.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: "GPT Q&A"
editor: visual
---
## Question & Answering with OpenAI's GPT Models

Question:

Answer:

3 changes: 2 additions & 1 deletion man/avAddin.Rd

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

14 changes: 14 additions & 0 deletions man/create_gpt_chat.Rd

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

11 changes: 11 additions & 0 deletions man/openai_chat.Rd

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

3 changes: 2 additions & 1 deletion man/sandgAddin.Rd

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

5 changes: 3 additions & 2 deletions man/summarize_data.Rd

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

3 changes: 2 additions & 1 deletion man/wpAddin.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("Spelling and grammer editing works", {
test_that("Commenting code works", {
mockr::local_mock(
gpt_edit = function(model = "code-davinci-edit-001",
instruction = "add comments to each line of code, explaining what the code does",
instruction = "some instructions",
temperature = 0.1) {
list("text" = "new text")
}
Expand Down

0 comments on commit e46e3e1

Please sign in to comment.