Skip to content

Commit cd153f5

Browse files
committed
Fix function call implemetation
1 parent 413b235 commit cd153f5

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ extension ChatGPTService {
356356
id: messageId,
357357
role: .function,
358358
content: content,
359+
name: call.name,
359360
summary: "Function `\(call.name)` not found."
360361
)
361362
await memory.appendMessage(responseMessage)

Tool/Sources/OpenAIService/CompletionStreamAPI.swift

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import AsyncAlgorithms
22
import Foundation
33
import Preferences
44

5-
typealias CompletionStreamAPIBuilder = (String, ChatFeatureProvider, URL, CompletionRequestBody) -> CompletionStreamAPI
5+
typealias CompletionStreamAPIBuilder = (String, ChatFeatureProvider, URL, CompletionRequestBody)
6+
-> CompletionStreamAPI
67

78
protocol CompletionStreamAPI {
89
func callAsFunction() async throws -> (
@@ -32,22 +33,22 @@ struct CompletionRequestBody: Encodable, Equatable {
3233
/// ```
3334
var function_call: MessageFunctionCall?
3435
}
35-
36+
3637
struct MessageFunctionCall: Codable, Equatable {
3738
/// The name of the
3839
var name: String
3940
/// A JSON string.
4041
var arguments: String?
4142
}
42-
43+
4344
enum FunctionCallStrategy: Encodable, Equatable {
4445
/// Forbid the bot to call any function.
4546
case none
4647
/// Let the bot choose what function to call.
4748
case auto
4849
/// Force the bot to call a function with the given name.
4950
case name(String)
50-
51+
5152
struct CallFunctionNamed: Codable {
5253
var name: String
5354
}
@@ -64,7 +65,7 @@ struct CompletionRequestBody: Encodable, Equatable {
6465
}
6566
}
6667
}
67-
68+
6869
struct Function: Codable {
6970
var name: String
7071
var description: String
@@ -86,7 +87,39 @@ struct CompletionRequestBody: Encodable, Equatable {
8687
var user: String?
8788
/// Pass nil to let the bot decide.
8889
var function_call: FunctionCallStrategy?
89-
var functions: [ChatGPTFunctionSchema]? = nil
90+
var functions: [ChatGPTFunctionSchema]?
91+
92+
init(
93+
model: String,
94+
messages: [Message],
95+
temperature: Double? = nil,
96+
top_p: Double? = nil,
97+
n: Double? = nil,
98+
stream: Bool? = nil,
99+
stop: [String]? = nil,
100+
max_tokens: Int? = nil,
101+
presence_penalty: Double? = nil,
102+
frequency_penalty: Double? = nil,
103+
logit_bias: [String: Double]? = nil,
104+
user: String? = nil,
105+
function_call: FunctionCallStrategy? = nil,
106+
functions: [ChatGPTFunctionSchema] = []
107+
) {
108+
self.model = model
109+
self.messages = messages
110+
self.temperature = temperature
111+
self.top_p = top_p
112+
self.n = n
113+
self.stream = stream
114+
self.stop = stop
115+
self.max_tokens = max_tokens
116+
self.presence_penalty = presence_penalty
117+
self.frequency_penalty = frequency_penalty
118+
self.logit_bias = logit_bias
119+
self.user = user
120+
self.function_call = function_call
121+
self.functions = functions.isEmpty ? nil : functions
122+
}
90123
}
91124

92125
struct CompletionStreamDataTrunk: Codable {
@@ -106,7 +139,7 @@ struct CompletionStreamDataTrunk: Codable {
106139
var name: String?
107140
var arguments: String?
108141
}
109-
142+
110143
var role: ChatMessage.Role?
111144
var content: String?
112145
var function_call: FunctionCall?

0 commit comments

Comments
 (0)