Skip to content

Commit c3ad62e

Browse files
Adding required
1 parent 9d2e7d3 commit c3ad62e

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Sources/OpenAI/Public/Shared/ToolChoice.swift

+23-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@
88
import Foundation
99

1010
/// string `none` means the model will not call a function and instead generates a message.
11+
///
1112
/// `auto` means the model can pick between generating a message or calling a function.
13+
///
1214
/// `object` Specifies a tool the model should use. Use to force the model to call a specific function. The type of the tool. Currently, only` function` is supported. `{"type: "function", "function": {"name": "my_function"}}`
15+
///
16+
/// `required` To force the model to always call one or more functions, you can set tool_choice: "required". The model will then select which function(s) to call.
17+
///
18+
/// [Function Calling](https://platform.openai.com/docs/guides/function-calling)
1319
public enum ToolChoice: Codable, Equatable {
1420
case none
1521
case auto
22+
case required
1623
case function(type: String = "function", name: String)
1724

1825
enum CodingKeys: String, CodingKey {
1926
case none = "none"
2027
case auto = "auto"
28+
case required = "required"
2129
case type = "type"
2230
case function = "function"
2331
}
@@ -34,32 +42,38 @@ public enum ToolChoice: Codable, Equatable {
3442
case .auto:
3543
var container = encoder.singleValueContainer()
3644
try container.encode(CodingKeys.auto.rawValue)
45+
case .required:
46+
var container = encoder.singleValueContainer()
47+
try container.encode(CodingKeys.required.rawValue)
3748
case .function(let type, let name):
3849
var container = encoder.container(keyedBy: CodingKeys.self)
3950
try container.encode(type, forKey: .type)
4051
var functionContainer = container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function)
4152
try functionContainer.encode(name, forKey: .name)
53+
4254
}
4355
}
4456

4557
public init(from decoder: Decoder) throws {
4658
// Handle the 'function' case:
4759
if let container = try? decoder.container(keyedBy: CodingKeys.self),
4860
let functionContainer = try? container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function) {
49-
let name = try functionContainer.decode(String.self, forKey: .name)
50-
self = .function(type: "function", name: name)
51-
return
61+
let name = try functionContainer.decode(String.self, forKey: .name)
62+
self = .function(type: "function", name: name)
63+
return
5264
}
53-
65+
5466
// Handle the 'auto' and 'none' cases
5567
let container = try decoder.singleValueContainer()
5668
switch try container.decode(String.self) {
57-
case "none":
58-
self = .none
59-
case "auto":
60-
self = .auto
69+
case "none":
70+
self = .none
71+
case "auto":
72+
self = .auto
73+
case "required":
74+
self = .required
6175
default:
62-
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid tool_choice structure")
76+
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid tool_choice structure")
6377
}
6478
}
6579
}

0 commit comments

Comments
 (0)