Skip to content

Commit 42016e3

Browse files
committed
Allow disabling auto function calling handling
1 parent ecd2b9f commit 42016e3

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ public class ChatGPTService: ChatGPTServiceType {
9898
var functionCall: ChatMessage.FunctionCall?
9999
var functionCallMessageID = ""
100100
var isInitialCall = true
101-
while functionCall != nil || isInitialCall {
101+
loop: while functionCall != nil || isInitialCall {
102102
isInitialCall = false
103103
if let call = functionCall {
104+
if !configuration.runFunctionsAutomatically {
105+
break loop
106+
}
104107
functionCall = nil
105108
await runFunctionCall(call, messageId: functionCallMessageID)
106109
}
@@ -148,6 +151,9 @@ public class ChatGPTService: ChatGPTServiceType {
148151
var finalResult = message?.content
149152
var functionCall = message?.functionCall
150153
while let call = functionCall {
154+
if !configuration.runFunctionsAutomatically {
155+
break
156+
}
151157
functionCall = nil
152158
await runFunctionCall(call)
153159
guard let nextMessage = try await sendMemoryAndWait() else { break }
@@ -375,23 +381,23 @@ extension ChatGPTService {
375381
content: nil,
376382
name: call.name
377383
)
378-
384+
379385
await memory.appendMessage(responseMessage)
380386

381387
function.reportProgress = { [weak self] summary in
382388
await self?.memory.updateMessage(id: messageId) { message in
383389
message.summary = summary
384390
}
385391
}
386-
392+
387393
do {
388394
// Run the function
389395
let result = try await function.call(argumentsJsonString: call.arguments)
390396

391397
await memory.updateMessage(id: messageId) { message in
392398
message.content = result.botReadableContent
393399
}
394-
400+
395401
return result.botReadableContent
396402
} catch {
397403
// For errors, use the error message as the result.

Tool/Sources/OpenAIService/Configuration/ChatGPTConfiguration.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public protocol ChatGPTConfiguration {
1010
var stop: [String] { get }
1111
var maxTokens: Int { get }
1212
var minimumReplyTokens: Int { get }
13+
var runFunctionsAutomatically: Bool { get }
1314
}
1415

1516
public extension ChatGPTConfiguration {
@@ -38,9 +39,17 @@ public extension ChatGPTConfiguration {
3839
}
3940

4041
func overriding(
41-
_ overrides: OverridingChatGPTConfiguration<Self>.Overriding = .init()
42+
_ overrides: OverridingChatGPTConfiguration<Self>.Overriding
4243
) -> OverridingChatGPTConfiguration<Self> {
4344
.init(overriding: self, with: overrides)
4445
}
46+
47+
func overriding(
48+
_ update: (inout OverridingChatGPTConfiguration<Self>.Overriding) -> Void = { _ in }
49+
) -> OverridingChatGPTConfiguration<Self> {
50+
var overrides = OverridingChatGPTConfiguration<Self>.Overriding()
51+
update(&overrides)
52+
return .init(overriding: self, with: overrides)
53+
}
4554
}
4655

Tool/Sources/OpenAIService/Configuration/UserPreferenceChatGPTConfiguration.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ public struct UserPreferenceChatGPTConfiguration: ChatGPTConfiguration {
3636
300
3737
}
3838

39+
public var runFunctionsAutomatically: Bool {
40+
true
41+
}
42+
3943
public init() {}
4044
}
4145

4246
public class OverridingChatGPTConfiguration<
4347
Configuration: ChatGPTConfiguration
4448
>: ChatGPTConfiguration {
4549
public struct Overriding {
46-
var featureProvider: ChatFeatureProvider?
47-
var temperature: Double?
48-
var model: String?
49-
var endPoint: String?
50-
var apiKey: String?
51-
var stop: [String]?
52-
var maxTokens: Int?
53-
var minimumReplyTokens: Int?
50+
public var featureProvider: ChatFeatureProvider?
51+
public var temperature: Double?
52+
public var model: String?
53+
public var endPoint: String?
54+
public var apiKey: String?
55+
public var stop: [String]?
56+
public var maxTokens: Int?
57+
public var minimumReplyTokens: Int?
58+
public var runFunctionsAutomatically: Bool?
5459

5560
public init(
5661
temperature: Double? = nil,
@@ -60,7 +65,8 @@ public class OverridingChatGPTConfiguration<
6065
minimumReplyTokens: Int? = nil,
6166
featureProvider: ChatFeatureProvider? = nil,
6267
endPoint: String? = nil,
63-
apiKey: String? = nil
68+
apiKey: String? = nil,
69+
runFunctionsAutomatically: Bool? = nil
6470
) {
6571
self.temperature = temperature
6672
self.model = model
@@ -70,14 +76,15 @@ public class OverridingChatGPTConfiguration<
7076
self.featureProvider = featureProvider
7177
self.endPoint = endPoint
7278
self.apiKey = apiKey
79+
self.runFunctionsAutomatically = runFunctionsAutomatically
7380
}
7481
}
7582

7683
private let configuration: Configuration
7784
public var overriding = Overriding()
7885

7986
public init(overriding configuration: Configuration, with overrides: Overriding = .init()) {
80-
self.overriding = overrides
87+
overriding = overrides
8188
self.configuration = configuration
8289
}
8390

@@ -116,5 +123,9 @@ public class OverridingChatGPTConfiguration<
116123
public var minimumReplyTokens: Int {
117124
overriding.minimumReplyTokens ?? configuration.minimumReplyTokens
118125
}
126+
127+
public var runFunctionsAutomatically: Bool {
128+
overriding.runFunctionsAutomatically ?? configuration.runFunctionsAutomatically
129+
}
119130
}
120131

0 commit comments

Comments
 (0)