Skip to content

Commit

Permalink
feat(core, webserver): add /v1/models api (#3164)
Browse files Browse the repository at this point in the history
* draft: feat(core, webserver): add /v1/models api

* seperate models file

* update model api

* embedding model pattern match

* polish

* fix comments

* rename return structure

* [autofix.ci] apply automated fixes

* fix comments except pass state

* [autofix.ci] apply automated fixes

* pass config to model api through state

* [autofix.ci] apply automated fixes

* draft: to be fix shared reference

* fix config pass through state

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
antimonyGu and autofix-ci[bot] authored Sep 30, 2024
1 parent dab77d0 commit 906e2c2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/tabby-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ pub struct HttpModelConfig {
/// Used by Completion API to construct a chat model.
#[builder(default)]
pub chat_template: Option<String>,

/// Used by Chat/Completion API allowing users to get supported models info.
#[builder(default)]
pub supported_models: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
2 changes: 2 additions & 0 deletions crates/tabby/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ mod chat;
mod completions;
mod events;
mod health;
mod models;
mod server_setting;

pub use chat::*;
pub use completions::*;
pub use events::*;
pub use health::*;
pub use models::*;
pub use server_setting::*;
53 changes: 53 additions & 0 deletions crates/tabby/src/routes/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::sync::Arc;

use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub struct ModelInfo {
completion: Option<Vec<String>>,
chat: Option<Vec<String>>,
}

impl From<tabby_common::config::Config> for ModelInfo {
fn from(value: tabby_common::config::Config) -> Self {
let models = value.model;
let mut http_model_configs: ModelInfo = ModelInfo {
completion: None,
chat: None,
};

if let Some(tabby_common::config::ModelConfig::Http(completion_http_config)) =
models.completion
{
if let Some(models) = completion_http_config.supported_models {
http_model_configs.completion = Some(models.clone());
}
}

if let Some(tabby_common::config::ModelConfig::Http(chat_http_config)) = models.chat {
if let Some(models) = chat_http_config.supported_models {
http_model_configs.chat = Some(models.clone());
}
}

http_model_configs
}
}

#[utoipa::path(
get,
path = "/v1beta/models",
tag = "v1beta",
operation_id = "config",
responses(
(status = 200, description = "Success", body = ServerSetting, content_type = "application/json"),
),
security(
("token" = [])
)
)]
pub async fn models(State(state): State<Arc<ModelInfo>>) -> Json<ModelInfo> {
Json(state.as_ref().clone())
}
2 changes: 2 additions & 0 deletions crates/tabby/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ async fn api_router(
"/v1/health",
routing::get(routes::health).with_state(health_state),
)
.route("/v1beta/models", routing::get(routes::models))
.with_state(Arc::new(config.clone().into()))
});

if let Some(completion_state) = completion_state {
Expand Down

0 comments on commit 906e2c2

Please sign in to comment.