Skip to content

Commit

Permalink
Set env vars in pkgdown action (tidyverse#214)
Browse files Browse the repository at this point in the history
In order to run code in vignettes. Fixes tidyverse#211

Includes two improvements for claude error handling.
  • Loading branch information
hadley authored Dec 12, 2024
1 parent 47ef8ec commit 35e20f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
permissions:
contents: write
steps:
Expand Down
41 changes: 27 additions & 14 deletions R/provider-claude.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ anthropic_key_exists <- function() {
}

method(chat_request, ProviderClaude) <- function(provider,
stream = TRUE,
turns = list(),
tools = list(),
type = NULL,
extra_args = list()) {
stream = TRUE,
turns = list(),
tools = list(),
type = NULL,
extra_args = list()) {

req <- request(provider@base_url)
# https://docs.anthropic.com/en/api/messages
Expand All @@ -87,17 +87,14 @@ method(chat_request, ProviderClaude) <- function(provider,
)

# <https://docs.anthropic.com/en/api/rate-limits>
# 529 is not documented, but we see it fairly frequently in tests
req <- req_retry(
req,
max_tries = 2,
is_transient = function(resp) resp_status(resp) %in% c(429, 503, 529)
)
req <- req_retry(req, max_tries = 2)

# <https://docs.anthropic.com/en/api/errors>
req <- req_error(req, body = function(resp) {
json <- resp_body_json(resp)
paste0(json$error$message, " [", json$error$type, "]")
if (resp_content_type(resp) == "application/json") {
json <- resp_body_json(resp)
paste0(json$error$message, " [", json$error$type, "]")
}
})

if (length(turns) >= 1 && is_system_prompt(turns[[1]])) {
Expand Down Expand Up @@ -180,7 +177,14 @@ method(stream_merge_chunks, ProviderClaude) <- function(provider, result, chunk)
result$stop_sequence <- chunk$delta$stop_sequence
result$usage$output_tokens <- chunk$usage$output_tokens
} else if (chunk$type == "error") {
cli::cli_abort("{chunk$error$message}")
if (chunk$error$type == "overloaded_error") {
# https://docs.anthropic.com/en/api/messages-streaming#error-events
# TODO: track number of retries
wait <- backoff_default(1)
Sys.sleep(wait)
} else {
cli::cli_abort("{chunk$error$message}")
}
} else {
cli::cli_inform(c("!" = "Unknown chunk type {.str {chunk$type}}."))
}
Expand Down Expand Up @@ -273,3 +277,12 @@ method(as_json, list(ProviderClaude, ToolDef)) <- function(provider, x) {
input_schema = compact(as_json(provider, x@arguments))
)
}



# Helpers ----------------------------------------------------------------

# From httr2
backoff_default <- function(i) {
round(min(stats::runif(1, min = 1, max = 2^i), 60), 1)
}

0 comments on commit 35e20f8

Please sign in to comment.