I'm dealing with encyclopedic articles, and quite often the model will request the "Introduction" section, even though no such string appears in the sections array. Sometimes it also hallucinates sections that it thinks ought to exist based on the prompt.
While I was debugging this, I constructed a simpler toy example. It might be easier if we talk about that instead. Here's a playground I made to demonstrate the issue:
import Playgrounds
import FoundationModels
#Playground {
struct CityInfo: Tool {
let validCities: [String]
let name: String = "getCityInfo"
let description: String = "Get information about a city."
var parameters: GenerationSchema {
GenerationSchema(
type: GeneratedContent.self,
properties: [
GenerationSchema.Property(
name: "city",
description: "The city to get information about.",
type: String.self,
guides: [.anyOf(validCities)]
)
]
)
}
func call(arguments: GeneratedContent) throws -> String {
print(arguments.generatedContent)
let cityName = try arguments.value(String.self, forProperty: "city")
let cityInfo = getCityInfo(for: cityName)
return cityInfo
}
func getCityInfo(for city: String) -> String {
switch city {
case "London":
return "Some info about London..."
case "New York":
return "Some info about New York..."
case "Paris":
return "Some info about Paris..."
default:
return "Unrecognized city!"
}
}
}
let citiesDefinedAtRuntime = ["London", "New York", "Paris"]
let tools = [CityInfo(validCities: citiesDefinedAtRuntime)]
let instructions = """
You are a travel guide. Your job is to pick a city for the user to travel to based on their requirements. Once you've picked a city you should provide some information to the user about the city and why it's a good choice. To help you, you can use the getCityInfo tool to get information about a city.
"""
let session = LanguageModelSession(tools: tools, instructions: instructions)
let response = try await session.respond(to: "I want to travel to a big city in China")
}
When I run this, it usually tries to request info about Beijing (the generated content is {"city":"Beijing"}). Or, if I change the prompt to "I want to travel to a big city in Japan", it will try to request info about Tokyo, etc. You might need to run it a few times to reproduce the issue.
My understanding is that this should not be physically possible with guided generation. So, I'm wondering if I've set up the GenerationSchema correctly?
Topic:
Machine Learning & AI
SubTopic:
Foundation Models