Skip to content

Commit

Permalink
big fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dwulff committed Nov 8, 2023
1 parent 36d4650 commit 4ed043b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
24 changes: 13 additions & 11 deletions R/infer.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' @param role a \code{character} string specifying the systems role in place of \code{role} in the general system instruction to the model. Default is \code{"assistant"}.
#' @param system a \code{character} string specifying the general system instruction to the model. Default is \code{"You are a helpful {role} who provides short, specific, and accurate category labels."}.
#' @param instruct a \code{character} string specifying the instruction for the model. Must contain the placeholder \code{"{examples}"}. Default is \code{"Generate a specific and accurate one or two word category label that captures the common meaning of the following examples: {examples}. Place '@' before and after the category label."}.
#' @param attempts an optional \code{integer} specifying the number of API request attempts for a given set of input labels.
#' @param verbose a \code{logical} specifying whether to show messages.'
#'
#' @return The function returns a \code{character} vector of category labels.
Expand All @@ -33,8 +34,9 @@ er_infer_labels <- function(labels,
api = "huggingface",
model = NULL,
role = "assistant",
instruct = NULL,
system = NULL,
instruct = NULL,
attempts = 50,
verbose = FALSE){


Expand All @@ -56,10 +58,10 @@ er_infer_labels <- function(labels,

# set instruct & system
if(is.null(instruct)){
instruct = glue::glue("Generate a specific and accurate one- or two- word category label that summarizes the following examples: {examples}, The best label can be among the examples. Place '@' before and after the label.")
instruct = "Generate a specific and accurate one- or two- word category label that summarizes the following examples: {examples}, The best label can be among the examples. Place '@' before and after the label."
}
if(is.null(system)){
system = glue::glue("You are a helpful {role}.")
system = "You are a helpful {role}."
}

# HUGGINGFACE -------
Expand All @@ -76,11 +78,11 @@ er_infer_labels <- function(labels,
# iterate through labels
for(i in 1:length(labels)){

n_attempts = 11
for(j in 1:n_attempts){
# do attempts
for(j in 1:attempts){

# break if n_attempts reached
if(j == n_attempts) stop("Too many errors. See messages printed in console.")
# break if attempts reached
if(j == attempts) stop("Too many errors. See messages printed in console.")

# setup prompt
examples = paste0('"',labels[[i]],'"') %>% paste0(collapse = ", ")
Expand Down Expand Up @@ -140,11 +142,11 @@ er_infer_labels <- function(labels,
# iterate through labels
for(i in 1:length(labels)){

n_attempts = 11
for(j in 1:n_attempts){
# do attempts
for(j in 1:attempts){

# break if n_attempts reached
if(j == n_attempts) stop("Too many errors. See messages printed in console.")
# break if attempts reached
if(j == attempts) stop("Too many errors. See messages printed in console.")

# setup prompt
if(is.null(model)) model = "gpt-4"
Expand Down
5 changes: 3 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ plot.embedR_tbl <- function(data,
size = !!dplyr::enquo(size),
label = !!dplyr::enquo(label))


# adjust labels
label_filter = dplyr::enquo(label_filter)
if(rlang::expr_text(label_filter)!= "~NULL"){
Expand All @@ -79,12 +78,14 @@ plot.embedR_tbl <- function(data,
ggplot2::geom_point() + ggplot2::theme_minimal()

# handle labels
if(!is.null(label)){
if("label" %in% names(data)){

p = p + ggrepel::geom_text_repel(point.padding = ggplot2::unit(pt_padding,"npc"),
box.padding = ggplot2::unit(box_padding,"npc"),
size = label_size,
min.segment.length = 0,
max.overlaps = 1000)

}

# add colors
Expand Down
7 changes: 5 additions & 2 deletions man/er_infer_labels.Rd

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

0 comments on commit 4ed043b

Please sign in to comment.