Guest User

Untitled

a guest
May 11th, 2026
397
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 140.55 KB | Source Code | 0 0
  1. import BackgroundAssets
  2. import CoreGraphics
  3. import Foundation
  4. import Observation
  5. /// A type that can be initialized from generated content.
  6. @available(iOS 26.0, macOS 26.0, *)
  7. @available(tvOS, unavailable)
  8. @available(watchOS, unavailable)
  9. public protocol ConvertibleFromGeneratedContent : SendableMetatype {
  10. /// init(
  11. Creates an instance with the content.
  12. content: GeneratedContent) throws
  13. _
  14. }
  15. /// A type that can be converted to generated content.
  16. @available(iOS 26.0, macOS 26.0, *)
  17. @available(tvOS, unavailable)
  18. @available(watchOS, unavailable)
  19. public protocol ConvertibleToGeneratedContent : InstructionsRepresentable,
  20. PromptRepresentable {
  21. /// An instance that represents the generated content.
  22. var generatedContent: GeneratedContent { get }
  23. }
  24. @available(iOS 26.0, macOS 26.0, *)
  25. @available(tvOS, unavailable)
  26. @available(watchOS, unavailable)
  27. extension ConvertibleToGeneratedContent {
  28. /// An instance that represents the instructions.
  29. public var instructionsRepresentation: Instructions { get }
  30. /// An instance that represents a prompt.
  31. public var promptRepresentation: Prompt { get }
  32. }
  33. /// The dynamic counterpart to the generation schema type that you use to construct schemas at
  34. runtime.
  35. ///
  36. /// An individual schema may reference other schemas by
  37. /// name, and references are resolved when converting a set of
  38. /// dynamic schemas into a ``GenerationSchema``
  39. .
  40. @available(iOS 26.0, macOS 26.0, *)
  41. @available(tvOS, unavailable)
  42. @available(watchOS, unavailable)
  43. public struct DynamicGenerationSchema : Sendable {
  44. /// Creates an object schema.
  45. ///
  46. /// - Parameters:
  47. /// - name: A name this dynamic schema can be referenced by.
  48. /// - description: A natural language description of this schema.
  49. /// - properties: The properties to associated with this schema.
  50. public init(name: String, description: String? = nil, properties:
  51. [DynamicGenerationSchema.Property])
  52. /// Creates an any-of schema.
  53. ///
  54. /// - Parameters:
  55. /// - name: A name this schema can be referenecd by.
  56. /// - description: A natural language description of this
  57. ``DynamicGenerationSchema``
  58. .
  59. /// - choices: An array of schemas this one will be a union of.
  60. public init(name: String, description: String? = nil, anyOf choices:
  61. [DynamicGenerationSchema])
  62. /// Creates an enum schema.
  63. ///
  64. /// - Parameters:
  65. /// - name: A name this schema can be referenced by.
  66. /// - description: A natural language description of this
  67. ``DynamicGenerationSchema``
  68. .
  69. /// - choices: An array of schemas this one will be a union of.
  70. public init(name: String, description: String? = nil, anyOf choices:
  71. [String])
  72. /// Creates an array schema.
  73. ///
  74. /// - Parameters:
  75. /// - arrayOf: A schema to use as the elements of the array.
  76. public init(arrayOf itemSchema: DynamicGenerationSchema,
  77. minimumElements: Int? = nil, maximumElements: Int? = nil)
  78. /// Creates a schema from a generable type and guides.
  79. ///
  80. /// - Parameters:
  81. /// - type: A `Generable` type
  82. /// - guides: Generation guides to apply to this `DynamicGenerationSchema`
  83. .
  84. public init<Value>(type: Value.Type, guides: [GenerationGuide<Value>] =
  85. []) where Value : Generable
  86. /// Creates an refrence schema.
  87. ///
  88. /// - Parameters:
  89. /// - name: The name of the ``DynamicGenerationSchema`` this is a reference to.
  90. public init(referenceTo name: String)
  91. /// A property that belongs to a dynamic generation schema.
  92. ///
  93. /// Fields are named members of object types. Fields are strongly
  94. /// typed and have optional descriptions.
  95. @available(iOS 26.0, macOS 26.0, *)
  96. @available(tvOS, unavailable)
  97. @available(watchOS, unavailable)
  98. public struct Property {
  99. /// ///
  100. Creates a property referencing a dynamic schema.
  101. /// - Parameters:
  102. /// - name: A name for this property.
  103. /// - description: An optional natural language description of this
  104. /// property's contents.
  105. /// - schema: A schema representing the type this property contains.
  106. /// - isOptional: Determines if this property is required or not.
  107. public init(name: String, description: String? = nil, schema:
  108. DynamicGenerationSchema, isOptional: Bool = false)
  109. }
  110. }
  111. /// ///
  112. /// /// A type that the model uses when responding to prompts.
  113. Annotate your Swift structure or enumeration with the `@Generable` macro to allow the model to
  114. respond to prompts by generating an instance of your type. Use the `@Guide` macro to provide
  115. natural
  116. /// language descriptions of your properties, and programmatically control the values that the model can
  117. /// generate.
  118. ///
  119. /// ```swift
  120. /// @Generable
  121. /// struct SearchSuggestions {
  122. /// @Guide(description: "A list of suggested search terms", .count(4))
  123. /// var searchTerms: [SearchTerm]
  124. ///
  125. /// @Generable
  126. /// struct SearchTerm {
  127. /// // Use a generation identifier for data structures the
  128. framework generates.
  129. /// var id: GenerationID
  130. ///
  131. /// @Guide(description: "A 2 or 3 word search term, like 'Beautiful
  132. sunsets'")
  133. /// var searchTerm: String
  134. /// }
  135. /// }
  136. /// ```
  137. @available(iOS 26.0, macOS 26.0, *)
  138. @available(tvOS, unavailable)
  139. @available(watchOS, unavailable)
  140. public protocol Generable : ConvertibleFromGeneratedContent,
  141. ConvertibleToGeneratedContent {
  142. /// A representation of partially generated content
  143. associatedtype PartiallyGenerated : ConvertibleFromGeneratedContent =
  144. Self
  145. /// An instance of the generation schema.
  146. static var generationSchema: GenerationSchema { get }
  147. }
  148. @available(iOS 26.0, macOS 26.0, *)
  149. @available(tvOS, unavailable)
  150. @available(watchOS, unavailable)
  151. extension Generable {
  152. /// The partially generated type of this struct.
  153. public func asPartiallyGenerated() -> Self.PartiallyGenerated
  154. }
  155. @available(iOS 26.0, macOS 26.0, *)
  156. @available(tvOS, unavailable)
  157. @available(watchOS, unavailable)
  158. extension Generable {
  159. /// A representation of partially generated content
  160. public typealias PartiallyGenerated = Self
  161. }
  162. /// Conforms a type to generable.
  163. ///
  164. /// You can apply this macro to structures and enumerations.
  165. ///
  166. /// ```swift
  167. /// @Generable
  168. /// struct NovelIdea {
  169. /// @Guide(description: "A short title")
  170. /// let title: String
  171. ///
  172. /// @Guide(description: "A short subtitle for the novel")
  173. /// let subtitle: String
  174. ///
  175. /// @Guide(description: "The genre of the novel")
  176. /// let genre: Genre
  177. /// }
  178. ///
  179. /// @Generable
  180. /// enum Genre {
  181. /// case fiction
  182. /// case nonFiction
  183. /// }
  184. /// ```
  185. @available(iOS 26.0, macOS 26.0, *)
  186. @available(tvOS, unavailable)
  187. @available(watchOS, unavailable)
  188. @attached(extension, conformances: Generable, names: named(init(_:)),
  189. named(generatedContent)) @attached(member, names: arbitrary) public macro
  190. Generable(description: String? = nil) = #externalMacro(module:
  191. "FoundationModelsMacros", type: "GenerableMacro")
  192. /// A type that represents structured, generated content.
  193. ///
  194. /// Generated content may contain a single value, an array, or key-value pairs with unique keys.
  195. @available(iOS 26.0, macOS 26.0, *)
  196. @available(tvOS, unavailable)
  197. @available(watchOS, unavailable)
  198. public struct GeneratedContent : Sendable, Equatable, Generable,
  199. CustomDebugStringConvertible {
  200. /// An instance of the generation schema.
  201. public static var generationSchema: GenerationSchema { get }
  202. /// ///
  203. /// /// A unique id that is stable for the duration of a generated response.
  204. A ``LanguageModelSession`` produces instances of `GeneratedContent` that have a
  205. non-nil `id`. When you stream a response, the `id` is the same for all partial generations in
  206. the
  207. /// response stream.
  208. ///
  209. /// Instances of `GeneratedContent` that you produce manually with initializers have a nil
  210. `id`
  211. /// because the framework didn't create them as part of a generation.
  212. public var id: GenerationID?
  213. /// ///
  214. /// public init(
  215. Creates generated content from another value.
  216. This is used to satisfy `Generable.init(_:)`
  217. .
  218. content: GeneratedContent) throws
  219. _
  220. /// A representation of this instance.
  221. public var generatedContent: GeneratedContent { get }
  222. /// Creates generated content representing a structure with the properties you specify.
  223. ///
  224. /// The order of properties is important. For ``Generable`` types, the order
  225. /// must match the order properties in the types `schema`
  226. .
  227. public init(properties: KeyValuePairs<String, any
  228. ConvertibleToGeneratedContent>, id: GenerationID? = nil)
  229. /// Creates new generated content from the key-value pairs in the given sequence,
  230. /// using a combining closure to determine the value for any duplicate keys.
  231. ///
  232. /// The order of properties is important. For ``Generable`` types, the order
  233. /// must match the order properties in the types `schema`
  234. .
  235. ///
  236. /// You use this initializer to create generated content when you have a sequence
  237. /// of key-value tuples that might have duplicate keys. As the content is
  238. /// built, the initializer calls the `combine` closure with the current and
  239. /// new values for any duplicate keys. Pass a closure as `combine` that
  240. /// returns the value to use in the resulting content: The closure can
  241. /// choose between the two values, combine them to produce a new value, or
  242. /// even throw an error.
  243. ///
  244. /// The following example shows how to choose the first and last values for
  245. /// any duplicate keys:
  246. ///
  247. /// ```swift
  248. /// let content = GeneratedContent(
  249. /// properties: [("name", "John"), ("name", "Jane"), ("married":
  250. true)],
  251. /// /// )
  252. /// /// ```
  253. ///
  254. uniquingKeysWith: { (first, _ in first }
  255. // GeneratedContent(["name": "John", "married": true])
  256. /// - Parameters:
  257. /// - properties: A sequence of key-value pairs to use for the new content.
  258. /// - id: A unique id associated with GeneratedContent.
  259. /// - uniquingKeysWith: A closure that is called with the values for any duplicate
  260. /// keys that are encountered. The closure returns the desired value for
  261. /// the final content.
  262. public init<S>(properties: S, id: GenerationID? = nil, uniquingKeysWith
  263. combine: (GeneratedContent, GeneratedContent) throws -> some
  264. ConvertibleToGeneratedContent) rethrows where S : Sequence, S.Element
  265. == (String, any ConvertibleToGeneratedContent)
  266. /// Creates content representing an array of elements you specify.
  267. public init<S>(elements: S, id: GenerationID? = nil) where S :
  268. Sequence, S.Element == any ConvertibleToGeneratedContent
  269. /// Creates content that contains a single value.
  270. ///
  271. /// - Parameters:
  272. /// - value: The underlying value.
  273. public init(
  274. value: some ConvertibleToGeneratedContent)
  275. _
  276. /// Creates content that contains a single value with a custom generation ID.
  277. ///
  278. /// - Parameters:
  279. /// - value: The underlying value.
  280. /// - id: The generation ID for this content.
  281. public init(
  282. _
  283. value: some ConvertibleToGeneratedContent, id:
  284. GenerationID)
  285. /// ///
  286. /// Creates equivalent content from a JSON string.
  287. The JSON string you provide may be incomplete. This is useful for correctly handling partially
  288. generated responses.
  289. ///
  290. /// ```swift
  291. /// @Generable struct NovelIdea {
  292. /// let title: String
  293. /// }
  294. ///
  295. /// let partial = #"{"title": "A story of"#
  296. /// let content = try GeneratedContent(json: partial)
  297. /// let idea = try NovelIdea(content)
  298. /// print(idea.title) // A story of
  299. /// ```
  300. public init(json: String) throws
  301. /// Returns a JSON string representation of the generated content.
  302. ///
  303. /// ## Examples
  304. ///
  305. /// ```swift
  306. /// // Object with properties
  307. /// let content = GeneratedContent(properties: [
  308. /// "name": "Johnny Appleseed",
  309. /// "age": 30,
  310. /// ])
  311. /// print(content.jsonString)
  312. /// // Output: {"name": "Johnny Appleseed", "age": 30}
  313. /// ```
  314. public var jsonString: String { get }
  315. /// Reads a top level, concrete partially generable type.
  316. public func value<Value>(
  317. _ type: Value.Type = Value.self) throws ->
  318. Value where Value : ConvertibleFromGeneratedContent
  319. /// Reads a concrete generable type from named property.
  320. public func value<Value>(
  321. _ type: Value.Type = Value.self, forProperty
  322. property: String) throws -> Value where Value :
  323. ConvertibleFromGeneratedContent
  324. /// Reads an optional, concrete generable type from named property.
  325. public func value<Value>(
  326. _ type: Value?.Type = Value?.self, forProperty
  327. property: String) throws -> Value? where Value :
  328. ConvertibleFromGeneratedContent
  329. /// A string representation for the debug description.
  330. public var debugDescription: String { get }
  331. /// A Boolean that indicates whether the generated content is completed.
  332. public var isComplete: Bool { get }
  333. /// Returns a Boolean value indicating whether two values are equal.
  334. ///
  335. /// Equality is the inverse of inequality. For any values `
  336. /// `a == b` implies that `a != b` is `false`
  337. a
  338. ` and `b`
  339. ,
  340. .
  341. ///
  342. /// - Parameters:
  343. /// - lhs: A value to compare.
  344. /// - rhs: Another value to compare.
  345. public static func == (a: GeneratedContent, b: GeneratedContent) -> Bool
  346. }
  347. @available(iOS 26.0, macOS 26.0, *)
  348. @available(tvOS, unavailable)
  349. @available(watchOS, unavailable)
  350. extension GeneratedContent {
  351. /// ///
  352. /// /// A representation of the different types of content that can be stored in `GeneratedContent`
  353. `Kind` represents the various types of JSON-compatible data that can be held within
  354. a `GeneratedContent` instance, including primitive types, arrays, and structured objects.
  355. public enum Kind : Equatable, Sendable {
  356. /// Represents a null value.
  357. case null
  358. /// Represents a boolean value.
  359. /// - Parameter value: The boolean value.
  360. case bool(Bool)
  361. .
  362. /// Represents a numeric value.
  363. /// - Parameter value: The numeric value as a Double.
  364. case number(Double)
  365. /// Represents a string value.
  366. /// - Parameter value: The string value.
  367. case string(String)
  368. /// Represents an array of `GeneratedContent` elements.
  369. /// - Parameter elements: An array of `GeneratedContent` instances.
  370. case array([GeneratedContent])
  371. /// Represents a structured object with key-value pairs.
  372. /// - Parameters:
  373. /// - properties: A dictionary mapping string keys to `GeneratedContent`
  374. values.
  375. /// - orderedKeys: An array of keys that specifies the order of properties.
  376. case structure(properties: [String : GeneratedContent],
  377. orderedKeys: [String])
  378. /// Returns a Boolean value indicating whether two values are equal.
  379. ///
  380. /// Equality is the inverse of inequality. For any values `
  381. /// `a == b` implies that `a != b` is `false`
  382. a
  383. ` and `b`
  384. ,
  385. .
  386. ///
  387. /// - Parameters:
  388. /// - lhs: A value to compare.
  389. /// - rhs: Another value to compare.
  390. public static func == (a: GeneratedContent.Kind, b:
  391. GeneratedContent.Kind) -> Bool
  392. }
  393. /// Creates a new `GeneratedContent` instance with the specified kind and generation ID.
  394. ///
  395. /// This initializer provides a convenient way to create content from its kind representation.
  396. ///
  397. /// - Parameters:
  398. /// - kind: The kind of content to create.
  399. /// - id: An optional generation ID to associate with this content.
  400. public init(kind: GeneratedContent.Kind, id: GenerationID? = nil)
  401. /// ///
  402. /// /// The kind representation of this generated content.
  403. This property provides access to the content in a strongly-typed enum representation,
  404. preserving the hierarchical structure of the data and the generation IDs.
  405. public var kind: GeneratedContent.Kind { get }
  406. }
  407. /// Guides that control how values are generated.
  408. @available(iOS 26.0, macOS 26.0, *)
  409. @available(tvOS, unavailable)
  410. @available(watchOS, unavailable)
  411. public struct GenerationGuide<Value> {
  412. }
  413. @available(iOS 26.0, macOS 26.0, *)
  414. @available(tvOS, unavailable)
  415. @available(watchOS, unavailable)
  416. extension GenerationGuide where Value == String {
  417. /// Enforces that the string be precisely the given value.
  418. public static func constant(
  419. _
  420. value: String) -> GenerationGuide<String>
  421. /// Enforces that the string be one of the provided values.
  422. public static func anyOf(
  423. _
  424. values: [String]) -> GenerationGuide<String>
  425. /// Enforces that the string follows the pattern.
  426. public static func pattern<Output>(
  427. GenerationGuide<String>
  428. _ regex: Regex<Output>) ->
  429. }
  430. @available(iOS 26.0, macOS 26.0, *)
  431. @available(tvOS, unavailable)
  432. @available(watchOS, unavailable)
  433. extension GenerationGuide where Value == Int {
  434. /// Enforces a minimum value.
  435. ///
  436. /// Use a `minimum` generation guide --- whose bounds are inclusive --- to ensure the model
  437. /// produces
  438. characters
  439. in your game start at level 1:
  440. a value greater than or equal to some minimum value. For example, you can specify that all
  441. /// ///
  442. /// ```swift
  443. /// @Generable
  444. /// struct struct GameCharacter {
  445. /// @Guide(description: "A creative name appropriate for a fantasy
  446. RPG character")
  447. /// var name: String
  448. ///
  449. /// /// var level: Int
  450. /// }
  451. /// ```
  452. public static func minimum(
  453. _
  454. @Guide(description: "A level for the character", .minimum(1))
  455. value: Int) -> GenerationGuide<Int>
  456. /// Enforces a maximum value.
  457. ///
  458. /// Use a `maximum` generation guide --- whose bounds are inclusive --- to ensure the model
  459. /// produces
  460. a value less than or equal to some maximum value. For example, you can specify that the
  461. highest level
  462. a character in your game can achieve is 100:
  463. /// ///
  464. /// ```swift
  465. /// @Generable
  466. /// struct struct GameCharacter {
  467. /// @Guide(description: "A creative name appropriate for a fantasy
  468. RPG character")
  469. /// var name: String
  470. ///
  471. /// /// var level: Int
  472. /// }
  473. /// ```
  474. public static func maximum(
  475. _
  476. @Guide(description: "A level for the character", .maximum(100))
  477. value: Int) -> GenerationGuide<Int>
  478. /// Enforces values fall within a range.
  479. ///
  480. /// Use a `
  481. range
  482. produces a
  483. /// game
  484. are between 1 and 100:
  485. ` generation guide --- whose bounds are inclusive --- to ensure the model
  486. value that falls within a range. For example, you can specify that the level of characters in your
  487. /// ///
  488. /// ```swift
  489. /// @Generable
  490. /// struct struct GameCharacter {
  491. /// @Guide(description: "A creative name appropriate for a fantasy
  492. RPG character")
  493. /// var name: String
  494. ///
  495. /// @Guide(description: "A level for the character",
  496. .range(1...100))
  497. /// var level: Int
  498. /// }
  499. /// ```
  500. public static func range(
  501. _ range: ClosedRange<Int>) ->
  502. GenerationGuide<Int>
  503. }
  504. @available(iOS 26.0, macOS 26.0, *)
  505. @available(tvOS, unavailable)
  506. @available(watchOS, unavailable)
  507. extension GenerationGuide where Value == Float {
  508. /// Enforces a minimum value.
  509. ///
  510. /// The bounds are inclusive.
  511. public static func minimum(
  512. _
  513. value: Float) -> GenerationGuide<Float>
  514. /// Enforces a maximum value.
  515. ///
  516. /// The bounds are inclusive.
  517. public static func maximum(
  518. _
  519. value: Float) -> GenerationGuide<Float>
  520. /// Enforces values fall within a range.
  521. public static func range(
  522. GenerationGuide<Float>
  523. _ range: ClosedRange<Float>) ->
  524. }
  525. @available(iOS 26.0, macOS 26.0, *)
  526. @available(tvOS, unavailable)
  527. @available(watchOS, unavailable)
  528. extension GenerationGuide where Value == Decimal {
  529. /// Enforces a minimum value.
  530. ///
  531. /// The bounds are inclusive.
  532. public static func minimum(
  533. value: Decimal) -> GenerationGuide<Decimal>
  534. _
  535. /// Enforces a maximum value.
  536. ///
  537. /// The bounds are inclusive.
  538. public static func maximum(
  539. _
  540. value: Decimal) -> GenerationGuide<Decimal>
  541. /// Enforces values fall within a range.
  542. public static func range(
  543. GenerationGuide<Decimal>
  544. _ range: ClosedRange<Decimal>) ->
  545. }
  546. @available(iOS 26.0, macOS 26.0, *)
  547. @available(tvOS, unavailable)
  548. @available(watchOS, unavailable)
  549. extension GenerationGuide where Value == Double {
  550. /// Enforces a minimum value.
  551. /// The bounds are inclusive.
  552. public static func minimum(
  553. value: Double) -> GenerationGuide<Double>
  554. _
  555. /// Enforces a maximum value.
  556. /// The bounds are inclusive.
  557. public static func maximum(
  558. _
  559. value: Double) -> GenerationGuide<Double>
  560. /// Enforces values fall within a range.
  561. public static func range(
  562. GenerationGuide<Double>
  563. _ range: ClosedRange<Double>) ->
  564. }
  565. @available(iOS 26.0, macOS 26.0, *)
  566. @available(tvOS, unavailable)
  567. @available(watchOS, unavailable)
  568. extension GenerationGuide {
  569. /// Enforces a minimum number of elements in the array.
  570. ///
  571. /// The bounds are inclusive.
  572. public static func minimumCount<Element>(
  573. count: Int) ->
  574. _
  575. GenerationGuide<[Element]> where Value == [Element]
  576. /// Enforces a maximum number of elements in the array.
  577. ///
  578. /// The bounds are inclusive.
  579. public static func maximumCount<Element>(
  580. count: Int) ->
  581. _
  582. GenerationGuide<[Element]> where Value == [Element]
  583. /// Enforces that the number of elements in the array fall within a closed range.
  584. public static func count<Element>(
  585. _ range: ClosedRange<Int>) ->
  586. GenerationGuide<[Element]> where Value == [Element]
  587. /// Enforces that the array has exactly a certain number elements.
  588. public static func count<Element>(
  589. count: Int) ->
  590. _
  591. GenerationGuide<[Element]> where Value == [Element]
  592. /// Enforces a guide on the elements within the array.
  593. public static func element<Element>(
  594. _ guide: GenerationGuide<Element>)
  595. -> GenerationGuide<[Element]> where Value == [Element]
  596. }
  597. @available(iOS 26.0, macOS 26.0, *)
  598. @available(tvOS, unavailable)
  599. @available(watchOS, unavailable)
  600. extension GenerationGuide where Value == [Never] {
  601. /// Enforces a minimum number of elements in the array.
  602. ///
  603. /// Bounds are inclusive.
  604. ///
  605. /// - Warning: This overload is only used for macro expansion. Don't call
  606. `GenerationGuide<[Never]>.minimumCount(_:)`
  607. on your own.
  608. public static func minimumCount(
  609. count: Int) -> GenerationGuide<Value>
  610. _
  611. /// Enforces a maximum number of elements in the array.
  612. ///
  613. /// Bounds are inclusive.
  614. ///
  615. /// - Warning: This overload is only used for macro expansion. Don't call
  616. `GenerationGuide<[Never]>.maximumCount(_:)`
  617. on your own.
  618. public static func maximumCount(
  619. count: Int) -> GenerationGuide<Value>
  620. _
  621. /// Enforces that the number of elements in the array fall within a closed range.
  622. ///
  623. /// - Warning: This overload is only used for macro expansion. Don't call
  624. `GenerationGuide<[Never]>.count(_:)`
  625. on your own.
  626. public static func count(
  627. _ range: ClosedRange<Int>) ->
  628. GenerationGuide<Value>
  629. /// Enforces that the array has exactly a certain number elements.
  630. ///
  631. /// - Warning: This overload is only used for macro expansion. Don't call
  632. `GenerationGuide<[Never]>.count(_:)`
  633. on your own.
  634. public static func count(
  635. count: Int) -> GenerationGuide<Value>
  636. _
  637. }
  638. /// A unique identifier that is stable for the duration of a response, but not across responses.
  639. ///
  640. /// /// /// The framework guarentees a `GenerationID` to be both present and stable when you
  641. receive it from a `LanguageModelSession`. When you create an instance of
  642. `GenerationID` there is no guarantee an identifier is present or stable.
  643. ///
  644. /// ```swift
  645. /// @Generable struct Person: Equatable {
  646. /// var id: GenerationID
  647. /// var name: String
  648. /// }
  649. ///
  650. /// struct PeopleView: View {
  651. /// @State private var session = LanguageModelSession()
  652. /// @State private var people = [Person.PartiallyGenerated]()
  653. ///
  654. /// var body: some View {
  655. /// // A person's name changes as the response is generated,
  656. /// // and two people can have the same name, so it is not suitable
  657. /// // for use as an id.
  658. /// //
  659. /// // `GenerationID` receives special treatment and is guaranteed
  660. /// // to be both present and stable.
  661. /// List {
  662. /// ForEach(people) { person in
  663. /// Text("Name: \(person.name)")
  664. /// }
  665. /// }
  666. /// .task {
  667. /// for try! await people in stream.streamResponse(
  668. /// to: "Who were the first 3 presidents of the US?",
  669. /// generating: [Person].self
  670. /// ) {
  671. /// withAnimation {
  672. /// self.people = people
  673. /// }
  674. /// }
  675. /// }
  676. /// }
  677. /// }
  678. /// ```
  679. @available(iOS 26.0, macOS 26.0, *)
  680. @available(tvOS, unavailable)
  681. @available(watchOS, unavailable)
  682. public struct GenerationID : Sendable, Hashable {
  683. /// public init()
  684. Create a new, unique `GenerationID`
  685. .
  686. /// Returns a Boolean value indicating whether two values are equal.
  687. ///
  688. /// Equality is the inverse of inequality. For any values `
  689. /// `a == b` implies that `a != b` is `false`
  690. a
  691. ` and `b`
  692. ,
  693. .
  694. ///
  695. /// - Parameters:
  696. /// - lhs: A value to compare.
  697. /// - rhs: Another value to compare.
  698. public static func == (a: GenerationID, b: GenerationID) -> Bool
  699. /// /// given hasher.
  700. ///
  701. /// /// Hashes the essential components of this value by feeding them into the
  702. Implement this method to conform to the `Hashable` protocol. The
  703. components used for hashing must be the same as the components compared
  704. /// in your type's `
  705. ==
  706. ` operator implementation. Call `hasher.combine(_:)`
  707. /// with each of these components.
  708. ///
  709. /// - Important: In your implementation of `hash(into:)`
  710. ,
  711. /// don't call `finalize()` on the `hasher` instance provided,
  712. /// or replace it with a different instance.
  713. /// Doing so may become a compile-time error in the future.
  714. ///
  715. /// - Parameter hasher: The hasher to use when combining the components
  716. /// of this instance.
  717. public func hash(into hasher: inout Hasher)
  718. /// The hash value.
  719. ///
  720. /// /// ///
  721. /// /// Hash values are not guaranteed to be equal across different executions of
  722. your program. Do not save hash values to use during a future execution.
  723. /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
  724. conform to `Hashable`, implement the `hash(into:)` requirement instead.
  725. The compiler provides an implementation for `hashValue` for you.
  726. public var hashValue: Int { get }
  727. }
  728. /// Options that control how the model generates its response to a prompt.
  729. ///
  730. /// Create a ``GenerationOptions`` structure when you want to adjust
  731. /// the way the model generates its response. Use this structure to
  732. /// perform various adjustments on how the model chooses output tokens,
  733. /// to specify the penalties for repeating tokens or generating
  734. /// longer responses.
  735. @available(iOS 26.0, macOS 26.0, *)
  736. @available(tvOS, unavailable)
  737. @available(watchOS, unavailable)
  738. public struct GenerationOptions : Sendable, Equatable {
  739. /// A type that defines how values are sampled from a probability distribution.
  740. ///
  741. /// A model builds its response to a prompt in a loop. At each iteration in the
  742. /// loop the model produces a probability distribution for all the tokens in its
  743. /// vocabulary. The sampling mode controls how a token is selected from that
  744. /// distribution.
  745. @available(iOS 26.0, macOS 26.0, *)
  746. @available(tvOS, unavailable)
  747. @available(watchOS, unavailable)
  748. public struct SamplingMode : Sendable, Equatable {
  749. /// ///
  750. /// /// /// /// A sampling mode that always chooses the most likely token.
  751. Using this mode will always result in the same output
  752. for a given input. Responses produced with greedy sampling
  753. are statistically likely, but may lack the human-like quality
  754. and variety of other sampling strategies.
  755. public static var greedy: GenerationOptions.SamplingMode { get }
  756. /// ///
  757. A sampling mode that considers a fixed number of high-probability tokens.
  758. /// Also known as top-k.
  759. ///
  760. /// /// /// /// /// During the token-selection process, the vocabulary is sorted by probability a
  761. token is selected from among the top K candidates. Smaller values of K will
  762. ensure only the most probable tokens are candidates for selection, resulting
  763. in more deterministic and confident answers. Larger values of K will allow less
  764. probably tokens to be selected, raising non-determinism and creativity.
  765. ///
  766. /// - Note: Setting a random seed is not guaranteed to result in fully deterministic
  767. /// output. It is best effort.
  768. ///
  769. /// - Parameters:
  770. /// - top: The number of tokens to consider.
  771. /// - seed: An optional random seed used to make output more deterministic.
  772. public static func random(top k: Int, seed: UInt64? = nil) ->
  773. GenerationOptions.SamplingMode
  774. /// A mode that considers a variable number of high-probability tokens
  775. /// based on the specified threshold.
  776. ///
  777. /// Also known as top-p or nucleus sampling.
  778. ///
  779. /// With nucleus sampling, tokens are sorted by probability and added to a
  780. /// pool of candidates until the cumulative probability of the pool exceeds
  781. /// the specified threshold, and then a token is sampled from the pool.
  782. ///
  783. /// Because the number of tokens isn't predetermined, the selection pool size
  784. /// will be larger when the distribution is flat and smaller when it is spikey.
  785. /// This variability can lead to a wider variety of options to choose from, and
  786. /// potentially more creative responses.
  787. ///
  788. /// - Note: Setting a random seed is not guaranteed to result in fully deterministic
  789. /// output. It is best effort.
  790. ///
  791. /// - Parameters:
  792. /// - probabilityThreshold: A number between `0.0` and `1.0` that
  793. /// increases sampling pool size.
  794. /// - seed: An optional random seed used to make output more deterministic.
  795. public static func random(probabilityThreshold: Double, seed:
  796. UInt64? = nil) -> GenerationOptions.SamplingMode
  797. /// Returns a Boolean value indicating whether two values are equal.
  798. ///
  799. /// Equality is the inverse of inequality. For any values `
  800. /// `a == b` implies that `a != b` is `false`
  801. a
  802. ` and `b`
  803. ,
  804. .
  805. ///
  806. /// - Parameters:
  807. /// - lhs: A value to compare.
  808. /// - rhs: Another value to compare.
  809. public static func == (a: GenerationOptions.SamplingMode, b:
  810. GenerationOptions.SamplingMode) -> Bool
  811. }
  812. /// /// response.
  813. A sampling strategy for how the model picks tokens when generating a
  814. ///
  815. /// /// /// /// /// /// ///
  816. /// When you execute a prompt on a model, the model produces a probability
  817. for every token in its vocabulary. The sampling strategy controls how
  818. the model narrows down the list of tokens to consider during that process.
  819. A strategy that picks the single most likely token yields a predictable
  820. response every time, but other strategies offer results that often
  821. sound more natural to a person.
  822. /// - Note: Leaving the `sampling` nil lets the system choose a
  823. a reasonable default on your behalf.
  824. public var sampling: GenerationOptions.SamplingMode?
  825. /// Temperature influences the confidence of the models response.
  826. ///
  827. /// The value of this property must be a number between `0` and `1` inclusive.
  828. ///
  829. /// Temperature is an adjustment applied to the probability distribution
  830. /// prior to sampling. A value of `1` results in no adjustment. Values less
  831. /// than `1` will make the probability distribution sharper, with already
  832. /// likely tokens becoming even more likely.
  833. ///
  834. /// The net effect is that low temperatures manifest as more stable and
  835. /// predictable responses, while high temperatures give the model more
  836. /// creative license.
  837. ///
  838. /// - Note: Leaving `temperature` nil lets the system choose a reasonable
  839. /// default on your behalf.
  840. public var temperature: Double?
  841. /// ///
  842. /// /// /// ///
  843. /// /// /// The maximum number of tokens the model is allowed to produce in its response.
  844. If the model produce `maximumResponseTokens` before it naturally completes its response,
  845. the response will be terminated early. No error will be thrown. This property
  846. can be used to protect against unexpectedly verbose responses and runaway generations.
  847. If no value is specified, then the model is allowed to produce the longest answer
  848. its context size supports. If the response exceeds that limit without terminating,
  849. an error will be thrown.
  850. public var maximumResponseTokens: Int?
  851. /// Creates generation options that control token sampling behavior.
  852. ///
  853. /// - Parameters:
  854. /// - sampling: A strategy to use for sampling from a distribution.
  855. /// - temperature: Increasing temperature makes it possible for the model to produce less
  856. likely
  857. /// responses. Must be between `0` and `1`, inclusive.
  858. /// - maximumResponseTokens: The maximum number of tokens the model is allowed
  859. /// to produce before being artificially halted. Must be positive.
  860. public init(sampling: GenerationOptions.SamplingMode? = nil,
  861. temperature: Double? = nil, maximumResponseTokens: Int? = nil)
  862. /// ///
  863. /// Returns a Boolean value indicating whether two values are equal.
  864. Equality is the inverse of inequality. For any values `
  865. a
  866. ` and `b`
  867. ,
  868. /// `a == b` implies that `a != b` is `false`
  869. .
  870. ///
  871. /// - Parameters:
  872. /// - lhs: A value to compare.
  873. /// - rhs: Another value to compare.
  874. public static func == (a: GenerationOptions, b: GenerationOptions) ->
  875. Bool
  876. }
  877. /// A type that describes the properties of an object and any guides
  878. /// on their values.
  879. ///
  880. /// Generation schemas guide the output of a ``SystemLanguageModel`` to deterministically
  881. /// ensure the output is in the desired format.
  882. @available(iOS 26.0, macOS 26.0, *)
  883. @available(tvOS, unavailable)
  884. @available(watchOS, unavailable)
  885. public struct GenerationSchema : Sendable, Codable,
  886. CustomDebugStringConvertible {
  887. /// A property that belongs to a generation schema.
  888. ///
  889. /// Fields are named members of object types. Fields are strongly
  890. /// typed and have optional descriptions and guides.
  891. @available(iOS 26.0, macOS 26.0, *)
  892. @available(tvOS, unavailable)
  893. @available(watchOS, unavailable)
  894. public struct Property : Sendable {
  895. /// Create a property that contains a generable type.
  896. ///
  897. /// - Parameters:
  898. /// - name: The property's name.
  899. /// - description: A natural language description of what content
  900. /// should be generated for this property.
  901. /// - type: The type this property represents.
  902. /// - guides: A list of guides to apply to this property.
  903. public init<Value>(name: String, description: String? = nil, type:
  904. Value.Type, guides: [GenerationGuide<Value>] = []) where Value :
  905. Generable
  906. /// Create an optional property that contains a generable type.
  907. ///
  908. /// - Parameters:
  909. /// - name: The property's name.
  910. /// - description: A natural language description of what content
  911. /// should be generated for this property.
  912. /// - type: The type this property represents.
  913. /// - guides: A list of guides to apply to this property.
  914. public init<Value>(name: String, description: String? = nil, type:
  915. Value?.Type, guides: [GenerationGuide<Value>] = []) where Value :
  916. Generable
  917. /// ///
  918. Create a property that contains a string type.
  919. /// - Parameters:
  920. /// - name: The property's name.
  921. /// - description: A natural language description of what content
  922. /// should be generated for this property.
  923. /// - type: The type this property represents.
  924. /// - guides: An array of regexes to be applied to this string. If there're multiple
  925. regexes in the array, only the last one will be applied.
  926. public init<RegexOutput>(name: String, description: String? = nil,
  927. type: String.Type, guides: [Regex<RegexOutput>] = [])
  928. /// Create an optional property that contains a generable type.
  929. ///
  930. /// - Parameters:
  931. /// - name: The property's name.
  932. /// - description: A natural language description of what content
  933. /// should be generated for this property.
  934. /// - type: The type this property represents.
  935. /// - guides: An array of regexes to be applied to this string. If there're multiple
  936. regexes in the array, only the last one will be applied.
  937. public init<RegexOutput>(name: String, description: String? = nil,
  938. type: String?.Type, guides: [Regex<RegexOutput>] = [])
  939. }
  940. /// ///
  941. /// A string representation of the debug description.
  942. This string is not localized and is not appropriate for display to end users.
  943. public var debugDescription: String { get }
  944. /// Creates a schema by providing an array of properties.
  945. ///
  946. /// - Parameters:
  947. /// - type: The type this schema represents.
  948. /// - description: A natural language description of this schema.
  949. /// - properties: An array of properties.
  950. public init(type: any Generable.Type, description: String? = nil,
  951. properties: [GenerationSchema.Property])
  952. /// Creates a schema for a string enumeration.
  953. ///
  954. /// - Parameters:
  955. /// - type: The type this schema represents.
  956. /// - description: A natural language description of this schema.
  957. /// - anyOf: The allowed choices.
  958. public init(type: any Generable.Type, description: String? = nil, anyOf
  959. choices: [String])
  960. /// Creates a schema as the union of several other types.
  961. ///
  962. /// - Parameters:
  963. /// - type: The type this schema represents.
  964. /// - description: A natural language description of this schema.
  965. /// - anyOf: The types this schema should be a union of.
  966. public init(type: any Generable.Type, description: String? = nil, anyOf
  967. types: [any Generable.Type])
  968. /// Creates a schema by providing an array of dynamic schemas.
  969. ///
  970. /// - Parameters:
  971. /// - root: The root schema.
  972. /// - dependencies: An array of dynamic schemas.
  973. /// - Throws: Throws there are schemas with naming conflicts or
  974. /// references to undefined types.
  975. public init(root: DynamicGenerationSchema, dependencies:
  976. [DynamicGenerationSchema]) throws
  977. /// A error that occurs when there is a problem creating a generation schema.
  978. @available(iOS 26.0, macOS 26.0, *)
  979. @available(tvOS, unavailable)
  980. @available(watchOS, unavailable)
  981. public enum SchemaError : Error, LocalizedError {
  982. /// The context in which the error occurred.
  983. @available(iOS 26.0, macOS 26.0, *)
  984. @available(tvOS, unavailable)
  985. @available(watchOS, unavailable)
  986. public struct Context : Sendable {
  987. /// ///
  988. /// A string representation of the debug description.
  989. This string is not localized and is not appropriate for display to end users.
  990. public let debugDescription: String
  991. public init(debugDescription: String)
  992. }
  993. /// An error that represents an attempt to construct a schema from dynamic schemas,
  994. /// and two or more of the subschemas have the same type name.
  995. case duplicateType(schema: String?, type: String, context:
  996. GenerationSchema.SchemaError.Context)
  997. /// An error that represents an attempt to construct a dynamic schema
  998. /// with properties that have conflicting names.
  999. case duplicateProperty(schema: String, property: String, context:
  1000. GenerationSchema.SchemaError.Context)
  1001. /// An error that represents an attempt to construct an anyOf schema with an
  1002. /// empty array of type choices.
  1003. case emptyTypeChoices(schema: String, context:
  1004. GenerationSchema.SchemaError.Context)
  1005. /// /// An error that represents an attempt to construct a schema from dynamic schemas,
  1006. and one of those schemas references an undefined schema.
  1007. case undefinedReferences(schema: String?, references: [String],
  1008. context: GenerationSchema.SchemaError.Context)
  1009. /// A string representation of the error description.
  1010. public var errorDescription: String? { get }
  1011. /// A suggestion that indicates how to handle the error.
  1012. public var recoverySuggestion: String? { get }
  1013. }
  1014. /// Creates a new instance by decoding from the given decoder.
  1015. ///
  1016. /// /// This initializer throws an error if reading from the decoder fails, or
  1017. if the data read is corrupted or otherwise invalid.
  1018. ///
  1019. /// - Parameter decoder: The decoder to read data from.
  1020. public init(from decoder: any Decoder) throws
  1021. /// ///
  1022. /// /// ///
  1023. /// ///
  1024. Encodes this value into the given encoder.
  1025. If the value fails to encode anything, `encoder` will encode an empty
  1026. keyed container in its place.
  1027. This function throws an error if any values are invalid for the given
  1028. /// encoder's format.
  1029. /// - Parameter encoder: The encoder to write data to.
  1030. public func encode(to encoder: any Encoder) throws
  1031. }
  1032. /// Allows for influencing the allowed values of properties of a generable type.
  1033. @available(iOS 26.0, macOS 26.0, *)
  1034. @available(tvOS, unavailable)
  1035. @available(watchOS, unavailable)
  1036. @attached(peer) public macro Guide<T>(description: String? = nil, _ guides:
  1037. GenerationGuide<T>...) = #externalMacro(module: "FoundationModelsMacros",
  1038. type: "GuideMacro") where T : Generable
  1039. /// Allows for influencing the allowed values of properties of a generable type.
  1040. @available(iOS 26.0, macOS 26.0, *)
  1041. @available(tvOS, unavailable)
  1042. @available(watchOS, unavailable)
  1043. @attached(peer) public macro Guide<RegexOutput>(description: String? = nil,
  1044. _ guides: Regex<RegexOutput>) = #externalMacro(module:
  1045. "FoundationModelsMacros", type: "GuideMacro")
  1046. /// Allows for influencing the allowed values of properties of a generable type.
  1047. @available(iOS 26.0, macOS 26.0, *)
  1048. @available(tvOS, unavailable)
  1049. @available(watchOS, unavailable)
  1050. @attached(peer) public macro Guide(description: String) =
  1051. #externalMacro(module: "FoundationModelsMacros", type: "GuideMacro")
  1052. /// ///
  1053. /// Instructions define the model's intended behavior on prompts.
  1054. Instructions are typically provided by you to define the role and behavior of the model. In the code
  1055. below,
  1056. /// the instructions specify that the model replies with topics rather than, for example, a recipe:
  1057. ///
  1058. /// ```swift
  1059. /// let instructions = """
  1060. /// Suggest related topics. Keep them concise (three to seven words)
  1061. and \
  1062. /// make sure they build naturally from the person's topic.
  1063. /// """
  1064. ///
  1065. /// let session = LanguageModelSession(instructions: instructions)
  1066. ///
  1067. /// let prompt = "Making homemade bread"
  1068. /// let response = try await session.respond(to: prompt)
  1069. /// ```
  1070. ///
  1071. /// Apple trains the model to obey instructions over any commands it receives in prompts, so don't
  1072. include
  1073. /// untrusted content in instructions. For more on how instructions impact generation quality and safety,
  1074. /// see <doc:improving-safety-from-generative-model-output>.
  1075. @available(iOS 26.0, macOS 26.0, *)
  1076. @available(tvOS, unavailable)
  1077. @available(watchOS, unavailable)
  1078. public struct Instructions {
  1079. /// public init(
  1080. Creates an instance with the content you specify.
  1081. _
  1082. content: some InstructionsRepresentable)
  1083. }
  1084. @available(iOS 26.0, macOS 26.0, *)
  1085. @available(tvOS, unavailable)
  1086. @available(watchOS, unavailable)
  1087. extension Instructions : InstructionsRepresentable {
  1088. /// An instance that represents the instructions.
  1089. public var instructionsRepresentation: Instructions { get }
  1090. }
  1091. @available(iOS 26.0, macOS 26.0, *)
  1092. @available(tvOS, unavailable)
  1093. @available(watchOS, unavailable)
  1094. extension Instructions {
  1095. public init(@InstructionsBuilder
  1096. rethrows
  1097. content: () throws -> Instructions)
  1098. _
  1099. }
  1100. /// A type that represents an instructions builder.
  1101. @available(iOS 26.0, macOS 26.0, *)
  1102. @available(tvOS, unavailable)
  1103. @available(watchOS, unavailable)
  1104. @resultBuilder public struct InstructionsBuilder {
  1105. /// Creates a builder with the a block.
  1106. public static func buildBlock<each I>(
  1107. _ components: repeat each I) ->
  1108. Instructions where repeat each I : InstructionsRepresentable
  1109. /// Creates a builder with the an array of prompts.
  1110. public static func buildArray(
  1111. instructions: [some
  1112. _
  1113. InstructionsRepresentable]) -> Instructions
  1114. /// Creates a builder with the first component.
  1115. public static func buildEither(first component: some
  1116. InstructionsRepresentable) -> Instructions
  1117. /// Creates a builder with the second component.
  1118. public static func buildEither(second component: some
  1119. InstructionsRepresentable) -> Instructions
  1120. /// Creates a builder with an optional component.
  1121. public static func buildOptional(
  1122. _
  1123. Instructions
  1124. instructions: Instructions?) ->
  1125. /// Creates a builder with a limited availability prompt.
  1126. public static func buildLimitedAvailability(
  1127. _
  1128. InstructionsRepresentable) -> Instructions
  1129. instructions: some
  1130. /// Creates a builder with an expression.
  1131. public static func buildExpression<I>(
  1132. InstructionsRepresentable
  1133. _ expression: I) -> I where I :
  1134. /// Creates a builder with a prompt expression.
  1135. public static func buildExpression(
  1136. Instructions
  1137. _ expression: Instructions) ->
  1138. }
  1139. /// Conforming types represent instructions.
  1140. @available(iOS 26.0, macOS 26.0, *)
  1141. @available(tvOS, unavailable)
  1142. @available(watchOS, unavailable)
  1143. public protocol InstructionsRepresentable {
  1144. /// An instance that represents the instructions.
  1145. @InstructionsBuilder var instructionsRepresentation: Instructions { get
  1146. }
  1147. }
  1148. @available(iOS 26.0, macOS 26.0, *)
  1149. @available(tvOS, unavailable)
  1150. @available(watchOS, unavailable)
  1151. public struct LanguageModelFeedback {
  1152. /// A sentiment regarding the model's response.
  1153. @available(iOS 26.0, macOS 26.0, *)
  1154. @available(tvOS, unavailable)
  1155. @available(watchOS, unavailable)
  1156. public enum Sentiment : Sendable, CaseIterable {
  1157. /// A positive sentiment
  1158. case positive
  1159. /// A negative sentiment
  1160. case negative
  1161. /// A neutral sentiment
  1162. case neutral
  1163. /// Returns a Boolean value indicating whether two values are equal.
  1164. ///
  1165. /// Equality is the inverse of inequality. For any values `
  1166. /// `a == b` implies that `a != b` is `false`
  1167. a
  1168. ` and `b`
  1169. ,
  1170. .
  1171. ///
  1172. /// - Parameters:
  1173. /// - lhs: A value to compare.
  1174. /// - rhs: Another value to compare.
  1175. public static func == (a: LanguageModelFeedback.Sentiment, b:
  1176. LanguageModelFeedback.Sentiment) -> Bool
  1177. /// A type that can represent a collection of all values of this type.
  1178. @available(iOS 26.0, macOS 26.0, *)
  1179. @available(tvOS, unavailable)
  1180. @available(watchOS, unavailable)
  1181. public typealias AllCases = [LanguageModelFeedback.Sentiment]
  1182. /// A collection of all values of this type.
  1183. nonisolated public static var allCases:
  1184. [LanguageModelFeedback.Sentiment] { get }
  1185. /// Hashes the essential components of this value by feeding them into the
  1186. /// given hasher.
  1187. ///
  1188. /// Implement this method to conform to the `Hashable` protocol. The
  1189. /// components used for hashing must be the same as the components compared
  1190. /// in your type's `
  1191. ==
  1192. ` operator implementation. Call `hasher.combine(_:)`
  1193. /// with each of these components.
  1194. ///
  1195. /// - Important: In your implementation of `hash(into:)`
  1196. ,
  1197. /// don't call `finalize()` on the `hasher` instance provided,
  1198. /// or replace it with a different instance.
  1199. /// Doing so may become a compile-time error in the future.
  1200. ///
  1201. /// - Parameter hasher: The hasher to use when combining the components
  1202. /// of this instance.
  1203. public func hash(into hasher: inout Hasher)
  1204. /// The hash value.
  1205. ///
  1206. /// /// ///
  1207. /// /// Hash values are not guaranteed to be equal across different executions of
  1208. your program. Do not save hash values to use during a future execution.
  1209. /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
  1210. conform to `Hashable`, implement the `hash(into:)` requirement instead.
  1211. The compiler provides an implementation for `hashValue` for you.
  1212. public var hashValue: Int { get }
  1213. }
  1214. /// An issue with the model's response.
  1215. @available(iOS 26.0, macOS 26.0, *)
  1216. @available(tvOS, unavailable)
  1217. @available(watchOS, unavailable)
  1218. public struct Issue : Sendable {
  1219. /// Categories for model response issues.
  1220. @available(iOS 26.0, macOS 26.0, *)
  1221. @available(tvOS, unavailable)
  1222. @available(watchOS, unavailable)
  1223. public enum Category : Sendable, CaseIterable {
  1224. /// ///
  1225. /// The response was not unhelpful.
  1226. An unhelpful issue might be where you asked for a recipe, and the model gave you
  1227. a list of
  1228. /// ingredients but not amounts.
  1229. case unhelpful
  1230. /// ///
  1231. /// The response was too verbose.
  1232. A verbose issue might be where you asked for a simple recipe, and the model wrote
  1233. introductory
  1234. /// and conclusion paragraphs.
  1235. case tooVerbose
  1236. /// ///
  1237. /// The model did not follow instructions correctly.
  1238. An instruction issue might be where you asked for a recipe in numbered steps, and
  1239. the model
  1240. /// provided a recipe but didn't number the steps.
  1241. case didNotFollowInstructions
  1242. /// ///
  1243. /// The model provided an incorrect response.
  1244. An incorrect issue might be where you asked how to make a pizza, and the model
  1245. suggested using glue.
  1246. case incorrect
  1247. /// ///
  1248. /// The model exhibited bias or perpetuated a sterotype.
  1249. A stereotype or bias issue might be where you ask the model to summarize an
  1250. article written by
  1251. /// a male, and the model doesn't state the authors sex, but the model uses male
  1252. pronouns.
  1253. case stereotypeOrBias
  1254. /// ///
  1255. /// The model produces suggestive or sexual material.
  1256. A suggestive or sexual issue might be where you ask the model to draft a script for a
  1257. school
  1258. /// play, and it includes a sex scene.
  1259. case suggestiveOrSexual
  1260. /// ///
  1261. /// The model produces vulgar or offensive material.
  1262. A vulgar or offensive issue might be where you ask the model to draft a complaint
  1263. about poor
  1264. /// customer service, and it uses profanity.
  1265. case vulgarOrOffensive
  1266. /// ///
  1267. /// The model throws a guardrail violation when it shouldn't.
  1268. An unexpected guardrail issue might be where you ask for a cake recipe, and the
  1269. framework
  1270. /// throws a guardrail violation error.
  1271. case triggeredGuardrailUnexpectedly
  1272. /// Returns a Boolean value indicating whether two values are equal.
  1273. ///
  1274. /// Equality is the inverse of inequality. For any values `
  1275. /// `a == b` implies that `a != b` is `false`
  1276. a
  1277. ` and `b`
  1278. ,
  1279. .
  1280. ///
  1281. /// - Parameters:
  1282. /// - lhs: A value to compare.
  1283. /// - rhs: Another value to compare.
  1284. public static func == (a: LanguageModelFeedback.Issue.Category,
  1285. b: LanguageModelFeedback.Issue.Category) -> Bool
  1286. /// A type that can represent a collection of all values of this type.
  1287. @available(iOS 26.0, macOS 26.0, *)
  1288. @available(tvOS, unavailable)
  1289. @available(watchOS, unavailable)
  1290. public typealias AllCases =
  1291. [LanguageModelFeedback.Issue.Category]
  1292. /// A collection of all values of this type.
  1293. nonisolated public static var allCases:
  1294. [LanguageModelFeedback.Issue.Category] { get }
  1295. /// Hashes the essential components of this value by feeding them into the
  1296. /// given hasher.
  1297. ///
  1298. /// Implement this method to conform to the `Hashable` protocol. The
  1299. /// components used for hashing must be the same as the components compared
  1300. /// in your type's `
  1301. ==
  1302. ` operator implementation. Call `hasher.combine(_:)`
  1303. /// with each of these components.
  1304. ///
  1305. /// - Important: In your implementation of `hash(into:)`
  1306. ,
  1307. /// don't call `finalize()` on the `hasher` instance provided,
  1308. /// or replace it with a different instance.
  1309. /// Doing so may become a compile-time error in the future.
  1310. ///
  1311. /// - Parameter hasher: The hasher to use when combining the components
  1312. /// of this instance.
  1313. public func hash(into hasher: inout Hasher)
  1314. /// The hash value.
  1315. ///
  1316. /// /// ///
  1317. /// /// Hash values are not guaranteed to be equal across different executions of
  1318. your program. Do not save hash values to use during a future execution.
  1319. /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
  1320. conform to `Hashable`, implement the `hash(into:)` requirement instead.
  1321. The compiler provides an implementation for `hashValue` for you.
  1322. public var hashValue: Int { get }
  1323. }
  1324. /// Creates a new issue
  1325. ///
  1326. /// - Parameters:
  1327. /// - category: A category for this issue.
  1328. /// - explanation: An optional explanation of this issue.
  1329. public init(category: LanguageModelFeedback.Issue.Category,
  1330. explanation: String? = nil)
  1331. }
  1332. }
  1333. @available(iOS 26.0, macOS 26.0, *)
  1334. @available(tvOS, unavailable)
  1335. @available(watchOS, unavailable)
  1336. extension LanguageModelFeedback.Sentiment : Equatable {
  1337. }
  1338. @available(iOS 26.0, macOS 26.0, *)
  1339. @available(tvOS, unavailable)
  1340. @available(watchOS, unavailable)
  1341. extension LanguageModelFeedback.Sentiment : Hashable {
  1342. }
  1343. @available(iOS 26.0, macOS 26.0, *)
  1344. @available(tvOS, unavailable)
  1345. @available(watchOS, unavailable)
  1346. extension LanguageModelFeedback.Issue.Category : Equatable {
  1347. }
  1348. @available(iOS 26.0, macOS 26.0, *)
  1349. @available(tvOS, unavailable)
  1350. @available(watchOS, unavailable)
  1351. extension LanguageModelFeedback.Issue.Category : Hashable {
  1352. }
  1353. /// ///
  1354. /// /// An object that represents a session that interacts with a language model.
  1355. A session is a single context that you use to generate content with, and maintains state between
  1356. requests. You can reuse the existing instance or create a new one each time you call the model.
  1357. When
  1358. /// you create a session you can provide instructions that tells the model what its role is and provides
  1359. /// guidance on how to respond.
  1360. ///
  1361. /// ```swift
  1362. /// let session = LanguageModelSession(instructions: """
  1363. /// You are a motivational workout coach that provides quotes to
  1364. inspire \
  1365. /// and motivate athletes.
  1366. /// """
  1367. /// )
  1368. /// let prompt = "Generate a motivational quote for my next workout."
  1369. /// let response = try await session.respond(to: prompt)
  1370. /// ```
  1371. ///
  1372. /// The framework records each call to the model in a ``Transcript`` that includes all prompts and
  1373. /// responses. If your session exceeds the available context size, it throws
  1374. /// ``LanguageModelSession/GenerationError/exceededContextWindowSize(_:)``
  1375. .
  1376. @available(iOS 26.0, macOS 26.0, *)
  1377. @available(tvOS, unavailable)
  1378. @available(watchOS, unavailable)
  1379. final public class LanguageModelSession {
  1380. /// A full history of interactions, including user inputs and model responses.
  1381. final public var transcript: Transcript { get }
  1382. /// A Boolean value that indicates a response is being generated.
  1383. ///
  1384. /// - Important: Attempting to call any of the respond methods while
  1385. /// this property is `true` is a programmer error.
  1386. final public var isResponding: Bool { get }
  1387. /// Start a new session in blank slate state with string-based instructions.
  1388. ///
  1389. /// - Parameters
  1390. /// - model: The language model to use for this session.
  1391. /// - tools: Tools to make available to the model for this session.
  1392. /// - instructions: Instructions that control the model's behavior.
  1393. public convenience init(model: SystemLanguageModel = .default, tools:
  1394. [any Tool] = [], instructions: String? = nil)
  1395. /// Start a new session in blank slate state with instructions builder.
  1396. ///
  1397. /// - Parameters
  1398. /// - model: The language model to use for this session.
  1399. /// - tools: Tools to make available to the model for this session.
  1400. /// - instructions: Instructions that control the model's behavior.
  1401. public convenience init(model: SystemLanguageModel = .default, tools:
  1402. [any Tool] = [], @InstructionsBuilder instructions: () throws ->
  1403. Instructions) rethrows
  1404. /// Start a new session in blank slate state with instructions.
  1405. ///
  1406. /// - Parameters
  1407. /// - model: The language model to use for this session.
  1408. /// - tools: Tools to make available to the model for this session.
  1409. /// - instructions: Instructions that control the model's behavior.
  1410. public convenience init(model: SystemLanguageModel = .default, tools:
  1411. [any Tool] = [], instructions: Instructions? = nil)
  1412. /// Start a session by rehydrating from a transcript.
  1413. ///
  1414. /// - Parameters
  1415. /// - model: The language model to use for this session.
  1416. /// - transcript: A transcript to resume from.
  1417. /// - tools: Tools to make available to the model for this session.
  1418. public convenience init(model: SystemLanguageModel = .default, tools:
  1419. [any Tool] = [], transcript: Transcript)
  1420. /// Requests that the system eagerly load the resources required for this session into memory and
  1421. /// ///
  1422. /// optionally caches a prefix of your prompt.
  1423. This method can be useful in cases where you have a strong signal that the user will interact
  1424. with
  1425. /// session within a few seconds. For example, you might call prewarm when the user begins typing
  1426. /// into a text field.
  1427. ///
  1428. /// If you know a prefix for the future prompt, passing it to prewarm will allow the system to process
  1429. the
  1430. /// ///
  1431. /// prompt eagerly and reduce latency for the future request.
  1432. - Important: You should only use prewarm when you have a window of at least 1s before
  1433. the
  1434. /// call to `respond(to:)`
  1435. .
  1436. ///
  1437. /// - Note: Calling this method does not guarantee that the system loads your assets
  1438. immediately,
  1439. /// particularly if your app is running in the background or the system is under load.
  1440. final public func prewarm(promptPrefix: Prompt? = nil)
  1441. /// A structure that stores the output of a response call.
  1442. @available(iOS 26.0, macOS 26.0, *)
  1443. @available(tvOS, unavailable)
  1444. @available(watchOS, unavailable)
  1445. public struct Response<Content> where Content : Generable {
  1446. /// The response content.
  1447. public let content: Content
  1448. /// The raw response content.
  1449. ///
  1450. /// When `Content` is `GeneratedContent`, this is the same as `content`
  1451. public let rawContent: GeneratedContent
  1452. .
  1453. /// The list of transcript entries.
  1454. public let transcriptEntries: ArraySlice<Transcript.Entry>
  1455. }
  1456. /// Produces a response to a prompt.
  1457. ///
  1458. /// - Parameters:
  1459. /// - prompt: A prompt for the model to respond to.
  1460. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1461. the model produces.
  1462. /// - Returns: A string composed of the tokens produced by sampling model output.
  1463. @discardableResult
  1464. nonisolated(nonsending) final public func respond(to prompt: Prompt,
  1465. options: GenerationOptions = GenerationOptions()) async throws ->
  1466. LanguageModelSession.Response<String>
  1467. /// Produces a response to a prompt.
  1468. ///
  1469. /// - Parameters:
  1470. /// - prompt: A prompt for the model to respond to.
  1471. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1472. the model produces.
  1473. /// - Returns: A string composed of the tokens produced by sampling model output.
  1474. @discardableResult
  1475. nonisolated(nonsending) final public func respond(to prompt: String,
  1476. options: GenerationOptions = GenerationOptions()) async throws ->
  1477. LanguageModelSession.Response<String>
  1478. /// ///
  1479. Produces a response to a prompt.
  1480. /// - Parameters:
  1481. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1482. the model produces.
  1483. /// - prompt: A prompt for the model to respond to.
  1484. /// - Returns: A string composed of the tokens produced by sampling model output.
  1485. @discardableResult
  1486. nonisolated(nonsending) final public func respond(options:
  1487. GenerationOptions = GenerationOptions(), @PromptBuilder prompt: ()
  1488. throws -> Prompt) async throws -> LanguageModelSession.Response<String>
  1489. /// ///
  1490. /// /// Produces a generated content type as a response to a prompt and schema.
  1491. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1492. .
  1493. The exception to the rule is when the model has knowledge about the expected response
  1494. format, either
  1495. because it has been trained on it, or because it has seen exhaustive examples during this
  1496. session.
  1497. ///
  1498. /// - Parameters:
  1499. /// - prompt: A prompt for the model to respond to.
  1500. /// - schema: A schema to guide the output with.
  1501. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1502. /// - options: Options that control how tokens are sampled from the distribution the model
  1503. produces.
  1504. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1505. schema.
  1506. @discardableResult
  1507. nonisolated(nonsending) final public func respond(to prompt: Prompt,
  1508. schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options:
  1509. GenerationOptions = GenerationOptions()) async throws ->
  1510. LanguageModelSession.Response<GeneratedContent>
  1511. /// ///
  1512. /// /// Produces a generated content type as a response to a prompt and schema.
  1513. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1514. .
  1515. The exception to the rule is when the model has knowledge about the expected response
  1516. format, either
  1517. because it has been trained on it, or because it has seen exhaustive examples during this
  1518. session.
  1519. ///
  1520. /// - Parameters:
  1521. /// - prompt: A prompt for the model to respond to.
  1522. /// - schema: A schema to guide the output with.
  1523. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1524. /// - options: Options that control how tokens are sampled from the distribution the model
  1525. produces.
  1526. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1527. schema.
  1528. @discardableResult
  1529. nonisolated(nonsending) final public func respond(to prompt: String,
  1530. schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options:
  1531. GenerationOptions = GenerationOptions()) async throws ->
  1532. LanguageModelSession.Response<GeneratedContent>
  1533. /// ///
  1534. /// /// Produces a generated content type as a response to a prompt and schema.
  1535. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1536. .
  1537. The exception to the rule is when the model has knowledge about the expected response
  1538. format, either
  1539. because it has been trained on it, or because it has seen exhaustive examples during this
  1540. session.
  1541. ///
  1542. /// - Parameters:
  1543. /// - schema: A schema to guide the output with.
  1544. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1545. /// - options: Options that control how tokens are sampled from the distribution the model
  1546. produces.
  1547. /// - prompt: A prompt for the model to respond to.
  1548. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1549. schema.
  1550. @discardableResult
  1551. nonisolated(nonsending) final public func respond(schema:
  1552. GenerationSchema, includeSchemaInPrompt: Bool = true, options:
  1553. GenerationOptions = GenerationOptions(), @PromptBuilder prompt: ()
  1554. throws -> Prompt) async throws ->
  1555. LanguageModelSession.Response<GeneratedContent>
  1556. /// ///
  1557. /// /// Produces a generable object as a response to a prompt.
  1558. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1559. .
  1560. The exception to the rule is when the model has knowledge about the expected response
  1561. format, either
  1562. because it has been trained on it, or because it has seen exhaustive examples during this
  1563. session.
  1564. ///
  1565. /// - Parameters:
  1566. /// - prompt: A prompt for the model to respond to.
  1567. /// - type: A type to produce as the response.
  1568. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1569. /// - options: Options that control how tokens are sampled from the distribution the model
  1570. produces.
  1571. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1572. schema.
  1573. @discardableResult
  1574. nonisolated(nonsending) final public func respond<Content>(to prompt:
  1575. Prompt, generating type: Content.Type = Content.self,
  1576. includeSchemaInPrompt: Bool = true, options: GenerationOptions =
  1577. GenerationOptions()) async throws ->
  1578. LanguageModelSession.Response<Content> where Content : Generable
  1579. /// ///
  1580. /// /// Produces a generable object as a response to a prompt.
  1581. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1582. .
  1583. The exception to the rule is when the model has knowledge about the expected response
  1584. format, either
  1585. because it has been trained on it, or because it has seen exhaustive examples during this
  1586. session.
  1587. ///
  1588. /// - Parameters:
  1589. /// - prompt: A prompt for the model to respond to.
  1590. /// - type: A type to produce as the response.
  1591. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1592. /// - options: Options that control how tokens are sampled from the distribution the model
  1593. produces.
  1594. /// - Returns: An instance of the `Generable` type.
  1595. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1596. schema.
  1597. @discardableResult
  1598. nonisolated(nonsending) final public func respond<Content>(to prompt:
  1599. String, generating type: Content.Type = Content.self,
  1600. includeSchemaInPrompt: Bool = true, options: GenerationOptions =
  1601. GenerationOptions()) async throws ->
  1602. LanguageModelSession.Response<Content> where Content : Generable
  1603. /// ///
  1604. /// /// Produces a generable object as a response to a prompt.
  1605. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1606. .
  1607. The exception to the rule is when the model has knowledge about the expected response
  1608. format, either
  1609. because it has been trained on it, or because it has seen exhaustive examples during this
  1610. session.
  1611. ///
  1612. /// - Parameters:
  1613. /// - prompt: A prompt for the model to respond to.
  1614. /// - type: A type to produce as the response.
  1615. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1616. /// - options: Options that control how tokens are sampled from the distribution the model
  1617. produces.
  1618. /// - prompt: A prompt for the model to respond to.
  1619. /// - Returns: ``GeneratedContent`` containing the fields and values defined in the
  1620. schema.
  1621. @discardableResult
  1622. nonisolated(nonsending) final public func respond<Content>(generating
  1623. type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true,
  1624. options: GenerationOptions = GenerationOptions(), @PromptBuilder
  1625. prompt: () throws -> Prompt) async throws ->
  1626. LanguageModelSession.Response<Content> where Content : Generable
  1627. /// ///
  1628. /// /// Produces a response stream to a prompt and schema.
  1629. Consider using the default value of `true` for `includeSchemaInPrompt`
  1630. .
  1631. The exception to the rule is when the model has knowledge about the expected response
  1632. format, either
  1633. /// because it has been trained on it, or because it has seen exhaustive examples during this
  1634. session.
  1635. ///
  1636. /// - Parameters:
  1637. /// - prompt: A prompt for the model to respond to.
  1638. /// - schema: A schema to guide the output with.
  1639. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1640. /// - options: Options that control how tokens are sampled from the distribution the model
  1641. produces.
  1642. /// - Returns: A response stream that produces ``GeneratedContent`` containing the
  1643. fields and values defined in the schema.
  1644. @available(iOS 26.0, macOS 26.0, *)
  1645. @available(tvOS, unavailable)
  1646. @available(watchOS, unavailable)
  1647. final public func streamResponse(to prompt: Prompt, schema:
  1648. GenerationSchema, includeSchemaInPrompt: Bool = true, options:
  1649. GenerationOptions = GenerationOptions()) -> sending
  1650. LanguageModelSession.ResponseStream<GeneratedContent>
  1651. @objc deinit
  1652. }
  1653. @available(iOS 26.0, macOS 26.0, *)
  1654. @available(tvOS, unavailable)
  1655. @available(watchOS, unavailable)
  1656. extension LanguageModelSession : @unchecked Sendable {
  1657. }
  1658. @available(iOS 26.0, macOS 26.0, *)
  1659. @available(tvOS, unavailable)
  1660. @available(watchOS, unavailable)
  1661. extension LanguageModelSession : nonisolated Observable {
  1662. }
  1663. extension LanguageModelSession {
  1664. /// An error that occurs while generating a response.
  1665. @available(iOS 26.0, macOS 26.0, *)
  1666. @available(tvOS, unavailable)
  1667. @available(watchOS, unavailable)
  1668. public enum GenerationError : Error, LocalizedError {
  1669. /// The context in which the error occurred.
  1670. @available(iOS 26.0, macOS 26.0, *)
  1671. @available(tvOS, unavailable)
  1672. @available(watchOS, unavailable)
  1673. public struct Context : Sendable {
  1674. /// ///
  1675. /// A debug description to help developers diagnose issues during development.
  1676. This string is not localized and is not appropriate for display to end users.
  1677. public let debugDescription: String
  1678. /// Creates a context.
  1679. ///
  1680. /// - Parameters:
  1681. /// - debugDescription: The debug description to help developers diagnose
  1682. issues during development.
  1683. public init(debugDescription: String)
  1684. }
  1685. /// ///
  1686. /// A refusal produced by a language model.
  1687. Refusal errors indicate that the model chose not to respond to a prompt. To make the
  1688. model
  1689. /// explain why it refused, catch the refusal error and access one of its explanation properties.
  1690. ///
  1691. /// ```swift
  1692. /// do {
  1693. /// let session = LanguageModelSession()
  1694. /// let response = try session.respond(to: "...")
  1695. /// } catch error as
  1696. LanguageModelSession.GenerationError.refusal(let refusal, _) {
  1697. /// let message = try await refusal.explanation
  1698. /// print(message)
  1699. /// } catch {
  1700. /// print("Something went wrong: \(error)")
  1701. /// }
  1702. /// ```
  1703. @available(iOS 26.0, macOS 26.0, *)
  1704. @available(tvOS, unavailable)
  1705. @available(watchOS, unavailable)
  1706. public struct Refusal : Sendable {
  1707. public init(transcriptEntries: [Transcript.Entry])
  1708. /// An explanation for why the model refused to respond.
  1709. public var explanation: LanguageModelSession.Response<String> {
  1710. get async throws }
  1711. /// A stream containing an explanation about why the model refused to respond.
  1712. public var explanationStream:
  1713. LanguageModelSession.ResponseStream<String> { get }
  1714. }
  1715. /// ///
  1716. /// /// /// /// ///
  1717. /// /// An error that signals the session reached its context window size limit.
  1718. This error occurs when you use the available tokens for the context window of 4,096
  1719. tokens. The
  1720. token count includes instructions, prompts, and outputs for a session instance. A single
  1721. token
  1722. corresponds to approximately three to four characters in languages like English, Spanish,
  1723. or
  1724. German, and one token per character in languages like Japanese, Chinese, and Korean.
  1725. Start a new session when you exceed the content window size, and try again using a
  1726. shorter
  1727. prompt or shorter output length.
  1728. case
  1729. .Context)
  1730. exceededContextWindowSize(LanguageModelSession.GenerationError
  1731. /// ///
  1732. /// /// /// ///
  1733. /// /// An error that indicates the assets required for the session are unavailable.
  1734. This may happen if you forget to check model availability to begin with,
  1735. or if the model assets are deleted. This can happen if the user disables
  1736. AppleIntelligence while your app is running.
  1737. You may be able to recover from this error by retrying later after the
  1738. device has freed up enough space to redownload model assets.
  1739. case assetsUnavailable(LanguageModelSession.GenerationError.Context)
  1740. /// /// case
  1741. An error that indicates the system's safety guardrails are triggered by content in a
  1742. prompt or the response generated by the model.
  1743. guardrailViolation(LanguageModelSession.GenerationError.Context)
  1744. /// An error that indicates a generation guide with an unsupported pattern was used.
  1745. case unsupportedGuide(LanguageModelSession.GenerationError.Context)
  1746. /// An error that indicates an error that occurs if the model is prompted to respond in a
  1747. language
  1748. that it does not support.
  1749. /// case
  1750. .Context)
  1751. unsupportedLanguageOrLocale(LanguageModelSession.GenerationError
  1752. /// An error that indicates the session failed to deserialize a valid generable type from model
  1753. output.
  1754. ///
  1755. /// This can happen if generation was terminated early.
  1756. case decodingFailure(LanguageModelSession.GenerationError.Context)
  1757. /// ///
  1758. /// /// An error that indicates your session has been rate limited.
  1759. This error will only happen if your app is running in the background
  1760. and exceeds the system defined rate limit.
  1761. case rateLimited(LanguageModelSession.GenerationError.Context)
  1762. /// /// case
  1763. An error that happens if you attempt to make a session respond to a
  1764. second prompt while it's still responding to the first one.
  1765. concurrentRequests(LanguageModelSession.GenerationError.Context)
  1766. /// An error indicating that the model refused to answer.
  1767. ///
  1768. /// This error can happen for prompts that do not violate any guardrail policy, but
  1769. /// the model isn't able to provide the kind of response you requested. You can
  1770. /// choose to handle this error by showing a predetermined message of your choice,
  1771. /// or you can use the `Refusal` to generate an explanation from the model itself.
  1772. case refusal(LanguageModelSession.GenerationError.Refusal,
  1773. LanguageModelSession.GenerationError.Context)
  1774. /// A string representation of the error description.
  1775. public var errorDescription: String? { get }
  1776. /// A string representation of the recovery suggestion.
  1777. public var recoverySuggestion: String? { get }
  1778. /// A string representation of the failure reason.
  1779. public var failureReason: String? { get }
  1780. }
  1781. /// An error that occurs while a system language model is calling a tool.
  1782. @available(iOS 26.0, macOS 26.0, *)
  1783. @available(tvOS, unavailable)
  1784. @available(watchOS, unavailable)
  1785. public struct ToolCallError : Error, LocalizedError {
  1786. /// The tool that produced the error.
  1787. public var tool: any Tool
  1788. /// The underlying error that was thrown during a tool call.
  1789. public var underlyingError: any Error
  1790. /// Creates a tool call error
  1791. ///
  1792. /// - Parameters:
  1793. /// - tool: The tool that produced the error.
  1794. public init(tool: any Tool, underlyingError: any Error)
  1795. /// A string representation of the error description.
  1796. public var errorDescription: String? { get }
  1797. }
  1798. }
  1799. extension LanguageModelSession {
  1800. /// An async sequence of snapshots of partially generated content.
  1801. @available(iOS 26.0, macOS 26.0, *)
  1802. @available(tvOS, unavailable)
  1803. @available(watchOS, unavailable)
  1804. public struct ResponseStream<Content> where Content : Generable {
  1805. /// A snapshot of partially generated content.
  1806. public struct Snapshot {
  1807. /// The content of the response.
  1808. public var content: Content.PartiallyGenerated
  1809. /// ///
  1810. /// The raw content of the response.
  1811. When `Content` is `GeneratedContent`, this is the same as `content`
  1812. public var rawContent: GeneratedContent
  1813. .
  1814. }
  1815. }
  1816. }
  1817. @available(iOS 26.0, macOS 26.0, *)
  1818. @available(tvOS, unavailable)
  1819. @available(watchOS, unavailable)
  1820. extension LanguageModelSession {
  1821. /// ///
  1822. /// /// Produces a response stream to a prompt and schema.
  1823. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1824. .
  1825. The exception to the rule is when the model has knowledge about the expected response
  1826. format, either
  1827. because it has been trained on it, or because it has seen exhaustive examples during this
  1828. session.
  1829. ///
  1830. /// - Parameters:
  1831. /// - prompt: A prompt for the model to respond to.
  1832. /// - schema: A schema to guide the output with.
  1833. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1834. /// - options: Options that control how tokens are sampled from the distribution the model
  1835. produces.
  1836. /// - Returns: A response stream that produces ``GeneratedContent`` containing the
  1837. fields and values defined in the schema.
  1838. final public func streamResponse(to prompt: String, schema:
  1839. GenerationSchema, includeSchemaInPrompt: Bool = true, options:
  1840. GenerationOptions = GenerationOptions()) -> sending
  1841. LanguageModelSession.ResponseStream<GeneratedContent>
  1842. /// ///
  1843. /// /// Produces a response stream to a prompt and schema.
  1844. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1845. .
  1846. The exception to the rule is when the model has knowledge about the expected response
  1847. format, either
  1848. because it has been trained on it, or because it has seen exhaustive examples during this
  1849. session.
  1850. ///
  1851. /// - Parameters:
  1852. /// - schema: A schema to guide the output with.
  1853. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1854. /// - options: Options that control how tokens are sampled from the distribution the model
  1855. produces.
  1856. /// - prompt: A prompt for the model to respond to.
  1857. /// - Returns: A response stream that produces ``GeneratedContent`` containing the
  1858. fields and values defined in the schema.
  1859. final public func streamResponse(schema: GenerationSchema,
  1860. includeSchemaInPrompt: Bool = true, options: GenerationOptions =
  1861. GenerationOptions(), @PromptBuilder prompt: () throws -> Prompt)
  1862. rethrows -> sending
  1863. LanguageModelSession.ResponseStream<GeneratedContent>
  1864. /// ///
  1865. /// /// Produces a response stream to a prompt and schema.
  1866. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1867. .
  1868. The exception to the rule is when the model has knowledge about the expected response
  1869. format, either
  1870. because it has been trained on it, or because it has seen exhaustive examples during this
  1871. session.
  1872. ///
  1873. /// - Parameters:
  1874. /// - prompt: A prompt for the model to respond to.
  1875. /// - type: A type to produce as the response.
  1876. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1877. /// - options: Options that control how tokens are sampled from the distribution the model
  1878. produces.
  1879. /// - Returns: A response stream that produces ``GeneratedContent`` containing the
  1880. fields and values defined in the schema.
  1881. final public func streamResponse<Content>(to prompt: Prompt, generating
  1882. type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true,
  1883. options: GenerationOptions = GenerationOptions()) -> sending
  1884. LanguageModelSession.ResponseStream<Content> where Content : Generable
  1885. /// ///
  1886. /// /// Produces a response stream to a prompt.
  1887. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1888. .
  1889. The exception to the rule is when the model has knowledge about the expected response
  1890. format, either
  1891. because it has been trained on it, or because it has seen exhaustive examples during this
  1892. session.
  1893. ///
  1894. /// - Parameters:
  1895. /// - prompt: A prompt for the model to respond to.
  1896. /// - type: A type to produce as the response.
  1897. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1898. /// - options: Options that control how tokens are sampled from the distribution the model
  1899. produces.
  1900. /// - Returns: A response stream that produces ``GeneratedContent`` containing the
  1901. fields and values defined in the schema.
  1902. final public func streamResponse<Content>(to prompt: String, generating
  1903. type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true,
  1904. options: GenerationOptions = GenerationOptions()) -> sending
  1905. LanguageModelSession.ResponseStream<Content> where Content : Generable
  1906. /// ///
  1907. /// /// Produces a response stream for a type.
  1908. /// Consider using the default value of `true` for `includeSchemaInPrompt`
  1909. .
  1910. The exception to the rule is when the model has knowledge about the expected response
  1911. format, either
  1912. because it has been trained on it, or because it has seen exhaustive examples during this
  1913. session.
  1914. ///
  1915. /// - Parameters:
  1916. /// - type: A type to produce as the response.
  1917. /// - includeSchemaInPrompt: Inject the schema into the prompt to bias the model.
  1918. /// - options: Options that control how tokens are sampled from the distribution the model
  1919. produces.
  1920. /// - Returns: A response stream.
  1921. final public func streamResponse<Content>(generating type: Content.Type
  1922. = Content.self, includeSchemaInPrompt: Bool = true, options:
  1923. GenerationOptions = GenerationOptions(), @PromptBuilder prompt: ()
  1924. throws -> Prompt) rethrows -> sending
  1925. LanguageModelSession.ResponseStream<Content> where Content : Generable
  1926. /// Produces a response stream to a prompt.
  1927. ///
  1928. /// - Parameters:
  1929. /// - prompt: A specific prompt for the model to respond to.
  1930. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1931. the model produces.
  1932. /// - Returns: A response stream that produces aggregated tokens.
  1933. final public func streamResponse(to prompt: Prompt, options:
  1934. GenerationOptions = GenerationOptions()) -> sending
  1935. LanguageModelSession.ResponseStream<String>
  1936. /// Produces a response stream to a prompt.
  1937. ///
  1938. /// - Parameters:
  1939. /// - prompt: A specific prompt for the model to respond to.
  1940. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1941. the model produces.
  1942. /// - Returns: A response stream that produces aggregated tokens.
  1943. final public func streamResponse(to prompt: String, options:
  1944. GenerationOptions = GenerationOptions()) -> sending
  1945. LanguageModelSession.ResponseStream<String>
  1946. /// Produces a response stream to a prompt.
  1947. ///
  1948. /// - Parameters:
  1949. /// - options: GenerationOptions that control how tokens are sampled from the distribution
  1950. the model produces.
  1951. /// - prompt: A specific prompt for the model to respond to.
  1952. /// - Returns: A response stream that produces aggregated tokens.
  1953. final public func streamResponse(options: GenerationOptions =
  1954. GenerationOptions(), @PromptBuilder prompt: () throws -> Prompt)
  1955. rethrows -> sending LanguageModelSession.ResponseStream<String>
  1956. }
  1957. extension LanguageModelSession {
  1958. /// Logs and serializes a feedback attachment that can be submitted to Apple.
  1959. ///
  1960. /// /// This method creates a structured feedback attachment containing the session's transcript
  1961. and any provided feedback information. The attachment can be saved to a file and submitted
  1962. /// to Apple using [Feedback Assistant](https://feedbackassistant.apple.com).
  1963. ///
  1964. /// /// If an error occurred during a previous response, any rejected entries that were rolled
  1965. back from the transcript are included in the feedback data.
  1966. ///
  1967. /// - Parameters:
  1968. /// - sentiment: An optional sentiment rating about the model's output (positive, negative,
  1969. or neutral).
  1970. /// - issues: An array of specific issues identified with the model's response. Defaults to an
  1971. empty array.
  1972. /// - desiredOutput: An optional transcript entry showing what the desired output should
  1973. have been.
  1974. /// - Returns: A `Data` object containing the JSON-encoded feedback attachment that can
  1975. be submitted to Feedback Assistant.
  1976. ///
  1977. /// ## Usage Example
  1978. /// ```swift
  1979. /// let session = LanguageModelSession()
  1980. /// let response = try await session.respond(to: "What is the capital
  1981. of France?")
  1982. ///
  1983. /// // Create feedback for a helpful response
  1984. /// let feedbackData = session.logFeedbackAttachment(sentiment:
  1985. .positive)
  1986. ///
  1987. /// // Or create feedback for a problematic response
  1988. /// let feedbackData = session.logFeedbackAttachment(
  1989. /// sentiment: .negative,
  1990. /// issues: [
  1991. /// LanguageModelFeedback.Issue(
  1992. /// category: .incorrect,
  1993. /// explanation: "The model provided outdated information"
  1994. /// )
  1995. /// ],
  1996. /// desiredOutput: Transcript.Entry.response(...)
  1997. /// )
  1998. /// ```
  1999. @available(iOS 26.0, macOS 26.0, *)
  2000. @available(tvOS, unavailable)
  2001. @available(watchOS, unavailable)
  2002. @discardableResult
  2003. final public func logFeedbackAttachment(sentiment:
  2004. LanguageModelFeedback.Sentiment?, issues:
  2005. [LanguageModelFeedback.Issue] = [], desiredOutput: Transcript.Entry? =
  2006. nil) -> Data
  2007. }
  2008. @available(iOS 26.0, macOS 26.0, *)
  2009. @available(tvOS, unavailable)
  2010. @available(watchOS, unavailable)
  2011. extension LanguageModelSession.ResponseStream : AsyncSequence {
  2012. /// The type of element produced by this asynchronous sequence.
  2013. public typealias Element =
  2014. LanguageModelSession.ResponseStream<Content>.Snapshot
  2015. /// The type of asynchronous iterator that produces elements of this
  2016. /// asynchronous sequence.
  2017. @available(iOS 26.0, macOS 26.0, *)
  2018. @available(tvOS, unavailable)
  2019. @available(watchOS, unavailable)
  2020. public struct AsyncIterator : AsyncIteratorProtocol {
  2021. /// /// ///
  2022. /// Asynchronously advances to the next element and returns it, or ends the
  2023. sequence if there is no next element.
  2024. - Returns: /// the sequence.
  2025. The next element, if it exists, or `nil` to signal the end of
  2026. public mutating func next(isolation actor: isolated (any Actor)? =
  2027. #isolation) async throws ->
  2028. LanguageModelSession.ResponseStream<Content>.Snapshot?
  2029. @available(iOS 26.0, macOS 26.0, *)
  2030. @available(tvOS, unavailable)
  2031. @available(watchOS, unavailable)
  2032. public typealias Element =
  2033. LanguageModelSession.ResponseStream<Content>.Snapshot
  2034. }
  2035. /// Creates the asynchronous iterator that produces elements of this
  2036. /// asynchronous sequence.
  2037. ///
  2038. /// - Returns: An instance of the `AsyncIterator` type used to produce
  2039. /// elements of the asynchronous sequence.
  2040. public func makeAsyncIterator() ->
  2041. LanguageModelSession.ResponseStream<Content>.AsyncIterator
  2042. /// The result from a streaming response, after it completes.
  2043. ///
  2044. /// /// If the streaming response was finished successfully before calling
  2045. `collect()`, this method `Response` returns immediately.
  2046. ///
  2047. /// If the streaming response was finished with an error before calling
  2048. /// `collect()`, this method propagates that error.
  2049. nonisolated(nonsending) public func collect() async throws -> sending
  2050. LanguageModelSession.Response<Content>
  2051. }
  2052. /// ///
  2053. /// /// ///
  2054. /// ```swift
  2055. /// ```
  2056. ///
  2057. /// A prompt from a person to the model.
  2058. Prompts can contain content written by you, an outside source, or input directly from people using
  2059. your app. You can initialize a `Prompt` from a string literal:
  2060. /// let prompt = Prompt("What are miniature schnauzers known for?")
  2061. Use ``PromptBuilder`` to dynamically control the prompt's content based on your app's state.
  2062. The
  2063. /// code below shows if the Boolean is `true`, the prompt includes a second line of text:
  2064. ///
  2065. /// ```swift
  2066. /// let responseShouldRhyme = true
  2067. /// let prompt = Prompt {
  2068. /// "Answer the following question from the user: \(userInput)"
  2069. /// if responseShouldRhyme {
  2070. /// "Your response MUST rhyme!"
  2071. /// }
  2072. /// }
  2073. /// ```
  2074. ///
  2075. /// /// If your prompt includes input from people, consider wrapping the input in a string template with your
  2076. own prompt to better steer the model's response. For more information on handling inputs in your
  2077. /// prompts, see <doc:improving-safety-from-generative-model-output>.
  2078. @available(iOS 26.0, macOS 26.0, *)
  2079. @available(tvOS, unavailable)
  2080. @available(watchOS, unavailable)
  2081. public struct Prompt : Sendable {
  2082. /// public init(
  2083. Creates an instance with the content you specify.
  2084. _
  2085. content: some PromptRepresentable)
  2086. }
  2087. @available(iOS 26.0, macOS 26.0, *)
  2088. @available(tvOS, unavailable)
  2089. @available(watchOS, unavailable)
  2090. extension Prompt : PromptRepresentable {
  2091. /// An instance that represents a prompt.
  2092. public var promptRepresentation: Prompt { get }
  2093. }
  2094. @available(iOS 26.0, macOS 26.0, *)
  2095. @available(tvOS, unavailable)
  2096. @available(watchOS, unavailable)
  2097. extension Prompt {
  2098. public init(@PromptBuilder _
  2099. content: () throws -> Prompt) rethrows
  2100. }
  2101. /// A type that represents a prompt builder.
  2102. @available(iOS 26.0, macOS 26.0, *)
  2103. @available(tvOS, unavailable)
  2104. @available(watchOS, unavailable)
  2105. @resultBuilder public struct PromptBuilder {
  2106. /// Creates a builder with the a block.
  2107. public static func buildBlock<each P>(
  2108. Prompt where repeat each P : PromptRepresentable
  2109. _ components: repeat each P) ->
  2110. /// Creates a builder with the an array of prompts.
  2111. public static func buildArray(
  2112. Prompt
  2113. _ prompts: [some PromptRepresentable]) ->
  2114. /// Creates a builder with the first component.
  2115. public static func buildEither(first component: some
  2116. PromptRepresentable) -> Prompt
  2117. /// Creates a builder with the second component.
  2118. public static func buildEither(second component: some
  2119. PromptRepresentable) -> Prompt
  2120. /// Creates a builder with an optional component.
  2121. public static func buildOptional(
  2122. _ component: Prompt?) -> Prompt
  2123. /// Creates a builder with a limited availability prompt.
  2124. public static func buildLimitedAvailability(
  2125. PromptRepresentable) -> Prompt
  2126. _ prompt: some
  2127. /// Creates a builder with an expression.
  2128. public static func buildExpression<P>(
  2129. PromptRepresentable
  2130. _ expression: P) -> P where P :
  2131. /// Creates a builder with a prompt expression.
  2132. public static func buildExpression(
  2133. _ expression: Prompt) -> Prompt
  2134. }
  2135. /// A protocol that represents a prompt.
  2136. @available(iOS 26.0, macOS 26.0, *)
  2137. @available(tvOS, unavailable)
  2138. @available(watchOS, unavailable)
  2139. public protocol PromptRepresentable {
  2140. /// An instance that represents a prompt.
  2141. @PromptBuilder var promptRepresentation: Prompt { get }
  2142. }
  2143. /// ///
  2144. /// /// An on-device large language model capable of text generation tasks.
  2145. The `SystemLanguageModel` refers to the on-device text foundation model that powers Apple
  2146. Intelligence. Use ``default`` to access the base version of the model and perform
  2147. general-purpose
  2148. /// text generation tasks. To access a specialized version of the model, initialize the model
  2149. /// with ``UseCase`` to perform tasks like ``UseCase/contentTagging``
  2150. .
  2151. ///
  2152. /// Verify the model availability before you use the model. Model availability depends on device factors
  2153. like:
  2154. ///
  2155. /// ///
  2156. /// /// * The device must support Apple Intelligence.
  2157. * Apple Intelligence must be turned on in Settings.
  2158. Use ``Availability`` to change what your app shows to people based on the availability
  2159. condition:
  2160. ///
  2161. /// ```swift
  2162. /// struct GenerativeView: View {
  2163. /// // Create a reference to the system language model.
  2164. /// private var model = SystemLanguageModel.default
  2165. ///
  2166. /// var body: some View {
  2167. /// switch model.availability {
  2168. /// case .available:
  2169. /// // Show your intelligence UI.
  2170. /// case .unavailable(.deviceNotEligible):
  2171. /// // Show an alternative UI.
  2172. /// case .unavailable(.appleIntelligenceNotEnabled):
  2173. /// // Ask the person to turn on Apple Intelligence.
  2174. /// case .unavailable(.modelNotReady):
  2175. /// // The model isn't ready because it's downloading or because
  2176. /// // of other system reasons.
  2177. /// case .unavailable(let other):
  2178. /// // The model is unavailable for an unknown reason.
  2179. /// }
  2180. /// }
  2181. /// }
  2182. /// ```
  2183. @available(iOS 26.0, macOS 26.0, *)
  2184. @available(tvOS, unavailable)
  2185. @available(watchOS, unavailable)
  2186. final public class SystemLanguageModel : Sendable {
  2187. /// The availability of the language model.
  2188. final public var availability: SystemLanguageModel.Availability { get }
  2189. /// A convenience getter to check if the system is entirely ready.
  2190. final public var isAvailable: Bool { get }
  2191. /// A type that represents the use case for prompting.
  2192. @available(iOS 26.0, macOS 26.0, *)
  2193. @available(tvOS, unavailable)
  2194. @available(watchOS, unavailable)
  2195. public struct UseCase : Sendable, Equatable {
  2196. /// ///
  2197. /// /// A use case for general prompting.
  2198. This is the default use case for the base version of the model, so if you use
  2199. `SystemLanguageModel.default`, you don't need to specify a use case.
  2200. public static let general: SystemLanguageModel.UseCase
  2201. /// ///
  2202. /// A use case for content tagging.
  2203. Content tagging produces a list of categorizing tags based on the input prompt. When
  2204. specializing
  2205. /// the model for the `contentTagging` use case, it always responds with tags. The
  2206. tagging
  2207. /// /// capabilities of the model include detecting topics, emotions, actions, and objects. For more
  2208. information about content tagging, see
  2209. <doc:categorizing-and-organizing-data-with-content-tags>.
  2210. public static let contentTagging: SystemLanguageModel.UseCase
  2211. /// Returns a Boolean value indicating whether two values are equal.
  2212. ///
  2213. /// Equality is the inverse of inequality. For any values `
  2214. /// `a == b` implies that `a != b` is `false`
  2215. a
  2216. ` and `b`
  2217. ,
  2218. .
  2219. ///
  2220. /// - Parameters:
  2221. /// - lhs: A value to compare.
  2222. /// - rhs: Another value to compare.
  2223. public static func == (a: SystemLanguageModel.UseCase, b:
  2224. SystemLanguageModel.UseCase) -> Bool
  2225. }
  2226. @objc deinit
  2227. }
  2228. @available(iOS 26.0, macOS 26.0, *)
  2229. @available(tvOS, unavailable)
  2230. @available(watchOS, unavailable)
  2231. extension SystemLanguageModel {
  2232. /// Controls the built-in safety guardrails for prompt and response filtering.
  2233. @available(iOS 26.0, macOS 26.0, *)
  2234. @available(tvOS, unavailable)
  2235. @available(watchOS, unavailable)
  2236. public struct Guardrails : Sendable {
  2237. /// Default guardrails. This mode ensures that unsafe content in prompts and responses will
  2238. be
  2239. /// blocked with a
  2240. /// error.
  2241. `LanguageModelSession.GenerationError.guardrailViolation`
  2242. public static let `default`: SystemLanguageModel.Guardrails
  2243. /// /// ///
  2244. /// /// Guardrails that allow for permissively transforming text input, including
  2245. potentially unsafe content, to text responses, such as summarizing an article.
  2246. In this mode, requests you make to the model that generate a `String` will not throw
  2247. /// `LanguageModelSession.GenerationError.guardrailViolation`
  2248. errors.
  2249. However, when the purpose of your instructions and prompts is not transforming user
  2250. input,
  2251. /// the model may still refuse to respond to potentially unsafe prompts by generating an
  2252. /// explanation.
  2253. ///
  2254. /// When you generate responses other than `String`, this mode behaves the same way
  2255. as `.default`
  2256. .
  2257. public static let permissiveContentTransformations:
  2258. SystemLanguageModel.Guardrails
  2259. }
  2260. }
  2261. @available(iOS 26.0, macOS 26.0, *)
  2262. @available(tvOS, unavailable)
  2263. @available(watchOS, unavailable)
  2264. extension SystemLanguageModel {
  2265. /// The availability status for a specific system language model.
  2266. @available(iOS 26.0, macOS 26.0, *)
  2267. @available(tvOS, unavailable)
  2268. @available(watchOS, unavailable)
  2269. @frozen public enum Availability : Equatable, Sendable {
  2270. /// The unavailable reason.
  2271. @available(iOS 26.0, macOS 26.0, *)
  2272. @available(tvOS, unavailable)
  2273. @available(watchOS, unavailable)
  2274. public enum UnavailableReason : Equatable, Sendable {
  2275. /// The device does not support Apple Intelligence.
  2276. case deviceNotEligible
  2277. /// Apple Intelligence is not enabled on the system.
  2278. case appleIntelligenceNotEnabled
  2279. /// ///
  2280. /// /// The model(s) aren't available on the user's device.
  2281. Models are downloaded automatically based on factors
  2282. like network status, battery level, and system load.
  2283. case modelNotReady
  2284. /// Returns a Boolean value indicating whether two values are equal.
  2285. ///
  2286. /// Equality is the inverse of inequality. For any values `
  2287. /// `a == b` implies that `a != b` is `false`
  2288. a
  2289. ` and `b`
  2290. ,
  2291. .
  2292. ///
  2293. /// - Parameters:
  2294. /// - lhs: A value to compare.
  2295. /// - rhs: Another value to compare.
  2296. public static func == (a:
  2297. SystemLanguageModel.Availability.UnavailableReason, b:
  2298. SystemLanguageModel.Availability.UnavailableReason) -> Bool
  2299. /// Hashes the essential components of this value by feeding them into the
  2300. /// given hasher.
  2301. ///
  2302. /// Implement this method to conform to the `Hashable` protocol. The
  2303. /// components used for hashing must be the same as the components compared
  2304. /// in your type's `
  2305. ==
  2306. ` operator implementation. Call `hasher.combine(_:)`
  2307. /// with each of these components.
  2308. ///
  2309. /// - Important: In your implementation of `hash(into:)`
  2310. ,
  2311. /// don't call `finalize()` on the `hasher` instance provided,
  2312. /// or replace it with a different instance.
  2313. /// Doing so may become a compile-time error in the future.
  2314. ///
  2315. /// - Parameter hasher: The hasher to use when combining the components
  2316. /// of this instance.
  2317. public func hash(into hasher: inout Hasher)
  2318. /// The hash value.
  2319. ///
  2320. /// /// ///
  2321. /// /// Hash values are not guaranteed to be equal across different executions of
  2322. your program. Do not save hash values to use during a future execution.
  2323. /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
  2324. conform to `Hashable`, implement the `hash(into:)` requirement instead.
  2325. The compiler provides an implementation for `hashValue` for you.
  2326. public var hashValue: Int { get }
  2327. }
  2328. /// The system is ready for making requests.
  2329. case available
  2330. /// Indicates that the system is not ready for requests.
  2331. case unavailable(SystemLanguageModel.Availability.UnavailableReason)
  2332. /// ///
  2333. /// Returns a Boolean value indicating whether two values are equal.
  2334. Equality is the inverse of inequality. For any values `
  2335. a
  2336. ` and `b`
  2337. ,
  2338. /// `a == b` implies that `a != b` is `false`
  2339. .
  2340. ///
  2341. /// - Parameters:
  2342. /// - lhs: A value to compare.
  2343. /// - rhs: Another value to compare.
  2344. public static func == (a: SystemLanguageModel.Availability, b:
  2345. SystemLanguageModel.Availability) -> Bool
  2346. }
  2347. /// The base version of the model.
  2348. ///
  2349. /// The base model is a generic model that is useful for a
  2350. /// wide variety of applications, but is not specialized to
  2351. /// any particular use case.
  2352. @available(iOS 26.0, macOS 26.0, *)
  2353. @available(tvOS, unavailable)
  2354. @available(watchOS, unavailable)
  2355. public static let `default`: SystemLanguageModel
  2356. /// Creates a system language model for a specific use case.
  2357. @available(iOS 26.0, macOS 26.0, *)
  2358. @available(tvOS, unavailable)
  2359. @available(watchOS, unavailable)
  2360. public convenience init(useCase: SystemLanguageModel.UseCase =
  2361. .general, guardrails: SystemLanguageModel.Guardrails =
  2362. Guardrails.default)
  2363. /// Creates the base version of the model with an adapter.
  2364. @available(iOS 26.0, macOS 26.0, *)
  2365. @available(tvOS, unavailable)
  2366. @available(watchOS, unavailable)
  2367. public convenience init(adapter: SystemLanguageModel.Adapter,
  2368. guardrails: SystemLanguageModel.Guardrails = .default)
  2369. /// ///
  2370. /// Languages that the model supports.
  2371. To check if a given locale is considered supported by the model, use
  2372. `supportsLocale(_:)`, which will also take into consideration language fallbacks.
  2373. final public var supportedLanguages: Set<Locale.Language> { get }
  2374. /// ///
  2375. /// Returns a Boolean indicating whether the given locale is supported by the model.
  2376. Use this method over `supportedLanguages` to check whether the given locale qualifies a
  2377. user for using this model, as this method will take into consideration language fallbacks.
  2378. final public func supportsLocale(
  2379. locale: Locale = Locale.current) ->
  2380. _
  2381. Bool
  2382. }
  2383. @available(iOS 26.0, macOS 26.0, *)
  2384. @available(tvOS, unavailable)
  2385. @available(watchOS, unavailable)
  2386. extension SystemLanguageModel : nonisolated Observable {
  2387. }
  2388. @available(iOS 26.0, macOS 26.0, *)
  2389. @available(tvOS, unavailable)
  2390. @available(watchOS, unavailable)
  2391. extension SystemLanguageModel {
  2392. /// A type that represents an adapter for a language model.
  2393. @available(iOS 26.0, macOS 26.0, *)
  2394. @available(tvOS, unavailable)
  2395. @available(watchOS, unavailable)
  2396. public struct Adapter {
  2397. /// Values read from the creator defined field of the adapter's metadata.
  2398. public var creatorDefinedMetadata: [String : Any] { get }
  2399. }
  2400. }
  2401. /// ///
  2402. /// /// /// Specializes the system language model for custom use cases.
  2403. Use the base system model for most prompt engineering, guided generation, and tools. If you need to
  2404. specialize the model, train a custom `Adapter` to alter the system model weights and optimize it for
  2405. your custom task. Use custom adapters only if you're comfortable training foundation models in
  2406. Python.
  2407. ///
  2408. /// > Important: You need to re-train an adapter for every new version of the base system model that
  2409. /// Apple releases. Adapters consume a large amount of storage space and isn't recommended for
  2410. /// most apps.
  2411. ///
  2412. /// For more on custom adapters, see [Get started with Foundation Models adapter
  2413. training](https://developer.apple
  2414. .com/apple-intelligence/foundation-models-adapter/).
  2415. @available(iOS 26.0, macOS 26.0, *)
  2416. @available(tvOS, unavailable)
  2417. @available(watchOS, unavailable)
  2418. extension SystemLanguageModel.Adapter {
  2419. /// Creates an adapter from the file URL.
  2420. ///
  2421. /// - Throws: An error of `AssetLoadingError` type when `fileURL`
  2422. /// is invalid.
  2423. public init(fileURL: URL) throws
  2424. /// Creates an adapter downloaded from the background assets framework.
  2425. ///
  2426. /// - Throws: An error of `AssetLoadingError` type when there are
  2427. /// no compatible asset packs with this adapter name downloaded.
  2428. public init(name: String) throws
  2429. /// /// Prepares an adapter before being used with a `LanguageModelSession`
  2430. You should call this if your adapter has a draft model.
  2431. public func compile() async throws
  2432. .
  2433. /// Get all compatible adapter identifiers compatible with current system models.
  2434. ///
  2435. /// - Parameters:
  2436. /// - adapterName: Name of the adapter.
  2437. ///
  2438. /// - Returns: All adapter identifiers compatible with current system models, listed in
  2439. /// /// descending
  2440. order in terms of system preference. You can determine which asset pack or on-demand
  2441. resource to download with compatible adapter identifiers.
  2442. ///
  2443. /// On devices that support Apple Intelligence, the result is guaranteed to be non-empty.
  2444. @available(iOS 26.0, macOS 26.0, *)
  2445. @available(tvOS, unavailable)
  2446. @available(watchOS, unavailable)
  2447. public static func compatibleAdapterIdentifiers(name: String) ->
  2448. [String]
  2449. /// Remove all obsolete adapters that are no longer compatible with current system models.
  2450. public static func removeObsoleteAdapters() throws
  2451. /// /// ///
  2452. /// /// Returns a Boolean value that indicates whether an asset pack is an on-device foundation model
  2453. adapter and is compatible with the system base model version on the runtime device.
  2454. Use this check when choosing an adapter asset pack to download. This check only validates the
  2455. asset pack name and metadata, so initializing the adapter with ``Adapter/init(name:)``
  2456. /// --- or
  2457. loading the adapter onto the base model with
  2458. ``SystemLanguageModel/init(adapter:)``
  2459. ---
  2460. may throw errors if the adapter has a compatibility issue despite having correct metadata.
  2461. /// ///
  2462. /// > Note: Run this check before you download an adapter asset pack to confirm if it's usable on
  2463. the
  2464. /// runtime device.
  2465. public static func isCompatible(
  2466. assetPack: AssetPack) -> Bool
  2467. _
  2468. }
  2469. @available(iOS 26.0, macOS 26.0, *)
  2470. @available(watchOS, unavailable)
  2471. @available(tvOS, unavailable)
  2472. extension SystemLanguageModel.Adapter {
  2473. @available(iOS 26.0, macOS 26.0, *)
  2474. @available(watchOS, unavailable)
  2475. @available(tvOS, unavailable)
  2476. public enum AssetError : Error, LocalizedError {
  2477. /// The context in which the error occurred.
  2478. @available(iOS 26.0, macOS 26.0, *)
  2479. @available(tvOS, unavailable)
  2480. @available(watchOS, unavailable)
  2481. public struct Context : Sendable {
  2482. /// ///
  2483. /// A debug description to help developers diagnose issues during development.
  2484. This string is not localized and is not appropriate for display to end users.
  2485. public let debugDescription: String
  2486. public init(debugDescription: String)
  2487. }
  2488. /// An error that happens if the provided asset files are invalid.
  2489. case invalidAsset(SystemLanguageModel.Adapter.AssetError.Context)
  2490. /// case
  2491. An error that happens if the provided adapter name is invalid.
  2492. invalidAdapterName(SystemLanguageModel.Adapter.AssetError.Context)
  2493. /// An error that happens if there are no compatible adapters for the current system base
  2494. model.
  2495. case
  2496. .Context)
  2497. compatibleAdapterNotFound(SystemLanguageModel.Adapter.AssetError
  2498. /// A string representation of the error description.
  2499. public var errorDescription: String? { get }
  2500. /// A localized message describing how one might recover from the failure.
  2501. public var recoverySuggestion: String? { get }
  2502. }
  2503. }
  2504. @available(iOS 26.0, macOS 26.0, *)
  2505. @available(tvOS, unavailable)
  2506. @available(watchOS, unavailable)
  2507. extension SystemLanguageModel.Availability.UnavailableReason : Hashable {
  2508. }
  2509. /// A tool that a model can call to gather information at runtime or perform side effects.
  2510. ///
  2511. /// /// /// /// Tool calling gives the model the ability to call your code to incorporate
  2512. up-to-date information like recent events and data from your app. A tool
  2513. includes a name and a description that the framework puts in the prompt to let
  2514. the model decide when and how often to call your tool.
  2515. ///
  2516. /// /// /// /// A `Tool` defines a ``call(arguments:)`` method that takes arguments that conforms to
  2517. ``ConvertibleFromGeneratedContent``, and returns an output of any type that conforms to
  2518. ``PromptRepresentable``, allowing the model to understand and reason about in subsequent
  2519. interactions. Typically, ``Output`` is a `String`
  2520. or any ``Generable`` types.
  2521. ///
  2522. /// ```swift
  2523. /// struct FindContacts: Tool {
  2524. /// let name = "findContacts"
  2525. /// let description = "Finds a specific number of contacts"
  2526. ///
  2527. /// @Generable
  2528. /// struct Arguments {
  2529. /// @Guide(description: "The number of contacts to get",
  2530. .range(1...10))
  2531. /// let count: Int
  2532. /// }
  2533. ///
  2534. /// /// /// /// func call(arguments: Arguments) async throws -> [String] {
  2535. var contacts: [CNContact] = []
  2536. // Fetch a number of contacts using the arguments.
  2537. let formattedContacts = contacts.map {
  2538. /// "\($0.givenName) \($0.familyName)"
  2539. /// }
  2540. /// return formattedContacts
  2541. /// }
  2542. /// }
  2543. /// ```
  2544. ///
  2545. /// /// /// Tools must conform to <doc://com.apple.documentation/documentation/swift/sendable>
  2546. so the framework can run them concurrently. If the model needs to pass the output
  2547. of one tool as the input to another, it executes back-to-back tool calls.
  2548. ///
  2549. /// You control the life cycle of your tool, so you can track the state of it between
  2550. /// calls to the model. For example, you might store a list of database records that
  2551. /// you don't want to reuse between tool calls.
  2552. @available(iOS 26.0, macOS 26.0, *)
  2553. @available(tvOS, unavailable)
  2554. @available(watchOS, unavailable)
  2555. public protocol Tool<Arguments, Output> : Sendable {
  2556. /// /// interactions.
  2557. ///
  2558. /// The output that this tool produces for the language model to reason about in subsequent
  2559. Typically output is either a ``String``
  2560. associatedtype Output : PromptRepresentable
  2561. or a ``Generable`` type.
  2562. /// ///
  2563. /// The arguments that this tool should accept.
  2564. Typically arguments are either a ``Generable`` type or ``GeneratedContent``
  2565. associatedtype Arguments : ConvertibleFromGeneratedContent
  2566. .
  2567. /// A unique name for the tool, such as "get_weather", "toggleDarkMode", or "search contacts".
  2568. var name: String { get }
  2569. /// A natural language description of when and how to use the tool.
  2570. var description: String { get }
  2571. /// A schema for the parameters this tool accepts.
  2572. var parameters: GenerationSchema { get }
  2573. /// /// ///
  2574. /// ///
  2575. /// /// If true, the model's name, description, and parameters schema will be injected
  2576. into the instructions of sessions that leverage this tool.
  2577. The default implementation is `true`
  2578. - Note: This should only be `false` if the model has been trained to have
  2579. innate knowledge of this tool. For zero-shot prompting, it should always be `true`
  2580. var includesSchemaInInstructions: Bool { get }
  2581. .
  2582. /// ///
  2583. /// /// ///
  2584. /// A language model will call this method when it wants to leverage this tool.
  2585. If errors are throw in the body of this method, they will be wrapped in a
  2586. ``LanguageModelSession.ToolCallError`` and rethrown at the call site
  2587. /// of ``LanguageModelSession.respond(to:)``
  2588. .
  2589. - Note: This method may be invoked concurrently with itself or with other tools.
  2590. func call(arguments: Self.Arguments) async throws -> Self.Output
  2591. }
  2592. @available(iOS 26.0, macOS 26.0, *)
  2593. @available(tvOS, unavailable)
  2594. @available(watchOS, unavailable)
  2595. extension Tool {
  2596. /// A unique name for the tool, such as "get_weather", "toggleDarkMode", or "search contacts".
  2597. public var name: String { get }
  2598. /// /// ///
  2599. /// ///
  2600. /// /// If true, the model's name, description, and parameters schema will be injected
  2601. into the instructions of sessions that leverage this tool.
  2602. The default implementation is `true`
  2603. - Note: This should only be `false` if the model has been trained to have
  2604. innate knowledge of this tool. For zero-shot prompting, it should always be `true`
  2605. public var includesSchemaInInstructions: Bool { get }
  2606. .
  2607. }
  2608. @available(iOS 26.0, macOS 26.0, *)
  2609. @available(tvOS, unavailable)
  2610. @available(watchOS, unavailable)
  2611. extension Tool where Self.Arguments : Generable {
  2612. /// A schema for the parameters this tool accepts.
  2613. public var parameters: GenerationSchema { get }
  2614. }
  2615. /// A transcript that documents interactions with a language model.
  2616. /// Transcripts contain an ordered list of entries, representing inputs to
  2617. /// and outputs from the model.
  2618. @available(iOS 26.0, macOS 26.0, *)
  2619. @available(tvOS, unavailable)
  2620. @available(watchOS, unavailable)
  2621. public struct Transcript : Sendable, Equatable, RandomAccessCollection {
  2622. /// A type that represents a position in the collection.
  2623. ///
  2624. /// Valid indices consist of the position of every element and a
  2625. /// "past the end" position that's not valid for use as a subscript
  2626. /// argument.
  2627. public typealias Index = Int
  2628. /// ///
  2629. /// /// ///
  2630. /// Accesses the element at the specified position.
  2631. The following example accesses an element of an array through its
  2632. subscript to print its value:
  2633. var streets = ["Adams", "Bryant", "Channing", "Douglas",
  2634. "Evarts"]
  2635. /// print(streets[1])
  2636. /// // Prints "Bryant"
  2637. ///
  2638. /// You can subscript a collection with any valid index other than the
  2639. /// collection's end index. The end index refers to the position one past
  2640. /// the last element of a collection, so it doesn't correspond with an
  2641. /// element.
  2642. ///
  2643. /// - Parameter position: The position of the element to access. `position`
  2644. /// must be a valid index of the collection that is not equal to the
  2645. /// `endIndex` property.
  2646. ///
  2647. /// - Complexity: O(1)
  2648. public subscript(index: Transcript.Index) -> Transcript.Entry
  2649. /// ///
  2650. /// The position of the first element in a nonempty collection.
  2651. If the collection is empty, `startIndex` is equal to `endIndex`
  2652. public var startIndex: Int { get }
  2653. .
  2654. /// /// The collection's "past the end" position---that is, the position one
  2655. greater than the last valid subscript argument.
  2656. ///
  2657. /// /// ..<
  2658. /// /// When you need a range that includes the last element of a collection, use
  2659. the half-open range operator (`
  2660. ..<
  2661. `) with `endIndex`. The `
  2662. creates a range that doesn't include the upper bound, so it's always
  2663. safe to use with `endIndex`. For example:
  2664. ///
  2665. /// let numbers = [10, 20, 30, 40, 50]
  2666. /// if let index = numbers.firstIndex(of: 30) {
  2667. /// print(numbers[index ..< numbers.endIndex])
  2668. /// }
  2669. /// // Prints "[30, 40, 50]"
  2670. ///
  2671. /// .
  2672. ` operator
  2673. If the collection is empty, `endIndex` is equal to `startIndex`
  2674. public var endIndex: Int { get }
  2675. /// Creates a transcript.
  2676. ///
  2677. /// - Parameters:
  2678. /// - entries: An array of entries to seed the transcript.
  2679. public init(entries: some Sequence<Transcript.Entry> = [])
  2680. /// An entry in a transcript.
  2681. @available(iOS 26.0, macOS 26.0, *)
  2682. @available(tvOS, unavailable)
  2683. @available(watchOS, unavailable)
  2684. public enum Entry : Sendable, Identifiable, Equatable {
  2685. /// Instructions, typically provided by you, the developer.
  2686. case instructions(Transcript.Instructions)
  2687. /// A prompt, typically sourced from an end user.
  2688. case prompt(Transcript.Prompt)
  2689. /// A tool call containing a tool name and the arguments to invoke it with.
  2690. case toolCalls(Transcript.ToolCalls)
  2691. /// An tool output provided back to the model.
  2692. case toolOutput(Transcript.ToolOutput)
  2693. /// A response from the model.
  2694. case response(Transcript.Response)
  2695. /// The stable identity of the entity associated with this instance.
  2696. public var id: String { get }
  2697. /// Returns a Boolean value indicating whether two values are equal.
  2698. ///
  2699. /// Equality is the inverse of inequality. For any values `
  2700. /// `a == b` implies that `a != b` is `false`
  2701. a
  2702. ` and `b`
  2703. ,
  2704. .
  2705. ///
  2706. /// - Parameters:
  2707. /// - lhs: A value to compare.
  2708. /// - rhs: Another value to compare.
  2709. public static func == (a: Transcript.Entry, b: Transcript.Entry) ->
  2710. Bool
  2711. /// A type representing the stable identity of the entity associated with
  2712. /// an instance.
  2713. @available(iOS 26.0, macOS 26.0, *)
  2714. @available(tvOS, unavailable)
  2715. @available(watchOS, unavailable)
  2716. public typealias ID = String
  2717. }
  2718. /// The types of segments that may be included in a transcript entry.
  2719. @available(iOS 26.0, macOS 26.0, *)
  2720. @available(tvOS, unavailable)
  2721. @available(watchOS, unavailable)
  2722. public enum Segment : Sendable, Identifiable, Equatable {
  2723. /// A segment containing text.
  2724. case text(Transcript.TextSegment)
  2725. /// A segment containing structured content
  2726. case structure(Transcript.StructuredSegment)
  2727. /// The stable identity of the entity associated with this instance.
  2728. public var id: String { get }
  2729. /// Returns a Boolean value indicating whether two values are equal.
  2730. ///
  2731. /// Equality is the inverse of inequality. For any values `
  2732. /// `a == b` implies that `a != b` is `false`
  2733. a
  2734. ` and `b`
  2735. ,
  2736. .
  2737. ///
  2738. /// - Parameters:
  2739. /// - lhs: A value to compare.
  2740. /// - rhs: Another value to compare.
  2741. public static func == (a: Transcript.Segment, b:
  2742. Transcript.Segment) -> Bool
  2743. /// A type representing the stable identity of the entity associated with
  2744. /// an instance.
  2745. @available(iOS 26.0, macOS 26.0, *)
  2746. @available(tvOS, unavailable)
  2747. @available(watchOS, unavailable)
  2748. public typealias ID = String
  2749. }
  2750. /// A segment containing text.
  2751. @available(iOS 26.0, macOS 26.0, *)
  2752. @available(tvOS, unavailable)
  2753. @available(watchOS, unavailable)
  2754. public struct TextSegment : Sendable, Identifiable, Equatable {
  2755. /// The stable identity of the entity associated with this instance.
  2756. public var id: String
  2757. public var content: String
  2758. public init(id: String = UUID().uuidString, content: String)
  2759. /// Returns a Boolean value indicating whether two values are equal.
  2760. ///
  2761. /// Equality is the inverse of inequality. For any values `
  2762. a
  2763. ` and `b`
  2764. ,
  2765. /// `a == b` implies that `a != b` is `false`
  2766. .
  2767. ///
  2768. /// - Parameters:
  2769. /// - lhs: A value to compare.
  2770. /// - rhs: Another value to compare.
  2771. public static func == (a: Transcript.TextSegment, b:
  2772. Transcript.TextSegment) -> Bool
  2773. /// A type representing the stable identity of the entity associated with
  2774. /// an instance.
  2775. @available(iOS 26.0, macOS 26.0, *)
  2776. @available(tvOS, unavailable)
  2777. @available(watchOS, unavailable)
  2778. public typealias ID = String
  2779. }
  2780. /// A segment containing structured content.
  2781. @available(iOS 26.0, macOS 26.0, *)
  2782. @available(tvOS, unavailable)
  2783. @available(watchOS, unavailable)
  2784. public struct StructuredSegment : Sendable, Identifiable, Equatable {
  2785. /// The stable identity of the entity associated with this instance.
  2786. public var id: String
  2787. /// A source that be used to understand which type content represents.
  2788. public var source: String
  2789. /// The content of the segment.
  2790. public var content: GeneratedContent
  2791. public init(id: String = UUID().uuidString, source: String,
  2792. content: GeneratedContent)
  2793. /// Returns a Boolean value indicating whether two values are equal.
  2794. ///
  2795. /// Equality is the inverse of inequality. For any values `
  2796. /// `a == b` implies that `a != b` is `false`
  2797. a
  2798. ` and `b`
  2799. ,
  2800. .
  2801. ///
  2802. /// - Parameters:
  2803. /// - lhs: A value to compare.
  2804. /// - rhs: Another value to compare.
  2805. public static func == (a: Transcript.StructuredSegment, b:
  2806. Transcript.StructuredSegment) -> Bool
  2807. /// A type representing the stable identity of the entity associated with
  2808. /// an instance.
  2809. @available(iOS 26.0, macOS 26.0, *)
  2810. @available(tvOS, unavailable)
  2811. @available(watchOS, unavailable)
  2812. public typealias ID = String
  2813. }
  2814. /// ///
  2815. /// Instructions you provide to the model that define its behavior.
  2816. Instructions are typically provided to define the role and behavior of the model. Apple trains the
  2817. model
  2818. /// to obey instructions over any commands it receives in prompts. This is a security mechanism to
  2819. help
  2820. /// mitigate prompt injection attacks.
  2821. @available(iOS 26.0, macOS 26.0, *)
  2822. @available(tvOS, unavailable)
  2823. @available(watchOS, unavailable)
  2824. public struct Instructions : Sendable, Identifiable, Equatable {
  2825. /// The stable identity of the entity associated with this instance.
  2826. public var id: String
  2827. /// The content of the instructions, in natural language.
  2828. ///
  2829. /// - Note: Instructions are often provided in English even when the
  2830. /// users interact with the model in another language.
  2831. public var segments: [Transcript.Segment]
  2832. /// A list of tools made available to the model.
  2833. public var toolDefinitions: [Transcript.ToolDefinition]
  2834. /// Initialize instructions by describing how you want the model to
  2835. /// behave using natural language.
  2836. ///
  2837. /// - Parameters:
  2838. /// - id: A unique identifier for this instructions segment.
  2839. /// - segments: An array of segments that make up the instructions.
  2840. /// - toolDefinitions: Tools that the model should be allowed to call.
  2841. public init(id: String = UUID().uuidString, segments:
  2842. [Transcript.Segment], toolDefinitions: [Transcript.ToolDefinition])
  2843. /// Returns a Boolean value indicating whether two values are equal.
  2844. ///
  2845. /// Equality is the inverse of inequality. For any values `
  2846. /// `a == b` implies that `a != b` is `false`
  2847. a
  2848. ` and `b`
  2849. ,
  2850. .
  2851. ///
  2852. /// - Parameters:
  2853. /// - lhs: A value to compare.
  2854. /// - rhs: Another value to compare.
  2855. public static func == (a: Transcript.Instructions, b:
  2856. Transcript.Instructions) -> Bool
  2857. /// A type representing the stable identity of the entity associated with
  2858. /// an instance.
  2859. @available(iOS 26.0, macOS 26.0, *)
  2860. @available(tvOS, unavailable)
  2861. @available(watchOS, unavailable)
  2862. public typealias ID = String
  2863. }
  2864. /// A definition of a tool.
  2865. @available(iOS 26.0, macOS 26.0, *)
  2866. @available(tvOS, unavailable)
  2867. @available(watchOS, unavailable)
  2868. public struct ToolDefinition : Sendable, Equatable {
  2869. /// The tool's name.
  2870. public var name: String
  2871. /// A description of how and when to use the tool.
  2872. public var description: String
  2873. public init(name: String, description: String, parameters:
  2874. GenerationSchema)
  2875. public init(tool: some Tool)
  2876. /// Returns a Boolean value indicating whether two values are equal.
  2877. ///
  2878. /// Equality is the inverse of inequality. For any values `
  2879. /// `a == b` implies that `a != b` is `false`
  2880. a
  2881. ` and `b`
  2882. ,
  2883. .
  2884. ///
  2885. /// - Parameters:
  2886. /// - lhs: A value to compare.
  2887. /// - rhs: Another value to compare.
  2888. public static func == (a: Transcript.ToolDefinition, b:
  2889. Transcript.ToolDefinition) -> Bool
  2890. }
  2891. /// A prompt from the user asking the model.
  2892. @available(iOS 26.0, macOS 26.0, *)
  2893. @available(tvOS, unavailable)
  2894. @available(watchOS, unavailable)
  2895. public struct Prompt : Sendable, Identifiable, Equatable {
  2896. /// The identifier of the prompt.
  2897. public var id: String
  2898. /// Ordered prompt segments.
  2899. public var segments: [Transcript.Segment]
  2900. /// Generation options associated with the prompt.
  2901. public var options: GenerationOptions
  2902. /// An optional response format that describes the desired output structure.
  2903. public var responseFormat: Transcript.ResponseFormat?
  2904. /// Creates a prompt.
  2905. ///
  2906. /// - Parameters:
  2907. /// - id: A ``Generable`` type to use as the response format.
  2908. /// - segments: An array of segments that make up the prompt.
  2909. /// - options: Options that control how tokens are sampled from the distribution the
  2910. model produces.
  2911. /// - responseFormat: A response format that describes the output structure.
  2912. public init(id: String = UUID().uuidString, segments:
  2913. [Transcript.Segment], options: GenerationOptions =
  2914. GenerationOptions(), responseFormat: Transcript.ResponseFormat? =
  2915. nil)
  2916. /// Returns a Boolean value indicating whether two values are equal.
  2917. ///
  2918. /// Equality is the inverse of inequality. For any values `
  2919. /// `a == b` implies that `a != b` is `false`
  2920. a
  2921. ` and `b`
  2922. ,
  2923. .
  2924. ///
  2925. /// - Parameters:
  2926. /// - lhs: A value to compare.
  2927. /// - rhs: Another value to compare.
  2928. public static func == (a: Transcript.Prompt, b: Transcript.Prompt)
  2929. -> Bool
  2930. /// A type representing the stable identity of the entity associated with
  2931. /// an instance.
  2932. @available(iOS 26.0, macOS 26.0, *)
  2933. @available(tvOS, unavailable)
  2934. @available(watchOS, unavailable)
  2935. public typealias ID = String
  2936. }
  2937. /// Specifies a response format that the model must conform its output to.
  2938. @available(iOS 26.0, macOS 26.0, *)
  2939. @available(tvOS, unavailable)
  2940. @available(watchOS, unavailable)
  2941. public struct ResponseFormat : Sendable, Equatable {
  2942. /// A name associated with the response format.
  2943. public var name: String { get }
  2944. /// ///
  2945. Creates a response format with type you specify.
  2946. /// - Parameters:
  2947. /// - type: A ``Generable`` type to use as the response format.
  2948. public init<Content>(type: Content.Type) where Content : Generable
  2949. /// Creates a response format with a schema.
  2950. ///
  2951. /// - Parameters:
  2952. /// - schema: A schema to use as the response format.
  2953. public init(schema: GenerationSchema)
  2954. /// Returns a Boolean value indicating whether two values are equal.
  2955. ///
  2956. /// Equality is the inverse of inequality. For any values `
  2957. /// `a == b` implies that `a != b` is `false`
  2958. a
  2959. ` and `b`
  2960. ,
  2961. .
  2962. ///
  2963. /// - Parameters:
  2964. /// - lhs: A value to compare.
  2965. /// - rhs: Another value to compare.
  2966. public static func == (a: Transcript.ResponseFormat, b:
  2967. Transcript.ResponseFormat) -> Bool
  2968. }
  2969. /// A collection tool calls generated by the model.
  2970. @available(iOS 26.0, macOS 26.0, *)
  2971. @available(tvOS, unavailable)
  2972. @available(watchOS, unavailable)
  2973. public struct ToolCalls : Sendable, Identifiable, Equatable,
  2974. RandomAccessCollection {
  2975. /// The stable identity of the entity associated with this instance.
  2976. public var id: String
  2977. public init<S>(id: String = UUID().uuidString, _
  2978. : Sequence, S.Element == Transcript.ToolCall
  2979. calls: S) where S
  2980. /// ///
  2981. /// /// ///
  2982. /// Accesses the element at the specified position.
  2983. The following example accesses an element of an array through its
  2984. subscript to print its value:
  2985. var streets = ["Adams", "Bryant", "Channing", "Douglas",
  2986. "Evarts"]
  2987. /// print(streets[1])
  2988. /// // Prints "Bryant"
  2989. ///
  2990. /// You can subscript a collection with any valid index other than the
  2991. /// collection's end index. The end index refers to the position one past
  2992. /// the last element of a collection, so it doesn't correspond with an
  2993. /// element.
  2994. ///
  2995. /// - Parameter position: The position of the element to access. `position`
  2996. /// must be a valid index of the collection that is not equal to the
  2997. /// `endIndex` property.
  2998. ///
  2999. /// - Complexity: O(1)
  3000. public subscript(position: Int) -> Transcript.ToolCall { get }
  3001. /// ///
  3002. /// The position of the first element in a nonempty collection.
  3003. If the collection is empty, `startIndex` is equal to `endIndex`
  3004. public var startIndex: Int { get }
  3005. .
  3006. /// /// The collection's "past the end" position---that is, the position one
  3007. greater than the last valid subscript argument.
  3008. ///
  3009. /// /// ..<
  3010. /// /// When you need a range that includes the last element of a collection, use
  3011. the half-open range operator (`
  3012. ..<
  3013. `) with `endIndex`. The `
  3014. creates a range that doesn't include the upper bound, so it's always
  3015. safe to use with `endIndex`. For example:
  3016. ///
  3017. /// let numbers = [10, 20, 30, 40, 50]
  3018. /// if let index = numbers.firstIndex(of: 30) {
  3019. /// print(numbers[index ..< numbers.endIndex])
  3020. /// }
  3021. /// // Prints "[30, 40, 50]"
  3022. ///
  3023. /// .
  3024. ` operator
  3025. If the collection is empty, `endIndex` is equal to `startIndex`
  3026. public var endIndex: Int { get }
  3027. /// Returns a Boolean value indicating whether two values are equal.
  3028. ///
  3029. /// Equality is the inverse of inequality. For any values `
  3030. /// `a == b` implies that `a != b` is `false`
  3031. a
  3032. ` and `b`
  3033. ,
  3034. .
  3035. ///
  3036. /// - Parameters:
  3037. /// - lhs: A value to compare.
  3038. /// - rhs: Another value to compare.
  3039. public static func == (a: Transcript.ToolCalls, b:
  3040. Transcript.ToolCalls) -> Bool
  3041. /// A type representing the sequence's elements.
  3042. @available(iOS 26.0, macOS 26.0, *)
  3043. @available(tvOS, unavailable)
  3044. @available(watchOS, unavailable)
  3045. public typealias Element = Transcript.ToolCall
  3046. /// A type representing the stable identity of the entity associated with
  3047. /// an instance.
  3048. @available(iOS 26.0, macOS 26.0, *)
  3049. @available(tvOS, unavailable)
  3050. @available(watchOS, unavailable)
  3051. public typealias ID = String
  3052. /// A type that represents a position in the collection.
  3053. ///
  3054. /// Valid indices consist of the position of every element and a
  3055. /// "past the end" position that's not valid for use as a subscript
  3056. /// argument.
  3057. @available(iOS 26.0, macOS 26.0, *)
  3058. @available(tvOS, unavailable)
  3059. @available(watchOS, unavailable)
  3060. public typealias Index = Int
  3061. /// A type that represents the indices that are valid for subscripting the
  3062. /// collection, in ascending order.
  3063. @available(iOS 26.0, macOS 26.0, *)
  3064. @available(tvOS, unavailable)
  3065. @available(watchOS, unavailable)
  3066. public typealias Indices = Range<Int>
  3067. /// A type that provides the collection's iteration interface and
  3068. /// encapsulates its iteration state.
  3069. ///
  3070. /// /// By default, a collection conforms to the `Sequence` protocol by
  3071. supplying `IndexingIterator` as its associated `Iterator`
  3072. /// type.
  3073. @available(iOS 26.0, macOS 26.0, *)
  3074. @available(tvOS, unavailable)
  3075. @available(watchOS, unavailable)
  3076. public typealias Iterator = IndexingIterator<Transcript.ToolCalls>
  3077. /// /// A collection representing a contiguous subrange of this collection's
  3078. elements. The subsequence shares indices with the original collection.
  3079. ///
  3080. /// The default subsequence type for collections that don't define their own
  3081. /// is `Slice`
  3082. .
  3083. @available(iOS 26.0, macOS 26.0, *)
  3084. @available(tvOS, unavailable)
  3085. @available(watchOS, unavailable)
  3086. public typealias SubSequence = Slice<Transcript.ToolCalls>
  3087. }
  3088. /// A tool call generated by the model containing the name of a tool and arguments to pass to it.
  3089. @available(iOS 26.0, macOS 26.0, *)
  3090. @available(tvOS, unavailable)
  3091. @available(watchOS, unavailable)
  3092. public struct ToolCall : Sendable, Identifiable, Equatable {
  3093. /// The stable identity of the entity associated with this instance.
  3094. public var id: String
  3095. /// The name of the tool being invoked.
  3096. public var toolName: String
  3097. /// Arguments to pass to the invoked tool.
  3098. public var arguments: GeneratedContent
  3099. public init(id: String, toolName: String, arguments:
  3100. GeneratedContent)
  3101. /// ///
  3102. /// ///
  3103. Returns a Boolean value indicating whether two values are equal.
  3104. Equality is the inverse of inequality. For any values `
  3105. /// `a == b` implies that `a != b` is `false`
  3106. .
  3107. a
  3108. ` and `b`
  3109. ,
  3110. /// - Parameters:
  3111. /// - lhs: A value to compare.
  3112. /// - rhs: Another value to compare.
  3113. public static func == (a: Transcript.ToolCall, b:
  3114. Transcript.ToolCall) -> Bool
  3115. /// A type representing the stable identity of the entity associated with
  3116. /// an instance.
  3117. @available(iOS 26.0, macOS 26.0, *)
  3118. @available(tvOS, unavailable)
  3119. @available(watchOS, unavailable)
  3120. public typealias ID = String
  3121. }
  3122. /// A tool output provided back to the model.
  3123. @available(iOS 26.0, macOS 26.0, *)
  3124. @available(tvOS, unavailable)
  3125. @available(watchOS, unavailable)
  3126. public struct ToolOutput : Sendable, Identifiable, Equatable {
  3127. /// A unique id for this tool output.
  3128. public var id: String
  3129. /// The name of the tool that produced this output.
  3130. public var toolName: String
  3131. /// Segments of the tool output.
  3132. public var segments: [Transcript.Segment]
  3133. public init(id: String, toolName: String, segments:
  3134. [Transcript.Segment])
  3135. /// Returns a Boolean value indicating whether two values are equal.
  3136. ///
  3137. /// Equality is the inverse of inequality. For any values `
  3138. a
  3139. ` and `b`
  3140. ,
  3141. /// `a == b` implies that `a != b` is `false`
  3142. .
  3143. ///
  3144. /// - Parameters:
  3145. /// - lhs: A value to compare.
  3146. /// - rhs: Another value to compare.
  3147. public static func == (a: Transcript.ToolOutput, b:
  3148. Transcript.ToolOutput) -> Bool
  3149. /// A type representing the stable identity of the entity associated with
  3150. /// an instance.
  3151. @available(iOS 26.0, macOS 26.0, *)
  3152. @available(tvOS, unavailable)
  3153. @available(watchOS, unavailable)
  3154. public typealias ID = String
  3155. }
  3156. /// A response from the model.
  3157. @available(iOS 26.0, macOS 26.0, *)
  3158. @available(tvOS, unavailable)
  3159. @available(watchOS, unavailable)
  3160. public struct Response : Sendable, Identifiable, Equatable {
  3161. /// The stable identity of the entity associated with this instance.
  3162. public var id: String
  3163. /// Version aware identifiers for all assets used to generate this response.
  3164. public var assetIDs: [String]
  3165. /// Ordered prompt segments.
  3166. public var segments: [Transcript.Segment]
  3167. public init(id: String = UUID().uuidString, assetIDs: [String],
  3168. segments: [Transcript.Segment])
  3169. /// Returns a Boolean value indicating whether two values are equal.
  3170. ///
  3171. /// Equality is the inverse of inequality. For any values `
  3172. /// `a == b` implies that `a != b` is `false`
  3173. a
  3174. ` and `b`
  3175. ,
  3176. .
  3177. ///
  3178. /// - Parameters:
  3179. /// - lhs: A value to compare.
  3180. /// - rhs: Another value to compare.
  3181. public static func == (a: Transcript.Response, b:
  3182. Transcript.Response) -> Bool
  3183. /// A type representing the stable identity of the entity associated with
  3184. /// an instance.
  3185. @available(iOS 26.0, macOS 26.0, *)
  3186. @available(tvOS, unavailable)
  3187. @available(watchOS, unavailable)
  3188. public typealias ID = String
  3189. }
  3190. /// Returns a Boolean value indicating whether two values are equal.
  3191. ///
  3192. /// Equality is the inverse of inequality. For any values `
  3193. /// `a == b` implies that `a != b` is `false`
  3194. a
  3195. ` and `b`
  3196. ,
  3197. .
  3198. ///
  3199. /// - Parameters:
  3200. /// - lhs: A value to compare.
  3201. /// - rhs: Another value to compare.
  3202. public static func == (a: Transcript, b: Transcript) -> Bool
  3203. /// A type representing the sequence's elements.
  3204. @available(iOS 26.0, macOS 26.0, *)
  3205. @available(tvOS, unavailable)
  3206. @available(watchOS, unavailable)
  3207. public typealias Element = Transcript.Entry
  3208. /// A type that represents the indices that are valid for subscripting the
  3209. /// collection, in ascending order.
  3210. @available(iOS 26.0, macOS 26.0, *)
  3211. @available(tvOS, unavailable)
  3212. @available(watchOS, unavailable)
  3213. public typealias Indices = Range<Transcript.Index>
  3214. /// A type that provides the collection's iteration interface and
  3215. /// encapsulates its iteration state.
  3216. ///
  3217. /// /// By default, a collection conforms to the `Sequence` protocol by
  3218. supplying `IndexingIterator` as its associated `Iterator`
  3219. /// type.
  3220. @available(iOS 26.0, macOS 26.0, *)
  3221. @available(tvOS, unavailable)
  3222. @available(watchOS, unavailable)
  3223. public typealias Iterator = IndexingIterator<Transcript>
  3224. /// /// A collection representing a contiguous subrange of this collection's
  3225. elements. The subsequence shares indices with the original collection.
  3226. ///
  3227. /// The default subsequence type for collections that don't define their own
  3228. /// is `Slice`
  3229. .
  3230. @available(iOS 26.0, macOS 26.0, *)
  3231. @available(tvOS, unavailable)
  3232. @available(watchOS, unavailable)
  3233. public typealias SubSequence = Slice<Transcript>
  3234. }
  3235. @available(iOS 26.0, macOS 26.0, *)
  3236. @available(watchOS, unavailable)
  3237. @available(tvOS, unavailable)
  3238. extension Transcript : Codable {
  3239. /// Creates a new instance by decoding from the given decoder.
  3240. ///
  3241. /// /// This initializer throws an error if reading from the decoder fails, or
  3242. if the data read is corrupted or otherwise invalid.
  3243. ///
  3244. /// - Parameter decoder: The decoder to read data from.
  3245. public init(from decoder: any Decoder) throws
  3246. /// ///
  3247. /// /// ///
  3248. /// ///
  3249. Encodes this value into the given encoder.
  3250. If the value fails to encode anything, `encoder` will encode an empty
  3251. keyed container in its place.
  3252. This function throws an error if any values are invalid for the given
  3253. /// encoder's format.
  3254. /// - Parameter encoder: The encoder to write data to.
  3255. public func encode(to encoder: any Encoder) throws
  3256. }
  3257. @available(iOS 26.0, macOS 26.0, *)
  3258. @available(watchOS, unavailable)
  3259. @available(tvOS, unavailable)
  3260. extension Transcript.Entry : CustomStringConvertible {
  3261. /// ///
  3262. A textual representation of this instance.
  3263. /// Calling this property directly is discouraged. Instead, convert an
  3264. /// instance of any type to a string by using the `String(describing:)`
  3265. /// initializer. This initializer works with any type, and uses the custom
  3266. /// `description` property for types that conform to
  3267. /// `CustomStringConvertible`
  3268. :
  3269. ///
  3270. /// struct Point: CustomStringConvertible {
  3271. /// let x: Int, y: Int
  3272. ///
  3273. /// var description: String {
  3274. /// return "(\(x), \(y))"
  3275. /// }
  3276. /// }
  3277. ///
  3278. /// let p = Point(x: 21, y: 30)
  3279. /// let s = String(describing: p)
  3280. /// print(s)
  3281. /// // Prints "(21, 30)"
  3282. ///
  3283. /// The conversion of `
  3284. p
  3285. ` to a string in the assignment to `
  3286. s
  3287. ` uses the
  3288. /// `Point` type's `description` property.
  3289. public var description: String { get }
  3290. }
  3291. @available(iOS 26.0, macOS 26.0, *)
  3292. @available(watchOS, unavailable)
  3293. @available(tvOS, unavailable)
  3294. extension Transcript.Segment : CustomStringConvertible {
  3295. /// A textual representation of this instance.
  3296. ///
  3297. /// Calling this property directly is discouraged. Instead, convert an
  3298. /// instance of any type to a string by using the `String(describing:)`
  3299. /// initializer. This initializer works with any type, and uses the custom
  3300. /// `description` property for types that conform to
  3301. /// `CustomStringConvertible`
  3302. :
  3303. ///
  3304. /// struct Point: CustomStringConvertible {
  3305. /// let x: Int, y: Int
  3306. ///
  3307. /// var description: String {
  3308. /// return "(\(x), \(y))"
  3309. /// }
  3310. /// }
  3311. ///
  3312. /// let p = Point(x: 21, y: 30)
  3313. /// let s = String(describing: p)
  3314. /// print(s)
  3315. /// // Prints "(21, 30)"
  3316. ///
  3317. /// The conversion of `
  3318. p
  3319. ` to a string in the assignment to `
  3320. s
  3321. ` uses the
  3322. /// `Point` type's `description` property.
  3323. public var description: String { get }
  3324. }
  3325. @available(iOS 26.0, macOS 26.0, *)
  3326. @available(watchOS, unavailable)
  3327. @available(tvOS, unavailable)
  3328. extension Transcript.TextSegment : CustomStringConvertible {
  3329. /// A textual representation of this instance.
  3330. ///
  3331. /// Calling this property directly is discouraged. Instead, convert an
  3332. /// instance of any type to a string by using the `String(describing:)`
  3333. /// initializer. This initializer works with any type, and uses the custom
  3334. /// `description` property for types that conform to
  3335. /// `CustomStringConvertible`
  3336. :
  3337. ///
  3338. /// struct Point: CustomStringConvertible {
  3339. /// let x: Int, y: Int
  3340. ///
  3341. /// var description: String {
  3342. /// return "(\(x), \(y))"
  3343. /// }
  3344. /// }
  3345. ///
  3346. /// let p = Point(x: 21, y: 30)
  3347. /// let s = String(describing: p)
  3348. /// print(s)
  3349. /// // Prints "(21, 30)"
  3350. ///
  3351. /// The conversion of `
  3352. p
  3353. ` to a string in the assignment to `
  3354. s
  3355. ` uses the
  3356. /// `Point` type's `description` property.
  3357. public var description: String { get }
  3358. }
  3359. @available(iOS 26.0, macOS 26.0, *)
  3360. @available(watchOS, unavailable)
  3361. @available(tvOS, unavailable)
  3362. extension Transcript.StructuredSegment : CustomStringConvertible {
  3363. /// A textual representation of this instance.
  3364. ///
  3365. /// Calling this property directly is discouraged. Instead, convert an
  3366. /// instance of any type to a string by using the `String(describing:)`
  3367. /// initializer. This initializer works with any type, and uses the custom
  3368. /// `description` property for types that conform to
  3369. /// `CustomStringConvertible`
  3370. :
  3371. ///
  3372. /// struct Point: CustomStringConvertible {
  3373. /// let x: Int, y: Int
  3374. ///
  3375. /// var description: String {
  3376. /// return "(\(x), \(y))"
  3377. /// }
  3378. /// }
  3379. ///
  3380. /// let p = Point(x: 21, y: 30)
  3381. /// let s = String(describing: p)
  3382. /// print(s)
  3383. /// // Prints "(21, 30)"
  3384. ///
  3385. /// The conversion of `
  3386. p
  3387. ` to a string in the assignment to `
  3388. /// `Point` type's `description` property.
  3389. public var description: String { get }
  3390. s
  3391. ` uses the
  3392. }
  3393. @available(iOS 26.0, macOS 26.0, *)
  3394. @available(watchOS, unavailable)
  3395. @available(tvOS, unavailable)
  3396. extension Transcript.Instructions : CustomStringConvertible {
  3397. /// A textual representation of this instance.
  3398. ///
  3399. /// Calling this property directly is discouraged. Instead, convert an
  3400. /// instance of any type to a string by using the `String(describing:)`
  3401. /// initializer. This initializer works with any type, and uses the custom
  3402. /// `description` property for types that conform to
  3403. /// `CustomStringConvertible`
  3404. :
  3405. ///
  3406. /// struct Point: CustomStringConvertible {
  3407. /// let x: Int, y: Int
  3408. ///
  3409. /// var description: String {
  3410. /// return "(\(x), \(y))"
  3411. /// }
  3412. /// }
  3413. ///
  3414. /// let p = Point(x: 21, y: 30)
  3415. /// let s = String(describing: p)
  3416. /// print(s)
  3417. /// // Prints "(21, 30)"
  3418. ///
  3419. /// The conversion of `
  3420. p
  3421. ` to a string in the assignment to `
  3422. s
  3423. ` uses the
  3424. /// `Point` type's `description` property.
  3425. public var description: String { get }
  3426. }
  3427. @available(iOS 26.0, macOS 26.0, *)
  3428. @available(watchOS, unavailable)
  3429. @available(tvOS, unavailable)
  3430. extension Transcript.Prompt : CustomStringConvertible {
  3431. /// ///
  3432. /// /// /// /// ///
  3433. /// ///
  3434. A textual representation of this instance.
  3435. Calling this property directly is discouraged. Instead, convert an
  3436. instance of any type to a string by using the `String(describing:)`
  3437. initializer. This initializer works with any type, and uses the custom
  3438. `description` property for types that conform to
  3439. /// `CustomStringConvertible`
  3440. :
  3441. /// struct Point: CustomStringConvertible {
  3442. let x: Int, y: Int
  3443. /// var description: String {
  3444. /// return "(\(x), \(y))"
  3445. /// }
  3446. /// }
  3447. ///
  3448. /// let p = Point(x: 21, y: 30)
  3449. /// let s = String(describing: p)
  3450. /// print(s)
  3451. /// // Prints "(21, 30)"
  3452. ///
  3453. /// The conversion of `
  3454. p
  3455. ` to a string in the assignment to `
  3456. /// `Point` type's `description` property.
  3457. public var description: String { get }
  3458. s
  3459. ` uses the
  3460. }
  3461. @available(iOS 26.0, macOS 26.0, *)
  3462. @available(watchOS, unavailable)
  3463. @available(tvOS, unavailable)
  3464. extension Transcript.ResponseFormat : CustomStringConvertible {
  3465. /// A textual representation of this instance.
  3466. ///
  3467. /// Calling this property directly is discouraged. Instead, convert an
  3468. /// instance of any type to a string by using the `String(describing:)`
  3469. /// initializer. This initializer works with any type, and uses the custom
  3470. /// `description` property for types that conform to
  3471. /// `CustomStringConvertible`
  3472. :
  3473. ///
  3474. /// struct Point: CustomStringConvertible {
  3475. /// let x: Int, y: Int
  3476. ///
  3477. /// var description: String {
  3478. /// return "(\(x), \(y))"
  3479. /// }
  3480. /// }
  3481. ///
  3482. /// let p = Point(x: 21, y: 30)
  3483. /// let s = String(describing: p)
  3484. /// print(s)
  3485. /// // Prints "(21, 30)"
  3486. ///
  3487. /// The conversion of `
  3488. p
  3489. ` to a string in the assignment to `
  3490. s
  3491. ` uses the
  3492. /// `Point` type's `description` property.
  3493. public var description: String { get }
  3494. }
  3495. @available(iOS 26.0, macOS 26.0, *)
  3496. @available(watchOS, unavailable)
  3497. @available(tvOS, unavailable)
  3498. extension Transcript.ToolCalls : CustomStringConvertible {
  3499. /// ///
  3500. /// /// /// A textual representation of this instance.
  3501. Calling this property directly is discouraged. Instead, convert an
  3502. instance of any type to a string by using the `String(describing:)`
  3503. initializer. This initializer works with any type, and uses the custom
  3504. /// `description` property for types that conform to
  3505. /// `CustomStringConvertible`
  3506. :
  3507. ///
  3508. /// struct Point: CustomStringConvertible {
  3509. let x: Int, y: Int
  3510. /// ///
  3511. /// var description: String {
  3512. /// return "(\(x), \(y))"
  3513. /// }
  3514. /// }
  3515. ///
  3516. /// let p = Point(x: 21, y: 30)
  3517. /// let s = String(describing: p)
  3518. /// print(s)
  3519. /// // Prints "(21, 30)"
  3520. ///
  3521. /// The conversion of `
  3522. p
  3523. ` to a string in the assignment to `
  3524. s
  3525. /// `Point` type's `description` property.
  3526. public var description: String { get }
  3527. ` uses the
  3528. }
  3529. @available(iOS 26.0, macOS 26.0, *)
  3530. @available(watchOS, unavailable)
  3531. @available(tvOS, unavailable)
  3532. extension Transcript.ToolCall : CustomStringConvertible {
  3533. /// A textual representation of this instance.
  3534. ///
  3535. /// Calling this property directly is discouraged. Instead, convert an
  3536. /// instance of any type to a string by using the `String(describing:)`
  3537. /// initializer. This initializer works with any type, and uses the custom
  3538. /// `description` property for types that conform to
  3539. /// `CustomStringConvertible`
  3540. :
  3541. ///
  3542. /// struct Point: CustomStringConvertible {
  3543. /// let x: Int, y: Int
  3544. ///
  3545. /// var description: String {
  3546. /// return "(\(x), \(y))"
  3547. /// }
  3548. /// }
  3549. ///
  3550. /// let p = Point(x: 21, y: 30)
  3551. /// let s = String(describing: p)
  3552. /// print(s)
  3553. /// // Prints "(21, 30)"
  3554. ///
  3555. /// The conversion of `
  3556. p
  3557. ` to a string in the assignment to `
  3558. s
  3559. ` uses the
  3560. /// `Point` type's `description` property.
  3561. public var description: String { get }
  3562. }
  3563. @available(iOS 26.0, macOS 26.0, *)
  3564. @available(watchOS, unavailable)
  3565. @available(tvOS, unavailable)
  3566. extension Transcript.ToolOutput : CustomStringConvertible {
  3567. /// A textual representation of this instance.
  3568. ///
  3569. /// Calling this property directly is discouraged. Instead, convert an
  3570. /// instance of any type to a string by using the `String(describing:)`
  3571. /// initializer. This initializer works with any type, and uses the custom
  3572. /// `description` property for types that conform to
  3573. /// `CustomStringConvertible`
  3574. :
  3575. ///
  3576. /// struct Point: CustomStringConvertible {
  3577. /// let x: Int, y: Int
  3578. ///
  3579. /// var description: String {
  3580. /// return "(\(x), \(y))"
  3581. /// }
  3582. /// }
  3583. ///
  3584. /// let p = Point(x: 21, y: 30)
  3585. /// let s = String(describing: p)
  3586. /// print(s)
  3587. /// // Prints "(21, 30)"
  3588. ///
  3589. /// The conversion of `
  3590. p
  3591. ` to a string in the assignment to `
  3592. s
  3593. ` uses the
  3594. /// `Point` type's `description` property.
  3595. public var description: String { get }
  3596. }
  3597. @available(iOS 26.0, macOS 26.0, *)
  3598. @available(watchOS, unavailable)
  3599. @available(tvOS, unavailable)
  3600. extension Transcript.Response : CustomStringConvertible {
  3601. /// A textual representation of this instance.
  3602. ///
  3603. /// Calling this property directly is discouraged. Instead, convert an
  3604. /// instance of any type to a string by using the `String(describing:)`
  3605. /// initializer. This initializer works with any type, and uses the custom
  3606. /// `description` property for types that conform to
  3607. /// `CustomStringConvertible`
  3608. :
  3609. ///
  3610. /// struct Point: CustomStringConvertible {
  3611. /// let x: Int, y: Int
  3612. ///
  3613. /// var description: String {
  3614. /// return "(\(x), \(y))"
  3615. /// }
  3616. /// }
  3617. ///
  3618. /// let p = Point(x: 21, y: 30)
  3619. /// let s = String(describing: p)
  3620. /// print(s)
  3621. /// // Prints "(21, 30)"
  3622. ///
  3623. /// The conversion of `
  3624. p
  3625. ` to a string in the assignment to `
  3626. s
  3627. ` uses the
  3628. /// `Point` type's `description` property.
  3629. public var description: String { get }
  3630. }
  3631. @available(iOS 26.0, macOS 26.0, *)
  3632. @available(tvOS, unavailable)
  3633. @available(watchOS, unavailable)
  3634. extension Optional where Wrapped : Generable {
  3635. public typealias PartiallyGenerated = Wrapped.PartiallyGenerated
  3636. }
  3637. @available(iOS 26.0, macOS 26.0, *)
  3638. @available(tvOS, unavailable)
  3639. @available(watchOS, unavailable)
  3640. extension Optional : ConvertibleToGeneratedContent, PromptRepresentable,
  3641. InstructionsRepresentable where Wrapped : ConvertibleToGeneratedContent {
  3642. /// An instance that represents the generated content.
  3643. public var generatedContent: GeneratedContent { get }
  3644. }
  3645. @available(iOS 26.0, macOS 26.0, *)
  3646. @available(tvOS, unavailable)
  3647. @available(watchOS, unavailable)
  3648. extension Bool : Generable {
  3649. /// An instance of the generation schema.
  3650. public static var generationSchema: GenerationSchema { get }
  3651. /// public init(
  3652. Creates an instance with the content.
  3653. content: GeneratedContent) throws
  3654. _
  3655. /// An instance that represents the generated content.
  3656. public var generatedContent: GeneratedContent { get }
  3657. }
  3658. @available(iOS 26.0, macOS 26.0, *)
  3659. @available(tvOS, unavailable)
  3660. @available(watchOS, unavailable)
  3661. extension String : Generable {
  3662. /// An instance of the generation schema.
  3663. public static var generationSchema: GenerationSchema { get }
  3664. /// public init(
  3665. Creates an instance with the content.
  3666. content: GeneratedContent) throws
  3667. _
  3668. /// An instance that represents the generated content.
  3669. public var generatedContent: GeneratedContent { get }
  3670. }
  3671. @available(iOS 26.0, macOS 26.0, *)
  3672. @available(tvOS, unavailable)
  3673. @available(watchOS, unavailable)
  3674. extension Int : Generable {
  3675. /// An instance of the generation schema.
  3676. public static var generationSchema: GenerationSchema { get }
  3677. /// public init(
  3678. Creates an instance with the content.
  3679. content: GeneratedContent) throws
  3680. _
  3681. /// An instance that represents the generated content.
  3682. public var generatedContent: GeneratedContent { get }
  3683. }
  3684. @available(iOS 26.0, macOS 26.0, *)
  3685. @available(tvOS, unavailable)
  3686. @available(watchOS, unavailable)
  3687. extension Float : Generable {
  3688. /// An instance of the generation schema.
  3689. public static var generationSchema: GenerationSchema { get }
  3690. /// public init(
  3691. Creates an instance with the content.
  3692. content: GeneratedContent) throws
  3693. _
  3694. /// An instance that represents the generated content.
  3695. public var generatedContent: GeneratedContent { get }
  3696. }
  3697. @available(iOS 26.0, macOS 26.0, *)
  3698. @available(tvOS, unavailable)
  3699. @available(watchOS, unavailable)
  3700. extension Double : Generable {
  3701. /// An instance of the generation schema.
  3702. public static var generationSchema: GenerationSchema { get }
  3703. /// public init(
  3704. Creates an instance with the content.
  3705. content: GeneratedContent) throws
  3706. _
  3707. /// An instance that represents the generated content.
  3708. public var generatedContent: GeneratedContent { get }
  3709. }
  3710. @available(iOS 26.0, macOS 26.0, *)
  3711. @available(tvOS, unavailable)
  3712. @available(watchOS, unavailable)
  3713. extension Decimal : Generable {
  3714. /// An instance of the generation schema.
  3715. public static var generationSchema: GenerationSchema { get }
  3716. /// public init(
  3717. Creates an instance with the content.
  3718. content: GeneratedContent) throws
  3719. _
  3720. /// An instance that represents the generated content.
  3721. public var generatedContent: GeneratedContent { get }
  3722. }
  3723. @available(iOS 26.0, macOS 26.0, *)
  3724. @available(tvOS, unavailable)
  3725. @available(watchOS, unavailable)
  3726. extension Array : Generable where Element : Generable {
  3727. /// A representation of partially generated content
  3728. public typealias PartiallyGenerated = [Element.PartiallyGenerated]
  3729. /// An instance of the generation schema.
  3730. public static var generationSchema: GenerationSchema { get }
  3731. }
  3732. @available(iOS 26.0, macOS 26.0, *)
  3733. @available(tvOS, unavailable)
  3734. @available(watchOS, unavailable)
  3735. extension Array : ConvertibleToGeneratedContent where Element :
  3736. ConvertibleToGeneratedContent {
  3737. /// An instance that represents the generated content.
  3738. public var generatedContent: GeneratedContent { get }
  3739. }
  3740. @available(iOS 26.0, macOS 26.0, *)
  3741. @available(tvOS, unavailable)
  3742. @available(watchOS, unavailable)
  3743. extension Array : ConvertibleFromGeneratedContent where Element :
  3744. ConvertibleFromGeneratedContent {
  3745. /// public init(
  3746. Creates an instance with the content.
  3747. content: GeneratedContent) throws
  3748. _
  3749. }
  3750. @available(iOS 26.0, macOS 26.0, *)
  3751. @available(tvOS, unavailable)
  3752. @available(watchOS, unavailable)
  3753. extension Never : Generable {
  3754. /// An instance of the generation schema.
  3755. public static var generationSchema: GenerationSchema { get }
  3756. /// public init(
  3757. Creates an instance with the content.
  3758. content: GeneratedContent) throws
  3759. _
  3760. /// An instance that represents the generated content.
  3761. public var generatedContent: GeneratedContent { get }
  3762. }
  3763. @available(iOS 26.0, macOS 26.0, *)
  3764. @available(tvOS, unavailable)
  3765. @available(watchOS, unavailable)
  3766. extension String : InstructionsRepresentable {
  3767. /// An instance that represents the instructions.
  3768. public var instructionsRepresentation: Instructions { get }
  3769. }
  3770. @available(iOS 26.0, macOS 26.0, *)
  3771. @available(tvOS, unavailable)
  3772. @available(watchOS, unavailable)
  3773. extension Array : InstructionsRepresentable where Element :
  3774. InstructionsRepresentable {
  3775. /// An instance that represents the instructions.
  3776. public var instructionsRepresentation: Instructions { get }
  3777. }
  3778. @available(iOS 26.0, macOS 26.0, *)
  3779. @available(tvOS, unavailable)
  3780. @available(watchOS, unavailable)
  3781. extension String : PromptRepresentable {
  3782. /// An instance that represents a prompt.
  3783. public var promptRepresentation: Prompt { get }
  3784. }
  3785. @available(iOS 26.0, macOS 26.0, *)
  3786. @available(tvOS, unavailable)
  3787. @available(watchOS, unavailable)
  3788. extension Array : PromptRepresentable where Element : PromptRepresentable {
  3789. /// An instance that represents a prompt.
  3790. public var promptRepresentation: Prompt { get }
  3791. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Pemexev
    1 day
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment