Platforms to show: All Mac Windows Linux Cross-Platform

/Swift/Foundation Models


Required plugins for this example: MBS Swift Plugin, MBS Main Plugin

Last modified Fri, 2nd Oct 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Swift/Foundation Models

Download this example: Foundation Models.zip

Project "Foundation Models.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control InputField Inherits DesktopTextField
ControlInstance InputField Inherits DesktopTextField
EventHandler Function KeyDown(key As String) As Boolean if asc(key) = 3 or asc(key) = 13 then SendRequest return true end if End EventHandler
End Control
Control OutputField Inherits DesktopTextArea
ControlInstance OutputField Inherits DesktopTextArea
End Control
Control AskButton Inherits DesktopButton
ControlInstance AskButton Inherits DesktopButton
EventHandler Sub Pressed() SendRequest End EventHandler
End Control
EventHandler Sub Opening() if not FoundationModelsMBS.Available then MessageBox "Please run on macOS 26." quit end if SystemLanguageModels = new SystemLanguageModelsMBS if SystemLanguageModels.Available then 'MessageBox "Available" else var u as integer = SystemLanguageModels.UnavailableReason Select case u case SystemLanguageModels.UnavailableReasonAppleIntelligenceNotEnabled MessageBox "Apple Intelligence not enabled." case SystemLanguageModels.UnavailableReasonDeviceNotEligible MessageBox "Device not eligible." case SystemLanguageModels.UnavailableReasonModelNotReady MessageBox "Model not ready." else MessageBox "UnavailableReason: "+u.ToString end Select quit end if // we can check for supported languages var supportsLocaleEN as boolean = SystemLanguageModels.supportsLocale("en") var supportsLocaleDE as boolean = SystemLanguageModels.supportsLocale("de") var languages() as string = SystemLanguageModels.supportedLanguages // and let's start a session var instructions as string = "Be polite. You run within a Xojo application. " session = new LanguageModelSessionsMBS(SystemLanguageModels, instructions) End EventHandler
Function FileSave() As Boolean var t as TranscriptMBS = session.transcript var json as string = t.JSON 'var entries() as Dictionary = t.Entries 'var json as string = GenerateJSON(entries) var file as FolderItem = GetSaveFolderitem("", "transcipt.json") if file <> nil then var b as BinaryStream = BinaryStream.Create(file, true) b.Write json end if Return True End Function
Sub AddText(s as string) 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
Sub Responded(Response as ResponseMBS, error as string, tag as Variant) if Response <> nil then var content as string = Response.Content AddText "> " + content end if if error <> "" then AddText "> " + error end if dim entries() as Dictionary = Response.transcriptEntries AskButton.Enabled = true End Sub
Sub SendRequest() AskButton.Enabled = false var p as PromptMBS = PromptMBS.promptString(InputField.Text) AddText InputField.Text session.respond( p, AddressOf Responded) InputField.Text = "" End Sub
Property SystemLanguageModels As SystemLanguageModelsMBS
Property session As LanguageModelSessionsMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileSave = "Save"
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

Download this example: Foundation Models.zip

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


The biggest plugin in space...