# LLamaContext
Namespace: LLama
A llama_context, which holds all the context required to interact with a model
```csharp
public sealed class LLamaContext : System.IDisposable
```
Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [LLamaContext](./llama.llamacontext.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)
### **Params**
The model params set for this model.
```csharp
public IModelParams Params { get; set; }
```
#### Property Value
[IModelParams](./llama.abstractions.imodelparams.md)
### **NativeHandle**
The native handle, which is used to be passed to the native APIs
```csharp
public SafeLLamaContextHandle NativeHandle { get; }
```
#### Property Value
[SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)
**Remarks:**
Be careful how you use this!
### **Encoding**
The encoding set for this model to deal with text input.
```csharp
public Encoding Encoding { get; }
```
#### Property Value
[Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)
### **EmbeddingLength**
The embedding length of the model, also known as `n_embed`
```csharp
public int EmbeddingLength { get; }
```
#### Property Value
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
## Constructors
### **LLamaContext(IModelParams, ILLamaLogger)**
#### Caution
Use the LLamaWeights.CreateContext instead
---
```csharp
public LLamaContext(IModelParams params, ILLamaLogger logger)
```
#### Parameters
`params` [IModelParams](./llama.abstractions.imodelparams.md)
Model params.
`logger` [ILLamaLogger](./llama.common.illamalogger.md)
The logger.
### **LLamaContext(LLamaWeights, IModelParams, ILLamaLogger)**
Create a new LLamaContext for the given LLamaWeights
```csharp
public LLamaContext(LLamaWeights model, IModelParams params, ILLamaLogger logger)
```
#### Parameters
`model` [LLamaWeights](./llama.llamaweights.md)
`params` [IModelParams](./llama.abstractions.imodelparams.md)
`logger` [ILLamaLogger](./llama.common.illamalogger.md)
#### Exceptions
[ObjectDisposedException](https://docs.microsoft.com/en-us/dotnet/api/system.objectdisposedexception)
## Methods
### **Clone()**
Create a copy of the current state of this context
```csharp
public LLamaContext Clone()
```
#### Returns
[LLamaContext](./llama.llamacontext.md)
### **Tokenize(String, Boolean)**
Tokenize a string.
```csharp
public Int32[] Tokenize(string text, bool addBos)
```
#### Parameters
`text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
`addBos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
Whether to add a bos to the text.
#### Returns
[Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **DeTokenize(IEnumerable<Int32>)**
Detokenize the tokens to text.
```csharp
public string DeTokenize(IEnumerable tokens)
```
#### Parameters
`tokens` [IEnumerable<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)
#### Returns
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
### **SaveState(String)**
Save the state to specified path.
```csharp
public void SaveState(string filename)
```
#### Parameters
`filename` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
### **GetStateData()**
#### Caution
Use `GetState` instead, this supports larger states (over 2GB)
---
Get the state data as a byte array.
```csharp
public Byte[] GetStateData()
```
#### Returns
[Byte[]](https://docs.microsoft.com/en-us/dotnet/api/system.byte)
### **GetState()**
Get the state data as an opaque handle
```csharp
public State GetState()
```
#### Returns
[State](./llama.llamacontext.state.md)
### **LoadState(String)**
Load the state from specified path.
```csharp
public void LoadState(string filename)
```
#### Parameters
`filename` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **LoadState(Byte[])**
Load the state from memory.
```csharp
public void LoadState(Byte[] stateData)
```
#### Parameters
`stateData` [Byte[]](https://docs.microsoft.com/en-us/dotnet/api/system.byte)
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **LoadState(State)**
Load the state from memory.
```csharp
public void LoadState(State state)
```
#### Parameters
`state` [State](./llama.llamacontext.state.md)
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **Sample(LLamaTokenDataArray, Nullable`1&, Single, MirostatType, Single, Single, Int32, Single, Single, Single, SafeLLamaGrammarHandle)**
Perform the sampling. Please don't use it unless you fully know what it does.
```csharp
public int Sample(LLamaTokenDataArray candidates, Nullable`1& mirostat_mu, float temperature, MirostatType mirostat, float mirostatTau, float mirostatEta, int topK, float topP, float tfsZ, float typicalP, SafeLLamaGrammarHandle grammar)
```
#### Parameters
`candidates` [LLamaTokenDataArray](./llama.native.llamatokendataarray.md)
`mirostat_mu` [Nullable`1&](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1&)
`temperature` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`mirostat` [MirostatType](./llama.common.mirostattype.md)
`mirostatTau` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`mirostatEta` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`topK` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
`topP` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`tfsZ` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`typicalP` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`grammar` [SafeLLamaGrammarHandle](./llama.native.safellamagrammarhandle.md)
#### Returns
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
### **ApplyPenalty(IEnumerable<Int32>, Dictionary<Int32, Single>, Int32, Single, Single, Single, Boolean)**
Apply the penalty for the tokens. Please don't use it unless you fully know what it does.
```csharp
public LLamaTokenDataArray ApplyPenalty(IEnumerable lastTokens, Dictionary logitBias, int repeatLastTokensCount, float repeatPenalty, float alphaFrequency, float alphaPresence, bool penalizeNL)
```
#### Parameters
`lastTokens` [IEnumerable<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)
`logitBias` [Dictionary<Int32, Single>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2)
`repeatLastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
`repeatPenalty` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`alphaFrequency` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`alphaPresence` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)
`penalizeNL` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)
#### Returns
[LLamaTokenDataArray](./llama.native.llamatokendataarray.md)
### **Eval(Int32[], Int32)**
```csharp
public int Eval(Int32[] tokens, int pastTokensCount)
```
#### Parameters
`tokens` [Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
`pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The updated `pastTokensCount`.
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **Eval(List<Int32>, Int32)**
```csharp
public int Eval(List tokens, int pastTokensCount)
```
#### Parameters
`tokens` [List<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1)
`pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The updated `pastTokensCount`.
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **Eval(ReadOnlyMemory<Int32>, Int32)**
```csharp
public int Eval(ReadOnlyMemory tokens, int pastTokensCount)
```
#### Parameters
`tokens` [ReadOnlyMemory<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.readonlymemory-1)
`pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The updated `pastTokensCount`.
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **Eval(ReadOnlySpan<Int32>, Int32)**
```csharp
public int Eval(ReadOnlySpan tokens, int pastTokensCount)
```
#### Parameters
`tokens` [ReadOnlySpan<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)
`pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
The updated `pastTokensCount`.
#### Exceptions
[RuntimeError](./llama.exceptions.runtimeerror.md)
### **GenerateResult(IEnumerable<Int32>)**
```csharp
internal IEnumerable GenerateResult(IEnumerable ids)
```
#### Parameters
`ids` [IEnumerable<Int32>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)
#### Returns
[IEnumerable<String>](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)
### **TokenToString(Int32)**
Convert a token into a string
```csharp
public string TokenToString(int token)
```
#### Parameters
`token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)
#### Returns
[String](https://docs.microsoft.com/en-us/dotnet/api/system.string)
### **Dispose()**
```csharp
public void Dispose()
```