Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kishikawakatsumi/1d2359eb95c7ace3c3da8d73889e9dcc to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/1d2359eb95c7ace3c3da8d73889e9dcc to your computer and use it in GitHub Desktop.
[FB18768055]Tool calling is not called.
I am trying tool calling in FoundationModels with the following code. When given the prompt “Schedule a meeting for next Wednesday at 10 a.m.”, tool calling is not called, and an unexpected response is returned. The response is "Please provide the name of the calendar where the event should be created.".
When the prompt is “Add a meeting to the schedule for next Wednesday at 10:00 a.m.”, the tool is called (although the parameters are incorrect).
import Cocoa
import FoundationModels
import EventKit
@main
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
Task {
let session = LanguageModelSession(
tools: [CalendarTool()],
instructions: "You are a secretary. Select one action to execute from ‘create’, ‘query’, ‘read’, or ‘update’ from the prompt, and pass the processing to CalendarTool."
)
let response = try await session.respond(to: Prompt("Schedule a meeting for next Wednesday at 10 a.m."))
print(response.content)
let feedback = LanguageModelFeedbackAttachment(
input: Array(session.transcript),
output: Array(response.transcriptEntries),
sentiment: .negative
)
let fileManager = FileManager.default
let tempDirectoryURL = fileManager.temporaryDirectory
// Convert the feedback to JSON.
let json = try! JSONEncoder().encode(feedback)
try! json.write(to: tempDirectoryURL.appendingPathComponent("feedback.json"))
print(tempDirectoryURL.absoluteString)
}
}
}
public struct CalendarTool: Tool {
public let name = "CalendarTool"
public let description = "Create, read, and query calendar events"
@Generable
public struct Arguments {
@Guide(description: "The action to perform: 'create', 'query', 'read', 'update'")
public var action: String
@Guide(description: "Event title for creating or updating")
public var title: String?
@Guide(description: "Start date in ISO format (YYYY-MM-DD HH:mm:ss)")
public var startDate: String?
@Guide(description: "End date in ISO format (YYYY-MM-DD HH:mm:ss)")
public var endDate: String?
@Guide(description: "Location for the event")
public var location: String?
@Guide(description: "Notes for the event")
public var notes: String?
@Guide(description: "Calendar name to use (defaults to default calendar)")
public var calendarName: String?
@Guide(description: "Number of days to query (for query action)")
public var daysAhead: Int?
@Guide(description: "Event identifier for reading or updating specific event")
public var eventId: String?
public init(
action: String = "",
title: String? = nil,
startDate: String? = nil,
endDate: String? = nil,
location: String? = nil,
notes: String? = nil,
calendarName: String? = nil,
daysAhead: Int? = nil,
eventId: String? = nil
) {
self.action = action
self.title = title
self.startDate = startDate
self.endDate = endDate
self.location = location
self.notes = notes
self.calendarName = calendarName
self.daysAhead = daysAhead
self.eventId = eventId
}
}
public init() {}
public func call(arguments: Arguments) async throws -> ToolOutput {
print(arguments.startDate)
print(arguments.endDate)
return ToolOutput("Created events \(arguments.startDate) - \(arguments.endDate)")
}
}
{"version":"1.0","type":"FoundationModels.LanguageModelFeedbackAttachment","feedback":{"type":"standardFeedback","standardFeedback":{"issues":[],"desiredOutputExamples":[],"input":[{"contents":[{"text":"You are a secretary. Select one action to execute from ‘create’, ‘query’, ‘read’, or ‘update’ from the prompt, and pass the processing to CalendarTool.","type":"text","id":"15192378-F09B-46A1-8E49-5796847FE6C7"}],"tools":[{"function":{"description":"Create, read, and query calendar events","parameters":{"title":"Arguments","type":"object","x-order":["action","title","startDate","endDate","location","notes","calendarName","daysAhead","eventId"],"required":["action"],"properties":{"endDate":{"description":"End date in ISO format (YYYY-MM-DD HH:mm:ss)","type":"string"},"startDate":{"description":"Start date in ISO format (YYYY-MM-DD HH:mm:ss)","type":"string"},"location":{"description":"Location for the event","type":"string"},"notes":{"description":"Notes for the event","type":"string"},"daysAhead":{"description":"Number of days to query (for query action)","type":"integer"},"eventId":{"description":"Event identifier for reading or updating specific event","type":"string"},"calendarName":{"description":"Calendar name to use (defaults to default calendar)","type":"string"},"action":{"description":"The action to perform: 'create', 'query', 'read', 'update'","type":"string"},"title":{"description":"Event title for creating or updating","type":"string"}},"additionalProperties":false},"name":"CalendarTool"},"type":"function"}],"id":"216DB8A2-04BA-473B-93E2-1687E45B6DCB","role":"instructions"},{"options":{},"role":"user","contents":[{"id":"22DA98C2-075C-44A3-AFD5-D64B21A9F809","text":"Schedule a meeting for next Wednesday at 10 a.m.","type":"text"}],"id":"0714398B-9B8F-4379-A19E-FB368042CFD8"},{"assets":["com.apple.fm.language.instruct_3b.tokenizer_12.0.0.13.202232,0","com.apple.fm.language.instruct_3b.fm_api_generic.draft_12.0.81407.13.202252,0","com.apple.fm.language.instruct_3b.fm_api_generic_12.0.0.13.101732,0"],"id":"86FA2805-671D-4B20-ABE9-2E108DD73F93","contents":[{"id":"0","type":"text","text":"Please provide the name of the calendar where the event should be created."}],"role":"response"}],"output":[{"assets":["com.apple.fm.language.instruct_3b.tokenizer_12.0.0.13.202232,0","com.apple.fm.language.instruct_3b.fm_api_generic.draft_12.0.81407.13.202252,0","com.apple.fm.language.instruct_3b.fm_api_generic_12.0.0.13.101732,0"],"id":"86FA2805-671D-4B20-ABE9-2E108DD73F93","contents":[{"type":"text","text":"Please provide the name of the calendar where the event should be created.","id":"0"}],"role":"response"}],"type":"standard","sentiment":"negative"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment