Platforms to show: All Mac Windows Linux Cross-Platform
/Tools/Llama/LLama Simple Chat Gemma 4
Last modified Tue, 13th 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 Gemma 4
Download this example: LLama Simple Chat Gemma 4.zip
Project "LLama Simple Chat Gemma 4.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Opening()
End EventHandler
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
lblWorking.Visible = True
MainWindow.Refresh(True)
// 1. Add the current user message to our history array
Var m As New LlamaChatMessageMBS
m.Role = "user"
m.Content = InputArea.Text
Messages.Add m
// --- MANUAL FORMATTING START ---
// We bypass the template engine and build the string manually for Gemma 4
Var fullPrompt As String = ""
For Each msg As LlamaChatMessageMBS In Messages
fullPrompt = fullPrompt + "<|im_start|>" + msg.Role + EndOfLine + msg.Content + "<|im_end|>" + EndOfLine
Next
// Add the final trigger to tell the AI it's its turn to speak
fullPrompt = fullPrompt + "<|im_start|>assistant" + EndOfLine
// --- MANUAL FORMATTING END ---
// 2. Ask the model for the response
// Note: We use 'fullPrompt' instead of 'formatted'
// Use a smaller max_tokens (e.g., 256) so it doesn't wander off for too long
Var answer As String = context.Ask(sampler, fullPrompt, 256)
// Immediately chop off anything after the first end tag
// This stops the 'looping' visually and keeps your chat history clean
If answer.Contains("<|im_end|>") Then
answer = answer.Left(answer.IndexOf("<|im_end|>"))
End If
// Also check for the start of a new turn
If answer.Contains("<|im_start|>") Then
answer = answer.Left(answer.IndexOf("<|im_start|>"))
End If
answer = answer.Trim
// 4. Save the assistant's response to history
m = New LlamaChatMessageMBS
m.Role = "assistant"
m.Content = answer
Messages.Add m
// 5. Update UI
AddText answer
InputArea.Text = ""
lblWorking.Visible = False
End EventHandler
End Control
Control lblWorking Inherits DesktopLabel
ControlInstance lblWorking Inherits DesktopLabel
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/google_gemma-4-E4B-it-Q4_K_M.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
// 1. Get the folder containing the executable
Var execFolder As FolderItem = App.ExecutableFile.Parent
// 2. Navigate to the parent of that folder, then find the specific file
Var modelFile As FolderItem = execFolder.Parent.Child("google_gemma-4-E4B-it-Q4_K_M.gguf")
// 3. Convert to NativePath string for use with Llama/MBS plugins
Var modelPath As String = ""
If modelFile <> Nil And modelFile.Exists Then
modelPath = modelFile.NativePath
Else
// Handle error: File not found
MessageBox("Model file not found at: " + modelFile.NativePath)
End If
// 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 FolderItem = execFolder.Parent.Child("Windows_llama-cpp")
'#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 - 20k is huge for a 4B model and slows down pre-fill
contextParams.n_ctx = 4096
// n_batch should usually be 512 for better performance on consumer hardware
contextParams.n_batch = 512
context = New LlamaContextMBS(model, contextParams)
// initialize the sampler
// 1. Setup Parameters
Var SampleParameters As New LlamaSamplerChainParametersMBS
// 2. Initialize the property
sampler = New LlamaSamplerMBS(SampleParameters)
// 3. Add the samplers that ARE supported and working
sampler.AddToChain( LlamaSamplerMBS.InitMinP(0.05, 1) )
sampler.AddToChain( LlamaSamplerMBS.InitTemp(0.8) )
sampler.AddToChain( LlamaSamplerMBS.InitDist(LlamaSamplerMBS.DefaultSeed) )
// Note: We are skipping InitStop because the MBS plugin handles the EOS (2)
// internally if the model's metadata is loaded correctly.
// 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 Gemma 4.zip
The items on this page are in the following plugins: MBS Tools Plugin.