llama-cpp-bindings-sys 0.31.0

Low level bindings to llama.cpp
Documentation
#pragma once

#include <ggml-backend.h>
#include <llama.h>

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

struct llama_model;
struct llama_sampler;
struct llama_vocab;

#include "wrapper_utils.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum llama_rs_json_schema_to_grammar_status {
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_OK = 0,
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_SCHEMA,
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_JSON,
    LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_json_schema_to_grammar_status;

llama_rs_json_schema_to_grammar_status llama_rs_json_schema_to_grammar(
    const char * schema_json,
    char ** out_grammar,
    char ** out_error);

typedef enum llama_rs_sampler_init_grammar_status {
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_OK = 0,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_sampler_init_grammar_status;

llama_rs_sampler_init_grammar_status llama_rs_sampler_init_grammar(
    const struct llama_vocab * vocab,
    const char * grammar_str,
    const char * grammar_root,
    struct llama_sampler ** out_sampler,
    char ** out_error);

typedef enum llama_rs_sampler_init_grammar_lazy_patterns_status {
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_OK = 0,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_INVALID_TRIGGER_PATTERN,
    LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_sampler_init_grammar_lazy_patterns_status;

llama_rs_sampler_init_grammar_lazy_patterns_status llama_rs_sampler_init_grammar_lazy_patterns(
    const struct llama_vocab * vocab,
    const char * grammar_str,
    const char * grammar_root,
    const char ** trigger_patterns,
    size_t num_trigger_patterns,
    const llama_token * trigger_tokens,
    size_t num_trigger_tokens,
    struct llama_sampler ** out_sampler,
    char ** out_error);

typedef enum llama_rs_sampler_accept_status {
    LLAMA_RS_SAMPLER_ACCEPT_OK = 0,
    LLAMA_RS_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_sampler_accept_status;

llama_rs_sampler_accept_status llama_rs_sampler_accept(
    struct llama_sampler * sampler,
    llama_token token,
    char ** out_error);

typedef enum llama_rs_sampler_sample_status {
    LLAMA_RS_SAMPLER_SAMPLE_OK = 0,
    LLAMA_RS_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_sampler_sample_status;

llama_rs_sampler_sample_status llama_rs_sampler_sample(
    struct llama_sampler * sampler,
    struct llama_context * ctx,
    int32_t idx,
    llama_token * out_token,
    char ** out_error);

char * llama_rs_string_dup(const char * value);

void llama_rs_string_free(char * ptr);

typedef enum llama_rs_memory_seq_pos_max_status {
    LLAMA_RS_MEMORY_SEQ_POS_MAX_OK = 0,
    LLAMA_RS_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_memory_seq_pos_max_status;

llama_rs_memory_seq_pos_max_status llama_rs_memory_seq_pos_max(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos * out_position,
    char ** out_error);

typedef enum llama_rs_memory_seq_add_status {
    LLAMA_RS_MEMORY_SEQ_ADD_OK = 0,
    LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_memory_seq_add_status;

llama_rs_memory_seq_add_status llama_rs_memory_seq_add(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos pos_start,
    llama_pos pos_end,
    llama_pos shift,
    char ** out_error);

typedef enum llama_rs_memory_seq_div_status {
    LLAMA_RS_MEMORY_SEQ_DIV_OK = 0,
    LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_memory_seq_div_status;

llama_rs_memory_seq_div_status llama_rs_memory_seq_div(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos pos_start,
    llama_pos pos_end,
    int divisor,
    char ** out_error);

typedef enum llama_rs_load_model_from_file_status {
    LLAMA_RS_LOAD_MODEL_FROM_FILE_OK = 0,
    LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL,
    LLAMA_RS_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_load_model_from_file_status;

llama_rs_load_model_from_file_status llama_rs_load_model_from_file(
    const char * path,
    struct llama_model_params params,
    struct llama_model ** out_model,
    char ** out_error);

typedef enum llama_rs_adapter_lora_init_status {
    LLAMA_RS_ADAPTER_LORA_INIT_OK = 0,
    LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_RETURNED_NULL,
    LLAMA_RS_ADAPTER_LORA_INIT_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_adapter_lora_init_status;

llama_rs_adapter_lora_init_status llama_rs_adapter_lora_init(
    struct llama_model * model,
    const char * path_lora,
    struct llama_adapter_lora ** out_adapter,
    char ** out_error);

typedef enum llama_rs_new_context_with_model_status {
    LLAMA_RS_NEW_CONTEXT_WITH_MODEL_OK = 0,
    LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL,
    LLAMA_RS_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_new_context_with_model_status;

llama_rs_new_context_with_model_status llama_rs_new_context_with_model(
    struct llama_model * model,
    struct llama_context_params params,
    struct llama_context ** out_ctx,
    char ** out_error);

typedef enum llama_rs_process_status {
    LLAMA_RS_PROCESS_OK = 0,
    LLAMA_RS_PROCESS_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_PROCESS_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_PROCESS_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_process_status;

llama_rs_process_status llama_rs_process(
    struct llama_context * ctx,
    enum llama_process_type process_type,
    struct llama_batch_ext * batch,
    int32_t * out_llama_cpp_return_code,
    char ** out_error);

typedef enum llama_rs_tokenize_status {
    LLAMA_RS_TOKENIZE_OK = 0,
    LLAMA_RS_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_tokenize_status;

llama_rs_tokenize_status llama_rs_tokenize(
    const struct llama_vocab * vocab,
    const char * text,
    int32_t text_len,
    llama_token * tokens,
    int32_t n_tokens_max,
    bool add_special,
    bool parse_special,
    int32_t * out_returned_count,
    char ** out_error);

typedef enum llama_rs_sampler_apply_status {
    LLAMA_RS_SAMPLER_APPLY_OK = 0,
    LLAMA_RS_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED,
    LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_OUT_OF_MEMORY,
    LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_sampler_apply_status;

llama_rs_sampler_apply_status llama_rs_sampler_apply(
    struct llama_sampler * sampler,
    struct llama_token_data_array * data_array,
    char ** out_error);

int32_t llama_rs_token_to_piece(
    const struct llama_vocab * vocab,
    llama_token token,
    char * buf,
    int32_t length,
    int32_t lstrip,
    bool special);

const char * llama_rs_backend_device_text(const char * text);

uint32_t llama_rs_backend_device_type(ggml_backend_dev_t dev);

#ifdef __cplusplus
}
#endif