Skip to content

Commit

Permalink
feat: make --model optional (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys authored Oct 30, 2023
1 parent 7330d75 commit c55e448
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Switch cpu backend to llama.cpp: https://github.com/TabbyML/tabby/pull/638
* add `server.completion_timeout` to control the code completion interface timeout: https://github.com/TabbyML/tabby/pull/637
* Switch cuda backend to llama.cpp: https://github.com/TabbyML/tabby/pull/656
* Make `--model` optional, so users can create a chat only instance.

# v0.4.0

Expand Down
2 changes: 1 addition & 1 deletion crates/tabby/src/serve/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
pub struct HealthState {
model: String,
model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
chat_model: Option<String>,
device: String,
Expand Down
49 changes: 34 additions & 15 deletions crates/tabby/src/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Device {
pub struct ServeArgs {
/// Model id for `/completions` API endpoint.
#[clap(long)]
model: String,
model: Option<String>,

/// Model id for `/chat/completions` API endpoints.
#[clap(long)]
Expand All @@ -129,7 +129,9 @@ pub async fn main(config: &Config, args: &ServeArgs) {
valid_args(args);

if args.device != Device::ExperimentalHttp {
download_model(&args.model).await;
if let Some(model) = &args.model {
download_model(model).await;
}
if let Some(chat_model) = &args.chat_model {
download_model(chat_model).await;
}
Expand Down Expand Up @@ -171,20 +173,22 @@ pub async fn main(config: &Config, args: &ServeArgs) {

fn api_router(args: &ServeArgs, config: &Config) -> Router {
let index_server = Arc::new(IndexServer::new());
let completion_state = {
let completion_state = if let Some(model) = &args.model {
let (
engine,
EngineInfo {
prompt_template, ..
},
) = create_engine(&args.model, args);
) = create_engine(model, args);
let engine = Arc::new(engine);
let state = completions::CompletionState::new(
engine.clone(),
index_server.clone(),
prompt_template,
);
Arc::new(state)
Some(Arc::new(state))
} else {
None
};

let chat_state = if let Some(chat_model) = &args.chat_model {
Expand Down Expand Up @@ -215,16 +219,18 @@ fn api_router(args: &ServeArgs, config: &Config) -> Router {
)
});

routers.push({
Router::new()
.route(
"/v1/completions",
routing::post(completions::completions).with_state(completion_state),
)
.layer(TimeoutLayer::new(Duration::from_secs(
config.server.completion_timeout,
)))
});
if let Some(completion_state) = completion_state {
routers.push({
Router::new()
.route(
"/v1/completions",
routing::post(completions::completions).with_state(completion_state),
)
.layer(TimeoutLayer::new(Duration::from_secs(
config.server.completion_timeout,
)))
});
}

if let Some(chat_state) = chat_state {
routers.push({
Expand Down Expand Up @@ -279,6 +285,19 @@ trait OpenApiOverride {

impl OpenApiOverride for utoipa::openapi::OpenApi {
fn override_doc(&mut self, args: &ServeArgs) {
if args.model.is_none() {
self.paths.paths.remove("/v1/completions");
if let Some(components) = self.components.as_mut() {
components.schemas.remove("CompletionRequest");
components.schemas.remove("CompletionResponse");
components.schemas.remove("Choice");
components.schemas.remove("DebugData");
components.schemas.remove("DebugOptions");
components.schemas.remove("Segments");
components.schemas.remove("Snippet");
}
}

if args.chat_model.is_none() {
self.paths.paths.remove("/v1beta/chat/completions");

Expand Down
2 changes: 1 addition & 1 deletion crates/tabby/ui/404.html

Large diffs are not rendered by default.

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

Loading

0 comments on commit c55e448

Please sign in to comment.