picoLLM Inference Engine
.NET API
API Reference for the picoLLM .NET SDK (NuGet).
namespace: Pv
PicoLLM.Create()
Factory method for picoLLM Inference Engine.
Parameters
accessKey
string : AccessKey obtained from Picovoice Console.modelPath
string : Absolute path to the file containing LLM parameters.device
string : String representation of the device (e.g., CPU or GPU) to use for inference. If set tobest
, picoLLM picks the most suitable device. If set togpu
, the engine uses the first available GPU device. To select a specific GPU device, set this argument togpu:${GPU_INDEX}
, where${GPU_INDEX}
is the index of the target GPU. If set tocpu
, the engine will run on the CPU with the default number of threads. To specify the number of threads, set this argument tocpu:${NUM_THREADS}
, where${NUM_THREADS}
is the desired number of threads. If set tonull
,best
device will be used.
Returns
PicoLLM
: An instance of picoLLM Inference Engine.
Throws
PicoLLM.GetAvailableDevices()
Lists all available devices that picoLLM can use for inference. Each entry in the list can be used as the device
argument of the .create()
factory method or the PicoLLM
constructor.
Returns
string[]
: List of all available devices that picoLLM can use for inference.
Throws
PicoLLM
Class for the picoLLM Inference Engine.
PicoLLM can be initialized either using the static constructor create()
.
Resources should be cleaned when you are done using the delete()
method.
PicoLLM.Model
Getter for model's name.
Returns
string
: Model name.
PicoLLM.ContextLength
Getter for model's context length.
Returns
int
: Context length.
PicoLLM.Version
Getter for version.
Returns
string
: Version string.
PicoLLM.MaxTopChoices
Getter for maximum number of top choices.
Returns
int
: Maximum number of top choices.
PicoLLM.Dispose()
Releases resources acquired by picoLLM.
PicoLLM.Generate()
Given a text prompt and a set of generation parameters, creates a completion text and relevant metadata.
Parameters
prompt
string : Prompt.completionTokenLimit
int? : Maximum number of tokens in the completion. If the generation process stops due to reaching this limit, the.endpoint
parameter inPicoLLMCompletion
output will bePicoLLMEndpoints.COMPLETION_TOKEN_LIMIT_REACHED
. Set tonull
to impose no limit.stopPhrases
string[] : The generation process stops when it encounters any of these phrases in the completion. The already generated completion, including the encountered stop phrase, will be returned. Theendpoint
parameter inPicoLLMCompletion
output will bePicoLLMEndpoints.STOP_PHRASE_ENCOUNTERED
. Set tonull
to turn off this feature.seed
int? : The internal random number generator uses it as its seed if set to a positive integer value. Seeding enforces deterministic outputs. Set tonull
for randomized outputs for a given prompt.presencePenalty
float : It penalizes logits already appearing in the partial completion if set to a positive value. If set to0.0
, it has no effect.frequencyPenalty
float : If set to a positive floating-point value, it penalizes logits proportional to the frequency of their appearance in the partial completion. If set to0.0
, it has no effect.temperature
float : Sampling temperature. Temperature is a non-negative floating-point value that controls the randomness of the sampler. A higher temperature smoothens the samplers' output, increasing the randomness. In contrast, a lower temperature creates a narrower distribution and reduces variability. Setting it to0
selects the maximum logit during sampling.topP
float : A positive floating-point number within 0 and 1. It restricts the sampler's choices to high-probability logits that form thetop_p
portion of the probability mass. Hence, it avoids randomly selecting unlikely logits. A value of1.
enables the sampler to pick any token with non-zero probability, turning off the feature.numTopChoices
int : If set to a positive value, picoLLM returns the list of the highest probability tokens for any generated token. Set to0
to turn off the feature. The maximum number of top choices is.max_top_choices
.streamCallback
Action<string> : If not set tonull
, picoLLM executes this callback every time a new piece of completion string becomes available.
Returns
PicoLLMCompletion
: Completion result.
Throws
PicoLLM.Interrupt()
Interrupts .Generate()
if generation is in progress. Otherwise, it has no effect.
Throws
PicoLLM.Tokenize()
Tokenizes a given text using the model's tokenizer. This is a low-level function meant for benchmarking and advanced usage. .Generate()
should be used when possible.
Parameters
text
string : Text.bos
bool : If set totrue
, the tokenizer prepends the beginning of the sentence token to the result.eos
bool : If set totrue
, the tokenizer appends the end of the sentence token to the result.
Returns
int[]
: Tokens representing the input text.
Throws
PicoLLM.Forward()
Performs a single forward pass given a token and returns the logits. This is a low-level function for benchmarking and advanced usage. .Generate()
should be used when possible.
Parameters
token
int : The input token for the forward pass.
Returns
float[]
: The logits resulting from the forward pass.
Throws
PicoLLM.Reset()
Resets the internal state of LLM. It should be called in conjunction with .Forward()
when processing a new sequence of tokens. This is a low-level function for benchmarking and advanced usage. .Generate()
should be used when possible.
Throws
PicoLLM.GetDialog()
Return the PicoLLMDialog
object corresponding to the loaded model. The model needs to be instruction-tuned and have a specific chat template.
Parameters
mode
string : Some models (e.g.,phi-2
) define multiple chat template modes. For example,phi-2
allows bothqa
andchat
templates.history
int? : History refers to the number of latest back-and-forths to include in the prompt. Setting history tonull
will embed the entire dialog in the prompt.system
string : System instruction to embed in the prompt for configuring the model's responses.
Returns
PicoLLMDialog
: Constructed dialog object.
Throws
PicoLLMException
Error thrown if an error occurs within picoLLM
Inference Engine.
Exceptions
PicoLLMUsage
Usage information.
PromptTokens
int : Number of tokens in the prompt.CompletionTokens
int : Number of tokens in the completion.
PicoLLMEndpoint
Reasons for ending the generation process.
END_OF_SENTENCE
: 0COMPLETION_TOKEN_LIMIT_REACHED
: 1STOP_PHRASE_ENCOUNTERED
: 2INTERRUPTED
: 3
PicoLLMToken
Generated token and its log probability.
Token
string : Token.LogProb
float : Log probability.
PicoLLMCompletionToken
Generated token within completion and top alternative tokens.
Token
PicoLLMToken : Token.TopChoices
PicoLLMToken[] : Top choices.
PicoLLMCompletion
LLM completion result.
Usage
PicoLLMUsage : Usage information.Endpoint
PicoLLMEndpoint : Reason for ending the generation process.CompletionTokens
PicoLLMCompletionToken[] : Generated tokens within completion and top alternative tokens.Completion
string : Completion string.
PicoLLMDialog
Dialog is a helper class that stores a chat dialog and formats it according to an instruction-tuned LLM's chat template. Dialog is the base class. Each supported instruction-tuned LLM has an accompanying concrete subclass.
PicoLLMDialog.PicoLLMDialog()
Constructor.
Parameters
history
int? : History refers to the number of latest back-and-forths to include in the prompt. Setting history tonull
will embed the entire dialog in the prompt.system
string : System instruction to embed in the prompt for configuring the model's responses.
Throws
PicoLLMDialog.AddHumanRequest()
Adds a human's request to the dialog.
Parameters
content
string : Human's request.
Throws
PicoLLMDialog.AddLLMResponse()
Adds LLM's response to the dialog.
Parameters
content
string : LLM's response.
Throws
GemmaChatDialog
Dialog helper for gemma-2b-it
and gemma-7b-it
.
GemmaChatDialog.Prompt()
Creates a prompt string for gemma-2b-it
and gemma-7b-it
models.
Returns
string
: Formatted prompt.
Throws
Llama2ChatDialog
Dialog helper for llama-2-7b-chat
, llama-2-13b-chat
, and llama-2-70b-chat
.
Llama2ChatDialog.Prompt()
Creates a prompt string for llama-2-7b-chat
, llama-2-13b-chat
, and llama-2-70b-chat
models.
Returns
string
: Formatted prompt.
Throws
Llama3ChatDialog
Dialog helper for llama-3-8b-instruct
and llama-3-70b-instruct
.
Llama3ChatDialog.Prompt()
Creates a prompt string for llama-3-8b-instruct
and llama-3-70b-instruct
models.
Returns
string
: Formatted prompt.
Throws
Llama32ChatDialog
Dialog helper for llama-3.2-1b-instruct
and llama-3.2-3b-instruct
.
Llama32ChatDialog.Prompt()
Creates a prompt string for llama-3.2-1b-instruct
and llama-3.2-3b-instruct
models.
Returns
string
: Formatted prompt.
Throws
MistralChatDialog
Dialog helper for mistral-7b-instruct-v0.1
and mistral-7b-instruct-v0.2
.
MistralChatDialog.Prompt()
Creates a prompt string for mistral-7b-instruct-v0.1
and mistral-7b-instruct-v0.2
models.
Returns
string
: Formatted prompt.
Throws
MixtralChatDialog
Dialog helper for mixtral-8x7b-instruct-v0.1
. This class inherits methods from MistralChatDialog
.
Phi2Dialog
Dialog helper for phi-2
. This is a base class, use one of the mode-specific subclasses.
Phi2Dialog.Phi2Dialog()
Constructor for the Phi2Dialog
class.
Parameters
humanRequestsTag
string : Tag for human input.llmResponsesTag
string : Tag for LLM input.history
int? : History refers to the number of latest back-and-forths to include in the prompt. Setting history tonull
will embed the entire dialog in the prompt.system
string : System instruction to embed in the prompt for configuring the model's responses.
Phi2Dialog.Prompt()
Creates a prompt string for phi-2
model.
Returns
string
: Formatted prompt.
Throws
Phi2QADialog
Dialog helper for phi-2
in qa
mode. This class inherits methods from Phi2Dialog
.
Phi2ChatDialog
Dialog helper for phi-2
in chat
mode. This class inherits methods from Phi2Dialog
.
Phi3Dialog
Dialog helper for phi3
.
Phi35Dialog
Dialog helper for phi3.5
.