Skip to content

Commit 74fecfd

Browse files
committed
Add tool invocation tests from upstream PR #114 (0840626)
1 parent 0667c18 commit 74fecfd

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

Libraries/IntegrationTestHelpers/IntegrationTestHelpers.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public actor IntegrationTestModels {
154154

155155
// MARK: - ChatSession Tests
156156

157-
private let generateParameters = GenerateParameters(maxTokens: 200)
157+
private let generateParameters = GenerateParameters(maxTokens: 200, temperature: 0)
158158

159159
public enum ChatSessionTests {
160160

@@ -250,6 +250,54 @@ public enum ChatSessionTests {
250250
!responseText.isEmpty || !toolCalls.isEmpty,
251251
"Expected either text or tool calls, got neither (generated \(info?.generationTokenCount ?? 0) tokens, stop reason: \(String(describing: info?.stopReason)))"
252252
)
253+
254+
// If we got tool calls, feed back a tool result and verify the model responds
255+
if !toolCalls.isEmpty {
256+
let followUp = try await streamAndCollect(
257+
session.streamResponse(
258+
to: "Foggy with a high in the low 60s, clearing later in the day",
259+
role: .tool, images: [], videos: []),
260+
label: "Tool result")
261+
try check(
262+
!followUp.isEmpty,
263+
"Expected a response after providing tool result, got empty string"
264+
)
265+
}
266+
}
267+
268+
public static func toolInvocation(container: LMModelContainer) async throws {
269+
struct EmptyInput: Codable {}
270+
271+
struct TimeOutput: Codable {
272+
let time: String
273+
}
274+
275+
let timeTool = Tool<EmptyInput, TimeOutput>(
276+
name: "get_time",
277+
description: "Get the current date and time including day of week.",
278+
parameters: []
279+
) { _ in
280+
TimeOutput(time: "Wed Feb 18 17:50:43 PST 2026")
281+
}
282+
283+
let session = ChatSession(
284+
container, generateParameters: generateParameters,
285+
tools: [timeTool.schema]
286+
) { toolCall in
287+
if toolCall.function.name == timeTool.name {
288+
return try await toolCall.execute(with: timeTool).toolResult
289+
}
290+
return "Unknown tool: \(toolCall.function.name)"
291+
}
292+
293+
let result = try await streamAndCollect(
294+
session.streamResponse(
295+
to: "What day of week is it?"), label: "Tool invocation")
296+
297+
try check(
298+
result.lowercased().contains("wed") || result.lowercased().contains("wednesday"),
299+
"Expected 'Wed' or 'Wednesday' in response, got: \(result)"
300+
)
253301
}
254302

255303
public static func promptRehydration(container: LMModelContainer) async throws {

0 commit comments

Comments
 (0)