Skip to content

Commit 097c20b

Browse files
authored
Update Chat History format
1 parent 19b5b72 commit 097c20b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Shared/ChatGPTAPI.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class ChatGPTAPI {
6969
return try JSONSerialization.data(withJSONObject: jsonBody)
7070
}
7171

72+
private func appendToHistoryList(userText: String, responseText: String) {
73+
self.historyList.append("User: \(userText)\n\n\nChatGPT: \(responseText)<|im_end|>\n")
74+
}
75+
7276
func sendMessageStream(text: String) async throws -> AsyncThrowingStream<String, Error> {
7377
var urlRequest = self.urlRequest
7478
urlRequest.httpBody = try jsonBody(text: text)
@@ -86,17 +90,17 @@ class ChatGPTAPI {
8690
return AsyncThrowingStream<String, Error> { continuation in
8791
Task(priority: .userInitiated) {
8892
do {
89-
var streamText = ""
93+
var responseText = ""
9094
for try await line in result.lines {
9195
if line.hasPrefix("data: "),
9296
let data = line.dropFirst(6).data(using: .utf8),
9397
let response = try? self.jsonDecoder.decode(CompletionResponse.self, from: data),
9498
let text = response.choices.first?.text {
95-
streamText += text
99+
responseText += text
96100
continuation.yield(text)
97101
}
98102
}
99-
self.historyList.append(streamText)
103+
self.appendToHistoryList(userText: text, responseText: responseText)
100104
continuation.finish()
101105
} catch {
102106
continuation.finish(throwing: error)
@@ -122,7 +126,7 @@ class ChatGPTAPI {
122126
do {
123127
let completionResponse = try self.jsonDecoder.decode(CompletionResponse.self, from: data)
124128
let responseText = completionResponse.choices.first?.text ?? ""
125-
self.historyList.append(responseText)
129+
self.appendToHistoryList(userText: text, responseText: responseText)
126130
return responseText
127131
} catch {
128132
throw error

0 commit comments

Comments
 (0)