Platforms to show: All Mac Windows Linux Cross-Platform

/Tools/Llama/LLama Simple Chat


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 Chat

Download this example: LLama Simple Chat.zip

Project "LLama Simple Chat.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Function UnhandledException(error As RuntimeException) As Boolean If error IsA LlamaExceptionMBS Then MessageBox error.Message Return True End If If error IsA OutOfMemoryException Then MessageBox error.Message Return True End If End EventHandler
End Class
Class MainWindow Inherits DesktopWindow
Control OutputArea Inherits DesktopTextArea
ControlInstance OutputArea Inherits DesktopTextArea
End Control
Control InputArea Inherits DesktopTextArea
ControlInstance InputArea Inherits DesktopTextArea
EventHandler Sub TextChanged() AskButton.Enabled = Me.Text.Length > 0 End EventHandler
End Control
Control AskButton Inherits DesktopButton
ControlInstance AskButton Inherits DesktopButton
EventHandler Sub Pressed() OutputArea.AddText "> "+InputArea.Text OutputArea.AddText EndOfLine+EndOfLine Var m As New LlamaChatMessageMBS m.Role = "user" m.Content = InputArea.Text Messages.Add m Var ModelTemplate As String = model.ChatTemplate("") // Default template // now build the formatted chat var formatted as string = LlamaMBS.ApplyChatTemplate(ModelTemplate, Messages, true) var answer as string = context.Ask(sampler, formatted, 2000) m = New LlamaChatMessageMBS m.Role = "assistant" m.Content = answer Messages.Add m AddText answer InputArea.Text = "" End EventHandler
End Control
EventHandler Sub Opening() // path to the model gguf file. You need to adjust them! // find files here: https://github.com/ggml-org/llama.cpp/releases #If TargetMacOS Then Var modelPath As String = "/Users/cs/Temp/gemma-3-1b-it.Q8_0.gguf" If Not LlamaMBS.LoadLibrary("/opt/homebrew/lib/libllama.dylib") Then MessageBox LlamaMBS.LoadErrorMessage return end if #ElseIf TargetLinux then If Not LlamaMBS.LoadLibrary("libggml.so") Then MessageBox LlamaMBS.LoadErrorMessage Return End If If Not LlamaMBS.LoadLibrary("libllama.so") Then MessageBox LlamaMBS.LoadErrorMessage Return End If #ElseIf TargetWindows Then Var modelPath As String = "C:\Users\cs\Desktop\gemma-3-1b-it.Q8_0.gguf" // tell Windows where the DLLs are, unless you copy them to the app folder #If TargetARM And Target64Bit Then Var dir As New FolderItem("C:\Users\cs\Desktop\llama-b7993-bin-win-cpu-arm64", folderitem.PathModes.Native) #ElseIf TargetX86 And Target64Bit Then Var dir As New FolderItem("C:\Users\cs\Desktop\llama-b7993-bin-win-cpu-x64", folderitem.PathModes.Native) #EndIf Call SetCurrentWorkingDirectoryMBS(dir) // load functions If Not LlamaMBS.LoadLibrary("ggml.dll") Then MessageBox LlamaMBS.LoadErrorMessage Return End If // some functions to query backend are in separate dll If Not LlamaMBS.LoadLibrary("ggml-base.dll") Then MessageBox LlamaMBS.LoadErrorMessage Return End If // and llama DL If Not LlamaMBS.LoadLibrary("llama.dll") Then MessageBox LlamaMBS.LoadErrorMessage Return End If #EndIf // load dynamic backends LlamaMBS.BackendLoadAll #if false then Var backends() As LlamaBackendMBS = LlamaMBS.Backends For Each backend As LlamaBackendMBS In backends Var devices() As LlamaDeviceMBS = backend.Devices Var name As String = backend.Name Var features As Dictionary = backend.Features Break Next #EndIf // initialize the model Var ModelParams As New LlamaModelParametersMBS model = new LlamaModelMBS(modelPath, ModelParams) // initialize the context var contextParams as new LlamaContextParametersMBS // n_ctx is the context size contextParams.n_ctx = 20480 // n_batch is the maximum number of tokens that can be processed in a single call to llama_decode contextParams.n_batch = 20480 context = new LlamaContextMBS(model, contextParams) // initialize the sampler Var SampleParameters As New LlamaSamplerChainParametersMBS sampler = new LlamaSamplerMBS(SampleParameters) sampler.AddToChain( LlamaSamplerMBS.InitMinP(0.05, 1)) sampler.AddToChain( LlamaSamplerMBS.InitTemp(0.8) ) sampler.AddToChain( LlamaSamplerMBS.InitDist(LlamaSamplerMBS.DefaultSeed) ) // wait for input InputArea.SetFocus End EventHandler
Sub AddText(s as string) var OutputField as DesktopTextArea = self.OutputArea var pos as integer = OutputField.Text.Length OutputField.AddText s OutputField.AddText EndOfLine+EndOfLine OutputField.VerticalScrollPosition = 9999999 // process markdown ** for bold text var t as string = OutputField.Text var p as integer = t.IndexOf(pos, "**") while p >= 0 var q as integer = t.IndexOf(p+2, "**") if q > p then // remove second quote OutputField.SelectionStart = q OutputField.SelectionLength = 2 OutputField.SelectedText = "" // remove first quote OutputField.SelectionStart = p OutputField.SelectionLength = 2 OutputField.SelectedText = "" // make text between bold OutputField.SelectionStart = p OutputField.SelectionLength = q-p-2 OutputField.SelectionBold = true end if t = OutputField.Text p = t.IndexOf(q+2, "**") wend // move cursor to the end OutputField.SelectionLength = 0 OutputField.SelectionStart = t.Length End Sub
Property Sampler As LlamaSamplerMBS
Property context As LlamaContextMBS
Property messages() As LlamaChatMessageMBS
Property model As LlamaModelMBS
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 Chat.zip

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


The biggest plugin in space...