Skip to content

Commit 63c5edc

Browse files
improved tool use with anthropic (#119)
1 parent 7078ec3 commit 63c5edc

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Sources/AIProxy/Anthropic/AnthropicMessageRequestBody.swift

+35
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,19 @@ public enum AnthropicInputContent: Encodable {
267267
case image(mediaType: AnthropicImageMediaType, data: String)
268268
case pdf(data: String)
269269
case text(String)
270+
case toolUse(id: String, name: String, input: [String: AIProxyJSONValue])
271+
case toolResult(toolUseId: String, content: String)
270272

271273
private enum CodingKeys: String, CodingKey {
272274
case image
273275
case source
274276
case text
275277
case type
278+
case id
279+
case name
280+
case input
281+
case toolUseId = "tool_use_id"
282+
case content
276283
}
277284

278285
private enum SourceCodingKeys: String, CodingKey {
@@ -299,6 +306,15 @@ public enum AnthropicInputContent: Encodable {
299306
case .text(let txt):
300307
try container.encode("text", forKey: .type)
301308
try container.encode(txt, forKey: .text)
309+
case .toolUse(id: let id, name: let name, input: let input):
310+
try container.encode("tool_use", forKey: .type)
311+
try container.encode(id, forKey: .id)
312+
try container.encode(name, forKey: .name)
313+
try container.encode(input, forKey: .input)
314+
case .toolResult(toolUseId: let toolUseId, content: let content):
315+
try container.encode("tool_result", forKey: .type)
316+
try container.encode(toolUseId, forKey: .toolUseId)
317+
try container.encode(content, forKey: .content)
302318
}
303319
}
304320
}
@@ -344,6 +360,25 @@ public enum AnthropicToolChoice: Encodable {
344360
case any
345361
case auto
346362
case tool(name: String)
363+
364+
public func encode(to encoder: Encoder) throws {
365+
var container = encoder.container(keyedBy: CodingKeys.self)
366+
367+
switch self {
368+
case .auto:
369+
try container.encode("auto", forKey: .type)
370+
case .any:
371+
try container.encode("any", forKey: .type)
372+
case .tool(let name):
373+
try container.encode("tool", forKey: .type)
374+
try container.encode(name, forKey: .name)
375+
}
376+
}
377+
378+
private enum CodingKeys: String, CodingKey {
379+
case type
380+
case name
381+
}
347382
}
348383

349384

Sources/AIProxy/Anthropic/AnthropicMessageResponseBody.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct AnthropicMessageResponseBody: Decodable {
3333

3434
public enum AnthropicMessageResponseContent: Decodable {
3535
case text(String)
36-
case toolUse(id: String, name: String, input: [String: Any])
36+
case toolUse(id: String, name: String, input: [String: AIProxyJSONValue])
3737

3838
private enum CodingKeys: String, CodingKey {
3939
case type
@@ -59,7 +59,7 @@ public enum AnthropicMessageResponseContent: Decodable {
5959
let id = try container.decode(String.self, forKey: .id)
6060
let name = try container.decode(String.self, forKey: .name)
6161
let input = try container.decode([String: AIProxyJSONValue].self, forKey: .input)
62-
self = .toolUse(id: id, name: name, input: input.untypedDictionary)
62+
self = .toolUse(id: id, name: name, input: input)
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)