Skip to content

Commit 9e49591

Browse files
committed
Update to generate non-stream response from stream reponse
1 parent 06cb361 commit 9e49591

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

Tool/Sources/OpenAIService/APIs/OpenAIChatCompletionsService.swift

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -278,34 +278,38 @@ actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI
278278
}
279279

280280
func callAsFunction() async throws -> ChatCompletionResponseBody {
281-
requestBody.stream = false
282-
var request = URLRequest(url: endpoint)
283-
request.httpMethod = "POST"
284-
let encoder = JSONEncoder()
285-
request.httpBody = try encoder.encode(requestBody)
286-
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
287-
288-
Self.setupAppInformation(&request)
289-
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
290-
291-
let (result, response) = try await URLSession.shared.data(for: request)
292-
guard let response = response as? HTTPURLResponse else {
293-
throw ChatGPTServiceError.responseInvalid
294-
}
295-
296-
guard response.statusCode == 200 else {
297-
let error = try? JSONDecoder().decode(CompletionAPIError.self, from: result)
298-
throw error ?? ChatGPTServiceError
299-
.otherError(String(data: result, encoding: .utf8) ?? "Unknown Error")
300-
}
301-
302-
do {
303-
let body = try JSONDecoder().decode(ResponseBody.self, from: result)
304-
return body.formalized()
305-
} catch {
306-
dump(error)
307-
throw error
281+
let stream: AsyncThrowingStream<ChatCompletionsStreamDataChunk, Error> =
282+
try await callAsFunction()
283+
284+
var body = ChatCompletionResponseBody(
285+
id: nil,
286+
object: "",
287+
model: "",
288+
message: .init(role: .assistant, content: ""),
289+
otherChoices: [],
290+
finishReason: ""
291+
)
292+
for try await chunk in stream {
293+
if let id = chunk.id {
294+
body.id = id
295+
}
296+
if let finishReason = chunk.finishReason {
297+
body.finishReason = finishReason
298+
}
299+
if let model = chunk.model {
300+
body.model = model
301+
}
302+
if let object = chunk.object {
303+
body.object = object
304+
}
305+
if let role = chunk.message?.role {
306+
body.message.role = role
307+
}
308+
if let text = chunk.message?.content {
309+
body.message.content += text
310+
}
308311
}
312+
return body
309313
}
310314

311315
static func setupAppInformation(_ request: inout URLRequest) {

0 commit comments

Comments
 (0)