Skip to content

Commit 174abe5

Browse files
committed
Fix that non-stream api was not correctly handling function call results
1 parent 6974682 commit 174abe5

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

Tool/Sources/OpenAIService/CompletionAPI.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@ protocol CompletionAPI {
1010

1111
/// https://platform.openai.com/docs/api-reference/chat/create
1212
struct CompletionResponseBody: Codable, Equatable {
13-
typealias Message = CompletionRequestBody.Message
13+
struct Message: Codable, Equatable {
14+
/// The role of the message.
15+
var role: ChatMessage.Role
16+
/// The content of the message.
17+
var content: String?
18+
/// When we want to reply to a function call with the result, we have to provide the
19+
/// name of the function call, and include the result in `content`.
20+
///
21+
/// - important: It's required when the role is `function`.
22+
var name: String?
23+
/// When the bot wants to call a function, it will reply with a function call in format:
24+
/// ```json
25+
/// {
26+
/// "name": "weather",
27+
/// "arguments": "{ \"location\": \"earth\" }"
28+
/// }
29+
/// ```
30+
var function_call: CompletionRequestBody.MessageFunctionCall?
31+
}
1432

1533
struct Choice: Codable, Equatable {
1634
var message: Message
@@ -89,7 +107,12 @@ struct OpenAICompletionAPI: CompletionAPI {
89107
.otherError(String(data: result, encoding: .utf8) ?? "Unknown Error")
90108
}
91109

92-
return try JSONDecoder().decode(CompletionResponseBody.self, from: result)
110+
do {
111+
return try JSONDecoder().decode(CompletionResponseBody.self, from: result)
112+
} catch {
113+
dump(error)
114+
fatalError()
115+
}
93116
}
94117
}
95118

0 commit comments

Comments
 (0)