Skip to content

Commit 1c08c7c

Browse files
committed
Refactor: Extract common payload handling into a helper
Added a private addCommonPayloadParams(QJsonObject &payload) that populates all shared request parameters (samplers, temperature, dynatemp settings, penalties, token limits, etc.). Replaced the repeated block of parameter assignments in generateMessage, followUpQuestions, and summarizeConversationTitle with a single call to this helper.
1 parent 3209451 commit 1c08c7c

File tree

1 file changed

+27
-60
lines changed

1 file changed

+27
-60
lines changed

llamachatmanager.cpp

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ Q_LOGGING_CATEGORY(llamaChatNetwork, "llama.cpp.chat.network", QtWarningMsg)
1111

1212
namespace LlamaCpp {
1313

14+
static void addCommonPayloadParams(QJsonObject &payload)
15+
{
16+
payload["samplers"] = settings().samplers.value();
17+
payload["temperature"] = settings().temperature.value();
18+
payload["dynatemp_range"] = settings().dynatemp_range.value();
19+
payload["dynatemp_exponent"] = settings().dynatemp_exponent.value();
20+
payload["top_k"] = settings().top_k.value();
21+
payload["top_p"] = settings().top_p.value();
22+
payload["min_p"] = settings().min_p.value();
23+
payload["typical_p"] = settings().typical_p.value();
24+
payload["xtc_probability"] = settings().xtc_probability.value();
25+
payload["xtc_threshold"] = settings().xtc_threshold.value();
26+
payload["repeat_last_n"] = settings().repeat_last_n.value();
27+
payload["repeat_penalty"] = settings().repeat_penalty.value();
28+
payload["presence_penalty"] = settings().presence_penalty.value();
29+
payload["frequency_penalty"] = settings().frequency_penalty.value();
30+
payload["dry_multiplier"] = settings().dry_multiplier.value();
31+
payload["dry_base"] = settings().dry_base.value();
32+
payload["dry_allowed_length"] = settings().dry_allowed_length.value();
33+
payload["dry_penalty_last_n"] = settings().dry_penalty_last_n.value();
34+
payload["max_tokens"] = settings().max_tokens.value();
35+
payload["timings_per_token"] = settings().showTokensPerSecond.value();
36+
}
37+
1438
ChatManager &ChatManager::instance()
1539
{
1640
static ChatManager inst;
@@ -192,26 +216,7 @@ void ChatManager::generateMessage(const QString &convId,
192216
payload["stream"] = true;
193217
payload["cache_prompt"] = true;
194218
payload["reasoning_format"] = "none";
195-
payload["samplers"] = settings().samplers.value();
196-
payload["temperature"] = settings().temperature.value();
197-
payload["dynatemp_range"] = settings().dynatemp_range.value();
198-
payload["dynatemp_exponent"] = settings().dynatemp_exponent.value();
199-
payload["top_k"] = settings().top_k.value();
200-
payload["top_p"] = settings().top_p.value();
201-
payload["min_p"] = settings().min_p.value();
202-
payload["typical_p"] = settings().typical_p.value();
203-
payload["xtc_probability"] = settings().xtc_probability.value();
204-
payload["xtc_threshold"] = settings().xtc_threshold.value();
205-
payload["repeat_last_n"] = settings().repeat_last_n.value();
206-
payload["repeat_penalty"] = settings().repeat_penalty.value();
207-
payload["presence_penalty"] = settings().presence_penalty.value();
208-
payload["frequency_penalty"] = settings().frequency_penalty.value();
209-
payload["dry_multiplier"] = settings().dry_multiplier.value();
210-
payload["dry_base"] = settings().dry_base.value();
211-
payload["dry_allowed_length"] = settings().dry_allowed_length.value();
212-
payload["dry_penalty_last_n"] = settings().dry_penalty_last_n.value();
213-
payload["max_tokens"] = settings().max_tokens.value();
214-
payload["timings_per_token"] = settings().showTokensPerSecond.value();
219+
addCommonPayloadParams(payload);
215220

216221
// parse custom JSON if present
217222
if (!settings().customJson.value().isEmpty()) {
@@ -333,26 +338,7 @@ void ChatManager::followUpQuestions(const QString &convId,
333338
payload["stream"] = false;
334339
payload["cache_prompt"] = true;
335340
payload["reasoning_format"] = "none";
336-
payload["samplers"] = settings().samplers.value();
337-
payload["temperature"] = settings().temperature.value();
338-
payload["dynatemp_range"] = settings().dynatemp_range.value();
339-
payload["dynatemp_exponent"] = settings().dynatemp_exponent.value();
340-
payload["top_k"] = settings().top_k.value();
341-
payload["top_p"] = settings().top_p.value();
342-
payload["min_p"] = settings().min_p.value();
343-
payload["typical_p"] = settings().typical_p.value();
344-
payload["xtc_probability"] = settings().xtc_probability.value();
345-
payload["xtc_threshold"] = settings().xtc_threshold.value();
346-
payload["repeat_last_n"] = settings().repeat_last_n.value();
347-
payload["repeat_penalty"] = settings().repeat_penalty.value();
348-
payload["presence_penalty"] = settings().presence_penalty.value();
349-
payload["frequency_penalty"] = settings().frequency_penalty.value();
350-
payload["dry_multiplier"] = settings().dry_multiplier.value();
351-
payload["dry_base"] = settings().dry_base.value();
352-
payload["dry_allowed_length"] = settings().dry_allowed_length.value();
353-
payload["dry_penalty_last_n"] = settings().dry_penalty_last_n.value();
354-
payload["max_tokens"] = settings().max_tokens.value();
355-
payload["timings_per_token"] = settings().showTokensPerSecond.value();
341+
addCommonPayloadParams(payload);
356342

357343
QNetworkRequest req(QUrl(settings().chatEndpoint.value() + "/v1/chat/completions"));
358344
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
@@ -471,26 +457,7 @@ void ChatManager::summarizeConversationTitle(const QString &convId,
471457
payload["stream"] = false;
472458
payload["cache_prompt"] = true;
473459
payload["reasoning_format"] = "none";
474-
payload["samplers"] = settings().samplers.value();
475-
payload["temperature"] = settings().temperature.value();
476-
payload["dynatemp_range"] = settings().dynatemp_range.value();
477-
payload["dynatemp_exponent"] = settings().dynatemp_exponent.value();
478-
payload["top_k"] = settings().top_k.value();
479-
payload["top_p"] = settings().top_p.value();
480-
payload["min_p"] = settings().min_p.value();
481-
payload["typical_p"] = settings().typical_p.value();
482-
payload["xtc_probability"] = settings().xtc_probability.value();
483-
payload["xtc_threshold"] = settings().xtc_threshold.value();
484-
payload["repeat_last_n"] = settings().repeat_last_n.value();
485-
payload["repeat_penalty"] = settings().repeat_penalty.value();
486-
payload["presence_penalty"] = settings().presence_penalty.value();
487-
payload["frequency_penalty"] = settings().frequency_penalty.value();
488-
payload["dry_multiplier"] = settings().dry_multiplier.value();
489-
payload["dry_base"] = settings().dry_base.value();
490-
payload["dry_allowed_length"] = settings().dry_allowed_length.value();
491-
payload["dry_penalty_last_n"] = settings().dry_penalty_last_n.value();
492-
payload["max_tokens"] = settings().max_tokens.value();
493-
payload["timings_per_token"] = settings().showTokensPerSecond.value();
460+
addCommonPayloadParams(payload);
494461

495462
QNetworkRequest req(QUrl(settings().chatEndpoint.value() + "/v1/chat/completions"));
496463
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

0 commit comments

Comments
 (0)