Help with dates in Foundation Model custom Tool

I have an app that stores lots of data that is of interest to the user. Analogies would be the Photos apps or the Health app.

I'm trying to use the Foundation Models framework to allow users to surface information they find interesting using natural language, for example, "Tell me about the widgets from yesterday" or "Tell me about the widgets for the last 3 days". Specifically, I'm trying to get a date range passed down to the Tool so that I can pull the relevant widgets from the database in the call function.

What is the right way to set up the Arguments to get at a date range?

Answered by MoppyMopperson in 867826022

Our recommendation is to write a date components like struct that is Generable and contains only the date components you care about. make the model generate that struct, and then manually convert it into a date object.

You might have something like this:

@Generable struct Arguments {
  var startDate: CalendarDateComponents?
  var endDate: CalendarDateComponents?
}

@Generable struct CalendarDateComponents {
  var year: Int
  var month: Int
  var day: Int
}

When prompting, make sure to tell the model the current date.

Accepted Answer

Our recommendation is to write a date components like struct that is Generable and contains only the date components you care about. make the model generate that struct, and then manually convert it into a date object.

You might have something like this:

@Generable struct Arguments {
  var startDate: CalendarDateComponents?
  var endDate: CalendarDateComponents?
}

@Generable struct CalendarDateComponents {
  var year: Int
  var month: Int
  var day: Int
}

When prompting, make sure to tell the model the current date.

Awesome, this is super helpful!

This is definitely getting me started on the right track, but I'm finding if I try to give the LLM relative dates, like "yesterday" it is populating the startDate and endDate arguments with nil or dates that are just wrong.

I've provided the current date to both the tool's description and the foundation model session description, as suggested.

Help with dates in Foundation Model custom Tool
 
 
Q