# SafeLlamaModelHandle Namespace: LLama.Native A reference to a set of llama model weights ```csharp public sealed class SafeLlamaModelHandle : SafeLLamaHandleBase, System.IDisposable ``` Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [CriticalFinalizerObject](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.constrainedexecution.criticalfinalizerobject) → [SafeHandle](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.safehandle) → [SafeLLamaHandleBase](./llama.native.safellamahandlebase.md) → [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
Implements [IDisposable](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable) ## Properties ### **VocabCount** Total number of tokens in vocabulary of this model ```csharp public int VocabCount { get; } ``` #### Property Value [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **ContextSize** Total number of tokens in the context ```csharp public int ContextSize { get; } ``` #### Property Value [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **RopeFrequency** Get the rope frequency this model was trained with ```csharp public float RopeFrequency { get; } ``` #### Property Value [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
### **EmbeddingSize** Dimension of embedding vectors ```csharp public int EmbeddingSize { get; } ``` #### Property Value [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **SizeInBytes** Get the size of this model in bytes ```csharp public ulong SizeInBytes { get; } ``` #### Property Value [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
### **ParameterCount** Get the number of parameters in this model ```csharp public ulong ParameterCount { get; } ``` #### Property Value [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
### **Description** Get a description of this model ```csharp public string Description { get; } ``` #### Property Value [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
### **MetadataCount** Get the number of metadata key/value pairs ```csharp public int MetadataCount { get; } ``` #### Property Value [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **IsInvalid** ```csharp public bool IsInvalid { get; } ``` #### Property Value [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
### **IsClosed** ```csharp public bool IsClosed { get; } ``` #### Property Value [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
## Constructors ### **SafeLlamaModelHandle()** ```csharp public SafeLlamaModelHandle() ``` ## Methods ### **ReleaseHandle()** ```csharp protected bool ReleaseHandle() ``` #### Returns [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
### **LoadFromFile(String, LLamaModelParams)** Load a model from the given file path into memory ```csharp public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams) ``` #### Parameters `modelPath` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`lparams` [LLamaModelParams](./llama.native.llamamodelparams.md)
#### Returns [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
#### Exceptions [RuntimeError](./llama.exceptions.runtimeerror.md)
### **llama_model_apply_lora_from_file(SafeLlamaModelHandle, String, Single, String, Int32)** Apply a LoRA adapter to a loaded model path_base_model is the path to a higher quality model to use as a base for the layers modified by the adapter. Can be NULL to use the current loaded model. The model needs to be reloaded before applying a new adapter, otherwise the adapter will be applied on top of the previous one ```csharp public static int llama_model_apply_lora_from_file(SafeLlamaModelHandle model_ptr, string path_lora, float scale, string path_base_model, int n_threads) ``` #### Parameters `model_ptr` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
`path_lora` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`scale` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`path_base_model` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`n_threads` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
Returns 0 on success ### **llama_model_meta_val_str(SafeLlamaModelHandle, Byte*, Byte*, Int64)** Get metadata value as a string by key name ```csharp public static int llama_model_meta_val_str(SafeLlamaModelHandle model, Byte* key, Byte* buf, long buf_size) ``` #### Parameters `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
`key` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
`buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
`buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)
#### Returns [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The length of the string on success, or -1 on failure ### **ApplyLoraFromFile(String, Single, String, Nullable<Int32>)** Apply a LoRA adapter to a loaded model ```csharp public void ApplyLoraFromFile(string lora, float scale, string modelBase, Nullable threads) ``` #### Parameters `lora` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`scale` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`modelBase` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
A path to a higher quality model to use as a base for the layers modified by the adapter. Can be NULL to use the current loaded model. `threads` [Nullable<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)
#### Exceptions [RuntimeError](./llama.exceptions.runtimeerror.md)
### **TokenToSpan(LLamaToken, Span<Byte>)** Convert a single llama token into bytes ```csharp public uint TokenToSpan(LLamaToken token, Span dest) ``` #### Parameters `token` [LLamaToken](./llama.native.llamatoken.md)
Token to decode `dest` [Span<Byte>](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)
A span to attempt to write into. If this is too small nothing will be written #### Returns [UInt32](https://docs.microsoft.com/en-us/dotnet/api/system.uint32)
The size of this token. **nothing will be written** if this is larger than `dest` ### **TokensToSpan(IReadOnlyList<LLamaToken>, Span<Char>, Encoding)** #### Caution Use a StreamingTokenDecoder instead --- Convert a sequence of tokens into characters. ```csharp internal Span TokensToSpan(IReadOnlyList tokens, Span dest, Encoding encoding) ``` #### Parameters `tokens` [IReadOnlyList<LLamaToken>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1)
`dest` [Span<Char>](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)
`encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
#### Returns [Span<Char>](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)
The section of the span which has valid data in it. If there was insufficient space in the output span this will be filled with as many characters as possible, starting from the _last_ token. ### **Tokenize(String, Boolean, Boolean, Encoding)** Convert a string of text into tokens ```csharp public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding) ``` #### Parameters `text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`add_bos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
`special` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
#### Returns [LLamaToken[]](./llama.native.llamatoken.md)
### **CreateContext(LLamaContextParams)** Create a new context for this model ```csharp public SafeLLamaContextHandle CreateContext(LLamaContextParams params) ``` #### Parameters `params` [LLamaContextParams](./llama.native.llamacontextparams.md)
#### Returns [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)
### **MetadataKeyByIndex(Int32)** Get the metadata key for the given index ```csharp public Nullable> MetadataKeyByIndex(int index) ``` #### Parameters `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The index to get #### Returns [Nullable<Memory<Byte>>](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)
The key, null if there is no such key or if the buffer was too small ### **MetadataValueByIndex(Int32)** Get the metadata value for the given index ```csharp public Nullable> MetadataValueByIndex(int index) ``` #### Parameters `index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The index to get #### Returns [Nullable<Memory<Byte>>](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1)
The value, null if there is no such value or if the buffer was too small ### **ReadMetadata()** ```csharp internal IReadOnlyDictionary ReadMetadata() ``` #### Returns [IReadOnlyDictionary<String, String>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlydictionary-2)
### **<llama_model_meta_key_by_index>g__llama_model_meta_key_by_index_native|23_0(SafeLlamaModelHandle, Int32, Byte*, Int64)** ```csharp internal static int g__llama_model_meta_key_by_index_native|23_0(SafeLlamaModelHandle model, int index, Byte* buf, long buf_size) ``` #### Parameters `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
`index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
`buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
`buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)
#### Returns [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **<llama_model_meta_val_str_by_index>g__llama_model_meta_val_str_by_index_native|24_0(SafeLlamaModelHandle, Int32, Byte*, Int64)** ```csharp internal static int g__llama_model_meta_val_str_by_index_native|24_0(SafeLlamaModelHandle model, int index, Byte* buf, long buf_size) ``` #### Parameters `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
`index` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
`buf` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
`buf_size` [Int64](https://docs.microsoft.com/en-us/dotnet/api/system.int64)
#### Returns [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)