Skip to content

Commit

Permalink
Fixed scalar function to handle over/under predictions and updated ll…
Browse files Browse the repository at this point in the history
…m_filter to handle null values in under predictions.
  • Loading branch information
dorbanianas committed Dec 25, 2024
1 parent 59d64ec commit c49c429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/functions/scalar/llm_filter/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ std::vector<std::string> LlmFilter::Operation(duckdb::DataChunk& args) {
std::vector<std::string> results;
results.reserve(responses.size());
for (const auto& response : responses) {
if (response.is_null()) {
results.emplace_back("True");
continue;
}
results.push_back(response.dump());
}

Expand Down
15 changes: 14 additions & 1 deletion src/functions/scalar/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,25 @@ nlohmann::json ScalarFunctionBase::BatchAndComplete(const std::vector<nlohmann::
response = Complete(batch_tuples, user_prompt, function_type, model);
} catch (const ExceededMaxOutputTokensError&) {
batch_tuples.clear();
const auto new_batch_size = static_cast<int>(batch_size * 0.1);
const auto new_batch_size = static_cast<int>(batch_size / 10);
batch_size = batch_size == 1 ? new_batch_size == 0 : new_batch_size;
accumulated_tuples_tokens = 0;
start_index = 0;
continue;
}

if (response.size() < batch_tuples.size()) {
for (int i = static_cast<int>(response.size()); i < batch_tuples.size(); i++) {
response.push_back(nullptr);
}
} else if (response.size() > batch_size) {
auto new_response = nlohmann::json::array();
for (int i = 0; i < batch_size; i++) {
new_response.push_back(response[i]);
}
response = new_response;
}

auto output_tokens_per_tuple = Tiktoken::GetNumTokens(response.dump()) / batch_tuples.size();

batch_size = model.GetModelDetails().max_output_tokens / output_tokens_per_tuple;
Expand Down

0 comments on commit c49c429

Please sign in to comment.