Skip to content

Commit

Permalink
docs: improve code comments and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaburg1 committed May 2, 2024
1 parent cd4227b commit 4b689ff
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions R/LLM_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ process_messages <- function(messages) {

}

#' Interrogate Language Model
#' Interrogate a Language Model
#'
#' This function sends requests to a specified language model provider (OpenAI,
#' Azure, or a locally running LLM server) and returns the response. It handles
Expand Down Expand Up @@ -183,7 +183,7 @@ prompt_llm <- function(

body$messages <- messages

# Force the LLM to answer in JSON format (only openai and azure)
# Force the LLM to answer in JSON format (not all models support this)
if (force_json) {
body$response_format <- list("type" = "json_object")
}
Expand All @@ -192,7 +192,8 @@ prompt_llm <- function(
llm_fun <- paste0("use_", provider, "_llm")

if (!exists(llm_fun, mode = "function")) {
stop("Unsupported LLM provider.")
stop("Unsupported LLM provider.
You can set it project-wide using the minutemaker_llm_provider option.")
}

llm_fun <- get(llm_fun)
Expand Down Expand Up @@ -252,13 +253,16 @@ prompt_llm <- function(
) |> message()
}

answer <- parsed$choices[[1]]
# Return the response
purrr::imap_chr(parsed$choices, \(ans, i) {
if (ans$finish_reason == "length") {
i <- if (length(parsed$choices) > 1) paste0(" ", i, " ") else " "

if (answer$finish_reason == "length") {
stop("Answer exhausted the context window!")
}
stop("Answer", i, "exhausted the context window!")
}

answer$message$content
ans$message$content
})
}

#' Use OpenAI Language Model
Expand Down

0 comments on commit 4b689ff

Please # to comment.