Skip to content

Commit

Permalink
Add /models API.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Jul 25, 2023
1 parent 5712c6c commit 0f0e1db
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ QQ Group for communication: 30920262

The API service starts at port 3000, and the data input and output format follow the Openai API specification.

* `/v1/models`
* `/models`
* `/v1/chat/completions`
* `/chat/completions`
* `/v1/completions`
Expand Down
2 changes: 2 additions & 0 deletions README_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ OpenAIのChatGPT APIインターフェースと互換性があります。

APIサービスは3000ポートで開始され、データ入力と出力の形式はOpenai APIの規格に従います。

* `/v1/models`
* `/models`
* `/v1/chat/completions`
* `/chat/completions`
* `/v1/completions`
Expand Down
2 changes: 2 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@

API 服务开启于 3000 端口, 数据输入已经输出格式遵循Openai API 规范。

- `/v1/models`
- `/models`
- `/v1/chat/completions`
- `/chat/completions`
- `/v1/completions`
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use anyhow::Result;
use axum::{routing::post, Router};
use axum::{
routing::{get, post},
Router,
};
use clap::Parser;
use flume::Receiver;
use itertools::Itertools;
Expand All @@ -21,6 +24,7 @@ use web_rwkv::{BackedModelState, Environment, Model, Tokenizer};
mod chat;
mod completion;
mod embedding;
mod models;
mod sampler;

use crate::{
Expand Down Expand Up @@ -339,6 +343,8 @@ async fn main() -> Result<()> {
std::thread::spawn(move || model_task(env, model_path, tokenizer_path, receiver));

let app = Router::new()
.route("/models", get(models::models))
.route("/v1/models", get(models::models))
.route("/completions", post(completion::completions))
.route("/v1/completions", post(completion::completions))
.route("/chat/completions", post(chat::chat_completions))
Expand Down
26 changes: 26 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use axum::{extract::State, Json};
use serde::Serialize;

use crate::ThreadState;

#[derive(Debug, Serialize)]
pub struct ModelChoice {
pub object: String,
pub id: String,
}

#[derive(Debug, Serialize)]
pub struct ModelResponse {
pub data: Vec<ModelChoice>,
}

pub async fn models(
State(ThreadState { model_name, .. }): State<ThreadState>,
) -> Json<ModelResponse> {
Json(ModelResponse {
data: vec![ModelChoice {
object: "models".into(),
id: model_name,
}],
})
}

0 comments on commit 0f0e1db

Please sign in to comment.