Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Summary of "Meet the Foundation Models framework"

Avatar for p0dee p0dee
June 26, 2025
330

Summary of "Meet the Foundation Models framework"

Avatar for p0dee

p0dee

June 26, 2025
Tweet

Transcript

  1. #Playground { let session = LanguageModelSession() let response = try

    await session.respond(to: ɹɹɹɹɹɹ“WWDCͷ๲େͳηογϣϯϏσΦΛޮ཰Α͘ফԽ͢ΔΞΠσΞΛ͍ͩ͘͞ʂ" ) } Foundation Models framework 🤖💭 “ޮ཰Α͘WWDCͷηογϣϯϏσΦΛফԽ͢Δํ๏͸͍͔ͭ͋͘Γ·͢ɻ·ͣɺڵ ຯͷ͋ΔτϐοΫΛࣄલʹௐ΂ͯɺಈըͷλΠτϧ΍಺༰ΛνΣοΫ͢Δͱྑ͍Ͱ ͠ΐ͏ɻ࣍ʹɺಈըΛ1ͭͷηογϣϯͰ·ͱΊΔಈը΍ཁ໿ಈըΛ୳͢ͷ΋༗ޮͰ ͢ɻ·ͨɺཁ఺Λ·ͱΊࠐΜͩεϥΠυ΍هࣄΛಡΉͱɺཧղ͕ਂ·Γ΍͍͢Ͱ͢ɻ ࠷ޙʹɺࣗݾ൓ল͠ͳ͕Βɺ࣍ͷΞοϓσʔτʹ޲͚ͯ४උΛ੔͑ͯΈ·͠ΐ͏ɻ”
  2. @Generable(description: "ΦϒδΣΫτͷઆ໌Ͱ͢") struct ObjectDescription { @Guide(description: “આ໌จͷ഑ྻ", .maximumCount(3)) var descriptions:

    [String] } —- let session = LanguageModelSession( instructions: “͋ͳͨ͸ɺ༩͑ΒΕͨΦϒδΣΫτͷ໊લΛ΋ͱʹɺ ͦͷΦϒδΣΫτͷҙ֎ͳ౾஌ࣝΛڭ͑ͯ͘ΕΔത࢜Ͱ͢ɻ” ) ΦϒδΣΫτͷ໊લ͔Βɺ؆୯ͳઆ໌จΛ࠷େ̏ͭग़ྗ͍ͤͨ͞
  3. @Generable(description: "ΦϒδΣΫτͷઆ໌Ͱ͢") struct ObjectDescription { @Guide(description: “આ໌จͷ഑ྻ”, .maximumCount(3)) var descriptions:

    [String] } —- let session = LanguageModelSession( instructions: “͋ͳͨ͸ɺ༩͑ΒΕͨΦϒδΣΫτͷ໊લΛ΋ͱʹɺ ɹɹɹɹɹɹɹͦͷΦϒδΣΫτͷҙ֎ͳ౾஌ࣝΛڭ͑ͯ͘ΕΔത࢜Ͱ͢ɻ” )
  4. Snapshot streaming { greenting _to : _Cup τʔΫϯ : …

    let stream = session.streamResponse( to: text, generating: Message.self) for try await partial in stream { print(partial) // Snapshot streaming Message.PartiallyGenerated(greeting: nil) Message.PartiallyGenerated(greeting: "") Message.PartiallyGenerated(greeting: "Welcome") Message.PartiallyGenerated(greeting: "Welcome to") Message.PartiallyGenerated(greeting: "Welcome to Cup") }
  5. // Expanded @Generable Macro nonisolated struct PartiallyGenerated: Identifiable, ConvertibleFromGeneratedContent {

    var id: GenerationID var descriptions: [String].PartiallyGenerated? nonisolated init(_ content: FoundationModels.GeneratedContent) throws { self.id = content.id ?? GenerationID() self.descriptions = try content.value(forProperty: "descriptions") } } @Generable ϚΫϩʹΑΓࣗಈੜ੒͞ΕΔ PartiallyGenerated
  6. import FoundationModels struct GetWeatherTool: Tool { let name = "getWeather"

    let description = "Retrieve the latest weather information for a city" @Generable struct Arguments { @Guide(description: "The city to fetch the weather for") var city: String } func call(arguments: Arguments) async throws -> ToolOutput { let places = try await CLGeocoder().geocodeAddressString(arguments.city) let weather = try await WeatherService.shared.weather(for: places.first!.location!) let temperature = weather.currentWeather.temperature.value let content = GeneratedContent(properties: ["temperature": temperature]) let output = ToolOutput(content) return output } } … let session = LanguageModelSession( tools: [GetWeatherTool()], instructions: "Help the user with weather forecasts." )
  7. Stateful session • LanguageModelSession ͸ঢ়ଶΛอ࣋ • Ϟσϧͱͷର࿩͸ίϯςΩετʹอ࣋͞ΕΔ • ηογϣϯ಺ͷաڈͷ΍ΓͱΓΛࢀরɺཧղͰ͖Δ •

    ͨͩ͠ɺίϯςΫετͷ্ݶ͸ 4096 τʔΫϯ • ௒ա࣌͸৽͍͠ηογϣϯΛ࡞੒͠Ҿ͖ܧ͙ඞཁ͕͋Δʢʣ • session.transcript Ͱաڈͷ΍ΓऔΓΛग़ྗՄೳ
  8. ؔ࿈ηογϣϯ - Deep dive into the Foundation Models framework -

    Code-along: Bring on-device AI to your app using the Foundation Models framework - Explore prompt design & safety for on-device foundation models - Explore large language models on Apple silicon with MLX
 ↑ Foundation Models Ͱ͸ͳ͘ɺΦϯσόΠεLLM ͰࣗલσʔλΛ௥Ճֶश͍ͤͨ͞έʔε