Building Tools for Foundation Models

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Building Tools for Foundation Models

Lesson two of this module demonstrated some weaknesses of LLMs and Apple Foundation Models. The most noticeable of these is that the model only contains the information trained into it. It has no information beyond that. If you attempt to ask something as simple as the current time, you’ll be told that’s not possible.

Unable to provide real-time information
Ipanfa zu dposace ceaj-qare avluhkilaup

struct WeatherForecastTool: Tool
let name = "weatherLookup"
let description = "Get the forecast temperatures and precipitation for the location provided as an argument."
@Generable
struct Arguments {
  @Guide(description: "The name of the location to get the forecast for.")
  var location: String
}
// 1
func call(arguments: Arguments) async throws -> WeatherForecast? {
  // 2
  var weatherForecast: WeatherForecast? = nil

  // 3
  if let coordinates = await getCoordinatesFor(arguments.location) {
    weatherForecast = try? await getForecastFor(coordinates: coordinates)
  }

  // 4
  return weatherForecast
}
@Generable
struct ForecastElements {
  @Guide(description: "Time of the forecast.")
  var time: String
  @Guide(description: "Forecast temperature at time.")
  var temperature: Float
  @Guide(description: "Probability of precipitation at time.")
  var precipitationProbability: Float
}
@Generable
struct WeatherForecast {
  @Guide(description: "The temperature and probability of precipitation element for a given time.")
  var forecasts: [ForecastElements]
}
messages = []
session = LanguageModelSession(
  tools: [WeatherForecastTool()],
  instructions: promptInstructions
)
@State private var session = LanguageModelSession(tools: [WeatherForecastTool()])
catch let error as LanguageModelSession.ToolCallError {
  addMessage(
    "Error occurred calling \(error.tool.name): \(error.localizedDescription)",
    isFromUser: false
  )
}
Give me the high temperature in Charlotte.
A warm day tomorrow in Charlotte.
I hopr zod siwinyoj ir Dqivhomhe.

Session transcript for tool request.
Niryoaz spuqxnfuzp teb fuur yexoabh.

What is the higher chance of rain for Atlanta tomorrow?
Not much chance of rain in Atlanta tomorrow.
Ves radr tqovhi ul beek uy Apxelfo nelodjel.

See forum comments
Download course materials from Github
Previous: Dynamic Guided Generation Next: Conclusion