Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/include/prompt_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ class PromptCache {
bool has_tool_call = msg.contains("tool_calls");
bool skip_this_message = (role == "tool") || has_tool_call;
if (template_type == chat_template_type_t::harmony) {
skip_this_message = false;
skip_this_message = false;
}

if (skip_this_message) continue;

if(i < messages.size() - 2)
check_sum_to_compare = _calculate_checksum(content.data(), content.size(), check_sum_to_compare);
new_checksum = _calculate_checksum(content.data(), content.size(), new_checksum);
Expand All @@ -138,16 +138,17 @@ class PromptCache {
uint64_t new_checksum = 0;
for (size_t i = 0; i < messages.size(); ++i) {
const auto& msg = messages[i];
const std::string role = msg.value("role", "");
const std::string content = msg.value("content", "");
bool has_tool_call = msg.contains("tool_calls");
bool skip_this_message = (role == "tool") || has_tool_call;
if (skip_this_message) continue;
new_checksum = _calculate_checksum(content.data(), content.size(), new_checksum);
}
checksum_ = new_checksum;
}

/// @brief Reset the checksum to force cache miss
/// @note This function increments the checksum value by 1 to ensure that
/// the next call to can_use_cache will result in a cache miss.
void reset() {
checksum_ = checksum_ + 1;
}
};
};
68 changes: 35 additions & 33 deletions src/server/rest_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ void RestHandler::ensure_embed_model_loaded(const std::string& model_tag) {
#endif
}

///@brief Clear KV cache and invalidate prompt cache
void RestHandler::reset_context() {
auto_chat_engine->clear_context();
prompt_cache.reset();
}

///@brief Configure chat engine parameters from options and request
///@param options the options JSON object
///@param request the request JSON object
Expand Down Expand Up @@ -475,21 +481,21 @@ void RestHandler::handle_generate(const json& request,
if (!success){
json error_response = {{"error", "Max length reached"}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
try {
auto_chat_engine->generate(meta_info, length_limit, ostream);
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
auto total_end_time = time_utils::now();
Expand All @@ -508,21 +514,21 @@ void RestHandler::handle_generate(const json& request,
if (!success){
json error_response = {{"error", "Max length reached"}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
try {
auto_chat_engine->generate(meta_info, length_limit, ostream);
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
std::string response_text = ss.str();
Expand Down Expand Up @@ -591,36 +597,36 @@ void RestHandler::handle_chat(const json& request,
if (!success){
json error_response = {{"error", "Max length reached"}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
try {
bool success = auto_chat_engine->insert(meta_info, uniformed_input);
if (!success){
json error_response = {{"error", "Max length reached"}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
auto total_end_time = time_utils::now();
meta_info.total_duration = (uint64_t)time_utils::duration_ns(total_start_time, total_end_time).first;

ostream.finalize_chat(meta_info);
// auto history = this->chat_engine->get_history();
// std::cout << "history: " << history.first << std::endl;
this->auto_chat_engine->clear_context();
this->reset_context();
} else {
// Non-streaming response
uniformed_input.messages = messages;
Expand All @@ -633,7 +639,7 @@ void RestHandler::handle_chat(const json& request,
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
//std::string response_text = chat_engine->generate_with_prompt(meta_info, prompts, length_limit, std::cout, payload);
Expand All @@ -660,7 +666,7 @@ void RestHandler::handle_chat(const json& request,

// auto history = this->chat_engine->get_history();
// std::cout << "history: " << history.first << std::endl;
this->auto_chat_engine->clear_context();
this->reset_context();
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
Expand Down Expand Up @@ -974,8 +980,7 @@ void RestHandler::handle_openai_chat_completion(const json& request,
meta_info.stop_reason = CANCEL_DETECTED;
header_print("❌ ", "Prefill Cancelled!");
ostream.finalize(meta_info);
this->auto_chat_engine->clear_context();
this->prompt_cache.reset();
this->reset_context();
return;
}

Expand All @@ -987,13 +992,13 @@ void RestHandler::handle_openai_chat_completion(const json& request,
}}
};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
header_print("FLM", "Start generating...");
Expand All @@ -1002,7 +1007,7 @@ void RestHandler::handle_openai_chat_completion(const json& request,
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
if (meta_info.stop_reason == CANCEL_DETECTED) {
Expand All @@ -1013,7 +1018,6 @@ void RestHandler::handle_openai_chat_completion(const json& request,
ostream.finalize(meta_info);
}
else {
this->auto_chat_engine->clear_context();
nullstream nstream;
json response;
std::string response_text;
Expand All @@ -1025,8 +1029,7 @@ void RestHandler::handle_openai_chat_completion(const json& request,
meta_info.stop_reason = CANCEL_DETECTED;
header_print("❌ ", "Prefill Cancelled!");
send_response(response);
this->auto_chat_engine->clear_context();
this->prompt_cache.reset();
this->reset_context();
return;
}
json error_response = {
Expand All @@ -1037,13 +1040,13 @@ void RestHandler::handle_openai_chat_completion(const json& request,
}}
};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
header_print("FLM", "Start generating...");
Expand All @@ -1052,7 +1055,7 @@ void RestHandler::handle_openai_chat_completion(const json& request,
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
// check response_text
Expand Down Expand Up @@ -1080,7 +1083,6 @@ void RestHandler::handle_openai_chat_completion(const json& request,
header_print("❌ ", "Generation Cancelled!");
}
send_response(response);
this->prompt_cache.reset();
}

} catch (const std::exception& e) {
Expand Down Expand Up @@ -1207,26 +1209,26 @@ void RestHandler::handle_openai_completion(const json& request,
if (!success) {
json error_response = { {"error", "Max length reached"} };
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
try {
auto_chat_engine->generate(meta_info, length_limit, ostream);
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
ostream.finalize(meta_info);

this->auto_chat_engine->clear_context();
this->reset_context();
}
else {
std::stringstream ss;
Expand All @@ -1238,21 +1240,21 @@ void RestHandler::handle_openai_completion(const json& request,
if (!success) {
json error_response = { {"error", "Max length reached"} };
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
try {
auto_chat_engine->generate(meta_info, length_limit, ostream);
} catch (const std::exception& e) {
json error_response = {{"error", e.what()}};
send_response(error_response);
this->auto_chat_engine->clear_context();
this->reset_context();
return;
}
std::string response_text = ss.str();
Expand Down
1 change: 1 addition & 0 deletions src/server/rest_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class RestHandler {
bool ensure_model_loaded(const std::string& model_tag);
void ensure_asr_model_loaded(const std::string& model_tag);
void ensure_embed_model_loaded(const std::string& model_tag);
void reset_context();
void configure_chat_engine_parameters(const json& options, const json& request);
json build_nstream_response(std::string response_text);

Expand Down