Skip to content

frr149/libfoundationmodels

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libfoundationmodels

C library that wraps Apple's on-device LLM (Apple Intelligence) for use from any programming language.

Use Apple's ~3B parameter model from Python, Rust, Go, Ruby, C, C++ — anything with a C FFI. No API key, no network, no cost. Runs entirely on the Neural Engine.

Quick start

# Build the dynamic library
make build

# Run the C example
make examples

# Run from Python
python3 examples/classify.py

API

Four functions. That's it.

#include "foundationmodels.h"

// Check availability and initialize
int32_t fm_init(void);           // returns 0 on success
int32_t fm_is_available(void);   // returns 1 if ready

// Generate free-form text
int32_t fm_generate(system, prompt, output, output_len);

// Force the model to pick from a list of choices
int32_t fm_classify(system, prompt, choices, output, output_len);

// Generate JSON
int32_t fm_generate_json(system, prompt, output, output_len);

All functions are synchronous (blocking). Typical latency: 200-800ms.

Error codes

Code Meaning
>= 0 Success (bytes written)
-1 Apple Intelligence not available
-2 Generation failed
-3 Output truncated (buffer too small)

Python example

import ctypes

fm = ctypes.CDLL(".build/release/libfoundationmodels.dylib")
fm.fm_init()

buf = ctypes.create_string_buffer(256)
fm.fm_classify(
    None,
    b"fix: handle nil response in OAuth refresh",
    b"fix\nfeat\nrefactor\ntest\ndocs\nchore",
    buf, 256
)
print(buf.value.decode())  # "fix"

C example

#include "foundationmodels.h"

char buf[256];
fm_init();
fm_classify(NULL, "Is this positive?", "positive\nnegative\nneutral", buf, 256);
printf("%s\n", buf);

How it works

The library compiles Apple's FoundationModels framework (macOS 26+) into a .dylib that exports plain C functions via @_cdecl. Async Swift calls are bridged to synchronous C calls using a semaphore.

Your code (any language)
    ↓ C FFI
libfoundationmodels.dylib
    ↓ Swift @_cdecl bridge
FoundationModels.framework
    ↓
Apple Neural Engine (on-device, ~3B params)

Requirements

  • Apple Silicon (M1+)
  • macOS 26 (Tahoe) or later
  • Apple Intelligence enabled
  • Xcode Command Line Tools with macOS 26 SDK

License

MIT

About

C library wrapping Apple Intelligence (FoundationModels) for use from any language. Python, Rust, Go, Ruby — anything with C FFI. No API key, no cloud, no cost.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors