In the name of God, please allow initializing GeneratedContent
from an array of key-value pairs. It’s literally the same thing KeyValuePairs
uses internally, but it would let us initialize structure-like GeneratedContent
from dynamic data without resorting to unsafeBitCast
hacks.
Foundation Models: Please Allow GeneratedContent Init from Array of Key-Value Pairs
For anyone looking for a temporary solution, you can (but probably shouldn't) do this:
struct DynamicKeyValuePairs<K, V> {
let _elements: [(K, V)]
init(_elements: [(K, V)]) {
self._elements = _elements
}
}
let kvp = unsafeBitCast(
DynamicKeyValuePairs(_elements: properties),
to: KeyValuePairs<String, any ConvertibleToGeneratedContent>.self
)
To explain a little why this might be useful:
– It allows for more use cases of Foundation Models from non-native frameworks like Flutter and React Native.
Here is an example of how you can create a bridge from Flutter to Foundation Models, with Dart Tools support.
– It also opens up use cases like MCP servers, where tools are dynamically fetched from some config/server. Though not very usable given current model constraints — but still.
I am trying to do the same for React Native