Platforms to show: All Mac Windows Linux Cross-Platform

/Tools/Llama/LLama Simple


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

Download this example: LLama Simple.zip

Project "LLama Simple.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits DesktopWindow
Control OutputArea Inherits DesktopTextArea
ControlInstance OutputArea Inherits DesktopTextArea
End Control
EventHandler Sub Opening() // 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 MessageBox LlamaMBS.LoadErrorMessage return end if // load dynamic backends LlamaMBS.BackendLoadAll // initialize the model Var ModelParams As New LlamaModelParametersMBS var model as new LlamaModelMBS(modelPath, ModelParams) If model.Handle = 0 Then Break // failed to load? end if 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 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 For Each id As Int32 In tokens var n as string = vocab.TokenToPiece(id) OutputArea.AddText n Next OutputArea.AddText EndOfLine OutputArea.AddText EndOfLine // 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 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) OutputArea.AddText buf // prepare the next batch with the sampled token batch = new LlamaBatchMBS(array(new_token_id)) n_decode = n_decode + 1 wend OutputArea.AddText EndOfLine 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
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
End Project

See also:

Download this example: LLama Simple.zip

The items on this page are in the following plugins: MBS Tools Plugin.


The biggest plugin in space...