AI Apple Foundation Models in Livecode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
matgarage
Posts: 79
Joined: Sat Apr 20, 2013 11:39 am

AI Apple Foundation Models in Livecode

Post by matgarage »

(Post Deleted)

Sorry, I had to delete this post because, after a few successful attempts, I’m now getting error codes and can’t figure out why.
I’ll finalise this approach before sharing it with the community.
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10416
Joined: Fri Feb 19, 2010 10:17 am

Re: AI Apple Foundation Models in Livecode

Post by richmond62 »

Indeed: but your title is interesting, so it would be wonderful if you at least describe what you are trying to do.
matgarage
Posts: 79
Joined: Sat Apr 20, 2013 11:39 am

Re: AI Apple Foundation Models in Livecode

Post by matgarage »

My goal is to access the text-processing capabilities of the on-device AI in recent Macs, outside of Apple’s intended framework.
Having an on-device AI is quite useful for processing data that you don’t want to share.
Please note that this solution requires macOS Tahoe and Xcode 26

So my approach is simple.
Apple Foundation Models is not designed to interact like an API and requires Swift for ‘LanguageModelSession’.
See: https://developer.apple.com/documentati ... ionmodels/

We therefore need a framework to act as a bridge between LiveCode and the model.
A specific query on Codex generated the necessary Swift code for me:

Code: Select all

import Foundation
import FoundationModels

let prompt = CommandLine.arguments.dropFirst().joined(separator: " ")

guard !prompt.isEmpty else {
    fputs("Erreur: prompt vide\n", stderr)
    exit(1)
}

Task {
    let model = SystemLanguageModel.default

    switch model.availability {
    case .available:
        break
    default:
        fputs("Erreur: Foundation Models indisponible sur cette machine\n", stderr)
        exit(2)
    }

    do {
        let session = LanguageModelSession(
            instructions: "Tu réponds en français, clairement et brièvement."
        )

        let response = try await session.respond(to: prompt)
        print(response.content)
        exit(0)

    } catch {
        fputs("Erreur génération: \(error)\n", stderr)
        exit(3)
    }
}

dispatchMain()
You’ll notice I’m French. For another language, you’ll need to translate some of the messages in the code.
I used Visual Studio Code to save a "fm_generate.swift" file with this code.

Next, you need to compile this code with Swift using this command line in Terminal:

Code: Select all

swiftc fm_generate.swift -o fm_generate
Don’t forget to replace ‘fm_generate.swift’ with the full path of the file you’ve just generated.

This framework must be added to the app’s resources.

This code must be integrated into the stack or card:

Code: Select all

command generateTextWithAppleFoundationModels pPrompt
   put specialFolderPath("resources") & "/fm_generate" into tToolPath

   if there is not a file tToolPath then
      answer error "Le helper Swift fm_generate est introuvable."
      exit generateTextWithAppleFoundationModels
   end if

   put quote & tToolPath & quote && shellEscape(pPrompt) into tCommand
   put shell(tCommand) into tResult

   if the result is not empty then
      answer error "Erreur Foundation Models:" & return & the result
      exit generateTextWithAppleFoundationModels
   end if

   put tResult into field "GeneratedText"
end generateTextWithAppleFoundationModels


function shellEscape pText
   replace "'" with "'\''" in pText
   return "'" & pText & "'"
end shellEscape
And this one into the trigger button:

Code: Select all

on mouseUp
   generateTextWithAppleFoundationModels field "Prompt"
end mouseUp
A ‘Prompt’ field and a ‘GeneratedText’ field, and off we go... to the limits.

Everyone can test it for their own use and discover the limits of Apple’s 3-billion-parameter model, whereas the old ChatGPT 3 has 175 billion...
Post Reply