Namespace: LLama
A llama_context, which holds all the context required to interact with a model
public sealed class LLamaContext : System.IDisposable
Inheritance Object → LLamaContext
Implements IDisposable
Total number of tokens in vocabulary of this model
public int VocabCount { get; }
Total number of tokens in the context
public int ContextSize { get; }
Dimension of embedding vectors
public int EmbeddingSize { get; }
The model params set for this model.
public IModelParams Params { get; set; }
The native handle, which is used to be passed to the native APIs
public SafeLLamaContextHandle NativeHandle { get; }
Remarks:
Be careful how you use this!
The encoding set for this model to deal with text input.
public Encoding Encoding { get; }
The embedding length of the model, also known as n_embed
public int EmbeddingLength { get; }
Use the LLamaWeights.CreateContext instead
public LLamaContext(IModelParams params, ILLamaLogger logger)
params IModelParams
Model params.
logger ILLamaLogger
The logger.
Create a new LLamaContext for the given LLamaWeights
public LLamaContext(LLamaWeights model, IModelParams params, ILLamaLogger logger)
model LLamaWeights
params IModelParams
logger ILLamaLogger
Create a copy of the current state of this context
public LLamaContext Clone()
Tokenize a string.
public Int32[] Tokenize(string text, bool addBos)
text String
addBos Boolean
Whether to add a bos to the text.
Detokenize the tokens to text.
public string DeTokenize(IEnumerable<int> tokens)
tokens IEnumerable<Int32>
Save the state to specified path.
public void SaveState(string filename)
filename String
Use GetState instead, this supports larger states (over 2GB)
Get the state data as a byte array.
public Byte[] GetStateData()
Get the state data as an opaque handle
public State GetState()
Load the state from specified path.
public void LoadState(string filename)
filename String
Load the state from memory.
public void LoadState(Byte[] stateData)
stateData Byte[]
Load the state from memory.
public void LoadState(State state)
state State
Perform the sampling. Please don't use it unless you fully know what it does.
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)
candidates LLamaTokenDataArray
mirostat_mu Nullable`1&
temperature Single
mirostat MirostatType
mirostatTau Single
mirostatEta Single
topK Int32
topP Single
tfsZ Single
typicalP Single
grammar SafeLLamaGrammarHandle
Apply the penalty for the tokens. Please don't use it unless you fully know what it does.
public LLamaTokenDataArray ApplyPenalty(IEnumerable<int> lastTokens, Dictionary<int, float> logitBias, int repeatLastTokensCount, float repeatPenalty, float alphaFrequency, float alphaPresence, bool penalizeNL)
lastTokens IEnumerable<Int32>
logitBias Dictionary<Int32, Single>
repeatLastTokensCount Int32
repeatPenalty Single
alphaFrequency Single
alphaPresence Single
penalizeNL Boolean
public int Eval(Int32[] tokens, int pastTokensCount)
tokens Int32[]
pastTokensCount Int32
Int32
The updated pastTokensCount.
public int Eval(List<int> tokens, int pastTokensCount)
tokens List<Int32>
pastTokensCount Int32
Int32
The updated pastTokensCount.
public int Eval(ReadOnlyMemory<int> tokens, int pastTokensCount)
tokens ReadOnlyMemory<Int32>
pastTokensCount Int32
Int32
The updated pastTokensCount.
public int Eval(ReadOnlySpan<int> tokens, int pastTokensCount)
tokens ReadOnlySpan<Int32>
pastTokensCount Int32
Int32
The updated pastTokensCount.
internal IEnumerable<string> GenerateResult(IEnumerable<int> ids)
Convert a token into a string
public string TokenToString(int token)
token Int32
public void Dispose()