llama-cpp-bindings-sys 0.34.0

Low level bindings to llama.cpp
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "wrapper_decision_model.h"
#include "wrapper_batch.h"
#include "wrapper_fault_injection.h"
#include "wrapper_report_current_exception.h"
#include "wrapper_utils.h"

#include <common/common.h>
#include <common/json.h>
#include <llama.h>
#include <server-common.h>
#include <server-decision.h>
#include <server-task.h>
#include <tools/mtmd/mtmd-helper.h>
#include <tools/mtmd/mtmd.h>

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <gsl/span>
#include <iterator>
#include <memory>
#include <new>
#include <numeric>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

struct llama_rs_decision_model {
    server_decision_context context;
};

struct llama_rs_decision_request {
    json state;
    std::vector<raw_buffer> files;
    std::vector<server_decision_question> questions;
    std::vector<std::vector<std::vector<float>>> variant_scores;
};

struct llama_rs_decision_prompt {
    server_task task = server_task(SERVER_TASK_TYPE_DECISION);
    std::vector<llama_token> token_ids;
    std::vector<llama_rs_decision_order> orders;
    bool joint = false;
    size_t question_index = 0;
};

namespace {
using server_decision_layout = decltype(server_task::decision);

auto decision_order_of(int32_t order) -> llama_rs_decision_order {
    switch (order) {
        case LLAMA_RS_DECISION_ORDER_NONE:
            return LLAMA_RS_DECISION_ORDER_NONE;
        case LLAMA_RS_DECISION_ORDER_QUESTION_NOUL:
            return LLAMA_RS_DECISION_ORDER_QUESTION_NOUL;
        case LLAMA_RS_DECISION_ORDER_QUESTION_CHOICE:
            return LLAMA_RS_DECISION_ORDER_QUESTION_CHOICE;
        case LLAMA_RS_DECISION_ORDER_QUESTION_SCORE:
            return LLAMA_RS_DECISION_ORDER_QUESTION_SCORE;
        case LLAMA_RS_DECISION_ORDER_OPTION:
            return LLAMA_RS_DECISION_ORDER_OPTION;
        default:
            throw std::logic_error("llama.cpp laid out a decision order the binding does not know");
    }
}

class decision_outputs_unreadable : public std::runtime_error {
  public:
    using std::runtime_error::runtime_error;
};

auto embedding_scores(
    const server_decision_layout & decision,
    llama_context * context,
    gsl::span<const int32_t> batch_indices,
    int32_t prompt_tokens) -> std::vector<float> {
    const auto outputs = static_cast<int32_t>(batch_indices.size());
    const int32_t pos_first = prompt_tokens - outputs;
    const int32_t n_embd_out = llama_model_n_embd_out(llama_get_model(context));
    const auto embeddings_of_output = [&](int32_t output) -> gsl::span<const float> {
        const float * embeddings =
            output >= 0 && output < outputs ? llama_get_embeddings_ith(context, batch_indices[output]) : nullptr;

        return embeddings == nullptr ? gsl::span<const float>()
                                     : gsl::span<const float>(embeddings, static_cast<size_t>(n_embd_out));
    };
    std::vector<float> scores;

    for (int32_t row = 0; row < decision.n_scores; row++) {
        const gsl::span<const float> embeddings = embeddings_of_output(row);

        if (embeddings.empty()) {
            throw decision_outputs_unreadable("failed to get embeddings");
        }
        scores.push_back(embeddings.front());
    }

    const int32_t n_pointer = n_embd_out / 2;
    const gsl::span<const float> query =
        decision.pointer >= 0 ? embeddings_of_output(decision.pointer - pos_first) : gsl::span<const float>();

    if (decision.column < 0 || decision.column >= n_embd_out) {
        throw decision_outputs_unreadable("the decision column lies outside the embeddings");
    }

    for (const int32_t marker : decision.markers) {
        const gsl::span<const float> marked = embeddings_of_output(marker - pos_first);

        if (marked.empty() || (decision.pointer >= 0 && query.empty())) {
            throw decision_outputs_unreadable(
                "failed to get embeddings, the question and its options must fit in one batch");
        }
        if (decision.pointer < 0) {
            scores.push_back(marked[static_cast<size_t>(decision.column)]);
            continue;
        }

        const gsl::span<const float> query_head = query.first(static_cast<size_t>(n_pointer));
        const gsl::span<const float> marked_head = marked.subspan(static_cast<size_t>(n_pointer), query_head.size());

        scores.push_back(std::inner_product(query_head.begin(), query_head.end(), marked_head.begin(), 0.0F) /
                         sqrtf(static_cast<float>(n_pointer)));
    }

    return scores;
}

auto label_scores(
    const server_decision_layout & decision,
    llama_context * context,
    gsl::span<const int32_t> batch_indices) -> std::vector<float> {
    if (batch_indices.empty()) {
        throw decision_outputs_unreadable("failed to get logits");
    }

    const float * logits = llama_get_logits_ith(context, batch_indices.back());

    if (logits == nullptr) {
        throw decision_outputs_unreadable("failed to get logits");
    }

    const int32_t n_vocab = llama_vocab_n_tokens(llama_model_get_vocab(llama_get_model(context)));
    const gsl::span<const float> vocabulary_logits(logits, static_cast<size_t>(n_vocab));
    std::vector<float> scores;

    for (const llama_token label : decision.labels) {
        if (label < 0 || label >= n_vocab) {
            throw decision_outputs_unreadable("a decision label lies outside the vocabulary");
        }
        scores.push_back(vocabulary_logits[static_cast<size_t>(label)]);
    }

    if (decision.label_groups.empty()) {
        return scores;
    }

    std::vector<float> grouped;
    size_t first = 0;

    for (const int32_t group_size : decision.label_groups) {
        if (group_size <= 0 || first + static_cast<size_t>(group_size) > scores.size()) {
            throw decision_outputs_unreadable("a decision label group lies outside the labels");
        }

        const auto group_begin = scores.begin() + static_cast<std::ptrdiff_t>(first);

        grouped.push_back(*std::max_element(group_begin, group_begin + group_size));
        first += static_cast<size_t>(group_size);
    }

    return grouped;
}

auto prompt_scores(
    const llama_rs_decision_prompt & prompt,
    llama_context * context,
    gsl::span<const int32_t> batch_indices,
    llama_seq_id sequence_id) -> std::vector<float> {
    const auto & decision = prompt.task.decision;

    if (decision.pooled) {
        const float * pooled = llama_get_embeddings_seq(context, sequence_id);

        if (pooled == nullptr) {
            throw decision_outputs_unreadable("failed to get the class scores");
        }

        const gsl::span<const float> class_scores(
            pooled, static_cast<size_t>(llama_model_n_embd_out(llama_get_model(context))));

        return {class_scores.begin(), class_scores.end()};
    }
    if (!decision.labels.empty()) {
        return label_scores(decision, context, batch_indices);
    }

    return embedding_scores(decision, context, batch_indices, prompt.task.n_tokens());
}
}  // namespace

namespace {
template <typename TFill>
auto fill_prompt(
    llama_rs_decision_model_handle model,
    llama_rs_decision_request_handle request,
    mtmd_context * projector,
    llama_rs_decision_prompt_handle * out_prompt,
    char ** out_error,
    bool joint,
    size_t question_index,
    TFill fill) -> llama_rs_decision_prompt_fill_status {
    *out_prompt = nullptr;
    *out_error = nullptr;

    try {
        if (!request->files.empty() && !model->context.can_use_images()) {
            return LLAMA_RS_DECISION_PROMPT_FILL_MEDIA_UNSUPPORTED_BY_DECISION_MODEL;
        }
        if (!request->files.empty() && projector == nullptr) {
            return LLAMA_RS_DECISION_PROMPT_FILL_MEDIA_REQUIRES_A_PROJECTOR;
        }

        auto prompt = std::make_unique<llama_rs_decision_prompt>();

        prompt->joint = joint;
        prompt->question_index = question_index;

        try {
            fill(mtmd_helper_init_opt_default(), prompt->task);
        } catch (const std::invalid_argument & err) {
            return llama_rs_report_exception_message(
                out_error,
                err.what(),
                LLAMA_RS_DECISION_PROMPT_FILL_REQUEST_REJECTED,
                LLAMA_RS_DECISION_PROMPT_FILL_ERROR_STRING_ALLOCATION_FAILED);
        }

        const auto & tokens = prompt->task.tokens;

        prompt->token_ids.reserve(tokens.size());
        for (size_t position = 0; position < tokens.size(); ++position) {
            prompt->token_ids.push_back(tokens[position]);
        }

        prompt->orders.reserve(prompt->task.decision.order.size());
        for (const int32_t order : prompt->task.decision.order) {
            prompt->orders.push_back(decision_order_of(order));
        }

        *out_prompt = prompt.release();

        return LLAMA_RS_DECISION_PROMPT_FILL_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_DECISION_PROMPT_FILL_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_DECISION_PROMPT_FILL_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_DECISION_PROMPT_FILL_ERROR_STRING_ALLOCATION_FAILED);
    }
}
}  // namespace

extern "C" auto llama_rs_decision_model_init(
    const struct llama_model * model,
    llama_rs_decision_model_handle * out_model,
    char ** out_error) -> llama_rs_decision_model_init_status {
    *out_model = nullptr;
    *out_error = nullptr;

    try {
        const common_decision_type decision_type = common_get_decision_type(model);

        if (decision_type == COMMON_DECISION_TYPE_NONE) {
            return LLAMA_RS_DECISION_MODEL_INIT_MODEL_IS_NOT_A_DECISION_MODEL;
        }
        if (decision_type == COMMON_DECISION_TYPE_UNKNOWN) {
            return LLAMA_RS_DECISION_MODEL_INIT_DECISION_TYPE_UNSUPPORTED_BY_LLAMA_CPP;
        }

        auto decision_model = std::make_unique<llama_rs_decision_model>();

        decision_model->context.init(model);
        *out_model = decision_model.release();

        return LLAMA_RS_DECISION_MODEL_INIT_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_DECISION_MODEL_INIT_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_DECISION_MODEL_INIT_LLAMA_CPP_REJECTED_MODEL,
            LLAMA_RS_DECISION_MODEL_INIT_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_decision_model_free(llama_rs_decision_model_handle model) -> void {
    std::unique_ptr<llama_rs_decision_model>(model).reset();
}

extern "C" auto llama_rs_decision_model_can_share_prompt(llama_rs_decision_model_handle model) -> bool {
    return model->context.can_share_prompt();
}

extern "C" auto llama_rs_decision_model_is_joint(llama_rs_decision_model_handle model) -> bool {
    return model->context.is_joint();
}

extern "C" auto llama_rs_decision_model_can_use_images(llama_rs_decision_model_handle model) -> bool {
    return model->context.can_use_images();
}

extern "C" auto llama_rs_decision_model_output_of(llama_rs_decision_model_handle model)
    -> llama_rs_decision_model_output {
    switch (model->context.type) {
        case COMMON_DECISION_TYPE_LAYA:
        case COMMON_DECISION_TYPE_KEV:
        case COMMON_DECISION_TYPE_CLEF:
        case COMMON_DECISION_TYPE_LFM2_D1_OMNI:
        case COMMON_DECISION_TYPE_VLLM_DECISION2:
            return LLAMA_RS_DECISION_MODEL_OUTPUT_TOKEN_EMBEDDINGS;
        case COMMON_DECISION_TYPE_MINICPM_OCR_JEV:
            return LLAMA_RS_DECISION_MODEL_OUTPUT_POOLED_EMBEDDINGS;
        default:
            return LLAMA_RS_DECISION_MODEL_OUTPUT_LOGITS;
    }
}

extern "C" auto llama_rs_decision_request_parse(
    llama_rs_decision_model_handle model,
    const char * body_json,
    size_t body_json_length,
    llama_rs_decision_request_handle * out_request,
    char ** out_error) -> llama_rs_decision_request_parse_status {
    *out_request = nullptr;
    *out_error = nullptr;

    try {
        json body;

        try {
            body = json::parse(std::string(body_json, body_json_length));
        } catch (const common_json_error & err) {
            return llama_rs_report_exception_message(
                out_error,
                err.what(),
                LLAMA_RS_DECISION_REQUEST_PARSE_BODY_IS_NOT_JSON,
                LLAMA_RS_DECISION_REQUEST_PARSE_ERROR_STRING_ALLOCATION_FAILED);
        }

        auto request = std::make_unique<llama_rs_decision_request>();

        try {
            request->questions = model->context.parse_questions(body);
            request->state = model->context.prepare_state(model->context.parse_state(body, request->files));
            request->variant_scores.resize(request->questions.size());
        } catch (const std::invalid_argument & err) {
            return llama_rs_report_exception_message(
                out_error,
                err.what(),
                LLAMA_RS_DECISION_REQUEST_PARSE_REQUEST_REJECTED,
                LLAMA_RS_DECISION_REQUEST_PARSE_ERROR_STRING_ALLOCATION_FAILED);
        }

        *out_request = request.release();

        return LLAMA_RS_DECISION_REQUEST_PARSE_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_DECISION_REQUEST_PARSE_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_DECISION_REQUEST_PARSE_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_DECISION_REQUEST_PARSE_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_decision_request_free(llama_rs_decision_request_handle request) -> void {
    std::unique_ptr<llama_rs_decision_request>(request).reset();
}

extern "C" auto llama_rs_decision_request_question_count(llama_rs_decision_request_handle request) -> size_t {
    return request->questions.size();
}

extern "C" auto llama_rs_decision_request_question_variant_count(
    llama_rs_decision_model_handle model,
    llama_rs_decision_request_handle request,
    size_t question_index) -> size_t {
    return model->context.n_variants(request->questions.at(question_index));
}

extern "C" auto llama_rs_decision_request_has_media(llama_rs_decision_request_handle request) -> bool {
    return !request->files.empty();
}

extern "C" auto llama_rs_decision_prompt_fill(
    llama_rs_decision_model_handle model,
    llama_rs_decision_request_handle request,
    size_t question_index,
    size_t variant,
    mtmd_context * projector,
    llama_rs_decision_prompt_handle * out_prompt,
    char ** out_error) -> llama_rs_decision_prompt_fill_status {
    return fill_prompt(
        model,
        request,
        projector,
        out_prompt,
        out_error,
        false,
        question_index,
        [&](const mtmd_helper_init_opt & init_opt, server_task & task) -> void {
            model->context.fill_task(
                request->state,
                request->questions,
                request->questions.at(question_index),
                variant,
                request->files,
                projector,
                init_opt,
                task);
        });
}

extern "C" auto llama_rs_decision_prompt_fill_joint(
    llama_rs_decision_model_handle model,
    llama_rs_decision_request_handle request,
    mtmd_context * projector,
    llama_rs_decision_prompt_handle * out_prompt,
    char ** out_error) -> llama_rs_decision_prompt_fill_status {
    return fill_prompt(
        model,
        request,
        projector,
        out_prompt,
        out_error,
        true,
        0,
        [&](const mtmd_helper_init_opt & init_opt, server_task & task) -> void {
            model->context.fill_task_joint(
                request->state, request->questions, request->files, projector, init_opt, task);
        });
}

extern "C" auto llama_rs_decision_prompt_free(llama_rs_decision_prompt_handle prompt) -> void {
    std::unique_ptr<llama_rs_decision_prompt>(prompt).reset();
}

extern "C" auto llama_rs_decision_prompts_shared_prefix(
    llama_rs_decision_prompt_handle * prompts,
    size_t prompt_count,
    size_t * out_shared_prefix_length) -> llama_rs_decision_prompts_share_status {
    const gsl::span<llama_rs_decision_prompt_handle> handles(prompts, prompt_count);

    try {
        if (llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_DECISION_PROMPTS_SHARE_RUNS_OUT_OF_MEMORY)) {
            throw std::bad_alloc();
        }

        std::vector<server_task> tasks;

        tasks.reserve(prompt_count);
        std::transform(handles.begin(), handles.end(), std::back_inserter(tasks),
            [](llama_rs_decision_prompt * prompt) -> server_task { return std::move(prompt->task); });

        std::vector<server_task> groups = server_decision_group_tasks(std::move(tasks), prompt_count);

        if (groups.size() == prompt_count) {
            for (size_t prompt_index = 0; prompt_index < prompt_count; prompt_index++) {
                handles[prompt_index]->task = std::move(groups[prompt_index]);
            }

            *out_shared_prefix_length = 0;

            return LLAMA_RS_DECISION_PROMPTS_SHARE_OK;
        }

        server_task & parent = groups.front();

        for (size_t prompt_index = 1; prompt_index < prompt_count; prompt_index++) {
            handles[prompt_index]->task = std::move(parent.child_tasks[prompt_index - 1]);
        }

        parent.child_tasks.clear();
        *out_shared_prefix_length = static_cast<size_t>(parent.n_tokens_shared);
        handles[0]->task = std::move(parent);

        return LLAMA_RS_DECISION_PROMPTS_SHARE_OK;
    } catch (const std::bad_alloc &) {
        return LLAMA_RS_DECISION_PROMPTS_SHARE_LLAMA_CPP_OUT_OF_MEMORY;
    }
}

extern "C" auto llama_rs_decision_media_marker() -> const char * {
    return get_media_marker();
}

extern "C" auto llama_rs_decision_prompt_media_chunk(llama_rs_decision_prompt_handle prompt, size_t position)
    -> const mtmd_input_chunk * {
    const auto & tokens = prompt->task.tokens;

    if (position >= tokens.size() || tokens[position] != LLAMA_TOKEN_NULL) {
        return nullptr;
    }

    try {
        return tokens.find_chunk(position).get();
    } catch (const std::runtime_error &) {
        return nullptr;
    }
}

extern "C" auto llama_rs_decision_prompt_layout_of(llama_rs_decision_prompt_handle prompt)
    -> llama_rs_decision_prompt_layout {
    const int32_t pos_first = prompt->task.decision.pos_first();
    const size_t last_position = prompt->token_ids.empty() ? 0 : prompt->token_ids.size() - 1;

    return {
        prompt->token_ids.data(),
        prompt->token_ids.size(),
        prompt->orders.data(),
        prompt->orders.size(),
        pos_first < 0 ? last_position : static_cast<size_t>(pos_first),
        prompt->task.decision.labels.empty() && !prompt->task.decision.pooled,
    };
}

extern "C" auto llama_rs_decision_request_read_answers(
    llama_rs_decision_model_handle model,
    llama_rs_decision_request_handle request,
    llama_rs_decision_prompt_handle prompt,
    struct llama_context * context,
    const int32_t * batch_indices,
    size_t batch_indices_count,
    llama_seq_id sequence_id,
    char ** out_answers_json,
    char ** out_error) -> llama_rs_decision_answers_read_status {
    *out_answers_json = nullptr;
    *out_error = nullptr;

    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_DECISION_ANSWERS_READ_THROWS);

        std::vector<float> scores;

        try {
            scores = prompt_scores(
                *prompt, context, gsl::span<const int32_t>(batch_indices, batch_indices_count), sequence_id);
        } catch (const decision_outputs_unreadable & err) {
            return llama_rs_report_exception_message(
                out_error,
                err.what(),
                LLAMA_RS_DECISION_ANSWERS_READ_OUTPUTS_UNREADABLE,
                LLAMA_RS_DECISION_ANSWERS_READ_ERROR_STRING_ALLOCATION_FAILED);
        }

        std::vector<size_t> answered_questions;

        if (prompt->joint) {
            size_t first = 0;

            for (size_t question_index = 0; question_index < request->questions.size(); ++question_index) {
                const size_t option_count = request->questions[question_index].options.size();

                if (first + option_count > scores.size()) {
                    return llama_rs_report_exception_message(
                        out_error,
                        "the joint scores do not cover every question",
                        LLAMA_RS_DECISION_ANSWERS_READ_OUTPUTS_UNREADABLE,
                        LLAMA_RS_DECISION_ANSWERS_READ_ERROR_STRING_ALLOCATION_FAILED);
                }

                const auto question_begin = scores.begin() + static_cast<std::ptrdiff_t>(first);

                request->variant_scores[question_index].emplace_back(question_begin, question_begin + static_cast<std::ptrdiff_t>(option_count));
                answered_questions.push_back(question_index);
                first += option_count;
            }
        } else {
            auto & variant_scores = request->variant_scores.at(prompt->question_index);

            variant_scores.push_back(std::move(scores));
            if (variant_scores.size() == model->context.n_variants(request->questions[prompt->question_index])) {
                answered_questions.push_back(prompt->question_index);
            }
        }

        json answers = json::object();

        for (const size_t question_index : answered_questions) {
            const auto & question = request->questions[question_index];

            try {
                answers[question.id] = model->context.format_answer(
                    question, request->variant_scores[question_index], !request->files.empty());
            } catch (const std::runtime_error & err) {
                return llama_rs_report_exception_message(
                    out_error,
                    err.what(),
                    LLAMA_RS_DECISION_ANSWERS_READ_SCORES_REJECTED,
                    LLAMA_RS_DECISION_ANSWERS_READ_ERROR_STRING_ALLOCATION_FAILED);
            }
        }

        *out_answers_json = llama_rs_dup_string(answers.dump());
        if (*out_answers_json == nullptr) {
            return LLAMA_RS_DECISION_ANSWERS_READ_ERROR_STRING_ALLOCATION_FAILED;
        }

        return LLAMA_RS_DECISION_ANSWERS_READ_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_DECISION_ANSWERS_READ_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_DECISION_ANSWERS_READ_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_DECISION_ANSWERS_READ_ERROR_STRING_ALLOCATION_FAILED);
    }
}