Platforms to show: All Mac Windows Linux Cross-Platform
/Tools/Llama/LLama Simple console
Required plugins for this example: MBS Tools Plugin
Last modified Wed, 7th Apr 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Tools/Llama/LLama Simple console
Download this example: LLama Simple console.zip
Project "LLama Simple console.xojo_binary_project"
Class App Inherits ConsoleApplication
EventHandler Function Run(args() as String) As Integer
#pragma unused args
// path to the model gguf file
Var modelPath As String = "/Users/cs/Downloads/jamba-reasoning-3b-Q4_K_M.gguf"
// prompt to generate text from
Var prompt As String = "Hello my name is"
// number of tokens to predict
Var n_predict As Integer = 32
If Not LlamaMBS.LoadLibrary("/opt/homebrew/lib/libllama.dylib") Then
System.DebugLog LlamaMBS.LoadErrorMessage
Return 2
End If
// load dynamic backends
'LlamaMBS.SetLogger AddressOf Log
LlamaMBS.BackendLoadAll
// initialize the model
Var ModelParams As New LlamaModelParametersMBS
Var model As New LlamaModelMBS(modelPath, ModelParams)
Var vocab As LlamaVocabMBS = model.Vocab
// tokenize the prompt
// find the number of tokens in the prompt
Var tokens() As Int32 = vocab.Tokenize(prompt, True, True)
If tokens = Nil Then
System.DebugLog "Failed to tokenize the prompt."
Return 1
End If
Var n_prompt As Integer = tokens.Count
// initialize the context
Var contextParams As New LlamaContextParametersMBS
// n_ctx is the context size
contextParams.n_ctx = n_prompt + n_predict
// n_batch is the maximum number of tokens that can be processed in a single call to llama_decode
contextParams.n_batch = n_prompt
// enable performance counters
contextParams.no_perf = False
Var context As New LlamaContextMBS(model, contextParams)
// initialize the sampler
Var SampleParameters As New LlamaSamplerChainParametersMBS
SampleParameters.no_perf = True
Var smpl As New LlamaSamplerMBS(SampleParameters)
smpl.AddToChain( LlamaSamplerMBS.InitGreedy )
// print the prompt token-by-token
var s as string
For Each id As Int32 In tokens
Var n As String = vocab.TokenToPiece(id)
s = s + n
Next
System.DebugLog s
// prepare a batch for the prompt
Var batch As New LlamaBatchMBS(tokens)
If model.HasEncoder Then
Var e As Integer = context.Encode(batch)
If e <> 0 Then
System.DebugLog "Failed to encode"
End If
Var decoder_start_token_id As Int32 = model.DecoderStartToken
If decoder_start_token_id = vocab.TokenNull Then
decoder_start_token_id = vocab.bos
End If
batch = New LlamaBatchMBS(Array(decoder_start_token_id))
End If
// main loop
Var StartTime As Int64 = LlamaMBS.TimeUS
Var n_decode As Integer = 0
Var new_token_id As Int32
Var n_pos As Integer = 0
var answer as string
While n_pos + batch.n_tokens < n_prompt + n_predict
// evaluate the current batch with the transformer model
Var e As Integer = context.decode(batch)
If e <> 0 Then
System.DebugLog "failed to decode."
Exit
End If
n_pos = n_pos + batch.n_tokens
// sample the next token
new_token_id = smpl.Sample(context, -1)
System.DebugLog "new_token_id: "+new_token_id.ToString
// is it an end of generation?
If vocab.isEOG(new_token_id) Then
Break
Exit
End If
Var buf As String = vocab.TokenToPiece(new_token_id)
answer = answer + buf
// prepare the next batch with the sampled token
batch = New LlamaBatchMBS(Array(new_token_id))
n_decode = n_decode + 1
Wend
System.DebugLog answer
Var EndTime As Int64 = LlamaMBS.TimeUS
System.DebugLog n_decode.ToString+" tokens decoded"
Var time As Double = (EndTime - StartTime) / 1000000.0
System.DebugLog time.ToString+" seconds."
End EventHandler
Sub Log(level as integer, message as string)
#Pragma unused level
System.DebugLog message
End Sub
End Class
Sign
End Sign
Class MyLlamaContextParametersMBS Inherits LlamaContextParametersMBS
End Class
Class MyLlamaModelParametersMBS Inherits LlamaModelParametersMBS
EventHandler Function Progress(progress as Single) As Boolean
System.DebugLog CurrentMethodName+" "+progress.ToString
Return True // continue
End EventHandler
End Class
End Project
See also:
Download this example: LLama Simple console.zip
The items on this page are in the following plugins: MBS Tools Plugin.