Post

Replies

Boosts

Views

Activity

Reply to Foundation Models Tools not invoking
What am I missing here? struct ContactsTool: Tool { let name = "getContacts" let description = """ Get a contact from the address book matching the name """ @Generable struct Arguments { @Guide(description: "The search query used to search the contact example name or email or contact number") let searchQuery: String? } func call(arguments: Arguments) async -> ToolOutput { do { // Request permission to access the person's contacts. let store = CNContactStore() try await store.requestAccess(for: .contacts) let keysToFetch = [CNContactGivenNameKey, CNContactBirthdayKey] as [CNKeyDescriptor] let request = CNContactFetchRequest(keysToFetch: keysToFetch) var contacts: [CNContact] = [] try store.enumerateContacts(with: request) { contact, stop in if contact.givenName.lowercased().contains(arguments.searchQuery?.lowercased() ?? "") { contacts.append(contact) } else if contact.middleName.lowercased().contains(arguments.searchQuery?.lowercased() ?? "") { contacts.append(contact) } else if contact.familyName.lowercased().contains(arguments.searchQuery?.lowercased() ?? "") { contacts.append(contact) } else if contact.emailAddresses.isEmpty == false { for email in contact.emailAddresses { if email.value.lowercased.contains(arguments.searchQuery?.lowercased() ?? "") { contacts.append(contact) break } } } } guard let pickedContact = contacts.shuffled().first else { return ToolOutput("No contact found") } return ToolOutput("These are the picked contact from your phone: name :\(pickedContact.givenName) \(pickedContact.familyName) email: \(pickedContact.emailAddresses.first?.value ?? "")") } catch { return ToolOutput("No contact found") } } } struct DocumentGenerationAssistTool: Tool { var name: String = "documentGenerationAssistTool" var description: String = "provides sample data for document generation" @Generable struct Arguments { @Guide(description: "the name or type of the document.") var documentName: String } func call(arguments: Arguments) async throws -> ToolOutput { return ToolOutput(GeneratedContent(SignRequest.sampleNonDisclosureAgreement.documet)) } }``` My session Creation: ```language static let modelInstruction = Instructions { "You are a document generation assistant for an eSignature platform. Your task is to convert plain text input into professional, well-structured HTML documents that will be used in legally binding signing requests. These documents will be edited in a rich text editor and exported to PDF." "The user may only specify the document name and may be a list of signers or receiver or approver" """ Always use the \(contactTool.name) tool to find the recipients """ """ Always use the \(documentAssistTool.name) tool for the document generation """ } private var session = LanguageModelSession(tools: [contactTool, documentAssistTool], instructions: modelInstruction)``` My user prompt -> "Generate a rental agreement with minimum of 2 pages need to be send for baarathi as a signer residing in Guduvancherry chennai pincode 603202"
Jun ’25