-
Notifications
You must be signed in to change notification settings - Fork 12k
Add the endpoints /api/tags and /api/chat #13659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -3702,6 +3702,7 @@ int main(int argc, char ** argv) { | |
"/health", | ||
"/models", | ||
"/v1/models", | ||
"/api/tags" | ||
}; | ||
|
||
// If API key is not set, skip validation | ||
|
@@ -3740,7 +3741,7 @@ int main(int argc, char ** argv) { | |
if (req.path == "/" || tmp.back() == "html") { | ||
res.set_content(reinterpret_cast<const char*>(loading_html), loading_html_len, "text/html; charset=utf-8"); | ||
res.status = 503; | ||
} else if (req.path == "/models" || req.path == "/v1/models") { | ||
} else if (req.path == "/models" || req.path == "/v1/models" || req.path == "/api/tags") { | ||
// allow the models endpoint to be accessed during loading | ||
return true; | ||
} else { | ||
|
@@ -4076,7 +4077,21 @@ int main(int argc, char ** argv) { | |
res_ok(res, {{ "success", true }}); | ||
}; | ||
|
||
const auto handle_api_show = [&ctx_server, &res_ok](const httplib::Request &, httplib::Response & res) { | ||
const auto handle_api_show = [&ctx_server, &state, &res_ok](const httplib::Request &, httplib::Response & res) { | ||
R-Dson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server_state current_state = state.load(); | ||
const auto* model = llama_get_model(ctx_server.ctx); | ||
|
||
// Get basic model info | ||
char arch_buf[64] = {0}; | ||
char param_size_buf[64] = {0}; | ||
llama_model_meta_val_str(model, "general.architecture", arch_buf, sizeof(arch_buf)); | ||
llama_model_meta_val_str(model, "general.parameter_count", param_size_buf, sizeof(param_size_buf)); | ||
|
||
json model_meta = nullptr; | ||
if (current_state == SERVER_STATE_READY) { | ||
model_meta = ctx_server.model_meta(); | ||
} | ||
|
||
json data = { | ||
{ | ||
"template", common_chat_templates_source(ctx_server.chat_templates.get()), | ||
|
@@ -4086,6 +4101,19 @@ int main(int argc, char ** argv) { | |
{ "llama.context_length", ctx_server.slots.back().n_ctx, }, | ||
} | ||
}, | ||
{"modelfile", ""}, // Specific to ollama and does not seem to be needed | ||
{"parameters", ""}, // TODO: add parameters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the API client expect this to be a string or an object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. modelfile is a string. Parameters i am not sure, but it seems to be enough as is. |
||
{"template", common_chat_templates_source(ctx_server.chat_templates.get())}, | ||
{"details", { | ||
{"parent_model", ""}, // TODO: add parent model if available | ||
{"format", "gguf"}, | ||
{"family", arch_buf}, | ||
{"families", {arch_buf}}, | ||
{"parameter_size", param_size_buf}, | ||
{"quantization_level", ""} // TODO: add quantization level if available | ||
}}, | ||
{"model_info", model_meta}, | ||
{"capabilities", {"completion"}} // TODO: add other capabilities if available | ||
}; | ||
|
||
res_ok(res, data); | ||
|
@@ -4409,8 +4437,43 @@ int main(int argc, char ** argv) { | |
if (current_state == SERVER_STATE_READY) { | ||
model_meta = ctx_server.model_meta(); | ||
} | ||
// Get file metadata | ||
struct stat file_stat; | ||
stat(params.model.path.c_str(), &file_stat); | ||
|
||
// Convert modified time to ISO 8601 | ||
char modified_buf[64]; | ||
strftime(modified_buf, sizeof(modified_buf), "%Y-%m-%dT%H:%M:%S%z", localtime(&file_stat.st_mtime)); | ||
|
||
R-Dson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const auto* model = llama_get_model(ctx_server.ctx); | ||
char arch_buf[64] = {0}; | ||
char param_size_buf[64] = {0}; | ||
llama_model_meta_val_str(model, "general.architecture", arch_buf, sizeof(arch_buf)); | ||
llama_model_meta_val_str(model, "general.parameter_count", param_size_buf, sizeof(param_size_buf)); | ||
R-Dson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
json models = { | ||
{"models", { | ||
{ | ||
{"name", params.model_alias.empty() ? params.model.path : params.model_alias}, | ||
{"model", params.model_alias.empty() ? params.model.path : params.model_alias}, | ||
{"modified_at", modified_buf}, | ||
{"size", file_stat.st_size}, | ||
{"digest", ""}, // TODO: add digest | ||
R-Dson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{"type", "model"}, | ||
{"description", ""}, | ||
{"tags", {arch_buf}}, | ||
{"capabilities", {"completion"}}, | ||
{"parameters", ""}, // TODO: add parameters | ||
{"details", { | ||
{"parent_model", ""}, // TODO: Add parent_model | ||
{"format", "gguf"}, | ||
{"family", arch_buf}, | ||
{"families", {arch_buf}}, | ||
{"parameter_size", param_size_buf}, | ||
{"quantization_level", ""} // TODO: add quantization level if available | ||
}} | ||
} | ||
}}, | ||
{"object", "list"}, | ||
{"data", { | ||
{ | ||
|
@@ -4420,7 +4483,7 @@ int main(int argc, char ** argv) { | |
{"owned_by", "llamacpp"}, | ||
{"meta", model_meta}, | ||
}, | ||
}} | ||
}} | ||
}; | ||
|
||
res_ok(res, models); | ||
|
@@ -4748,11 +4811,13 @@ int main(int argc, char ** argv) { | |
svr->Post("/api/show", handle_api_show); | ||
svr->Get ("/models", handle_models); // public endpoint (no API key check) | ||
svr->Get ("/v1/models", handle_models); // public endpoint (no API key check) | ||
svr->Get ("/api/tags", handle_models); // ollama specific endpoint. public endpoint (no API key check) | ||
svr->Post("/completion", handle_completions); // legacy | ||
svr->Post("/completions", handle_completions); | ||
svr->Post("/v1/completions", handle_completions_oai); | ||
svr->Post("/chat/completions", handle_chat_completions); | ||
svr->Post("/v1/chat/completions", handle_chat_completions); | ||
svr->Post("/api/chat", handle_chat_completions); // ollama specific endpoint | ||
svr->Post("/infill", handle_infill); | ||
svr->Post("/embedding", handle_embeddings); // legacy | ||
svr->Post("/embeddings", handle_embeddings); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
during loading,
common_chat_templates_source
call will failUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to remove that endpoint from the if-case?
Edit:
The current code does not use
common_chat_templates_source
in that endpoint after the latest changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I think it's ok to keep this then