LanguageModelSession with multiple tools and structured outpout

Hi,

I'm using LanguageModelSession and giving it two different tools to query data from a local database. I'm wondering how I can have the session generate structured content as the response that includes data one or both tools (or no tool at all).

Here is an example of what I'm trying to do: Let's say the app has access to a database that contains information about exercise and sleep data (this is just an analogy). There are two tools, GetExerciseData() and GetSleepData(). The user may then prompt something like, "how well did I sleep in November". I have this working so that it calls through to the right tool, which would return a SleepSummary. However, I can't figure out how to have the session return the right structured data.

I can do this and get back good text data: let response = session.respond(to: userInput), but I believe I want to do something like: let response = session.respond(to: trimmed, generating: <SomeStructure?>) Sometimes the model I run one tool or the other, or both tools, or no tool at all.

Any help of what the right way to go about this would be much appreciated. Most of the example I found have to do with 1 tool.

Answered by Apple Designer in 870947022

Hello!

Sorry for the delay, most of us at Apple have been on winter break :)

For this particular problem, we don't currently have a great solution. To be clear, from your description I'm interpreting your problem as:

  1. Given 2 tools Tool A and Tool B

  2. Each tool should return a different kind of structured output, OutA and OutB.

  3. Currently the model can correctly choose between Tool A and Tool B, but you're stuck with the model returning a single type: either string, OutA, or OutB. But really you want OutA or OutB depending on the user's query.

The best I can currently offer you is a workaround, which would work as follows:

  1. Create a struct kinda like a view model (let's call it ResponseViewModel), that you give Tool A and Tool B upon initializing each tool.

  2. Still use the model to choose the correct tool based on the user's requests, and let the model call Tool A or Tool B with the correct dynamically-generated arguments.

  3. If Tool A is called, in the call body, set the return structure you want, OutA, on the ResponseViewModel.

  4. If Tool B is called, in the call body, set the return structure you want, OutB, on the ResponseViewModel.

  5. Look at ResponseViewModel to determine which tool was called and access the data you need.

Let me know if this doesn't solve your problem, there may be some additional strategies to try. Thanks!

Accepted Answer

Hello!

Sorry for the delay, most of us at Apple have been on winter break :)

For this particular problem, we don't currently have a great solution. To be clear, from your description I'm interpreting your problem as:

  1. Given 2 tools Tool A and Tool B

  2. Each tool should return a different kind of structured output, OutA and OutB.

  3. Currently the model can correctly choose between Tool A and Tool B, but you're stuck with the model returning a single type: either string, OutA, or OutB. But really you want OutA or OutB depending on the user's query.

The best I can currently offer you is a workaround, which would work as follows:

  1. Create a struct kinda like a view model (let's call it ResponseViewModel), that you give Tool A and Tool B upon initializing each tool.

  2. Still use the model to choose the correct tool based on the user's requests, and let the model call Tool A or Tool B with the correct dynamically-generated arguments.

  3. If Tool A is called, in the call body, set the return structure you want, OutA, on the ResponseViewModel.

  4. If Tool B is called, in the call body, set the return structure you want, OutB, on the ResponseViewModel.

  5. Look at ResponseViewModel to determine which tool was called and access the data you need.

Let me know if this doesn't solve your problem, there may be some additional strategies to try. Thanks!

LanguageModelSession with multiple tools and structured outpout
 
 
Q