-
Notifications
You must be signed in to change notification settings - Fork 13.3k
server / ranking : add sorting and management of top_n #16403
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
Open
YannFollet
wants to merge
5
commits into
ggml-org:master
Choose a base branch
from
YannFollet:rerank-top_n
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−48
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
374b648
server / ranking : add sorting and management of top_n
YannFollet b86bfdb
Make the retro compatible if no top_n will return
YannFollet fd143c7
use resize() instead for(...)
YannFollet a200520
simplify top_n init since no need to return error
YannFollet 314c218
Merge remote-tracking branch 'origin/master' into rerank-top_n
YannFollet 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
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 |
---|---|---|
|
@@ -849,46 +849,47 @@ static json format_response_rerank( | |
const json & request, | ||
const json & ranks, | ||
bool is_tei_format, | ||
std::vector<std::string> & texts) { | ||
json res; | ||
if (is_tei_format) { | ||
// TEI response format | ||
res = json::array(); | ||
bool return_text = json_value(request, "return_text", false); | ||
for (const auto & rank : ranks) { | ||
int index = json_value(rank, "index", 0); | ||
json elem = json{ | ||
{"index", index}, | ||
{"score", json_value(rank, "score", 0.0)}, | ||
}; | ||
if (return_text) { | ||
elem["text"] = std::move(texts[index]); | ||
} | ||
res.push_back(elem); | ||
} | ||
} else { | ||
// Jina response format | ||
json results = json::array(); | ||
int32_t n_tokens = 0; | ||
for (const auto & rank : ranks) { | ||
results.push_back(json{ | ||
{"index", json_value(rank, "index", 0)}, | ||
{"relevance_score", json_value(rank, "score", 0.0)}, | ||
}); | ||
|
||
n_tokens += json_value(rank, "tokens_evaluated", 0); | ||
std::vector<std::string> & texts, | ||
int top_n) { | ||
json results; | ||
int32_t n_tokens = 0; | ||
bool return_text = is_tei_format && json_value(request, "return_text", false); | ||
std::vector<json> elements; // Temporary vector to hold unsorted elements | ||
std::string score_label = is_tei_format ? "score" : "relevance_score"; | ||
for (const auto & rank : ranks) { | ||
int index = json_value(rank, "index", 0); | ||
json elem = json{ | ||
{"index", index}, | ||
{score_label, json_value(rank, "score", 0.0)}, | ||
}; | ||
n_tokens += json_value(rank, "tokens_evaluated", 0); | ||
if (return_text) { | ||
elem["text"] = std::move(texts[index]); | ||
} | ||
elements.push_back(elem); | ||
} | ||
|
||
std::sort(elements.begin(), elements.end(), [score_label](const json& a, const json& b) { | ||
return json_value(a, score_label, 0.0) > json_value(b, score_label, 0.0); | ||
}); | ||
|
||
res = json{ | ||
{"model", json_value(request, "model", std::string(DEFAULT_OAICOMPAT_MODEL))}, | ||
{"object", "list"}, | ||
{"usage", json{ | ||
{"prompt_tokens", n_tokens}, | ||
{"total_tokens", n_tokens} | ||
}}, | ||
{"results", results} | ||
}; | ||
results = json::array(); | ||
int count = 0; | ||
for (const auto & elem : elements) { | ||
if (++count > top_n) break; | ||
results.push_back(elem); | ||
} | ||
|
||
if (is_tei_format) return results; | ||
|
||
json res = json{ | ||
{"model", json_value(request, "model", std::string(DEFAULT_OAICOMPAT_MODEL))}, | ||
{"object", "list"}, | ||
{"usage", json{ | ||
{"prompt_tokens", n_tokens}, | ||
{"total_tokens", n_tokens} | ||
}}, | ||
{"results", results} | ||
}; | ||
|
||
return res; | ||
} | ||
|
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.
does this work?