人工智能Swift
SwiftIntelligence
面向应用文本的设备端搜索、问答和信息脱敏,建立在 Apple 框架之上。
8星标数最新版本: v2.0.0更新时间: 2026年10月15日
支持平台:iOS 18macOS 15
许可证: MIT
一个让 iOS 或 macOS 应用中的文本可以在设备上被搜索和问答的 Swift package。文档会被加入一个 KnowledgeBase actor,它负责分块、按需脱敏、生成 embedding 并建立索引;搜索会用 reciprocal rank fusion 把 BM25 和向量排序结合起来;在 iOS 26 和 macOS 26 上,Apple 的设备端模型会根据最相关的段落给出带引用来源的回答。建立在 NaturalLanguage、Accelerate 和 Foundation Models 之上,没有依赖。
功能特性
- 混合搜索:每个段落都按词(BM25)和向量两种方式建立索引,两种排序结果用 reciprocal rank fusion 合并
- 带来源的回答:answer(_:) 要求 Apple 的设备端模型只根据最相关的段落回答并引用来源;groundedPrompt(for:) 为任何其他模型生成同样的 prompt
- 可逆的八类个人信息脱敏;卡号和 IBAN 必须通过校验位检测,匹配过程在文本的折叠副本上进行
- 三种 embedder(hashing、Apple sentence、Apple contextual),也可以通过 TextEmbedder 协议使用自己的模型
- 保存与恢复:脱敏后的段落和它们的向量保存在同一个文件中,加载时会对照 embedder 和格式版本进行校验
- 文本分析:用 TextAnalyzer 分析语言、情感、人名、关键词和句子
快速开始
swift
1let knowledge = KnowledgeBase()2try await knowledge.add("You can return any item within 30 days of delivery for a full refund.", id: "returns")3try await knowledge.add("Orders ship within two business days; delivery takes three to five days.", id: "shipping")4try await knowledge.add("Every laptop comes with a two-year warranty against defects.", id: "warranty")5 6let results = try await knowledge.search("How do I return an item for a refund?")7print(results.first?.documentID ?? "nothing found") // returns8print(results.first?.text ?? "") // You can return any item within 30 days …安装
Swift Package Manager
swift
1dependencies: [2 .package(url: "https://github.com/muhittincamdali/SwiftIntelligence.git", from: "2.0.0")3],4targets: [5 .target(name: "YourApp", dependencies: [6 .product(name: "SwiftIntelligence", package: "SwiftIntelligence")7 ])8]Xcode
text
1https://github.com/muhittincamdali/SwiftIntelligence.git截图






时间线
仓库以一个宽泛的 AI/ML 框架开放;标记了 v1.0.0 和 v1.1.0
文档、README 和 CI 相关工作;标记了 v1.2.0 到 v1.2.2
精简:移除了 release 脚本和未使用的模块
重建为 2.0.0:单一无依赖的 library target;未保留任何 1.x API