Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add open router backend #264

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Collate:
'provider-github.R'
'provider-groq.R'
'provider-ollama.R'
'provider-openrouter.R'
'provider-perplexity.R'
'provider-snowflake.R'
'provider-vllm.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(chat_github)
export(chat_groq)
export(chat_ollama)
export(chat_openai)
export(chat_openrouter)
export(chat_perplexity)
export(chat_snowflake)
export(chat_vllm)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ellmer (development version)

* `chat_openrouter()` provides support for OpenRouter models (#212)

* `chat_deepseek()` provides support for DeepSeek models (#242)

* `option(ellmer_verbosity)` is no longer supported; instead use the standard httr2 verbosity functions which now support streaming data.
Expand Down
157 changes: 157 additions & 0 deletions R/provider-openrouter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#' @include provider-openai.R
NULL

#' Chat with one of the many models hosted on OpenRouter
#'
#' @description
#' Sign up at <https://openrouter.ai>.
#'
#' Support for features depends on the underlying model that you use; see
#' <https://openrouter.ai/models> for details.
#'
#' @export
#' @family chatbots
#' @inheritParams chat_openai
#' @inherit chat_openai return
#' @examples
#' \dontrun{
#' chat <- chat_openrouter()
#' chat$chat("Tell me three jokes about statisticians")
#' }
chat_openrouter <- function(
system_prompt = NULL,
turns = NULL,
api_key = openrouter_key(),
model = NULL,
seed = NULL,
api_args = list(),
echo = c("none", "text", "all")
) {
turns <- normalize_turns(turns, system_prompt)
model <- set_default(model, "gpt-4o")
echo <- check_echo(echo)

if (is_testing() && is.null(seed)) {
seed <- seed %||% 1014
}

provider <- ProviderOpenRouter(
base_url = "https://openrouter.ai/api/v1",
model = model,
seed = seed,
extra_args = api_args,
api_key = api_key
)
Chat$new(provider = provider, turns = turns, echo = echo)
}

chat_openrouter_test <- function(...) {
chat_openrouter(..., model = "openai/gpt-4o-mini-2024-07-18")
}

ProviderOpenRouter <- new_class(
"ProviderOpenRouter",
parent = ProviderOpenAI,
)

openrouter_key <- function() {
key_get("OPENROUTER_API_KEY")
}

method(chat_request, ProviderOpenRouter) <- function(
provider,
stream = TRUE,
turns = list(),
tools = list(),
type = NULL,
extra_args = list()
) {
req <- chat_request(
super(provider, ProviderOpenAI),
stream = stream,
turns = turns,
tools = tools,
type = type,
extra_args = extra_args
)

# https://openrouter.ai/docs/api-keys
req <- req_headers(
req,
`HTTP-Referer` = "https://ellmer.tidyverse.org",
`X-Title` = "ellmer"
)

req
}

method(value_turn, ProviderOpenRouter) <- function(provider, result, has_type = FALSE) {
# https://openrouter.ai/docs/errors
check_openrouter_error(result$error)

value_turn(
super(provider, ProviderOpenAI),
result = result,
has_type = has_type
)
}

method(stream_parse, ProviderOpenRouter) <- function(provider, event) {
if (is.null(event)) {
cli::cli_abort("Connection closed unexpectedly")
}

if (identical(event$data, "[DONE]")) {
return(NULL)
}

result <- jsonlite::parse_json(event$data)
check_openrouter_error(result$error)
result
}

check_openrouter_error <- function(error, call = caller_env()) {
if (is.null(error)) {
return()
}
message <- error$message
if (is.null(error$metadata$raw$data)) {
details <- NULL
} else {
details <- prettify(error$metadata$raw$data)
# don't line wrap
details <- gsub(" ", "\u00a0", details, fixed = TRUE)
}

abort(
c("message", i = if (!is.null(details)) details),
call = call
)
}

method(chat_resp_stream, ProviderOpenRouter) <- function(provider, resp) {
repeat({
event <- resp_stream_sse(resp)
if (is.null(event)) {
break
}

# https://openrouter.ai/docs/responses#sse-streaming-comments
if (!identical(event$data, character())) {
break
}
Sys.sleep(0.1)
})

event
}

method(as_json, list(ProviderOpenRouter, ContentText)) <- function(provider, x) {
if (identical(x@text, "")) {
# Tool call requests can include a Content with empty text,
# but it doesn't like it if you send this back
NULL
} else {
list(type = "text", text = x@text)
}
}
2 changes: 1 addition & 1 deletion R/provider.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ value_turn <- new_generic("value_turn", "provider")
as_json <- new_generic("as_json", c("provider", "x"))

method(as_json, list(Provider, class_list)) <- function(provider, x) {
lapply(x, as_json, provider = provider)
compact(lapply(x, as_json, provider = provider))
}

method(as_json, list(Provider, ContentJson)) <- function(provider, x) {
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pretty_json <- function(x) {
jsonlite::toJSON(x, pretty = TRUE, auto_unbox = TRUE)
}

prettify <- function(x) {
tryCatch(
jsonlite::prettify(x),
error = function(cnd) x
)
}

check_echo <- function(echo = NULL) {
if (is.null(echo) || identical(echo, c("none", "text", "all"))) {
if (env_is_user_facing(parent.frame(2)) && !is_testing()) {
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ellmer supports a wide variety of model providers:
* Groq: `chat_groq()`.
* Ollama: `chat_ollama()`.
* OpenAI: `chat_openai()`.
* OpenRouter: `chat_openrouter()`.
* perplexity.ai: `chat_perplexity()`.
* Snowflake Cortex: `chat_cortex()`.
* VLLM: `chat_vllm()`.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ellmer supports a wide variety of model providers:
- Groq: `chat_groq()`.
- Ollama: `chat_ollama()`.
- OpenAI: `chat_openai()`.
- OpenRouter: `chat_openrouter()`.
- perplexity.ai: `chat_perplexity()`.
- Snowflake Cortex: `chat_cortex()`.
- VLLM: `chat_vllm()`.
Expand Down
1 change: 1 addition & 0 deletions man/chat_bedrock.Rd

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

1 change: 1 addition & 0 deletions man/chat_claude.Rd

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

1 change: 1 addition & 0 deletions man/chat_cortex.Rd

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

1 change: 1 addition & 0 deletions man/chat_databricks.Rd

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

1 change: 1 addition & 0 deletions man/chat_gemini.Rd

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

1 change: 1 addition & 0 deletions man/chat_github.Rd

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

1 change: 1 addition & 0 deletions man/chat_groq.Rd

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

1 change: 1 addition & 0 deletions man/chat_ollama.Rd

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

1 change: 1 addition & 0 deletions man/chat_openai.Rd

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

76 changes: 76 additions & 0 deletions man/chat_openrouter.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/chat_perplexity.Rd

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

Loading
Loading