-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #140
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' @include provider-openai.R | ||
#' @include content.R | ||
NULL | ||
|
||
#' Chat with a model hosted by vLLM | ||
#' | ||
#' @description | ||
#' [vLLM](https://docs.vllm.ai/en/latest/) is an open source library that | ||
#' provides an efficient and convenient LLMs model server. You can use | ||
#' `chat_vllm()` to connect to endpoints powered by vLLM. | ||
#' | ||
#' @inheritParams chat_openai | ||
#' @inherit chat_openai return | ||
#' @export | ||
chat_vllm <- function(base_url, | ||
system_prompt = NULL, | ||
turns = NULL, | ||
model, | ||
seed = NULL, | ||
api_args = list(), | ||
api_key = vllm_key(), | ||
echo = NULL) { | ||
|
||
check_string(base_url) | ||
turns <- normalize_turns(turns, system_prompt) | ||
check_string(api_key) | ||
if (missing(model)) { | ||
models <- vllm_models(base_url, api_key) | ||
cli::cli_abort(c( | ||
"Must specify {.arg model}.", | ||
i = "Available models: {.str {models}}." | ||
)) | ||
} | ||
if (is_testing() && is.null(seed)) { | ||
seed <- seed %||% 1014 | ||
} | ||
echo <- check_echo(echo) | ||
|
||
provider <- ProviderVllm( | ||
base_url = base_url, | ||
model = model, | ||
seed = seed, | ||
extra_args = api_args, | ||
api_key = api_key | ||
) | ||
Chat$new(provider = provider, turns = turns, echo = echo) | ||
} | ||
|
||
ProviderVllm <- new_class( | ||
"ProviderVllm", | ||
parent = ProviderOpenAI, | ||
package = "elmer", | ||
) | ||
|
||
# Just like OpenAI but no strict | ||
method(as_json, list(ProviderVllm, ToolDef)) <- function(provider, x) { | ||
list( | ||
type = "function", | ||
"function" = compact(list( | ||
name = x@name, | ||
description = x@description, | ||
parameters = as_json(provider, x@arguments) | ||
)) | ||
) | ||
} | ||
|
||
vllm_key <- function() { | ||
key_get("VLLM_KEY") | ||
} | ||
|
||
vllm_models <- function(base_url, key = vllm_key()) { | ||
req <- request(base_url) | ||
req <- req_auth_bearer_token(req, key) | ||
req <- req_url_path(req, "/v1/models") | ||
resp <- req_perform(req) | ||
json <- resp_body_json(resp) | ||
|
||
map_chr(json$data, "[[", "id") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.