Skip to content

Commit

Permalink
enh: Grab API token from header if exists for OpenAI-compatible endpo…
Browse files Browse the repository at this point in the history
…ints
  • Loading branch information
jeffreyftang committed Feb 23, 2024
1 parent 48c29e1 commit 95c6eb4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions router/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,18 @@ example = json ! ({"error": "Incomplete generation"})),
async fn completions_v1(
default_return_full_text: Extension<bool>,
infer: Extension<Infer>,
headers: HeaderMap,
req: Json<CompletionRequest>,
) -> Result<Response, (StatusCode, Json<ErrorResponse>)> {
let req = req.0;
let mut gen_req = CompatGenerateRequest::from(req);

match headers.get("Authorization") {
Some(x) => x.strip_prefix("Bearer :").and_then(|token| gen_req.parameters.api_token = token)
// TODO: Just for testing, don't merge
None => println!("headers: {headers}")
}

// default return_full_text given the pipeline_tag
if gen_req.parameters.return_full_text.is_none() {
gen_req.parameters.return_full_text = Some(default_return_full_text.0)
Expand Down Expand Up @@ -171,11 +178,18 @@ example = json ! ({"error": "Incomplete generation"})),
async fn chat_completions_v1(
default_return_full_text: Extension<bool>,
infer: Extension<Infer>,
headers: HeaderMap,
req: Json<ChatCompletionRequest>,
) -> Result<Response, (StatusCode, Json<ErrorResponse>)> {
let req = req.0;
let mut gen_req = CompatGenerateRequest::from(req);

match headers.get("Authorization") {
Some(x) => x.strip_prefix("Bearer :").and_then(|token| gen_req.parameters.api_token = token)
// TODO: Just for testing, don't merge
None => println!("headers: {headers}")
}

// default return_full_text given the pipeline_tag
if gen_req.parameters.return_full_text.is_none() {
gen_req.parameters.return_full_text = Some(default_return_full_text.0)
Expand Down

0 comments on commit 95c6eb4

Please sign in to comment.