![]() |
Ai Chatbot With Apple Foundation Models, Swiftui, Swiftdata
![]() If you want to build an **AI Chatbot** using **Apple Foundation Models + SwiftUI + SwiftData**, the recommended architecture is: ```text SwiftUI (UI) │ ▼ ChatViewModel (@Observable) │ ▼ Foundation Models (LanguageModelSession) │ ▼ SwiftData (ChatMessage, Conversation) ``` ### Tech Stack * **SwiftUI** - Chat interface * **Foundation Models Framework** - Apple Intelligence on-device LLM * **SwiftData** - Store chat history * **MVVM Architecture** * **Async/Await** * **Streaming Responses** (optional) Apple's Foundation Models framework lets apps access the same on-device language model used by Apple Intelligence. It runs locally, preserving privacy and avoiding API costs, but requires supported hardware and Apple Intelligence to be enabled. ([SwiftyPlace][1]) ## Project Structure ``` AIChatBot │ ├── Models │ ├── ChatMessage.swift │ └── Conversation.swift │ ├── ViewModels │ └── ChatViewModel.swift │ ├── Views │ ├── ChatView.swift │ ├── MessageBubble.swift │ └── InputBar.swift │ ├── Services │ └── AIService.swift │ └── AIChatBotApp.swift ``` --- ## SwiftData Model ```swift import SwiftData @Model final class ChatMessage { var text: String var isUser: Bool var createdAt: Date init(text: String, isUser: Bool, createdAt: Date = .now) { self.text = text self.isUser = isUser self.createdAt = createdAt } } ``` SwiftData uses `@Model` classes and `ModelContext` to persist data automatically within your app. ([Apple Developer][2]) --- ## AI Service ```swift import FoundationModels class AIService { private let session = LanguageModelSession() func ask(_ prompt: String) async throws -> String { let response = try await session.respond(to: prompt) return response.content } } ``` --- ## ViewModel ```swift @Observable class ChatViewModel { var messages: [ChatMessage] = [] let ai = AIService() func send(text: String) async { let user = ChatMessage( text: text, isUser: true ) messages.append(user) do { let reply = try await ai.ask(text) let bot = ChatMessage( text: reply, isUser: false ) messages.append(bot) } catch { messages.append( ChatMessage( text: error.localizedDescription, isUser: false ) ) } } } ``` --- ## SwiftUI Chat Screen ```swift ScrollView { LazyVStack { ForEach(messages) { message in MessageBubble(message: message) } } } InputBar() ``` --- ## Store Messages ```swift @Environment(\.modelContext) private var context context.insert(message) try? context.save() ``` --- ## Foundation Models Features You can also implement: * ✅ Multi-turn conversation * ✅ Streaming responses * ✅ Tool Calling * ✅ Structured JSON output * ✅ Custom prompts * ✅ System instructions * ✅ Local inference * ✅ Offline chatbot The framework supports session-based conversations, guided generation, streaming output, and tool-calling for more advanced assistants. ([Conor Luddy][3]) --- ## Suggested Folder Architecture ``` AIChatBot │ ├── App │ ├── Models │ ├── Services │ AIService.swift │ ├── Database │ SwiftDataManager.swift │ ├── ViewModels │ ChatViewModel.swift │ ├── Views │ ChatView.swift │ BubbleView.swift │ InputView.swift │ ├── Components │ ├── Extensions │ └── Resources ``` ## Device Requirements Apple Foundation Models are available only on devices that support Apple Intelligence. In practice, this means recent Apple Silicon Macs and supported iPhones/iPads (for example, iPhone 15 Pro or newer with the appropriate OS version and Apple Intelligence enabled). ([Conor Luddy][3]) ## Learning Resources If your goal is to build a production-quality chatbot, I can also provide a complete **Xcode project** with: * SwiftUI chat UI (similar to ChatGPT) * Apple Foundation Models integration * SwiftData conversation history * Streaming AI responses * Markdown rendering * Dark/Light mode * Clean MVVM architecture * Ready to run in Xcode 26 on iOS 26/macOS Tahoe. Цитата:
|
| Часовой пояс GMT +3, время: 14:22. |
vBulletin® Version 3.6.8.
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Перевод: zCarot