# SafeLLamaContextHandle Namespace: LLama.Native A safe wrapper around a llama_context ```csharp public sealed class SafeLLamaContextHandle : 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) → [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.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)
### **EmbeddingSize** Dimension of embedding vectors ```csharp public int EmbeddingSize { get; } ``` #### Property Value [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **ModelHandle** Get the model which this context is using ```csharp public SafeLlamaModelHandle ModelHandle { get; } ``` #### Property Value [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
### **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 ### **SafeLLamaContextHandle(IntPtr, SafeLlamaModelHandle)** Create a new SafeLLamaContextHandle ```csharp public SafeLLamaContextHandle(IntPtr handle, SafeLlamaModelHandle model) ``` #### Parameters `handle` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)
pointer to an allocated llama_context `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
the model which this context was created from ## Methods ### **ReleaseHandle()** ```csharp protected bool ReleaseHandle() ``` #### Returns [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
### **Create(SafeLlamaModelHandle, LLamaContextParams)** Create a new llama_state for the given model ```csharp public static SafeLLamaContextHandle Create(SafeLlamaModelHandle model, LLamaContextParams lparams) ``` #### Parameters `model` [SafeLlamaModelHandle](./llama.native.safellamamodelhandle.md)
`lparams` [LLamaContextParams](./llama.native.llamacontextparams.md)
#### Returns [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)
#### Exceptions [RuntimeError](./llama.exceptions.runtimeerror.md)
### **Clone(LLamaContextParams)** Create a new llama context with a clone of the current llama context state ```csharp public SafeLLamaContextHandle Clone(LLamaContextParams lparams) ``` #### Parameters `lparams` [LLamaContextParams](./llama.native.llamacontextparams.md)
#### Returns [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)
### **Tokenize(String, Boolean, Encoding)** Convert the given text into tokens ```csharp public Int32[] Tokenize(string text, bool add_bos, Encoding encoding) ``` #### Parameters `text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
The text to tokenize `add_bos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
Whether the "BOS" token should be added `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
Encoding to use for the text #### Returns [Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Exceptions [RuntimeError](./llama.exceptions.runtimeerror.md)
### **GetLogits()** Token logits obtained from the last call to llama_eval() The logits for the last token are stored in the last row Can be mutated in order to change the probabilities of the next token.
Rows: n_tokens
Cols: n_vocab ```csharp public Span GetLogits() ``` #### Returns [Span<Single>](https://docs.microsoft.com/en-us/dotnet/api/system.span-1)
### **TokenToString(Int32, Encoding)** Convert a token into a string ```csharp public string TokenToString(int token, Encoding encoding) ``` #### Parameters `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
Token to decode into a string `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
#### Returns [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
### **TokenToString(Int32, Encoding, StringBuilder)** Append a single llama token to a string builder ```csharp public void TokenToString(int token, Encoding encoding, StringBuilder dest) ``` #### Parameters `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
Token to decode `encoding` [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
`dest` [StringBuilder](https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder)
string builder to append the result to ### **TokenToSpan(Int32, Span<Byte>)** Convert a single llama token into bytes ```csharp public int TokenToSpan(int token, Span dest) ``` #### Parameters `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
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 [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The size of this token. **nothing will be written** if this is larger than `dest` ### **Eval(ReadOnlySpan<Int32>, Int32, Int32)** Run the llama inference to obtain the logits and probabilities for the next token. ```csharp public bool Eval(ReadOnlySpan tokens, int n_past, int n_threads) ``` #### Parameters `tokens` [ReadOnlySpan<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)
The provided batch of new tokens to process `n_past` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
the number of tokens to use from previous eval calls `n_threads` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
Returns true on success ### **GetStateSize()** Get the size of the state, when saved as bytes ```csharp public ulong GetStateSize() ``` #### Returns [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
### **GetState(Byte*, UInt64)** Get the raw state of this context, encoded as bytes. Data is written into the `dest` pointer. ```csharp public ulong GetState(Byte* dest, ulong size) ``` #### Parameters `dest` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
Destination to write to `size` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
Number of bytes available to write to in dest (check required size with `GetStateSize()`) #### Returns [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
The number of bytes written to dest #### Exceptions [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)
Thrown if dest is too small ### **GetState(IntPtr, UInt64)** Get the raw state of this context, encoded as bytes. Data is written into the `dest` pointer. ```csharp public ulong GetState(IntPtr dest, ulong size) ``` #### Parameters `dest` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)
Destination to write to `size` [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
Number of bytes available to write to in dest (check required size with `GetStateSize()`) #### Returns [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
The number of bytes written to dest #### Exceptions [ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception)
Thrown if dest is too small ### **SetState(Byte*)** Set the raw state of this context ```csharp public ulong SetState(Byte* src) ``` #### Parameters `src` [Byte*](https://docs.microsoft.com/en-us/dotnet/api/system.byte*)
The pointer to read the state from #### Returns [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
Number of bytes read from the src pointer ### **SetState(IntPtr)** Set the raw state of this context ```csharp public ulong SetState(IntPtr src) ``` #### Parameters `src` [IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr)
The pointer to read the state from #### Returns [UInt64](https://docs.microsoft.com/en-us/dotnet/api/system.uint64)
Number of bytes read from the src pointer