Skip to content

Commit

Permalink
feat: manage outputs longer than token limit
Browse files Browse the repository at this point in the history
The changes improve the handling of LLM responses that exceed the output token limit. Instead of stopping the process, the user is now prompted to decide how to proceed. They can choose to let the LLM complete the answer, keep the incomplete answer, or stop the process. The incomplete answer is also saved to a file for reference.
  • Loading branch information
bakaburg1 committed May 22, 2024
1 parent e9e578a commit 18cfada
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions R/LLM_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,59 @@ prompt_llm <- function(

# Return the response
purrr::imap_chr(parsed$choices, \(ans, i) {
ans_content <- ans$message$content

# Manage the case when the answer is cut off due to exceeding the
# output token limit
if (ans$finish_reason == "length") {
i <- if (length(parsed$choices) > 1) paste0(" ", i, " ") else " "

stop("Answer", i, "exhausted the context window!")
}

ans$message$content
warning("Answer", i, "exhausted the context window!")

file_name <- paste0("output_", Sys.time(), ".txt")

warning(
"Answer", i, "exhausted the context window!\n",
"The answer has been saved to a file: ", file_name
)

readr::write_lines(ans_content, file_name)

choice <- utils::menu(
c(
"Try to complete the answer",
"Keep the incomplete answer",
"Stop the process"),
title = "How do you want to proceed?"
)

if (choice == 1) {
# Ask the model to continue the answer
messages_new <- c(
messages,
list(list(
role = "assistant",
content = ans_content
)),
list(list(
role = "user",
content = "continue"
))
)

ans_new <- prompt_llm(
messages_new, provider = provider, params = params,
force_json = force_json,
log_request = log_request, ...
)

return(paste0(ans_content, ans_new))
} else if (choice == 2) {
return(ans_content)
} else {
stop("The process has been stopped.")
}
} else ans_content
})
}

Expand Down

0 comments on commit 18cfada

Please # to comment.