Archived
nemotron 3.5 rerank 8b implementation
This commit is contained in:
1 parent
d773992abc
commit
74e7fddd79
35 files changed
+1788
-90
No files matched your search
@@ -6,8 +6,8 @@
|
||||
* None of these touch the step path (no admission, no Sink, no cancellation); each is answered on
|
||||
* the HTTP thread before it returns, so they are kept out of server.cpp.
|
||||
*
|
||||
* Deliberately absent, answering 404: vLLM's /pooling, /classify, /score and /rerank (no
|
||||
* classifier or cross-encoder head), /sleep, /wake_up and /is_sleeping (no sleep mode; weights
|
||||
* Deliberately absent, answering 404: vLLM's /pooling, /classify and /score,
|
||||
* /sleep, /wake_up and /is_sleeping (no sleep mode; weights
|
||||
* are placed once, spec §5), /invocations (a SageMaker envelope), stored Responses-API responses
|
||||
* (would need a conversation store), and llama-server's /slots and /lora-adapters (a runtime LoRA
|
||||
* adapter is chosen per request by `model`; vLLM's load and unload routes are in server.cpp).
|
||||
@@ -383,14 +383,16 @@ HttpResponse Server::handle_tokenizer_info(const HttpRequest& req) {
|
||||
/* ================================================================== capabilities */
|
||||
json Server::capabilities() const {
|
||||
json caps;
|
||||
caps["chat"] = deps_.chat != nullptr;
|
||||
caps["tools"] = deps_.chat && deps_.chat->supports_tools();
|
||||
caps["reasoning"] = deps_.chat && deps_.chat->supports_reasoning();
|
||||
const bool rerank = is_reranker(deps_.embed);
|
||||
caps["chat"] = !rerank && deps_.chat != nullptr;
|
||||
caps["tools"] = !rerank && deps_.chat && deps_.chat->supports_tools();
|
||||
caps["reasoning"] = !rerank && deps_.chat && deps_.chat->supports_reasoning();
|
||||
/* An image model reads pictures only for edits and writes no text a grammar could constrain,
|
||||
* whatever its conditioning network's processor would take. */
|
||||
const bool img = opt_.image_model || opt_.video_model;
|
||||
caps["grammar"] = !img && deps_.grammar != nullptr;
|
||||
caps["embedding"] = deps_.embed != nullptr;
|
||||
caps["grammar"] = !img && !rerank && deps_.grammar != nullptr;
|
||||
caps["embedding"] = deps_.embed != nullptr && !rerank;
|
||||
if (rerank) caps["rerank"] = true;
|
||||
caps["image"] = deps_.mm && deps_.mm->accepts(MediaKind::Image);
|
||||
caps["video"] = !img && deps_.mm && deps_.mm->accepts(MediaKind::Video);
|
||||
caps["audio"] = !img && deps_.mm && deps_.mm->accepts(MediaKind::Audio);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "server/nemotron35.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "llama/src/unicode.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace rad::server {
|
||||
bool nemotron35_retrieval_meta(const RadModelMeta* meta) {
|
||||
static bool nemotron35_meta(const RadModelMeta* meta) {
|
||||
if (!meta) return false;
|
||||
const auto is = [&](const char* key, const char* value) {
|
||||
return !std::strcmp(rad_meta_gets(meta, key, ""), value);
|
||||
@@ -14,6 +15,92 @@ bool nemotron35_retrieval_meta(const RadModelMeta* meta) {
|
||||
noncausal("text_config.is_causal");
|
||||
}
|
||||
|
||||
bool nemotron35_rerank_meta(const RadModelMeta* meta) {
|
||||
return nemotron35_meta(meta) && !std::strcmp(rad_meta_gets(meta, "architectures", ""),
|
||||
"Mistral3ForSequenceClassification");
|
||||
}
|
||||
|
||||
bool nemotron35_retrieval_meta(const RadModelMeta* meta) {
|
||||
return nemotron35_meta(meta) && !nemotron35_rerank_meta(meta);
|
||||
}
|
||||
|
||||
int nemotron35_rerank_format(const json& messages, std::vector<Nemotron35Piece>* pieces,
|
||||
std::string* why) {
|
||||
if (!pieces)
|
||||
return RAD_E_INVAL;
|
||||
pieces->clear();
|
||||
auto no = [&](const char* message) {
|
||||
if (why)
|
||||
*why = message;
|
||||
pieces->clear();
|
||||
return RAD_E_INVAL;
|
||||
};
|
||||
if (!messages.is_array() || messages.size() != 2 || !messages[0].is_object() ||
|
||||
!messages[1].is_object() || messages[0].value("role", json()) != "query" ||
|
||||
messages[1].value("role", json()) != "document" || !messages[0].contains("content") ||
|
||||
!messages[1].contains("content"))
|
||||
return no("reranking requires one text query followed by one document");
|
||||
std::string texts[2];
|
||||
json image;
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
const auto& content = messages[i]["content"];
|
||||
if (content.is_string())
|
||||
texts[i] = content.get<std::string>();
|
||||
else if (content.is_array()) {
|
||||
bool first = true;
|
||||
for (const auto& block : content) {
|
||||
if (!block.is_object())
|
||||
return no("content must contain typed text/image blocks");
|
||||
const json type = block.value("type", json());
|
||||
if (type == "text") {
|
||||
if (!block.contains("text") || !block["text"].is_string())
|
||||
return no("text blocks require string text");
|
||||
if (!first)
|
||||
texts[i] += "\n\n";
|
||||
texts[i] += block["text"].get<std::string>();
|
||||
first = false;
|
||||
} else if (i == 1 && (type == "image" || type == "image_url")) {
|
||||
if (!image.is_null())
|
||||
return no("at most one image per document is supported");
|
||||
image = block;
|
||||
} else
|
||||
return no("queries must be text; documents support text and one image");
|
||||
}
|
||||
} else
|
||||
return no("content must be a string or a list of text/image blocks");
|
||||
}
|
||||
const auto escape = [](const std::string& s) {
|
||||
std::string out;
|
||||
for (char c : s) {
|
||||
out += c;
|
||||
if (c == '[' || c == '<')
|
||||
out += "\xe2\x80\x8c";
|
||||
}
|
||||
return out;
|
||||
};
|
||||
std::string rendered = "query: " + escape(texts[0]) + "\n\npassage: " + escape(texts[1]);
|
||||
if (!image.is_null()) {
|
||||
/* Jinja's Python trim includes Unicode whitespace. The fixed image/query prefix has
|
||||
* no leading whitespace; only the rendered text's trailing edge can be trimmed. */
|
||||
auto cps = unicode_cpts_from_utf8(rendered);
|
||||
while (!cps.empty() && (unicode_cpt_flags_from_cpt(cps.back()).is_whitespace ||
|
||||
(cps.back() >= 0x1c && cps.back() <= 0x1f)))
|
||||
cps.pop_back();
|
||||
rendered.clear();
|
||||
for (auto cp : cps)
|
||||
rendered += unicode_cpt_to_utf8(cp);
|
||||
Nemotron35Piece p;
|
||||
p.image = true;
|
||||
p.content = std::move(image);
|
||||
pieces->push_back(std::move(p));
|
||||
rendered.insert(0, " ");
|
||||
}
|
||||
Nemotron35Piece p;
|
||||
p.text = std::move(rendered);
|
||||
pieces->push_back(std::move(p));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int nemotron35_format(const json& messages, std::vector<Nemotron35Piece>* pieces,
|
||||
std::string* why) {
|
||||
if (!pieces) return RAD_E_INVAL;
|
||||
|
||||
@@ -14,5 +14,8 @@ struct Nemotron35Piece {
|
||||
};
|
||||
|
||||
bool nemotron35_retrieval_meta(const RadModelMeta* meta);
|
||||
bool nemotron35_rerank_meta(const RadModelMeta* meta);
|
||||
int nemotron35_format(const json& messages, std::vector<Nemotron35Piece>* pieces, std::string* why);
|
||||
int nemotron35_rerank_format(const json& messages, std::vector<Nemotron35Piece>* pieces,
|
||||
std::string* why);
|
||||
} // namespace rad::server
|
||||
+120
-5
@@ -10,6 +10,7 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
@@ -2392,10 +2393,12 @@ static int embedding_input(const std::string& text,
|
||||
* boundaries, and no implicit BOS/EOS is added to the retrieval template's output. */
|
||||
static int nemotron35_embedding_input(const json& messages, const OaiDeps& d, const OaiLimits& lim,
|
||||
int32_t image_tokens, const char* field, OaiRequest* out,
|
||||
ApiError* err) {
|
||||
ApiError* err, bool rerank = false) {
|
||||
std::vector<Nemotron35Piece> pieces;
|
||||
std::string why;
|
||||
if (nemotron35_format(messages, &pieces, &why) < 0) {
|
||||
const int formatted = rerank ? nemotron35_rerank_format(messages, &pieces, &why)
|
||||
: nemotron35_format(messages, &pieces, &why);
|
||||
if (formatted < 0) {
|
||||
*err = err_bad(field, why);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
@@ -2416,8 +2419,9 @@ static int nemotron35_embedding_input(const json& messages, const OaiDeps& d, co
|
||||
std::string type = piece.content["type"].get<std::string>();
|
||||
if (type == "image")
|
||||
type = "image_url"; // template spelling; transport still requires a data URL
|
||||
const char* part_field =
|
||||
!std::strcmp(field, "messages") ? "messages[].content[]" : "input[]";
|
||||
const char* part_field = rerank ? "documents[].content[]"
|
||||
: !std::strcmp(field, "messages") ? "messages[].content[]"
|
||||
: "input[]";
|
||||
const int rc = read_media_part(piece.content, type, part_field, d, lim, &kind, &bytes, err);
|
||||
if (rc < 0) return rc;
|
||||
if (rc == 0 || kind != MediaKind::Image) {
|
||||
@@ -2451,7 +2455,8 @@ static int nemotron35_embedding_input(const json& messages, const OaiDeps& d, co
|
||||
}
|
||||
|
||||
int check_embedding_template(const IEmbedder* embed, bool overridden, std::string* why) {
|
||||
if (overridden && embed && nemotron35_retrieval_meta(embed->metadata())) {
|
||||
if (overridden && embed &&
|
||||
(nemotron35_retrieval_meta(embed->metadata()) || is_reranker(embed))) {
|
||||
if (why)
|
||||
*why = "Nemotron 3.5 uses its reference retrieval formatter; "
|
||||
"--override-chat-template is not supported for this model. "
|
||||
@@ -2463,6 +2468,10 @@ int check_embedding_template(const IEmbedder* embed, bool overridden, std::strin
|
||||
|
||||
int parse_embeddings_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
|
||||
OaiRequest* out, ApiError* err) {
|
||||
if (is_reranker(d.embed)) {
|
||||
*err = err_unsupported("model", "this is a reranker; use /rerank with query and documents");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
json b;
|
||||
RAD_TRY(parse_request_object(body, lim, &b, err));
|
||||
RAD_TRY(vet_keys(b, Endpoint::Embedding, err));
|
||||
@@ -2701,6 +2710,89 @@ int parse_embeddings_request(const std::string& body, const OaiDeps& d, const Oa
|
||||
return finish_inputs();
|
||||
}
|
||||
|
||||
bool is_reranker(const IEmbedder* embed) {
|
||||
return embed && nemotron35_rerank_meta(embed->metadata());
|
||||
}
|
||||
|
||||
int parse_rerank_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
|
||||
OaiRequest* out, ApiError* err) {
|
||||
if (!is_reranker(d.embed) || d.embed->dim() != 1) {
|
||||
*err = err_unsupported("model", "/rerank requires a reranking model");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
json b;
|
||||
RAD_TRY(parse_request_object(body, lim, &b, err));
|
||||
for (auto it = b.begin(); it != b.end(); ++it)
|
||||
if (it.key() != "model" && it.key() != "query" && it.key() != "documents" &&
|
||||
it.key() != "top_n" && it.key() != "return_documents" && it.key() != "use_activation") {
|
||||
*err = err_unsupported(it.key(), "this reranker serves query, documents and top_n "
|
||||
"without truncation, instructions or activation");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
out->endpoint = Endpoint::Rerank;
|
||||
out->id = random_id("rerank-");
|
||||
out->created = unix_now();
|
||||
out->model = lim.model_id;
|
||||
bool activation = false;
|
||||
if (!get_str(b, "model", &out->model, err) || !get_int(b, "top_n", &out->top_n, err) ||
|
||||
!get_bool(b, "return_documents", &out->return_documents, err) ||
|
||||
!get_bool(b, "use_activation", &activation, err))
|
||||
return RAD_E_INVAL;
|
||||
if (out->model.empty())
|
||||
out->model = lim.model_id;
|
||||
if (activation) {
|
||||
*err = err_unsupported("use_activation", "this model returns raw relevance logits");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (out->top_n < 0) {
|
||||
*err = err_bad("top_n", "must be zero (all documents) or positive");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (!b.contains("query") || !b["query"].is_string()) {
|
||||
*err = err_bad("query", "expected a text string");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (!b.contains("documents") || !b["documents"].is_array() || b["documents"].empty() ||
|
||||
b["documents"].size() > (size_t)INT_MAX) {
|
||||
*err = err_bad("documents", "expected a nonempty array of strings or content objects");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
int64_t cap = lim.max_ctx > 0 ? std::min<int64_t>(lim.max_ctx, 32768) : 32768;
|
||||
if (d.embed->max_input() > 0)
|
||||
cap = std::min(cap, d.embed->max_input());
|
||||
for (const auto& doc : b["documents"]) {
|
||||
json content, echo;
|
||||
if (doc.is_string()) {
|
||||
content = doc;
|
||||
echo = {{"text", doc}};
|
||||
} else if (doc.is_object() && doc.size() == 1 && doc.contains("content") &&
|
||||
doc["content"].is_array()) {
|
||||
content = doc["content"];
|
||||
echo = {{"multi_modal", content}};
|
||||
} else {
|
||||
*err =
|
||||
err_bad("documents", "each document must be a string or {content: [typed blocks]}");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
const json messages = json::array({{{"role", "query"}, {"content", b["query"]}},
|
||||
{{"role", "document"}, {"content", content}}});
|
||||
RAD_TRY(nemotron35_embedding_input(messages, d, lim, 0, "documents", out, err, true));
|
||||
const int64_t n = (int64_t)out->prompts.back().tokens.size();
|
||||
if (n <= 0 || n > cap) {
|
||||
*err = err_bad("documents",
|
||||
"query/document pair " + std::to_string(out->prompts.size() - 1) +
|
||||
" has " + std::to_string(n) +
|
||||
" tokens; this deployment accepts 1 through " + std::to_string(cap));
|
||||
err->code = "context_length_exceeded";
|
||||
err->n_prompt_tokens = n;
|
||||
err->n_ctx = cap;
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
out->rerank_documents.push_back(std::move(echo));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ================================================================== responses */
|
||||
|
||||
static json tool_calls_json(const std::vector<ToolCall>& tcs) {
|
||||
@@ -2924,6 +3016,29 @@ std::string embeddings_body(const OaiRequest& r, const std::vector<std::vector<f
|
||||
return o.dump();
|
||||
}
|
||||
|
||||
std::string rerank_body(const OaiRequest& r, const std::vector<std::vector<float>>& scores,
|
||||
const UsageOut& u) {
|
||||
std::vector<size_t> order(scores.size());
|
||||
for (size_t i = 0; i < order.size(); ++i)
|
||||
order[i] = i;
|
||||
std::stable_sort(order.begin(), order.end(),
|
||||
[&](size_t a, size_t b) { return scores[a][0] > scores[b][0]; });
|
||||
if (r.top_n > 0 && (uint64_t)r.top_n < order.size())
|
||||
order.resize((size_t)r.top_n);
|
||||
json results = json::array();
|
||||
for (size_t i : order) {
|
||||
json row = {{"index", i}, {"relevance_score", scores[i][0]}};
|
||||
if (r.return_documents)
|
||||
row["document"] = r.rerank_documents[i];
|
||||
results.push_back(std::move(row));
|
||||
}
|
||||
return json({{"id", r.id},
|
||||
{"model", r.model},
|
||||
{"results", std::move(results)},
|
||||
{"usage", {{"prompt_tokens", u.prompt}, {"total_tokens", u.prompt}}}})
|
||||
.dump();
|
||||
}
|
||||
|
||||
static json model_card(const ModelCard& c) {
|
||||
json m;
|
||||
m["id"] = c.id;
|
||||
|
||||
+10
-1
@@ -59,7 +59,7 @@ std::string json_dump(const json& j);
|
||||
|
||||
/* ================================================================== a parsed request */
|
||||
|
||||
enum class Endpoint { Chat, Completion, Embedding };
|
||||
enum class Endpoint { Chat, Completion, Embedding, Rerank };
|
||||
|
||||
/* The API a chat request came in through, which decides the shape of its answer. Every shape is
|
||||
* parsed into the same chat request (Anthropic's is translated first, anthropic_to_chat) and
|
||||
@@ -160,6 +160,10 @@ struct OaiRequest {
|
||||
int64_t dimensions = 0;
|
||||
/* /v1/embeddings: a soft-token budget for the request's images, 0 the server's. */
|
||||
int32_t image_tokens = 0;
|
||||
/* /rerank: original document order is retained through scheduler batching. */
|
||||
std::vector<json> rerank_documents;
|
||||
int64_t top_n = 0;
|
||||
bool return_documents = true;
|
||||
|
||||
/* `images_config`: which of the checkpoint's picture modes a request's pictures get, and the
|
||||
* sizes it states (Unlimited-OCR's gundam and base modes). 0 keeps the processor's choice
|
||||
@@ -308,6 +312,9 @@ int check_embedding_template(const IEmbedder* embed, bool overridden, std::strin
|
||||
/* Embeddings: fills `out->prompts` with one entry per input. */
|
||||
int parse_embeddings_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
|
||||
OaiRequest* out, ApiError* err);
|
||||
bool is_reranker(const IEmbedder* embed);
|
||||
int parse_rerank_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
|
||||
OaiRequest* out, ApiError* err);
|
||||
|
||||
/* ================================================================== responses */
|
||||
|
||||
@@ -392,6 +399,8 @@ std::string text_completion_body(const OaiRequest& r, const std::vector<ChoiceOu
|
||||
const UsageOut& u, const TimingsOut& t = TimingsOut());
|
||||
std::string embeddings_body(const OaiRequest& r, const std::vector<std::vector<float>>& vecs,
|
||||
const UsageOut& u);
|
||||
std::string rerank_body(const OaiRequest& r, const std::vector<std::vector<float>>& scores,
|
||||
const UsageOut& u);
|
||||
/* One model entry: what /v1/models lists and /v1/models/:id returns. */
|
||||
struct ModelCard {
|
||||
std::string id;
|
||||
|
||||
+37
-8
@@ -92,6 +92,8 @@ void Server::register_routes() {
|
||||
router_.post("/v1/chat/completions", [this](const HttpRequest& q) { return handle_chat(q); });
|
||||
router_.post("/v1/completions", [this](const HttpRequest& q) { return handle_completion(q); });
|
||||
router_.post("/v1/embeddings", [this](const HttpRequest& q) { return handle_embeddings(q); });
|
||||
router_.post("/rerank", [this](const HttpRequest& q) { return handle_rerank(q); });
|
||||
router_.post("/v1/rerank", [this](const HttpRequest& q) { return handle_rerank(q); });
|
||||
router_.post("/v1/images/generations", [this](const HttpRequest& q) { return handle_images(q, false); });
|
||||
router_.post("/v1/images/edits", [this](const HttpRequest& q) { return handle_images(q, true); });
|
||||
router_.post("/v1/systemone", [this](const HttpRequest& q) { return handle_systemone(q); });
|
||||
@@ -420,7 +422,9 @@ HttpResponse Server::error_response(const ApiError& e, const std::string& peer,
|
||||
void Server::log_admitted(const OaiRequest& r) const {
|
||||
int64_t n_prompt = 0;
|
||||
for (const auto& p : r.prompts) n_prompt += (int64_t)p.tokens.size();
|
||||
const char* kind = r.endpoint == Endpoint::Chat ? "chat" : "completion";
|
||||
const char* kind = r.endpoint == Endpoint::Chat ? "chat"
|
||||
: r.endpoint == Endpoint::Rerank ? "rerank"
|
||||
: "completion";
|
||||
if (r.endpoint == Endpoint::Chat)
|
||||
RAD_INFO("req %s %s%s%s model=%s msgs=%d tools=%d prompt=%lld tok n=%d max_tokens=%lld",
|
||||
r.id.c_str(), kind, r.stream ? " stream" : "", from_peer(r.peer).c_str(),
|
||||
@@ -1346,6 +1350,7 @@ HttpResponse Server::streaming_completion(const HttpRequest& req, OaiRequest& r)
|
||||
std::optional<HttpResponse> Server::refuse_text(const std::string& peer, ApiShape shape) {
|
||||
const char* why =
|
||||
!deps_.sched || !deps_.tok ? "the server has no engine attached"
|
||||
: is_reranker(deps_.embed) ? "this is a reranker: it serves /rerank and generates no text"
|
||||
: opt_.embedding_model ? "this is an embedding model: it serves /v1/embeddings and "
|
||||
"generates no text"
|
||||
: opt_.image_model ? "this is an image model: it serves /v1/images/generations "
|
||||
@@ -1616,10 +1621,20 @@ HttpResponse Server::handle_completion(const HttpRequest& req) {
|
||||
return serve(req, r);
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_embeddings(const HttpRequest& req) {
|
||||
HttpResponse Server::handle_embeddings(const HttpRequest& req) { return serve_pooled(req, false); }
|
||||
|
||||
HttpResponse Server::handle_rerank(const HttpRequest& req) { return serve_pooled(req, true); }
|
||||
|
||||
/* Embedding and reranking programs both return one pooled row per whole input. Share their
|
||||
* admission, batching and cancellation; only embeddings truncate and normalize that row. */
|
||||
HttpResponse Server::serve_pooled(const HttpRequest& req, bool rerank) {
|
||||
if (!deps_.sched || !deps_.tok)
|
||||
return error_response(err_not_implemented("the server has no engine attached"),
|
||||
req.peer);
|
||||
if (rerank && (!is_reranker(deps_.embed) || deps_.embed->dim() != 1))
|
||||
return error_response(err_not_implemented("/rerank requires a reranking model"), req.peer);
|
||||
if (!rerank && is_reranker(deps_.embed))
|
||||
return error_response(err_not_implemented("this is a reranker; use /rerank"), req.peer);
|
||||
if (!deps_.embed)
|
||||
return error_response(err_not_implemented(
|
||||
opt_.image_model ? "/v1/embeddings requires an embedding model; this one makes images"
|
||||
@@ -1630,7 +1645,9 @@ HttpResponse Server::handle_embeddings(const HttpRequest& req) {
|
||||
|
||||
OaiRequest r;
|
||||
ApiError err;
|
||||
if (parse_embeddings_request(req.body, odeps_, lim_, &r, &err) < 0)
|
||||
const int parsed = rerank ? parse_rerank_request(req.body, odeps_, lim_, &r, &err)
|
||||
: parse_embeddings_request(req.body, odeps_, lim_, &r, &err);
|
||||
if (parsed < 0)
|
||||
return error_response(err, req.peer);
|
||||
r.peer = req.peer;
|
||||
|
||||
@@ -1720,17 +1737,29 @@ HttpResponse Server::handle_embeddings(const HttpRequest& req) {
|
||||
return error_response(err_internal(why), req.peer);
|
||||
}
|
||||
std::vector<float> out(v.begin(), v.begin() + keep);
|
||||
double ss = 0.0;
|
||||
for (float x : out) ss += (double)x * x;
|
||||
const float inv = ss > 0.0 ? (float)(1.0 / std::sqrt(ss)) : 0.0f;
|
||||
for (float& x : out) x *= inv;
|
||||
if (rerank) {
|
||||
if (out.size() != 1 || !std::isfinite(out[0])) {
|
||||
retire(gens);
|
||||
release(n_in);
|
||||
return error_response(err_internal("the reranker returned a non-finite score"),
|
||||
req.peer);
|
||||
}
|
||||
} else {
|
||||
double ss = 0.0;
|
||||
for (float x : out)
|
||||
ss += (double)x * x;
|
||||
const float inv = ss > 0.0 ? (float)(1.0 / std::sqrt(ss)) : 0.0f;
|
||||
for (float& x : out)
|
||||
x *= inv;
|
||||
}
|
||||
u.prompt += (int64_t)r.prompts[(size_t)g.prompt_index].tokens.size();
|
||||
vecs.push_back(std::move(out));
|
||||
}
|
||||
retire(gens);
|
||||
release(n_in);
|
||||
metrics_.observe_http(200);
|
||||
return HttpResponse::json_body(200, embeddings_body(r, vecs, u));
|
||||
return HttpResponse::json_body(200,
|
||||
rerank ? rerank_body(r, vecs, u) : embeddings_body(r, vecs, u));
|
||||
}
|
||||
|
||||
/* /v1/systemone (systemone.h): one engine request per question, each reading its answer
|
||||
|
||||
@@ -243,6 +243,8 @@ private:
|
||||
HttpResponse handle_native_completion(const HttpRequest& req);
|
||||
HttpResponse handle_infill(const HttpRequest& req);
|
||||
HttpResponse handle_embeddings(const HttpRequest& req);
|
||||
HttpResponse handle_rerank(const HttpRequest& req);
|
||||
HttpResponse serve_pooled(const HttpRequest& req, bool rerank);
|
||||
HttpResponse handle_systemone(const HttpRequest& req);
|
||||
HttpResponse handle_apply_template(const HttpRequest& req);
|
||||
using ToChat = int (*)(const std::string& body, const OaiLimits& lim, std::string* chat,
|
||||
|
||||
Reference in new issue
Block a user