Module: LlamaCpp

Defined in:
lib/llama_cpp.rb,
lib/llama_cpp/version.rb,
ext/llama_cpp/llama_cpp.c

Overview

llama_cpp.rb provides Ruby bindings for the llama.cpp.

Defined Under Namespace

Classes: LlamaAdapterLora, LlamaBatch, LlamaChatMessage, LlamaContext, LlamaContextParams, LlamaLogitBias, LlamaMemoryT, LlamaModel, LlamaModelKvOverride, LlamaModelParams, LlamaModelQuantizeParams, LlamaModelTensorBuftOverride, LlamaPerfContextData, LlamaPerfSamplerData, LlamaSampler, LlamaSamplerChainParams, LlamaTokenData, LlamaTokenDataArray, LlamaVocab

Constant Summary collapse

VERSION =

The version of llama_cpp.rb you install.

'0.23.3'
LLAMA_CPP_VERSION =

The supported version of llama.cpp.

'b6730'
LLAMA_DEFAULT_SEED =
rb_str_new2(tmp)
LLAMA_TOKEN_NULL =
INT2NUM(LLAMA_TOKEN_NULL)
LLAMA_STATE_SEQ_FLAGS_SWA_ONLY =
INT2NUM(LLAMA_STATE_SEQ_FLAGS_SWA_ONLY)
LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY =
INT2NUM(LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY)
LLAMA_FILE_MAGIC_GGLA =
rb_str_new2(tmp)
LLAMA_FILE_MAGIC_GGSN =
rb_str_new2(tmp)
LLAMA_FILE_MAGIC_GGSQ =
rb_str_new2(tmp)
LLAMA_SESSION_MAGIC =
rb_str_new2(tmp)
LLAMA_SESSION_VERSION =
INT2NUM(LLAMA_SESSION_VERSION)
LLAMA_STATE_SEQ_MAGIC =
rb_str_new2(tmp)
LLAMA_STATE_SEQ_VERSION =
INT2NUM(LLAMA_STATE_SEQ_VERSION)
LLAMA_VOCAB_TYPE_NONE =
INT2NUM(LLAMA_VOCAB_TYPE_NONE)
LLAMA_VOCAB_TYPE_SPM =
INT2NUM(LLAMA_VOCAB_TYPE_SPM)
LLAMA_VOCAB_TYPE_BPE =
INT2NUM(LLAMA_VOCAB_TYPE_BPE)
LLAMA_VOCAB_TYPE_WPM =
INT2NUM(LLAMA_VOCAB_TYPE_WPM)
LLAMA_VOCAB_TYPE_UGM =
INT2NUM(LLAMA_VOCAB_TYPE_UGM)
LLAMA_VOCAB_TYPE_RWKV =
INT2NUM(LLAMA_VOCAB_TYPE_RWKV)
LLAMA_VOCAB_TYPE_PLAMO2 =
INT2NUM(LLAMA_VOCAB_TYPE_PLAMO2)
LLAMA_ROPE_TYPE_NONE =
INT2NUM(LLAMA_ROPE_TYPE_NONE)
LLAMA_ROPE_TYPE_NORM =
INT2NUM(LLAMA_ROPE_TYPE_NORM)
LLAMA_ROPE_TYPE_NEOX =
INT2NUM(LLAMA_ROPE_TYPE_NEOX)
LLAMA_ROPE_TYPE_MROPE =
INT2NUM(LLAMA_ROPE_TYPE_MROPE)
LLAMA_ROPE_TYPE_VISION =
INT2NUM(LLAMA_ROPE_TYPE_VISION)
LLAMA_TOKEN_TYPE_UNDEFINED =
INT2NUM(LLAMA_TOKEN_TYPE_UNDEFINED)
LLAMA_TOKEN_TYPE_NORMAL =
INT2NUM(LLAMA_TOKEN_TYPE_NORMAL)
LLAMA_TOKEN_TYPE_UNKNOWN =
INT2NUM(LLAMA_TOKEN_TYPE_UNKNOWN)
LLAMA_TOKEN_TYPE_CONTROL =
INT2NUM(LLAMA_TOKEN_TYPE_CONTROL)
LLAMA_TOKEN_TYPE_USER_DEFINED =
INT2NUM(LLAMA_TOKEN_TYPE_USER_DEFINED)
LLAMA_TOKEN_TYPE_UNUSED =
INT2NUM(LLAMA_TOKEN_TYPE_UNUSED)
LLAMA_TOKEN_TYPE_BYTE =
INT2NUM(LLAMA_TOKEN_TYPE_BYTE)
LLAMA_TOKEN_ATTR_UNDEFINED =
INT2NUM(LLAMA_TOKEN_ATTR_UNDEFINED)
LLAMA_TOKEN_ATTR_UNKNOWN =
INT2NUM(LLAMA_TOKEN_ATTR_UNKNOWN)
LLAMA_TOKEN_ATTR_UNUSED =
INT2NUM(LLAMA_TOKEN_ATTR_UNUSED)
LLAMA_TOKEN_ATTR_NORMAL =
INT2NUM(LLAMA_TOKEN_ATTR_NORMAL)
LLAMA_TOKEN_ATTR_CONTROL =
INT2NUM(LLAMA_TOKEN_ATTR_CONTROL)
LLAMA_TOKEN_ATTR_USER_DEFINED =
INT2NUM(LLAMA_TOKEN_ATTR_USER_DEFINED)
LLAMA_TOKEN_ATTR_BYTE =
INT2NUM(LLAMA_TOKEN_ATTR_BYTE)
LLAMA_TOKEN_ATTR_NORMALIZED =
INT2NUM(LLAMA_TOKEN_ATTR_NORMALIZED)
LLAMA_TOKEN_ATTR_LSTRIP =
INT2NUM(LLAMA_TOKEN_ATTR_LSTRIP)
LLAMA_TOKEN_ATTR_RSTRIP =
INT2NUM(LLAMA_TOKEN_ATTR_RSTRIP)
LLAMA_TOKEN_ATTR_SINGLE_WORD =
INT2NUM(LLAMA_TOKEN_ATTR_SINGLE_WORD)
LLAMA_FTYPE_ALL_F32 =
INT2NUM(LLAMA_FTYPE_ALL_F32)
LLAMA_FTYPE_MOSTLY_F16 =
INT2NUM(LLAMA_FTYPE_MOSTLY_F16)
LLAMA_FTYPE_MOSTLY_Q4_0 =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q4_0)
LLAMA_FTYPE_MOSTLY_Q4_1 =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q4_1)
LLAMA_FTYPE_MOSTLY_Q8_0 =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q8_0)
LLAMA_FTYPE_MOSTLY_Q5_0 =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q5_0)
LLAMA_FTYPE_MOSTLY_Q5_1 =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q5_1)
LLAMA_FTYPE_MOSTLY_Q2_K =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q2_K)
LLAMA_FTYPE_MOSTLY_Q3_K_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q3_K_S)
LLAMA_FTYPE_MOSTLY_Q3_K_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q3_K_M)
LLAMA_FTYPE_MOSTLY_Q3_K_L =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q3_K_L)
LLAMA_FTYPE_MOSTLY_Q4_K_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q4_K_S)
LLAMA_FTYPE_MOSTLY_Q4_K_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q4_K_M)
LLAMA_FTYPE_MOSTLY_Q5_K_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q5_K_S)
LLAMA_FTYPE_MOSTLY_Q5_K_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q5_K_M)
LLAMA_FTYPE_MOSTLY_Q6_K =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q6_K)
LLAMA_FTYPE_MOSTLY_IQ2_XXS =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ2_XXS)
LLAMA_FTYPE_MOSTLY_IQ2_XS =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ2_XS)
LLAMA_FTYPE_MOSTLY_Q2_K_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_Q2_K_S)
LLAMA_FTYPE_MOSTLY_IQ3_XS =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ3_XS)
LLAMA_FTYPE_MOSTLY_IQ3_XXS =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ3_XXS)
LLAMA_FTYPE_MOSTLY_IQ1_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ1_S)
LLAMA_FTYPE_MOSTLY_IQ4_NL =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ4_NL)
LLAMA_FTYPE_MOSTLY_IQ3_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ3_S)
LLAMA_FTYPE_MOSTLY_IQ3_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ3_M)
LLAMA_FTYPE_MOSTLY_IQ2_S =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ2_S)
LLAMA_FTYPE_MOSTLY_IQ2_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ2_M)
LLAMA_FTYPE_MOSTLY_IQ4_XS =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ4_XS)
LLAMA_FTYPE_MOSTLY_IQ1_M =
INT2NUM(LLAMA_FTYPE_MOSTLY_IQ1_M)
LLAMA_FTYPE_MOSTLY_BF16 =
INT2NUM(LLAMA_FTYPE_MOSTLY_BF16)
LLAMA_FTYPE_MOSTLY_TQ1_0 =
INT2NUM(LLAMA_FTYPE_MOSTLY_TQ1_0)
LLAMA_FTYPE_MOSTLY_TQ2_0 =
INT2NUM(LLAMA_FTYPE_MOSTLY_TQ2_0)
LLAMA_FTYPE_MOSTLY_MXFP4_MOE =
INT2NUM(LLAMA_FTYPE_MOSTLY_MXFP4_MOE)
LLAMA_FTYPE_GUESSED =
INT2NUM(LLAMA_FTYPE_GUESSED)
LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED)
LLAMA_ROPE_SCALING_TYPE_NONE =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_NONE)
LLAMA_ROPE_SCALING_TYPE_LINEAR =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_LINEAR)
LLAMA_ROPE_SCALING_TYPE_YARN =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_YARN)
LLAMA_ROPE_SCALING_TYPE_LONGROPE =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_LONGROPE)
LLAMA_ROPE_SCALING_TYPE_MAX_VALUE =
INT2NUM(LLAMA_ROPE_SCALING_TYPE_MAX_VALUE)
LLAMA_POOLING_TYPE_UNSPECIFIED =
INT2NUM(LLAMA_POOLING_TYPE_UNSPECIFIED)
LLAMA_POOLING_TYPE_NONE =
INT2NUM(LLAMA_POOLING_TYPE_NONE)
LLAMA_POOLING_TYPE_MEAN =
INT2NUM(LLAMA_POOLING_TYPE_MEAN)
LLAMA_POOLING_TYPE_CLS =
INT2NUM(LLAMA_POOLING_TYPE_CLS)
LLAMA_POOLING_TYPE_LAST =
INT2NUM(LLAMA_POOLING_TYPE_LAST)
LLAMA_POOLING_TYPE_RANK =
INT2NUM(LLAMA_POOLING_TYPE_RANK)
LLAMA_ATTENTION_TYPE_UNSPECIFIED =
INT2NUM(LLAMA_ATTENTION_TYPE_UNSPECIFIED)
LLAMA_ATTENTION_TYPE_CAUSAL =
INT2NUM(LLAMA_ATTENTION_TYPE_CAUSAL)
LLAMA_ATTENTION_TYPE_NON_CAUSAL =
INT2NUM(LLAMA_ATTENTION_TYPE_NON_CAUSAL)
LLAMA_FLASH_ATTN_TYPE_AUTO =
INT2NUM(LLAMA_FLASH_ATTN_TYPE_AUTO)
LLAMA_FLASH_ATTN_TYPE_DISABLED =
INT2NUM(LLAMA_FLASH_ATTN_TYPE_DISABLED)
LLAMA_FLASH_ATTN_TYPE_ENABLED =
INT2NUM(LLAMA_FLASH_ATTN_TYPE_ENABLED)
LLAMA_SPLIT_MODE_NONE =
INT2NUM(LLAMA_SPLIT_MODE_NONE)
LLAMA_SPLIT_MODE_LAYER =
INT2NUM(LLAMA_SPLIT_MODE_LAYER)
LLAMA_SPLIT_MODE_ROW =
INT2NUM(LLAMA_SPLIT_MODE_ROW)
LLAMA_KV_OVERRIDE_TYPE_INT =
INT2NUM(LLAMA_KV_OVERRIDE_TYPE_INT)
LLAMA_KV_OVERRIDE_TYPE_FLOAT =
INT2NUM(LLAMA_KV_OVERRIDE_TYPE_FLOAT)
LLAMA_KV_OVERRIDE_TYPE_BOOL =
INT2NUM(LLAMA_KV_OVERRIDE_TYPE_BOOL)
LLAMA_KV_OVERRIDE_TYPE_STR =
INT2NUM(LLAMA_KV_OVERRIDE_TYPE_STR)

Class Method Summary collapse

Class Method Details

.generate(context, prompt, n_predict: 128) ⇒ String

Generates sentences following the given prompt for operation check.

Parameters:

  • context (LlamaCpp::LlamaContext)

    The context to use.

  • prompt (String)

    The prompt to start generation with.

  • n_predict (Integer) (defaults to: 128)

    The number of tokens to predict.

Returns:

  • (String)

Raises:

  • (ArgumentError)


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
# File 'lib/llama_cpp.rb', line 16

def generate(context, prompt, n_predict: 128) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  raise ArgumentError, 'context must be a LlamaContext' unless context.is_a?(LlamaCpp::LlamaContext)
  raise ArgumentError, 'prompt must be a String' unless prompt.is_a?(String)

  model = LlamaCpp.llama_get_model(context)
  vocab = LlamaCpp.llama_model_get_vocab(model)

  n_prompt = -LlamaCpp.llama_tokenize(vocab, prompt, [], 0, true, true)

  prompt_tokens = []
  raise 'Failed to tokenize the prompt' if LlamaCpp.llama_tokenize(vocab, prompt, prompt_tokens, n_prompt, true,
                                                                   true).negative?

  ctx_params = LlamaCpp::LlamaContextParams.new
  ctx_params.n_ctx = n_prompt + n_predict - 1
  ctx_params.n_batch = n_prompt
  ctx_params.no_perf = false

  ctx = LlamaCpp.llama_init_from_model(model, ctx_params)

  sparams = LlamaCpp::LlamaSamplerChainParams.new
  sparams.no_perf = false
  smpl = LlamaCpp.llama_sampler_chain_init(sparams)
  LlamaCpp.llama_sampler_chain_add(smpl, LlamaCpp.llama_sampler_init_greedy)

  batch = LlamaCpp.llama_batch_get_one(prompt_tokens)

  n_pos = 0
  output = []
  while n_pos + batch.n_tokens < n_prompt + n_predict
    break if LlamaCpp.llama_decode(ctx, batch) != 0

    n_pos += batch.n_tokens

    new_token_id = LlamaCpp.llama_sampler_sample(smpl, ctx, -1)
    break if llama_vocab_is_eog?(vocab, new_token_id)

    buf = llama_token_to_piece(vocab, new_token_id, 0, true)
    output << buf

    batch = LlamaCpp.llama_batch_get_one([new_token_id])
  end

  output.join
end

.ggml_backend_load_allNilClass

Returns:

  • (NilClass)


25
26
27
28
# File 'ext/llama_cpp/llama_cpp.c', line 25

static VALUE rb_ggml_backend_load_all(VALUE self) {
  ggml_backend_load_all();
  return Qnil;
}

.llama_adapter_get_alora_n_invocation_tokens(adapter) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1985
1986
1987
1988
1989
1990
1991
1992
# File 'ext/llama_cpp/llama_cpp.c', line 1985

static VALUE rb_llama_adapter_get_alora_n_invocation_tokens(VALUE self, VALUE adapter) {
  if (!rb_obj_is_kind_of(adapter, rb_cLlamaAdapterLora)) {
    rb_raise(rb_eArgError, "adapter must be a LlamaAdapterLora");
    return Qnil;
  }
  llama_adapter_lora_wrapper* adapter_wrapper = get_llama_adapter_lora_wrapper(adapter);
  return ULONG2NUM(llama_adapter_get_alora_n_invocation_tokens(adapter_wrapper->adapter));
}

.llama_adapter_lora_free(adapter) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
# File 'ext/llama_cpp/llama_cpp.c', line 1966

static VALUE rb_llama_adapter_lora_free(VALUE self, VALUE adapter) {
  if (!rb_obj_is_kind_of(adapter, rb_cLlamaAdapterLora)) {
    rb_raise(rb_eArgError, "adapter must be a LlamaAdapterLora");
    return Qnil;
  }
  llama_adapter_lora_wrapper* adapter_wrapper = get_llama_adapter_lora_wrapper(adapter);
  if (adapter_wrapper->adapter != NULL) {
    llama_adapter_lora_free(adapter_wrapper->adapter);
    adapter_wrapper->adapter = NULL;
  }
  RB_GC_GUARD(adapter);
  return Qnil;
}

.llama_adapter_lora_init(model, path_lora) ⇒ LlamaAdapterLora

Parameters:

Returns:



1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
# File 'ext/llama_cpp/llama_cpp.c', line 1862

static VALUE rb_llama_adapter_lora_init(VALUE self, VALUE model, VALUE path_lora) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  if (!RB_TYPE_P(path_lora, T_STRING)) {
    rb_raise(rb_eArgError, "path_lora must be a String");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  const char* path_lora_ = StringValueCStr(path_lora);
  llama_adapter_lora_wrapper* adapter_wrapper = (llama_adapter_lora_wrapper*)ruby_xmalloc(sizeof(llama_adapter_lora_wrapper));
  adapter_wrapper->adapter = llama_adapter_lora_init(model_wrapper->model, path_lora_);
  RB_GC_GUARD(model);
  RB_GC_GUARD(path_lora);
  return TypedData_Wrap_Struct(rb_cLlamaAdapterLora, &llama_adapter_lora_wrapper_data_type, adapter_wrapper);
}

.llama_adapter_meta_count(adapter) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1885
1886
1887
1888
1889
1890
1891
1892
# File 'ext/llama_cpp/llama_cpp.c', line 1885

static VALUE rb_llama_adapter_meta_count(VALUE self, VALUE adapter) {
  if (!rb_obj_is_kind_of(adapter, rb_cLlamaAdapterLora)) {
    rb_raise(rb_eArgError, "adapter must be a LlamaAdapterLora");
    return Qnil;
  }
  llama_adapter_lora_wrapper* adapter_wrapper = get_llama_adapter_lora_wrapper(adapter);
  return INT2NUM(llama_adapter_meta_count(adapter_wrapper->adapter));
}

.llama_backend_freeNilClass

Returns:

  • (NilClass)


1183
1184
1185
1186
# File 'ext/llama_cpp/llama_cpp.c', line 1183

static VALUE rb_llama_backend_free(VALUE self) {
  llama_backend_free();
  return Qnil;
}

.llama_backend_initNilClass

Returns:

  • (NilClass)


1174
1175
1176
1177
# File 'ext/llama_cpp/llama_cpp.c', line 1174

static VALUE rb_llama_backend_init(VALUE self) {
  llama_backend_init();
  return Qnil;
}

.llama_batch_free(batch) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
# File 'ext/llama_cpp/llama_cpp.c', line 2350

static VALUE rb_llama_batch_free(VALUE self, VALUE batch) {
  if (!rb_obj_is_kind_of(batch, rb_cLlamaBatch)) {
    rb_raise(rb_eArgError, "batch must be a LlamaBatch");
    return Qnil;
  }
  llama_batch* batch_ = get_llama_batch(batch);
  llama_batch_free(*batch_);
  batch_ = NULL;
  RB_GC_GUARD(batch);
  return Qnil;
}

.llama_batch_get_one(tokens) ⇒ LlamaBatch

Parameters:

  • tokens (Array<Integer>)

Returns:



2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
# File 'ext/llama_cpp/llama_cpp.c', line 2295

static VALUE rb_llama_batch_get_one(VALUE self, VALUE tokens) {
  if (!RB_TYPE_P(tokens, T_ARRAY)) {
    rb_raise(rb_eArgError, "tokens must be an Array");
    return Qnil;
  }
  const size_t n_tokens = RARRAY_LEN(tokens);
  if (n_tokens == 0) {
    return Qnil;
  }
  llama_token* tokens_ = (llama_token*)ruby_xmalloc(sizeof(llama_token) * n_tokens);
  for (size_t i = 0; i < n_tokens; i++) {
    VALUE token = rb_ary_entry(tokens, i);
    if (!RB_INTEGER_TYPE_P(token)) {
      ruby_xfree(tokens_);
      rb_raise(rb_eArgError, "tokens must be an Array of Integers");
      return Qnil;
    }
    tokens_[i] = NUM2INT(token);
  }
  llama_batch* batch = (llama_batch*)ruby_xmalloc(sizeof(llama_batch));
  *batch = llama_batch_get_one(NULL, (int32_t)n_tokens);
  batch->token = tokens_;
  return TypedData_Wrap_Struct(rb_cLlamaBatch, &llama_batch_type, batch);
}

.llama_batch_init(n_tokens, embd, n_seq_max) ⇒ LlamaBatch

Parameters:

  • n_tokens (Integer)
  • embd (Integer)
  • n_seq_max (Integer)

Returns:



2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'ext/llama_cpp/llama_cpp.c', line 2327

static VALUE rb_llama_batch_init(VALUE self, VALUE n_tokens, VALUE embd, VALUE n_seq_max) {
  if (!RB_INTEGER_TYPE_P(n_tokens)) {
    rb_raise(rb_eArgError, "n_tokens must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(embd)) {
    rb_raise(rb_eArgError, "embd must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(n_seq_max)) {
    rb_raise(rb_eArgError, "n_seq_max must be an Integer");
    return Qnil;
  }
  llama_batch* batch = (llama_batch*)ruby_xmalloc(sizeof(llama_batch));
  *batch = llama_batch_init(NUM2INT(n_tokens), NUM2INT(embd), NUM2INT(n_seq_max));
  return TypedData_Wrap_Struct(rb_cLlamaBatch, &llama_batch_type, batch);
}

.llama_clear_adapter_lora(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
# File 'ext/llama_cpp/llama_cpp.c', line 1950

static VALUE rb_llama_clear_adapter_lora(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_clear_adapter_lora(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_decode(context, batch) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
# File 'ext/llama_cpp/llama_cpp.c', line 2391

static VALUE rb_llama_decode(VALUE self, VALUE ctx, VALUE batch) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(batch, rb_cLlamaBatch)) {
    rb_raise(rb_eArgError, "batch must be a LlamaBatch");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_batch* batch_ = get_llama_batch(batch);
  const int32_t res = llama_decode(context_wrapper->context, *batch_);
  RB_GC_GUARD(ctx);
  RB_GC_GUARD(batch);
  return INT2NUM(res);
}

.llama_detokenize(vocab, tokens, remove_special, unparse_special) ⇒ String

Parameters:

  • vocab (LlamaVocab)
  • tokens (Array<Integer>)
  • remove_special (Boolean)
  • unparse_special (Boolean)

Returns:

  • (String)


3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
# File 'ext/llama_cpp/llama_cpp.c', line 3022

static VALUE rb_llama_detokenize(VALUE self, VALUE vocab, VALUE tokens, VALUE remove_special, VALUE unparse_special) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_TYPE_P(tokens, T_ARRAY)) {
    rb_raise(rb_eArgError, "tokens must be an Array");
    return Qnil;
  }

  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t n_tokens = (int32_t)RARRAY_LEN(tokens);
  if (n_tokens == 0) {
    return Qnil;
  }
  llama_token* tokens_ = (llama_token*)ruby_xmalloc(sizeof(llama_token) * n_tokens);
  for (int32_t i = 0; i < n_tokens; i++) {
    tokens_[i] = NUM2INT(rb_ary_entry(tokens, i));
  }
  const int32_t text_len_max = n_tokens > 1024 ? n_tokens : 1024;
  char* text = (char*)ruby_xmalloc(sizeof(char) * text_len_max);
  const bool remove_special_ = RTEST(remove_special) ? true : false;
  const bool unparse_special_ = RTEST(unparse_special) ? true : false;

  int32_t n_chars = llama_detokenize(vocab_wrapper->vocab, tokens_, n_tokens, text, text_len_max, remove_special_, unparse_special_);

  if (n_chars < 0) {
    ruby_xfree(text);
    text = (char*)ruby_xmalloc(sizeof(char) * -n_chars);
    n_chars = llama_detokenize(vocab_wrapper->vocab, tokens_, n_tokens, text, -n_chars, remove_special_, unparse_special_);
    if (n_chars <= (int32_t)strlen(text)) {
      ruby_xfree(tokens_);
      ruby_xfree(text);
      rb_raise(rb_eRuntimeError, "Failed to detokenize");
      return Qnil;
    }
  }

  VALUE ret = rb_utf8_str_new_cstr(text);
  ruby_xfree(tokens_);
  ruby_xfree(text);
  RB_GC_GUARD(vocab);

  return ret;
}

.llama_encode(context, batch) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
# File 'ext/llama_cpp/llama_cpp.c', line 2368

static VALUE rb_llama_encode(VALUE self, VALUE ctx, VALUE batch) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(batch, rb_cLlamaBatch)) {
    rb_raise(rb_eArgError, "batch must be a LlamaBatch");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_batch* batch_ = get_llama_batch(batch);
  const int32_t res = llama_encode(context_wrapper->context, *batch_);
  RB_GC_GUARD(ctx);
  RB_GC_GUARD(batch);
  return INT2NUM(res);
}

.llama_flash_attn_type_name(flash_attn_type) ⇒ String

Parameters:

  • flash_attn_type (Integer)

Returns:

  • (String)


3935
3936
3937
3938
3939
3940
3941
3942
# File 'ext/llama_cpp/llama_cpp.c', line 3935

static VALUE rb_llama_flash_attn_type_name(VALUE self, VALUE flash_attn_type) {
  if (!RB_INTEGER_TYPE_P(flash_attn_type)) {
    rb_raise(rb_eArgError, "flash_attn_type must be an Integer");
    return Qnil;
  }
  const char* name = llama_flash_attn_type_name((enum llama_flash_attn_type)NUM2INT(flash_attn_type));
  return rb_utf8_str_new_cstr(name);
}

.llama_free(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'ext/llama_cpp/llama_cpp.c', line 1329

static VALUE rb_llama_free(VALUE self, VALUE context) {
  if (!rb_obj_is_kind_of(context, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "context must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(context);
  if (context_wrapper->context != NULL) {
    llama_free(context_wrapper->context);
    context_wrapper->context = NULL;
  }
  return Qnil;
}

.llama_get_memory(context) ⇒ LlamaMemoryT

Parameters:

Returns:



2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
# File 'ext/llama_cpp/llama_cpp.c', line 2214

static VALUE rb_llama_get_memory(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_memory_t memory = llama_get_memory(context_wrapper->context);
  llama_memory_t_wrapper* memory_wrapper = (llama_memory_t_wrapper*)ruby_xmalloc(sizeof(llama_memory_t_wrapper));
  memory_wrapper->memory = memory;
  RB_GC_GUARD(ctx);
  return TypedData_Wrap_Struct(rb_cLlamaMemoryT, &llama_memory_t_wrapper_data_type, memory_wrapper);
}

.llama_get_model(context) ⇒ LlamaModel

Parameters:

Returns:



1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
# File 'ext/llama_cpp/llama_cpp.c', line 1459

static VALUE rb_llama_get_model(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a Context");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_model_wrapper* model_wrapper = (llama_model_wrapper*)ruby_xmalloc(sizeof(llama_model_wrapper));
  model_wrapper->model = (struct llama_model*)llama_get_model(context_wrapper->context);
  model_wrapper->copied = true;
  RB_GC_GUARD(ctx);
  return TypedData_Wrap_Struct(rb_cLlamaModel, &llama_model_wrapper_data_type, model_wrapper);
}

.llama_init_from_model(model, params) ⇒ LlamaContext

Parameters:

Returns:



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
# File 'ext/llama_cpp/llama_cpp.c', line 1306

static VALUE rb_llama_init_from_model(VALUE self, VALUE model, VALUE params) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(params, rb_cLlamaContextParams)) {
    rb_raise(rb_eArgError, "params must be a LlamaContextParams");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  struct llama_context_params* params_ = get_llama_context_params(params);
  llama_context_wrapper* context_wrapper = (llama_context_wrapper*)ruby_xmalloc(sizeof(llama_context_wrapper));
  context_wrapper->context = llama_init_from_model(model_wrapper->model, *params_);
  RB_GC_GUARD(model);
  RB_GC_GUARD(params);
  return TypedData_Wrap_Struct(rb_cLlamaContext, &llama_context_wrapper_data_type, context_wrapper);
}

.llama_max_devicesInteger

Returns:

  • (Integer)


1354
1355
1356
# File 'ext/llama_cpp/llama_cpp.c', line 1354

static VALUE rb_llama_max_devices(VALUE self) {
  return SIZET2NUM(llama_max_devices());
}

.llama_max_parallel_sequencesInteger

Returns:

  • (Integer)


1362
1363
1364
# File 'ext/llama_cpp/llama_cpp.c', line 1362

static VALUE rb_llama_max_parallel_sequences(VALUE self) {
  return SIZET2NUM(llama_max_parallel_sequences());
}

.llama_memory_breakdown_print(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
# File 'ext/llama_cpp/llama_cpp.c', line 3184

static VALUE rb_llama_memory_breakdown_print(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_memory_breakdown_print(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_memory_can_shift?(memory) ⇒ Boolean

llama_memory_can_shift

Returns:

  • (Boolean)


2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
# File 'ext/llama_cpp/llama_cpp.c', line 2198

static VALUE rb_llama_memory_can_shift(VALUE self, VALUE memory) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  const bool can_shift = llama_memory_can_shift(memory_wrapper->memory);
  RB_GC_GUARD(memory);
  return can_shift ? Qtrue : Qfalse;
}

.llama_memory_clear(memory, data) ⇒ Object

llama_memory_clear



2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
# File 'ext/llama_cpp/llama_cpp.c', line 2038

static VALUE rb_llama_memory_clear(VALUE self, VALUE memory, VALUE data) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_memory_clear(memory_wrapper->memory, RTEST(data) ? true : false);
  RB_GC_GUARD(memory);
  return Qnil;
}

.llama_memory_seq_add(memory, seq_id, p0, p1, delta) ⇒ Object

llama_memory_seq_add



2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# File 'ext/llama_cpp/llama_cpp.c', line 2114

static VALUE rb_llama_memory_seq_add(VALUE self, VALUE memory, VALUE seq_id, VALUE p0, VALUE p1, VALUE delta) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p0)) {
    rb_raise(rb_eArgError, "p0 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p1)) {
    rb_raise(rb_eArgError, "p1 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(delta)) {
    rb_raise(rb_eArgError, "delta must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_memory_seq_add(memory_wrapper->memory, NUM2INT(seq_id), NUM2INT(p0), NUM2INT(p1), NUM2INT(delta));
  RB_GC_GUARD(memory);
  return Qnil;
}

.llama_memory_seq_cp(memory, seq_id_src, seq_id_dst, p0, p1) ⇒ Object

llama_memory_seq_cp



2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
# File 'ext/llama_cpp/llama_cpp.c', line 2072

static VALUE rb_llama_memory_seq_cp(VALUE self, VALUE memory, VALUE seq_id_src, VALUE seq_id_dst, VALUE p0, VALUE p1) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id_src)) {
    rb_raise(rb_eArgError, "seq_id_src must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id_dst)) {
    rb_raise(rb_eArgError, "seq_id_dst must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p0)) {
    rb_raise(rb_eArgError, "p0 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p1)) {
    rb_raise(rb_eArgError, "p1 must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_memory_seq_cp(memory_wrapper->memory, NUM2INT(seq_id_src), NUM2INT(seq_id_dst), NUM2INT(p0), NUM2INT(p1));
  RB_GC_GUARD(memory);
  return Qnil;
}

.llama_memory_seq_div(memory, seq_id, p0, p1, d) ⇒ Object

llama_memory_seq_div



2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
# File 'ext/llama_cpp/llama_cpp.c', line 2141

static VALUE rb_llama_memory_seq_div(VALUE self, VALUE memory, VALUE seq_id, VALUE p0, VALUE p1, VALUE d) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p0)) {
    rb_raise(rb_eArgError, "p0 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p1)) {
    rb_raise(rb_eArgError, "p1 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(d)) {
    rb_raise(rb_eArgError, "d must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_memory_seq_div(memory_wrapper->memory, NUM2INT(seq_id), NUM2INT(p0), NUM2INT(p1), NUM2INT(d));
  RB_GC_GUARD(memory);
  return Qnil;
}

.llama_memory_seq_keep(memory, seq_id) ⇒ Object

llama_memory_seq_keep



2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
# File 'ext/llama_cpp/llama_cpp.c', line 2099

static VALUE rb_llama_memory_seq_keep(VALUE self, VALUE memory, VALUE seq_id) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_memory_seq_keep(memory_wrapper->memory, NUM2INT(seq_id));
  RB_GC_GUARD(memory);
  return Qnil;
}

.llama_memory_seq_pos_max(memory, seq_id) ⇒ Object

llama_memory_seq_pos_max



2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
# File 'ext/llama_cpp/llama_cpp.c', line 2183

static VALUE rb_llama_memory_seq_pos_max(VALUE self, VALUE memory, VALUE seq_id) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_pos pos_max = llama_memory_seq_pos_max(memory_wrapper->memory, NUM2INT(seq_id));
  RB_GC_GUARD(memory);
  return INT2NUM(pos_max);
}

.llama_memory_seq_pos_min(memory, seq_id) ⇒ Object

llama_memory_seq_pos_min



2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
# File 'ext/llama_cpp/llama_cpp.c', line 2168

static VALUE rb_llama_memory_seq_pos_min(VALUE self, VALUE memory, VALUE seq_id) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  llama_pos pos_min = llama_memory_seq_pos_min(memory_wrapper->memory, NUM2INT(seq_id));
  RB_GC_GUARD(memory);
  return INT2NUM(pos_min);
}

.llama_memory_seq_rm(memory, seq_id, p0, p1) ⇒ Object

llama_memory_seq_rm



2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
# File 'ext/llama_cpp/llama_cpp.c', line 2049

static VALUE rb_llama_memory_seq_rm(VALUE self, VALUE memory, VALUE seq_id, VALUE p0, VALUE p1) {
  if (!rb_obj_is_kind_of(memory, rb_cLlamaMemoryT)) {
    rb_raise(rb_eArgError, "memory must be a LlamaMemoryT");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p0)) {
    rb_raise(rb_eArgError, "p0 must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(p1)) {
    rb_raise(rb_eArgError, "p1 must be an Integer");
    return Qnil;
  }
  llama_memory_t_wrapper* memory_wrapper = get_llama_memory_t_wrapper(memory);
  const bool res = llama_memory_seq_rm(memory_wrapper->memory, NUM2INT(seq_id), NUM2INT(p0), NUM2INT(p1));
  RB_GC_GUARD(memory);
  return res ? Qtrue : Qfalse;
}

.llama_model_cls_label(model, id) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
# File 'ext/llama_cpp/llama_cpp.c', line 1636

static VALUE rb_llama_model_cls_label(VALUE self, VALUE model, VALUE id) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(id)) {
    rb_raise(rb_eArgError, "id must be an Integer");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  const char* str = llama_model_cls_label(model_wrapper->model, NUM2UINT(id));
  RB_GC_GUARD(model);
  return rb_utf8_str_new_cstr(str);
}

.llama_model_decoder_start_token(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1775
1776
1777
1778
1779
1780
1781
1782
# File 'ext/llama_cpp/llama_cpp.c', line 1775

static VALUE rb_llama_model_decoder_start_token(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_decoder_start_token(model_wrapper->model));
}

.llama_model_desc(model) ⇒ String

Parameters:

Returns:

  • (String)


1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
# File 'ext/llama_cpp/llama_cpp.c', line 1688

static VALUE rb_llama_model_desc(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  char buf[128];
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  llama_model_desc(model_wrapper->model, buf, sizeof(buf));
  RB_GC_GUARD(model);
  return rb_utf8_str_new_cstr(buf);
}

.llama_model_free(model) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'ext/llama_cpp/llama_cpp.c', line 1207

static VALUE rb_llama_model_free(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  if (model_wrapper->model != NULL) {
    llama_model_free(model_wrapper->model);
    model_wrapper->model = NULL;
  }
  return Qnil;
}

.llama_model_get_vocab(model) ⇒ LlamaVocab

Parameters:

Returns:



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
# File 'ext/llama_cpp/llama_cpp.c', line 1491

static VALUE rb_llama_model_get_vocab(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  llama_vocab_wrapper* vocab_wrapper = (llama_vocab_wrapper*)ruby_xmalloc(sizeof(llama_vocab_wrapper));
  vocab_wrapper->vocab = (struct llama_vocab*)llama_model_get_vocab(model_wrapper->model);
  vocab_wrapper->copied = true;
  RB_GC_GUARD(model);
  return TypedData_Wrap_Struct(rb_cLlamaVocab, &llama_vocab_wrapper_data_type, vocab_wrapper);
}

.llama_model_has_decoder(model) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1761
1762
1763
1764
1765
1766
1767
1768
# File 'ext/llama_cpp/llama_cpp.c', line 1761

static VALUE rb_llama_model_has_decoder(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return llama_model_has_decoder(model_wrapper->model) ? Qtrue : Qfalse;
}

.llama_model_has_encoder?(model) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1747
1748
1749
1750
1751
1752
1753
1754
# File 'ext/llama_cpp/llama_cpp.c', line 1747

static VALUE rb_llama_model_has_encoder(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return llama_model_has_encoder(model_wrapper->model) ? Qtrue : Qfalse;
}

.llama_model_is_diffusion?(model) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1817
1818
1819
1820
1821
1822
1823
1824
# File 'ext/llama_cpp/llama_cpp.c', line 1817

static VALUE rb_llama_model_is_diffusion(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return llama_model_is_diffusion(model_wrapper->model) ? Qtrue : Qfalse;
}

.llama_model_is_hybrid?(model) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1803
1804
1805
1806
1807
1808
1809
1810
# File 'ext/llama_cpp/llama_cpp.c', line 1803

static VALUE rb_llama_model_is_hybrid(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return llama_model_is_hybrid(model_wrapper->model) ? Qtrue : Qfalse;
}

.llama_model_is_recurrent?(model) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1789
1790
1791
1792
1793
1794
1795
1796
# File 'ext/llama_cpp/llama_cpp.c', line 1789

static VALUE rb_llama_model_is_recurrent(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return llama_model_is_recurrent(model_wrapper->model) ? Qtrue : Qfalse;
}

.llama_model_load_from_file(path_model) ⇒ LlamaModel

Parameters:

Returns:



1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
# File 'ext/llama_cpp/llama_cpp.c', line 1226

static VALUE rb_llama_model_load_from_file(VALUE self, VALUE path_model, VALUE params) {
  if (!RB_TYPE_P(path_model, T_STRING)) {
    rb_raise(rb_eArgError, "path_model must be a String");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(params, rb_cLlamaModelParams)) {
    rb_raise(rb_eArgError, "params must be a LlamaModelParams");
    return Qnil;
  }
  const char* path_model_ = StringValueCStr(path_model);
  struct llama_model_params* params_ = get_llama_model_params(params);
  llama_model_wrapper* model_wrapper = (llama_model_wrapper*)ruby_xmalloc(sizeof(llama_model_wrapper));
  model_wrapper->model = llama_model_load_from_file(path_model_, *params_);
  RB_GC_GUARD(path_model);
  RB_GC_GUARD(params);
  return TypedData_Wrap_Struct(rb_cLlamaModel, &llama_model_wrapper_data_type, model_wrapper);
}

.llama_model_load_from_splits(paths, params) ⇒ LlamaModel

Parameters:

Returns:



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'ext/llama_cpp/llama_cpp.c', line 1250

static VALUE rb_llama_model_load_from_splits(VALUE self, VALUE paths, VALUE params) {
  if (!RB_TYPE_P(paths, T_ARRAY)) {
    rb_raise(rb_eArgError, "paths must be an Array");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(params, rb_cLlamaModelParams)) {
    rb_raise(rb_eArgError, "params must be a LlamaModelParams");
    return Qnil;
  }
  size_t n_paths = RARRAY_LEN(paths);
  const char** paths_ = ALLOCA_N(const char*, n_paths);
  for (size_t i = 0; i < n_paths; i++) {
    VALUE path = rb_ary_entry(paths, i);
    if (!RB_TYPE_P(path, T_STRING)) {
      rb_raise(rb_eArgError, "paths must be an Array of Strings");
      return Qnil;
    }
    paths_[i] = StringValueCStr(path);
  }
  struct llama_model_params* params_ = get_llama_model_params(params);
  llama_model_wrapper* model_wrapper = (llama_model_wrapper*)ruby_xmalloc(sizeof(llama_model_wrapper));
  model_wrapper->model = llama_model_load_from_splits(paths_, n_paths, *params_);
  RB_GC_GUARD(paths);
  RB_GC_GUARD(params);
  return TypedData_Wrap_Struct(rb_cLlamaModel, &llama_model_wrapper_data_type, model_wrapper);
}

.llama_model_n_cls_out(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1621
1622
1623
1624
1625
1626
1627
1628
# File 'ext/llama_cpp/llama_cpp.c', line 1621

static VALUE rb_llama_model_n_cls_out(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return UINT2NUM(llama_model_n_cls_out(model_wrapper->model));
}

.llama_model_n_ctx_train(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1523
1524
1525
1526
1527
1528
1529
1530
# File 'ext/llama_cpp/llama_cpp.c', line 1523

static VALUE rb_llama_model_n_ctx_train(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_ctx_train(model_wrapper->model));
}

.llama_model_n_embd(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/llama_cpp/llama_cpp.c', line 1537

static VALUE rb_llama_model_n_embd(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_embd(model_wrapper->model));
}

.llama_model_n_head(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1565
1566
1567
1568
1569
1570
1571
1572
# File 'ext/llama_cpp/llama_cpp.c', line 1565

static VALUE rb_llama_model_n_head(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_head(model_wrapper->model));
}

.llama_model_n_head_kv(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1579
1580
1581
1582
1583
1584
1585
1586
# File 'ext/llama_cpp/llama_cpp.c', line 1579

static VALUE rb_llama_model_n_head_kv(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_head_kv(model_wrapper->model));
}

.llama_model_n_layer(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1551
1552
1553
1554
1555
1556
1557
1558
# File 'ext/llama_cpp/llama_cpp.c', line 1551

static VALUE rb_llama_model_n_layer(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_layer(model_wrapper->model));
}

.llama_model_n_params(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1733
1734
1735
1736
1737
1738
1739
1740
# File 'ext/llama_cpp/llama_cpp.c', line 1733

static VALUE rb_llama_model_n_params(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return ULONG2NUM(llama_model_n_params(model_wrapper->model));
}

.llama_model_n_swa(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1593
1594
1595
1596
1597
1598
1599
1600
# File 'ext/llama_cpp/llama_cpp.c', line 1593

static VALUE rb_llama_model_n_swa(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_n_swa(model_wrapper->model));
}

.llama_model_quantize(fname_inp, fname_out, params) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'ext/llama_cpp/llama_cpp.c', line 1833

static VALUE rb_llama_model_quantize(VALUE self, VALUE fname_inp, VALUE fname_out, VALUE params) {
  if (!RB_TYPE_P(fname_inp, T_STRING)) {
    rb_raise(rb_eArgError, "fname_inp must be a String");
    return Qnil;
  }
  if (!RB_TYPE_P(fname_out, T_STRING)) {
    rb_raise(rb_eArgError, "fname_out must be a String");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(params, rb_cLlamaModelQuantizeParams)) {
    rb_raise(rb_eArgError, "params must be a LlamaModelQuantizeParams");
    return Qnil;
  }
  const char* fname_inp_ = StringValueCStr(fname_inp);
  const char* fname_out_ = StringValueCStr(fname_out);
  const llama_model_quantize_params* params_ = get_llama_model_quantize_params(params);
  const uint32_t res = llama_model_quantize(fname_inp_, fname_out_, params_);
  RB_GC_GUARD(fname_inp);
  RB_GC_GUARD(fname_out);
  RB_GC_GUARD(params);
  return res == 0 ? Qtrue : Qfalse;
}

.llama_model_rope_freq_scale_train(model) ⇒ Float

Parameters:

Returns:

  • (Float)


1607
1608
1609
1610
1611
1612
1613
1614
# File 'ext/llama_cpp/llama_cpp.c', line 1607

static VALUE rb_llama_model_rope_freq_scale_train(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return DBL2NUM(llama_model_rope_freq_scale_train(model_wrapper->model));
}

.llama_model_rope_type(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1509
1510
1511
1512
1513
1514
1515
1516
# File 'ext/llama_cpp/llama_cpp.c', line 1509

static VALUE rb_llama_model_rope_type(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return INT2NUM(llama_model_rope_type(model_wrapper->model));
}

.llama_model_save_to_file(model, path_model) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
# File 'ext/llama_cpp/llama_cpp.c', line 1283

static VALUE rb_llama_model_save_to_file(VALUE self, VALUE model, VALUE path_model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  if (!RB_TYPE_P(path_model, T_STRING)) {
    rb_raise(rb_eArgError, "path_model must be a String");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  const char* path_model_ = StringValueCStr(path_model);
  llama_model_save_to_file(model_wrapper->model, path_model_);
  RB_GC_GUARD(model);
  RB_GC_GUARD(path_model);
  return Qnil;
}

.llama_model_size(model) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1705
1706
1707
1708
1709
1710
1711
1712
# File 'ext/llama_cpp/llama_cpp.c', line 1705

static VALUE rb_llama_model_size(VALUE self, VALUE model) {
  if (!rb_obj_is_kind_of(model, rb_cLlamaModel)) {
    rb_raise(rb_eArgError, "model must be a LlamaModel");
    return Qnil;
  }
  llama_model_wrapper* model_wrapper = get_llama_model_wrapper(model);
  return ULONG2NUM(llama_model_size(model_wrapper->model));
}

.llama_n_batch(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1417
1418
1419
1420
1421
1422
1423
1424
# File 'ext/llama_cpp/llama_cpp.c', line 1417

static VALUE rb_llama_n_batch(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  return UINT2NUM(llama_n_batch(context_wrapper->context));
}

.llama_n_ctx(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1403
1404
1405
1406
1407
1408
1409
1410
# File 'ext/llama_cpp/llama_cpp.c', line 1403

static VALUE rb_llama_n_ctx(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  return UINT2NUM(llama_n_ctx(context_wrapper->context));
}

.llama_n_seq_max(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1445
1446
1447
1448
1449
1450
1451
1452
# File 'ext/llama_cpp/llama_cpp.c', line 1445

static VALUE rb_llama_n_seq_max(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  return UINT2NUM(llama_n_seq_max(context_wrapper->context));
}

.llama_n_threads(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
# File 'ext/llama_cpp/llama_cpp.c', line 2439

static VALUE rb_llama_n_threads(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const int32_t n_threads = llama_n_threads(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return INT2NUM(n_threads);
}

.llama_n_threads_batch(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
# File 'ext/llama_cpp/llama_cpp.c', line 2455

static VALUE rb_llama_n_threads_batch(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const int32_t n_threads_batch = llama_n_threads_batch(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return INT2NUM(n_threads_batch);
}

.llama_n_ubatch(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1431
1432
1433
1434
1435
1436
1437
1438
# File 'ext/llama_cpp/llama_cpp.c', line 1431

static VALUE rb_llama_n_ubatch(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  return UINT2NUM(llama_n_ubatch(context_wrapper->context));
}

.llama_numa_init(numa) ⇒ NilClass

Parameters:

  • numa (Integer)

Returns:

  • (NilClass)


1193
1194
1195
1196
1197
1198
1199
1200
# File 'ext/llama_cpp/llama_cpp.c', line 1193

static VALUE rb_llama_numa_init(VALUE self, VALUE numa) {
  if (!RB_INTEGER_TYPE_P(numa)) {
    rb_raise(rb_eArgError, "numa must be an Integer");
    return Qnil;
  }
  llama_numa_init((enum ggml_numa_strategy)NUM2INT(numa));
  return Qnil;
}

.llama_perf_context(context) ⇒ LlamaPerfContextData

Parameters:

Returns:



3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
# File 'ext/llama_cpp/llama_cpp.c', line 3837

static VALUE rb_llama_perf_context(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* ctx_wrapper = get_llama_context_wrapper(ctx);
  struct llama_perf_context_data* data = (struct llama_perf_context_data*)ruby_xmalloc(sizeof(struct llama_perf_context_data));
  *data = llama_perf_context(ctx_wrapper->context);
  RB_GC_GUARD(ctx);
  return TypedData_Wrap_Struct(rb_cLlamaPerfContextData, &llama_perf_context_data_type, data);
}

.llama_perf_context_print(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
# File 'ext/llama_cpp/llama_cpp.c', line 3854

static VALUE rb_llama_perf_context_print(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* ctx_wrapper = get_llama_context_wrapper(ctx);
  llama_perf_context_print(ctx_wrapper->context);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_perf_context_reset(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
# File 'ext/llama_cpp/llama_cpp.c', line 3886

static VALUE rb_llama_perf_context_reset(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* ctx_wrapper = get_llama_context_wrapper(ctx);
  llama_perf_context_reset(ctx_wrapper->context);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_perf_sampler(chain) ⇒ LlamaPerfSamplerData

Parameters:

Returns:



3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
# File 'ext/llama_cpp/llama_cpp.c', line 3902

static VALUE rb_llama_perf_sampler(VALUE self, VALUE chain) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  struct llama_perf_sampler_data* data = (struct llama_perf_sampler_data*)ruby_xmalloc(sizeof(struct llama_perf_sampler_data));
  *data = llama_perf_sampler(chain_);
  RB_GC_GUARD(chain);
  return TypedData_Wrap_Struct(rb_cLlamaPerfSamplerData, &llama_perf_sampler_data_type, data);
}

.llama_perf_sampler_print(chain) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
# File 'ext/llama_cpp/llama_cpp.c', line 3870

static VALUE rb_llama_perf_sampler_print(VALUE self, VALUE chain) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  llama_perf_sampler_print(chain_);
  RB_GC_GUARD(chain);
  return Qnil;
}

.llama_perf_sampler_reset(chain) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
# File 'ext/llama_cpp/llama_cpp.c', line 3919

static VALUE rb_llama_perf_sampler_reset(VALUE self, VALUE chain) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  llama_perf_sampler_reset(chain_);
  RB_GC_GUARD(chain);
  return Qnil;
}

.llama_pooling_type(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1477
1478
1479
1480
1481
1482
1483
1484
# File 'ext/llama_cpp/llama_cpp.c', line 1477

static VALUE rb_llama_pooling_type(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  return INT2NUM(llama_pooling_type(context_wrapper->context));
}

.llama_print_system_infoString

Returns:

  • (String)


3709
3710
3711
3712
# File 'ext/llama_cpp/llama_cpp.c', line 3709

static VALUE rb_llama_print_system_info(VALUE self) {
  const char* info = llama_print_system_info();
  return rb_utf8_str_new_cstr(info);
}

.llama_rm_adapter_lora(context, adapter) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
# File 'ext/llama_cpp/llama_cpp.c', line 1928

static VALUE rb_llama_rm_adapter_lora(VALUE self, VALUE ctx, VALUE adapter) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(adapter, rb_cLlamaAdapterLora)) {
    rb_raise(rb_eArgError, "adapter must be a LlamaAdapterLora");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_adapter_lora_wrapper* adapter_wrapper = get_llama_adapter_lora_wrapper(adapter);
  const int32_t res = llama_rm_adapter_lora(context_wrapper->context, adapter_wrapper->adapter);
  RB_GC_GUARD(ctx);
  RB_GC_GUARD(adapter);
  return NUM2INT(res);
}

.llama_sampler_accept(sampler, token) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
# File 'ext/llama_cpp/llama_cpp.c', line 3124

static VALUE rb_llama_sampler_accept(VALUE self, VALUE sampler, VALUE token) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  llama_token token_ = NUM2INT(token);
  llama_sampler_accept(sampler_, token_);
  RB_GC_GUARD(sampler);
  return Qnil;
}

.llama_sampler_apply(sampler, cur_p) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
# File 'ext/llama_cpp/llama_cpp.c', line 3146

static VALUE rb_llama_sampler_apply(VALUE self, VALUE sampler, VALUE cur_p) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(cur_p, rb_cLlamaTokenDataArray)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaTokenDataArray");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  llama_token_data_array* cur_p_ = get_llama_token_data_array(cur_p);
  llama_sampler_apply(sampler_, cur_p_);
  RB_GC_GUARD(sampler);
  RB_GC_GUARD(cur_p);
  return Qnil;
}

.llama_sampler_chain_add(chain, smpl) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
# File 'ext/llama_cpp/llama_cpp.c', line 3250

static VALUE rb_llama_sampler_chain_add(VALUE self, VALUE chain, VALUE smpl) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(smpl, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "smpl must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  struct llama_sampler* smpl_ = get_llama_sampler(smpl);
  llama_sampler_chain_add(chain_, smpl_);
  RB_GC_GUARD(chain);
  RB_GC_GUARD(smpl);
  return Qnil;
}

.llama_sampler_chain_get(chain, i) ⇒ LlamaSampler

Parameters:

Returns:



3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
# File 'ext/llama_cpp/llama_cpp.c', line 3273

static VALUE rb_llama_sampler_chain_get(VALUE self, VALUE chain, VALUE i) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(i)) {
    rb_raise(rb_eArgError, "i must be an Integer");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  struct llama_sampler* smpl = llama_sampler_chain_get(chain_, NUM2INT(i));
  RB_GC_GUARD(chain);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, smpl);
}

.llama_sampler_chain_init(params) ⇒ LlamaSampler

Parameters:

Returns:



3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
# File 'ext/llama_cpp/llama_cpp.c', line 3233

static VALUE rb_llama_sampler_chain_init(VALUE self, VALUE params) {
  if (!rb_obj_is_kind_of(params, rb_cLlamaSamplerChainParams)) {
    rb_raise(rb_eArgError, "params must be a LlamaSamplerChainParams");
    return Qnil;
  }
  llama_sampler_chain_params* params_ = get_llama_sampler_chain_params(params);
  struct llama_sampler* sampler = llama_sampler_chain_init(*params_);
  RB_GC_GUARD(params);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_chain_n(chain) ⇒ Integer

Parameters:

Returns:

  • (Integer)


3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
# File 'ext/llama_cpp/llama_cpp.c', line 3293

static VALUE rb_llama_sampler_chain_n(VALUE self, VALUE chain) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  const int n = llama_sampler_chain_n(chain_);
  RB_GC_GUARD(chain);
  return INT2NUM(n);
}

.llama_sampler_chain_remove(chain, i) ⇒ LlamaSampler

Parameters:

Returns:



3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
# File 'ext/llama_cpp/llama_cpp.c', line 3310

static VALUE rb_llama_sampler_chain_remove(VALUE self, VALUE chain, VALUE i) {
  if (!rb_obj_is_kind_of(chain, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "chain must be a LlamaSampler");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(i)) {
    rb_raise(rb_eArgError, "i must be an Integer");
    return Qnil;
  }
  struct llama_sampler* chain_ = get_llama_sampler(chain);
  struct llama_sampler* smpl = llama_sampler_chain_remove(chain_, NUM2INT(i));
  RB_GC_GUARD(chain);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, smpl);
}

.llama_sampler_clone(sampler) ⇒ LlamaSampler

Parameters:

Returns:



3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
# File 'ext/llama_cpp/llama_cpp.c', line 3200

static VALUE rb_llama_sampler_clone(VALUE self, VALUE sampler) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  struct llama_sampler* clone = llama_sampler_clone(sampler_);
  RB_GC_GUARD(sampler);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, clone);
}

.llama_sampler_free(sampler) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
# File 'ext/llama_cpp/llama_cpp.c', line 3216

static VALUE rb_llama_sampler_free(VALUE self, VALUE sampler) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  llama_sampler_free(sampler_);
  sampler_ = NULL;
  RB_GC_GUARD(sampler);
  return Qnil;
}

.llama_sampler_get_seed(sampler) ⇒ Integer

Parameters:

Returns:

  • (Integer)


3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
# File 'ext/llama_cpp/llama_cpp.c', line 3666

static VALUE rb_llama_sampler_get_seed(VALUE self, VALUE smpl) {
  if (!rb_obj_is_kind_of(smpl, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "smpl must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* smpl_ = get_llama_sampler(smpl);
  const uint32_t seed = llama_sampler_get_seed(smpl_);
  RB_GC_GUARD(smpl);
  return UINT2NUM(seed);
}

.llama_sampler_init_dist(seed) ⇒ LlamaSampler

Parameters:

  • seed (Integer)

Returns:



3339
3340
3341
3342
3343
3344
3345
3346
# File 'ext/llama_cpp/llama_cpp.c', line 3339

static VALUE rb_llama_sampler_init_dist(VALUE self, VALUE seed) {
  if (!RB_INTEGER_TYPE_P(seed)) {
    rb_raise(rb_eArgError, "seed must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_dist(NUM2UINT(seed));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_grammar(vocab, grammar_str, grammar_root) ⇒ LlamaSampler

Parameters:

  • vocab (LlamaVocab)
  • grammar_str (String)
  • grammar_root (String)

Returns:



3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
# File 'ext/llama_cpp/llama_cpp.c', line 3565

static VALUE rb_llama_sampler_init_grammar(VALUE self, VALUE vocab, VALUE grammar_str, VALUE grammar_root) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_TYPE_P(grammar_str, T_STRING)) {
    rb_raise(rb_eArgError, "grammar_str must be a String");
    return Qnil;
  }
  if (!RB_TYPE_P(grammar_root, T_STRING)) {
    rb_raise(rb_eArgError, "grammar_root must be a String");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const char* grammar_str_ = StringValueCStr(grammar_str);
  const char* grammar_root_ = StringValueCStr(grammar_root);
  struct llama_sampler* sampler = llama_sampler_init_grammar(vocab_wrapper->vocab, grammar_str_, grammar_root_);
  RB_GC_GUARD(vocab);
  RB_GC_GUARD(grammar_str);
  RB_GC_GUARD(grammar_root);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_greedyLlamaSampler

Returns:



3329
3330
3331
3332
# File 'ext/llama_cpp/llama_cpp.c', line 3329

static VALUE rb_llama_sampler_init_greedy(VALUE self) {
  struct llama_sampler* sampler = llama_sampler_init_greedy();
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_infill(vocab) ⇒ LlamaSampler

Parameters:

Returns:



3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
# File 'ext/llama_cpp/llama_cpp.c', line 3650

static VALUE rb_llama_sampler_init_infill(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  struct llama_sampler* sampler = llama_sampler_init_infill(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_logit_bias(n_vocab, n_logit_bias, logit_bias) ⇒ LlamaSampler

Parameters:

  • n_vocab (Integer)
  • n_logit_bias (Integer)
  • logit_bias (LlamaLogitBias)

Returns:



3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
# File 'ext/llama_cpp/llama_cpp.c', line 3624

static VALUE rb_llama_sampler_init_logit_bias(VALUE self, VALUE n_vocab, VALUE n_logit_bias, VALUE logit_bias) {
  if (!RB_INTEGER_TYPE_P(n_vocab)) {
    rb_raise(rb_eArgError, "n_vocab must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(n_logit_bias)) {
    rb_raise(rb_eArgError, "n_logit_bias must be an Integer");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(logit_bias, rb_cLlamaLogitBias)) {
    rb_raise(rb_eArgError, "logit_bias must be a LlamaLogitBias");
    return Qnil;
  }
  const int32_t n_vocab_ = NUM2INT(n_vocab);
  const int32_t n_logit_bias_ = NUM2INT(n_logit_bias);
  const llama_logit_bias* logit_bias_ = get_llama_logit_bias(logit_bias);
  struct llama_sampler* sampler = llama_sampler_init_logit_bias(n_vocab_, n_logit_bias_, logit_bias_);
  RB_GC_GUARD(logit_bias);
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_min_p(p, min_keep) ⇒ LlamaSampler

Parameters:

  • p (Float)
  • min_keep (Integer)

Returns:



3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
# File 'ext/llama_cpp/llama_cpp.c', line 3387

static VALUE rb_llama_sampler_init_min_p(VALUE self, VALUE p, VALUE min_keep) {
  if (!RB_FLOAT_TYPE_P(p)) {
    rb_raise(rb_eArgError, "p must be a Float");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(min_keep)) {
    rb_raise(rb_eArgError, "min_keep must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_min_p(NUM2DBL(p), NUM2SIZET(min_keep));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_mirostat(n_vocab, seed, tau, eta, m) ⇒ LlamaSampler

Parameters:

  • n_vocab (Integer)
  • seed (Integer)
  • tau (Float)
  • eta (Float)
  • m (Integer)

Returns:



3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
# File 'ext/llama_cpp/llama_cpp.c', line 3509

static VALUE rb_llama_sampler_init_mirostat(VALUE self, VALUE n_vocab, VALUE seed, VALUE tau, VALUE eta, VALUE m) {
  if (!RB_INTEGER_TYPE_P(n_vocab)) {
    rb_raise(rb_eArgError, "n_vocab must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seed)) {
    rb_raise(rb_eArgError, "seed must be an Integer");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(tau)) {
    rb_raise(rb_eArgError, "tau must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(eta)) {
    rb_raise(rb_eArgError, "eta must be a Float");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(m)) {
    rb_raise(rb_eArgError, "m must be an Integer ");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_mirostat(NUM2INT(n_vocab), NUM2UINT(seed), NUM2DBL(tau), NUM2DBL(eta), NUM2INT(m));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_mirostat_v2(seed, tau, eta) ⇒ LlamaSampler

Parameters:

  • seed (Integer)
  • tau (Float)
  • eta (Float)

Returns:



3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
# File 'ext/llama_cpp/llama_cpp.c', line 3541

static VALUE rb_llama_sampler_init_mirostat_v2(VALUE self, VALUE seed, VALUE tau, VALUE eta) {
  if (!RB_INTEGER_TYPE_P(seed)) {
    rb_raise(rb_eArgError, "seed must be an Integer");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(tau)) {
    rb_raise(rb_eArgError, "tau must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(eta)) {
    rb_raise(rb_eArgError, "eta must be a Float");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_mirostat_v2(NUM2UINT(seed), NUM2DBL(tau), NUM2DBL(eta));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_penalties(penalty_last_n, penalty_repeat, penalty_freq, penalty_present) ⇒ LlamaSampler

Parameters:

  • penalty_last_n (Integer)
  • penalty_repeat (Float)
  • penalty_freq (Float)
  • penalty_present (Float)

Returns:



3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
# File 'ext/llama_cpp/llama_cpp.c', line 3596

static VALUE rb_llama_sampler_init_penalties(VALUE self, VALUE penalty_last_n, VALUE penalty_repeat, VALUE penalty_freq, VALUE penalty_present) {
  if (!RB_INTEGER_TYPE_P(penalty_last_n)) {
    rb_raise(rb_eArgError, "penalty_last_n must be an Integer");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(penalty_repeat)) {
    rb_raise(rb_eArgError, "penalty_repeat must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(penalty_freq)) {
    rb_raise(rb_eArgError, "penalty_freq must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(penalty_present)) {
    rb_raise(rb_eArgError, "penalty_present must be a Float");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_penalties(NUM2INT(penalty_last_n), NUM2DBL(penalty_repeat), NUM2DBL(penalty_freq), NUM2DBL(penalty_present));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_temp(t) ⇒ LlamaSampler

Parameters:

  • t (Float)

Returns:



3424
3425
3426
3427
3428
3429
3430
3431
# File 'ext/llama_cpp/llama_cpp.c', line 3424

static VALUE rb_llama_sampler_init_temp(VALUE self, VALUE t) {
  if (!RB_FLOAT_TYPE_P(t)) {
    rb_raise(rb_eArgError, "t must be a Float");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_temp(NUM2DBL(t));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_temp_ext(t, delta, exponent) ⇒ LlamaSampler

Parameters:

  • t (Float)
  • delta (Float)
  • exponent (Float)

Returns:



3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
# File 'ext/llama_cpp/llama_cpp.c', line 3440

static VALUE rb_llama_sampler_init_temp_ext(VALUE self, VALUE t, VALUE delta, VALUE exponent) {
  if (!RB_FLOAT_TYPE_P(t)) {
    rb_raise(rb_eArgError, "t must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(delta)) {
    rb_raise(rb_eArgError, "delta must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(exponent)) {
    rb_raise(rb_eArgError, "exponent must be a Float");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_temp_ext(NUM2DBL(t), NUM2DBL(delta), NUM2DBL(exponent));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_top_k(k) ⇒ LlamaSampler

Parameters:

  • k (Integer)

Returns:



3353
3354
3355
3356
3357
3358
3359
3360
# File 'ext/llama_cpp/llama_cpp.c', line 3353

static VALUE rb_llama_sampler_init_top_k(VALUE self, VALUE k) {
  if (!RB_INTEGER_TYPE_P(k)) {
    rb_raise(rb_eArgError, "k must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_top_k(NUM2INT(k));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_top_n_sigma(n) ⇒ LlamaSampler

Parameters:

  • n (Float)

Returns:



3491
3492
3493
3494
3495
3496
3497
3498
# File 'ext/llama_cpp/llama_cpp.c', line 3491

static VALUE rb_llama_sampler_init_top_n_sigma(VALUE self, VALUE n) {
  if (!RB_FLOAT_TYPE_P(n)) {
    rb_raise(rb_eArgError, "n must be a Float");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_top_n_sigma(NUM2DBL(n));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_top_p(p, min_keep) ⇒ LlamaSampler

Parameters:

  • p (Float)
  • min_keep (Integer)

Returns:



3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
# File 'ext/llama_cpp/llama_cpp.c', line 3368

static VALUE rb_llama_sampler_init_top_p(VALUE self, VALUE p, VALUE min_keep) {
  if (!RB_FLOAT_TYPE_P(p)) {
    rb_raise(rb_eArgError, "p must be a Float");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(min_keep)) {
    rb_raise(rb_eArgError, "min_keep must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_top_p(NUM2DBL(p), NUM2SIZET(min_keep));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_typical(p, min_keep) ⇒ LlamaSampler

Parameters:

  • p (Float)
  • min_keep (Integer)

Returns:



3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
# File 'ext/llama_cpp/llama_cpp.c', line 3406

static VALUE rb_llama_sampler_init_typical(VALUE self, VALUE p, VALUE min_keep) {
  if (!RB_FLOAT_TYPE_P(p)) {
    rb_raise(rb_eArgError, "p must be a Float");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(min_keep)) {
    rb_raise(rb_eArgError, "min_keep must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_typical(NUM2DBL(p), NUM2SIZET(min_keep));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_init_xtc(p, t, min_keep, seed) ⇒ LlamaSampler

Parameters:

  • p (Float)
  • t (Float)
  • min_keep (Integer)
  • seed (Integer)

Returns:



3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
# File 'ext/llama_cpp/llama_cpp.c', line 3465

static VALUE rb_llama_sampler_init_xtc(VALUE self, VALUE p, VALUE t, VALUE min_keep, VALUE seed ) {
  if (!RB_FLOAT_TYPE_P(p)) {
    rb_raise(rb_eArgError, "p must be a Float");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(t)) {
    rb_raise(rb_eArgError, "t must be a Float");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(min_keep)) {
    rb_raise(rb_eArgError, "min_keep must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seed)) {
    rb_raise(rb_eArgError, "seed must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = llama_sampler_init_xtc(NUM2DBL(p), NUM2DBL(t), NUM2SIZET(min_keep), NUM2UINT(seed));
  return TypedData_Wrap_Struct(rb_cLlamaSampler, &llama_sampler_data_type, sampler);
}

.llama_sampler_name(sampler) ⇒ String

Parameters:

Returns:

  • (String)


3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
# File 'ext/llama_cpp/llama_cpp.c', line 3106

static VALUE rb_llama_sampler_name(VALUE self, VALUE sampler) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  const char* name = llama_sampler_name(sampler_);
  VALUE ret = rb_utf8_str_new_cstr(name);
  RB_GC_GUARD(sampler);
  return ret;
}

.llama_sampler_reset(sampler) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
# File 'ext/llama_cpp/llama_cpp.c', line 3168

static VALUE rb_llama_sampler_reset(VALUE self, VALUE sampler) {
  if (!rb_obj_is_kind_of(sampler, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "sampler must be a LlamaSampler");
    return Qnil;
  }
  struct llama_sampler* sampler_ = get_llama_sampler(sampler);
  llama_sampler_reset(sampler_);
  RB_GC_GUARD(sampler);
  return Qnil;
}

.llama_sampler_sample(sampler, context, idx) ⇒ Integer

Parameters:

Returns:

  • (Integer)


3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
# File 'ext/llama_cpp/llama_cpp.c', line 3684

static VALUE rb_llama_sampler_sample(VALUE self, VALUE smpl, VALUE ctx, VALUE idx) {
  if (!rb_obj_is_kind_of(smpl, rb_cLlamaSampler)) {
    rb_raise(rb_eArgError, "smpl must be a LlamaSampler");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(idx)) {
    rb_raise(rb_eArgError, "idx must be an Integer");
    return Qnil;
  }
  struct llama_sampler* sampler = get_llama_sampler(smpl);
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const int32_t token = llama_sampler_sample(sampler, context_wrapper->context, NUM2INT(idx));
  RB_GC_GUARD(smpl);
  RB_GC_GUARD(ctx);
  return INT2NUM(token);
}

.llama_set_adapter_lora(context, adapter, scale) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
# File 'ext/llama_cpp/llama_cpp.c', line 1901

static VALUE rb_llama_set_adapter_lora(VALUE self, VALUE ctx, VALUE adapter, VALUE scale) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!rb_obj_is_kind_of(adapter, rb_cLlamaAdapterLora)) {
    rb_raise(rb_eArgError, "adapter must be a LlamaAdapterLora");
    return Qnil;
  }
  if (!RB_FLOAT_TYPE_P(scale)) {
    rb_raise(rb_eArgError, "scale must be a Float");
    return Qnil;
  }
  llama_adapter_lora_wrapper* adapter_wrapper = get_llama_adapter_lora_wrapper(adapter);
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const int32_t res = llama_set_adapter_lora(context_wrapper->context, adapter_wrapper->adapter, (float)NUM2DBL(scale));
  RB_GC_GUARD(ctx);
  RB_GC_GUARD(adapter);
  return NUM2INT(res);
}

.llama_set_causal_attn(context, causal_attn) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
# File 'ext/llama_cpp/llama_cpp.c', line 2490

static VALUE rb_llama_set_causal_attn(VALUE self, VALUE ctx, VALUE causal_attn) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const bool causal_attn_ = RTEST(causal_attn) ? true : false;
  llama_set_causal_attn(context_wrapper->context, causal_attn_);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_set_embeddings(context, embeddings) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
# File 'ext/llama_cpp/llama_cpp.c', line 2472

static VALUE rb_llama_set_embeddings(VALUE self, VALUE ctx, VALUE embeddings) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const bool embeddings_ = RTEST(embeddings) ? true : false;
  llama_set_embeddings(context_wrapper->context, embeddings_);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_set_n_threads(context, n_threads, n_threads_batch) ⇒ NilClass

Parameters:

  • context (LlamaContext)
  • n_threads (Integer)
  • n_threads_batch (Integer)

Returns:

  • (NilClass)


2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
# File 'ext/llama_cpp/llama_cpp.c', line 2415

static VALUE rb_llama_set_n_threads(VALUE self, VALUE ctx, VALUE n_threads, VALUE n_threads_batch) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(n_threads)) {
    rb_raise(rb_eArgError, "n_threads must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(n_threads_batch)) {
    rb_raise(rb_eArgError, "n_threads_batch must be an Integer");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_set_n_threads(context_wrapper->context, NUM2INT(n_threads), NUM2INT(n_threads_batch));
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_set_warmup(context, warmup) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
# File 'ext/llama_cpp/llama_cpp.c', line 2508

static VALUE rb_llama_set_warmup(VALUE self, VALUE ctx, VALUE warmup) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const bool warmup_ = RTEST(warmup) ? true : false;
  llama_set_warmup(context_wrapper->context, warmup_);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_state_get_size(context) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
# File 'ext/llama_cpp/llama_cpp.c', line 2232

static VALUE rb_llama_state_get_size(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const size_t size = llama_state_get_size(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return SIZET2NUM(size);
}

.llama_state_seq_get_size(context, seq_id) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
# File 'ext/llama_cpp/llama_cpp.c', line 2249

static VALUE rb_llama_state_seq_get_size(VALUE self, VALUE ctx, VALUE seq_id) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const size_t size = llama_state_seq_get_size(context_wrapper->context, NUM2INT(seq_id));
  RB_GC_GUARD(ctx);
  return SIZET2NUM(size);
}

.llama_state_seq_get_size_ext(context, seq_id, flags) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
# File 'ext/llama_cpp/llama_cpp.c', line 2271

static VALUE rb_llama_state_seq_get_size_ext(VALUE self, VALUE ctx, VALUE seq_id, VALUE flags) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(seq_id)) {
    rb_raise(rb_eArgError, "seq_id must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(flags)) {
    rb_raise(rb_eArgError, "flags must be an Integer");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  const size_t size = llama_state_seq_get_size_ext(context_wrapper->context, NUM2INT(seq_id), (uint32_t)NUM2UINT(flags));
  RB_GC_GUARD(ctx);
  return SIZET2NUM(size);
}

.llama_supports_gpu_offload?Boolean

Returns:

  • (Boolean)


1386
1387
1388
# File 'ext/llama_cpp/llama_cpp.c', line 1386

static VALUE rb_llama_supports_gpu_offload(VALUE self) {
  return llama_supports_gpu_offload() ? Qtrue : Qfalse;
}

.llama_supports_mlock?Boolean

Returns:

  • (Boolean)


1378
1379
1380
# File 'ext/llama_cpp/llama_cpp.c', line 1378

static VALUE rb_llama_supports_mlock(VALUE self) {
  return llama_supports_mlock() ? Qtrue : Qfalse;
}

.llama_supports_mmap?Boolean

Returns:

  • (Boolean)


1370
1371
1372
# File 'ext/llama_cpp/llama_cpp.c', line 1370

static VALUE rb_llama_supports_mmap(VALUE self) {
  return llama_supports_mmap() ? Qtrue : Qfalse;
}

.llama_supports_rpc?Boolean

Returns:

  • (Boolean)


1394
1395
1396
# File 'ext/llama_cpp/llama_cpp.c', line 1394

static VALUE rb_llama_supports_rpc(VALUE self) {
  return llama_supports_rpc() ? Qtrue : Qfalse;
}

.llama_synchronize(context) ⇒ NilClass

Parameters:

Returns:

  • (NilClass)


2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
# File 'ext/llama_cpp/llama_cpp.c', line 2525

static VALUE rb_llama_synchronize(VALUE self, VALUE ctx) {
  if (!rb_obj_is_kind_of(ctx, rb_cLlamaContext)) {
    rb_raise(rb_eArgError, "ctx must be a LlamaContext");
    return Qnil;
  }
  llama_context_wrapper* context_wrapper = get_llama_context_wrapper(ctx);
  llama_synchronize(context_wrapper->context);
  RB_GC_GUARD(ctx);
  return Qnil;
}

.llama_time_usInteger

Returns:

  • (Integer)


1346
1347
1348
# File 'ext/llama_cpp/llama_cpp.c', line 1346

static VALUE rb_llama_time_us(VALUE self) {
  return LONG2NUM(llama_time_us());
}

.llama_token_to_piece(vocab, token, lstrip, special) ⇒ String

Parameters:

  • vocab (LlamaVocab)
  • token (Integer)
  • lstrip (Integer)
  • special (Boolean)

Returns:

  • (String)


2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
# File 'ext/llama_cpp/llama_cpp.c', line 2975

static VALUE rb_llama_token_to_piece(VALUE self, VALUE vocab, VALUE token, VALUE lstrip, VALUE special) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(lstrip)) {
    rb_raise(rb_eArgError, "lstrip must be an Integer");
    return Qnil;
  }

  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  llama_token token_ = NUM2INT(token);
  const int32_t lstrip_ = NUM2INT(lstrip);
  const bool special_ = RTEST(special) ? true : false;
  char *buf = (char*)ruby_xmalloc(sizeof(char) * 8);
  const int32_t n_tokens = llama_token_to_piece(vocab_wrapper->vocab, token_, buf, 8, lstrip_, special_);

  if (n_tokens < 0) {
    ruby_xfree(buf);
    buf = (char*)ruby_xmalloc(sizeof(char) * -n_tokens);
    const int32_t check = llama_token_to_piece(vocab_wrapper->vocab, token_, buf, -n_tokens, lstrip_, special_);
    if (check != -n_tokens) {
      ruby_xfree(buf);
      rb_raise(rb_eRuntimeError, "Failed to convert");
      return Qnil;
    }
  }

  VALUE ret = rb_utf8_str_new_cstr(buf);
  ruby_xfree(buf);
  RB_GC_GUARD(vocab);

  return ret;
}

.llama_tokenize(vocab, text, tokens, n_tokens_max, add_special, parse_special) ⇒ Integer

Parameters:

  • vocab (LlamaVocab)
  • text (String)
  • tokens (Array<Integer>)
  • n_tokens_max (Integer)
  • add_special (Boolean)
  • parse_special (Boolean)

Returns:

  • (Integer)


2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
# File 'ext/llama_cpp/llama_cpp.c', line 2907

static VALUE rb_llama_tokenize(VALUE self, VALUE vocab, VALUE text, VALUE tokens, VALUE n_tokens_max, VALUE add_special, VALUE parse_special) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_TYPE_P(text, T_STRING)) {
    rb_raise(rb_eArgError, "text must be a String");
    return Qnil;
  }
  if (!RB_TYPE_P(tokens, T_ARRAY)) {
    rb_raise(rb_eArgError, "tokens must be an Array");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(n_tokens_max)) {
    rb_raise(rb_eArgError, "n_tokens_max must be an Integer");
    return Qnil;
  }

  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const char* text_ = StringValueCStr(text);
  const int32_t text_len = (int32_t)strlen(text_);
  int32_t n_tokens_max_ = NUM2INT(n_tokens_max);
  const bool add_special_ = RTEST(add_special) ? true : false;
  const bool parse_special_ = RTEST(parse_special) ? true : false;

  if (text_len <= 0) {
    rb_raise(rb_eArgError, "text must not be empty");
    return Qnil;
  }

  llama_token* tokens_ = n_tokens_max <= 0 ? NULL : ALLOCA_N(llama_token, n_tokens_max);
  const int32_t sz_tokens = (int32_t)RARRAY_LEN(tokens);

  for (int32_t i = 0; i < n_tokens_max_; i++) {
    if (i >= sz_tokens) break;
    VALUE token = rb_ary_entry(tokens, i);
    if (!RB_INTEGER_TYPE_P(token)) {
      rb_raise(rb_eArgError, "tokens must be an Array of Integers");
      return Qnil;
    }
    tokens_[i] = NUM2INT(token);
  }

  const int32_t n_tokens = llama_tokenize(vocab_wrapper->vocab, text_, text_len, tokens_, n_tokens_max_, add_special_, parse_special_);

  if (n_tokens < 0) {
    return INT2NUM(n_tokens);
  }

  rb_ary_resize(tokens, n_tokens);
  for (int i = 0; i < n_tokens; i++) {
    rb_ary_store(tokens, i, INT2NUM(tokens_[i]));
  }

  RB_GC_GUARD(vocab);
  RB_GC_GUARD(text);

  return INT2NUM(n_tokens);
}

.llama_vocab_bos(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
# File 'ext/llama_cpp/llama_cpp.c', line 2646

static VALUE rb_llama_vocab_bos(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_bos(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_eos(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
# File 'ext/llama_cpp/llama_cpp.c', line 2662

static VALUE rb_llama_vocab_eos(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_eos(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_eot(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
# File 'ext/llama_cpp/llama_cpp.c', line 2678

static VALUE rb_llama_vocab_eot(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_eot(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_mid(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
# File 'ext/llama_cpp/llama_cpp.c', line 2838

static VALUE rb_llama_vocab_fim_mid(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_mid(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_pad(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
# File 'ext/llama_cpp/llama_cpp.c', line 2854

static VALUE rb_llama_vocab_fim_pad(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_pad(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_pre(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
# File 'ext/llama_cpp/llama_cpp.c', line 2806

static VALUE rb_llama_vocab_fim_pre(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_pre(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_rep(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
# File 'ext/llama_cpp/llama_cpp.c', line 2870

static VALUE rb_llama_vocab_fim_rep(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_rep(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_sep(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
# File 'ext/llama_cpp/llama_cpp.c', line 2886

static VALUE rb_llama_vocab_fim_sep(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_sep(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_fim_suf(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
# File 'ext/llama_cpp/llama_cpp.c', line 2822

static VALUE rb_llama_vocab_fim_suf(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_fim_suf(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_get_add_bosBoolean

Parameters:

Returns:

  • (Boolean)


2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
# File 'ext/llama_cpp/llama_cpp.c', line 2758

static VALUE rb_llama_vocab_get_add_bos(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const bool flag = llama_vocab_get_add_bos(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return flag ? Qtrue : Qfalse;
}

.llama_vocab_get_add_eos(vocab) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
# File 'ext/llama_cpp/llama_cpp.c', line 2774

static VALUE rb_llama_vocab_get_add_eos(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const bool flag = llama_vocab_get_add_eos(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return flag ? Qtrue : Qfalse;
}

.llama_vocab_get_add_sep(vocab) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
# File 'ext/llama_cpp/llama_cpp.c', line 2790

static VALUE rb_llama_vocab_get_add_sep(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const bool flag = llama_vocab_get_add_sep(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return flag ? Qtrue : Qfalse;
}

.llama_vocab_get_attr(vocab, token) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
# File 'ext/llama_cpp/llama_cpp.c', line 2584

static VALUE rb_llama_vocab_get_attr(VALUE self, VALUE vocab, VALUE token) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t attr = llama_vocab_get_attr(vocab_wrapper->vocab, NUM2INT(token));
  RB_GC_GUARD(vocab);
  return INT2NUM(attr);
}

.llama_vocab_get_score(vocab, token) ⇒ Float

Parameters:

Returns:

  • (Float)


2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
# File 'ext/llama_cpp/llama_cpp.c', line 2563

static VALUE rb_llama_vocab_get_score(VALUE self, VALUE vocab, VALUE token) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const float score = llama_vocab_get_score(vocab_wrapper->vocab, NUM2INT(token));
  RB_GC_GUARD(vocab);
  return DBL2NUM(score);
}

.llama_vocab_get_text(vocab, token) ⇒ String

Parameters:

Returns:

  • (String)


2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
# File 'ext/llama_cpp/llama_cpp.c', line 2542

static VALUE rb_llama_vocab_get_text(VALUE self, VALUE vocab, VALUE token) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const char* text = llama_vocab_get_text(vocab_wrapper->vocab, NUM2INT(token));
  RB_GC_GUARD(vocab);
  return rb_utf8_str_new_cstr(text);
}

.llama_vocab_is_control?(vocab, token) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
# File 'ext/llama_cpp/llama_cpp.c', line 2626

static VALUE rb_llama_vocab_is_control(VALUE self, VALUE vocab, VALUE token) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "model must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const bool is_control = llama_vocab_is_control(vocab_wrapper->vocab, NUM2INT(token));
  RB_GC_GUARD(vocab);
  return is_control ? Qtrue : Qfalse;
}

.llama_vocab_is_eog?(vocab, token) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
# File 'ext/llama_cpp/llama_cpp.c', line 2605

static VALUE rb_llama_vocab_is_eog(VALUE self, VALUE vocab, VALUE token) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  if (!RB_INTEGER_TYPE_P(token)) {
    rb_raise(rb_eArgError, "token must be an Integer");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const bool is_eog = llama_vocab_is_eog(vocab_wrapper->vocab, NUM2INT(token));
  RB_GC_GUARD(vocab);
  return is_eog ? Qtrue : Qfalse;
}

.llama_vocab_mask(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
# File 'ext/llama_cpp/llama_cpp.c', line 2742

static VALUE rb_llama_vocab_mask(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_mask(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_n_tokens(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
# File 'ext/llama_cpp/llama_cpp.c', line 1672

static VALUE rb_llama_vocab_n_tokens(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  VALUE n_tokens = INT2NUM(llama_vocab_n_tokens(vocab_wrapper->vocab));
  RB_GC_GUARD(vocab);
  return n_tokens;
}

.llama_vocab_nl(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
# File 'ext/llama_cpp/llama_cpp.c', line 2710

static VALUE rb_llama_vocab_nl(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_nl(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_pad(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
# File 'ext/llama_cpp/llama_cpp.c', line 2726

static VALUE rb_llama_vocab_pad(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_pad(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_sep(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
# File 'ext/llama_cpp/llama_cpp.c', line 2694

static VALUE rb_llama_vocab_sep(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  const int32_t token = llama_vocab_sep(vocab_wrapper->vocab);
  RB_GC_GUARD(vocab);
  return INT2NUM(token);
}

.llama_vocab_type(vocab) ⇒ Integer

Parameters:

Returns:

  • (Integer)


1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
# File 'ext/llama_cpp/llama_cpp.c', line 1656

static VALUE rb_llama_vocab_type(VALUE self, VALUE vocab) {
  if (!rb_obj_is_kind_of(vocab, rb_cLlamaVocab)) {
    rb_raise(rb_eArgError, "vocab must be a LlamaVocab");
    return Qnil;
  }
  llama_vocab_wrapper* vocab_wrapper = get_llama_vocab_wrapper(vocab);
  VALUE vt = INT2NUM(llama_vocab_type(vocab_wrapper->vocab));
  RB_GC_GUARD(vocab);
  return vt;
}