一个与供应商无关的 Swift AI 工具包,受 Vercel AI SDK 启发,为本地和云端 AI 模型提供统一 API,支持工具调用、流式输出和 SwiftUI 集成。

Stars

38

7 天增长

暂无数据

Fork 数

2

开放 Issue

0

开源协议

MIT

最近更新

2026-07-21

AI 仓库情报摘要
FR-AI / ANALYSIS

为什么值得关注

它在 Swift 原生、Sendable 安全的 API 下,将 Apple 本地 Foundation 模型与 OpenAI/Anthropic 等云提供商无缝衔接,并直接兼容 Vercel UI 消息流协议,使现有 Vercel AI SDK 后端可直接用于 iOS 应用。

适合谁使用

  • 构建 AI 应用 iOS/macOS 开发者
  • 已使用 Vercel AI SDK 并希望获得原生 Swift 支持的团队
  • 需要离线本地模型并降级到云端的开发者
  • 探索结构化输出和智能体循环的 Swift 开发者

典型使用场景

  • 构建在本地离线模型和云端 LLM 之间无缝切换的聊天界面
  • 使用 generateObject 从自然语言中提取结构化 JSON 并自动映射到 Swift Codable 类型
  • 创建可进行多步骤推理并调用外部 API 的工具使用智能体
  • 实现支持客户端工具执行的语音到语音或实时对话会话

项目优势

  • 覆盖 20+ 个提供商(包括 Apple Foundation 模型、OpenAI、Anthropic 和本地 Ollama)的统一 API
  • 完全遵循 Swift 6 严格并发,所有公共接口为 Sendable,无 @unchecked
  • 与 Vercel AI SDK v5+ SSE 协议线级兼容,可直接复用现有后端路由
  • 拥有 376 个单元测试的广泛测试套件,包含针对参考 AI SDK 验证的线路格式黄金文件

使用前须知

  • 需要 Swift 6 和 Xcode 16+(@Observable 对话会话要求 iOS 17+/macOS 14+)
  • 当前为 v0.1 版本,部分功能可能处于实验阶段或提供商覆盖不完整
  • 因 Swift Package Manager 和 FoundationModels 依赖,仅支持 Apple 平台(iOS、macOS 等)

README 快速开始

swift-ai-sdk

Same code, on-device or cloud. swift-ai-sdk is a provider-agnostic AI toolkit for Swift, built on the Vercel AI SDK's design. You get generateText, streamText, and generateObject, tool calling with the agentic loop handled for you, a swappable transport layer that speaks Vercel's UI message stream protocol, and an @Observable chat session for SwiftUI. Swift 6 strict concurrency, Sendable throughout.

Why

On-device and cloud sit behind one API. FoundationModelsModel() is free, private, and works offline; AnthropicModel() and OpenAIModel() are a one-line swap away, and orFallback picks for you based on availability.

If you already run a Vercel AI SDK backend, your Swift app can use it as-is. HTTPChatTransport speaks the UI message stream protocol exactly (text-delta with a delta field, tool-input-delta with inputTextDelta, data: [DONE]), so the same /api/chat route that serves your React app serves your iOS app.

And it's Swift-6-native: everything is Sendable, streaming is AsyncThrowingStream, strict concurrency is on, and there's no @unchecked in the public surface.

The one abstraction

Every provider implements a single method:

protocol LanguageModel: Sendable {
    func stream(_ request: LanguageModelRequest) async throws -> AsyncThrowingStream
}

generateText, streamText, generateObject, and the chat transports are all just consumers of that stream. One code path, every backend.

Tour

Tools + the agentic loop

let weather = Tool(
    name: "weather",
    description: "Current weather for a city",
    parameters: ["type": "object", "properties": ["city": ["type": "string"]], "required": ["city"]]
) { args in
    ["tempC": 31, "city": args["city"] ?? .null]
}

let result = try await generateText(
    model: model,
    prompt: "Weather in Mumbai?",
    tools: [weather],
    stopWhen: [stepCountIs(4)],          // bound the loop (Vercel's stopWhen)
    prepareStep: { context in            // per-step overrides (model, messages, tools)
        context.stepNumber > 2 ? PrepareStepResult(model: cheaperModel) : nil
    }
)
result.text        // final answer, after the model called the tool
result.steps       // every round-trip: text, toolCalls, toolResults, usage

The loop streams too: streamText(...).fullStream yields step boundaries, tool calls, and to

项目描述

The AI SDK for your iOS and macOS Apps

相关仓库与替代方案

根据分类、Topic 和编程语言匹配的相似项目。

Blaizzy
精选
Blaizzy GitHub avatar

nativ

Nativ is a native macOS app that lets you run AI models locally on Apple silicon, offering chat, model management, performance analytics, and an OpenAI/Anthropic-compatible API server.

AI 与机器学习AI 视觉与语音
985
uzairansaruzi
精选
uzairansaruzi GitHub avatar

hermex

Hermex is a native SwiftUI iPhone app that lets you control a self-hosted Hermes AI agent directly from your phone, with no subscriptions, tracking, or third-party relay.

AI 与机器学习大语言模型
941
simonw
精选
simonw GitHub avatar

speech-analyzer-cli

A macOS command-line tool that uses Apple's on-device speech APIs to transcribe prerecorded audio into plain text, JSON, JSONL, SRT, or WebVTT.

开发者工具CLI 与终端
20