Home›Local LLM / Open Models›Apple IntelligenceのローカルLLMをPythonから呼び出したい

Apple IntelligenceのローカルLLMをPythonから呼び出したいThis article explains how to invoke Apple Intelligence's on-device LLM directly…

元記事を読むRead original articlezenn.dev(新しいタブで開きます)(opens in a new tab)
AI 要約AI summary

macOS上で動作するApple IntelligenceのローカルLLMをPython経由で直接呼び出す手法を解説した記事。オンデバイスAIをサードパーティアプリから活用できる点が注目される。

This article explains how to invoke Apple Intelligence's on-device LLM directly from Python, enabling developers to leverage Apple's private local model in their own applications without relying on cloud APIs.

AI 生成の要約と解説(解説は要約と収集メタデータに基づく)で、元記事全文の転載・翻訳ではありません。日本語版と英語版は言語ごとに独立して生成しています。正確性は元記事 (zenn.dev) でご確認ください。AI-generated summary and explainer (the latter based on the summaries and collected metadata), not a copy or translation of the full source article. Generated independently for each language. Verify accuracy at the original source (zenn.dev).

Swiftワーカー経由

Apple Intelligenceのローカルな言語モデルを、使い慣れたPythonから呼び出す方法を扱った記事です。利用するのはFoundation Models APIですが、公式APIはSwiftで提供されています。そのためPythonから直接呼び出すのではなく、Swiftで書いたワーカーをローカル推論サーバーとして扱う構成が採られています。

Swift Packageで .macOS(.v26) を使う場合は、swift-tools-version に 6.2 を指定する必要があります。コード例には、受け取った1行の文字列をUTF-8のデータに変換し、JSONDecoderで処理する部分も登場します。

4,096トークンの予算

SystemLanguageModelのコンテキスト上限は4,096トークンです。この上限はプロンプトだけに適用されるものではありません。instructions、会話履歴、Tool定義、構造化出力のSchema、モデルの出力まで含めた、セッション全体の予算だと説明されています。

セッションと履歴

LanguageModelSessionは単一のコンテキストを保持します。同じインスタンスを再利用すると、過去のプロンプトと応答が後続リクエストのコンテキストにも含まれます。会話履歴は上記の予算に数えられるため、セッションを使い回す際にはこの点を意識する必要があります。

SchemaとToolの役割

Foundation ModelsのSchemaは、出力されたJSONの形式を後処理で検査する仕組みではありません。生成時に、指定された構造へ出力を制約するものです。ただし、型や構造が正しいことと内容が正しいことは別だと強調されています。

Toolについては、モデルによる選択、引数の生成、Swiftコードの実行、結果のセッションへの再投入をFoundation Modelsが処理します。一方、Toolの具体的な機能やデータ取得の部分は、アプリ側で実装する必要があります。

Calling Apple's on-device LLM from Python

A Zenn article explains how to call Apple Intelligence's local LLM from Python through the Foundation Models API. It matters for anyone who wants to use the on-device model from a familiar scripting language rather than writing everything in Swift.

Apple SystemLanguageModel specifications

The article begins with the model's specification. The context limit is 4,096 tokens. According to the author, this is not just a prompt limit. It is a budget for the whole session, covering instructions, conversation history, tool definitions, the structured-output schema, and the model's own output.

A LanguageModelSession holds a single context. If the same instance is reused, earlier prompts and responses are included in the context of later requests.

The article also describes how Foundation Models treats schemas. A schema does not check the output JSON in post-processing. It constrains generation to the specified structure. The author adds a caveat: a correct type and structure does not mean the content is correct.

Tool handling

For tools, the article says Foundation Models handles the model's tool selection, argument generation, execution of Swift code, and feeding of tool results back into the session. The concrete functionality of each tool and the data retrieval behind it are implemented on the app side.

A Swift worker as local server

The official Foundation Models API is Swift. For that reason, the article does not have Python call Foundation Models directly. Instead, it treats a Swift worker as a local inference server, and Python talks to that worker. The excerpted code includes a guard that converts a line of text to UTF-8 data and a JSONDecoder call, which suggests the worker exchanges line-based JSON messages. The article does not spell out the protocol in the portion available here, so that reading is an inference from the code fragments.

On build requirements, the article notes that a Swift Package targeting .macOS(.v26) needs swift-tools-version 6.2.

In short, the Python route described here runs through a Swift intermediary, and the article's notes on the 4,096-token budget, session reuse, schema constraints, and tool responsibilities describe the limits that apply when working with the on-device model.

ポコとTECHガイドPoko & TECH Guide

ポコと当サイトのTECHガイドが、記事の要約と収集した情報から読みどころを語るAI生成の対話です。Poko and TECH Dashboard's own TECH Guide discuss the article's highlights in an AI-generated conversation based on its summary and collected source details.

  1. ポコPokoコンテキスト上限が4,096トークンって書いてあったけど、これって大きいこと?The context limit is 4,096 tokens. Is that a big number?
  2. TECHガイドTECH Guide大小の評価は書かれていないんだ。ただ、プロンプトだけでなく、指示や会話履歴、Tool定義、出力まで全部含む予算だとあるよ。The article doesn't judge whether it's big or small. But it says the budget covers the whole session: instructions, history, tool definitions, and output, not just your prompt.
  3. ポコPokoSchemaって、出力の形を決めるもの?それがあれば答えも正しくなるのかな?So a Schema sets the shape of the output? Does that mean the answers will be correct too?
  4. TECHガイドTECH Guide生成時に出力を指定の構造へ制約する仕組みだよ。ただ、形が正しいことと中身が正しいことは別だと記事も注意しているね。It constrains the output to the structure you specify while it's generated. But the article is clear: a correct structure and correct content are two different things.
  5. ポコPokoじゃあ私がPythonで使いたいとき、そのまま呼べるの?So if I want to use this from Python, can I just call it directly?
  6. TECHガイドTECH Guide公式APIはSwiftなので、直接ではなくSwiftワーカーをローカル推論サーバーにする形だよ。.macOS(.v26)ならswift-tools-version 6.2が必要とあるね。The official API is Swift, so the article treats a Swift worker as a local inference server instead. With .macOS(.v26), it also needs swift-tools-version 6.2.
記事情報・収集記録Article details & collection record
出典Source
Zenn LLMコミュニティCommunity
公開日時 (JST)Published (JST)
収集日時 (JST)Collected (JST)
配信形式Format
ブログBlog
原文言語Source language
日本語Japanese
重要度の位置づけImportance in this lane
MediumMedium priority(Local LLM / Open Models 239件中、同等以上 192件)(192 of 239 Local LLM / Open Models entries are equal or higher)
出典の平均重要度(掲載直近 30 件・最大 30 件)Source average (last 30 listed, up to 30)
2.1 / 31=情報 · 2=中 · 3=高1=Info · 2=Medium · 3=High
情報の寿命Half-life
中期 (チュートリアル)Medium-term (tutorial)

🏠Local LLM / Open Models の他の記事More from Local LLM / Open Modelsもっと見る →View more →

ページ上部へScroll to top