Skip to content

Commit 7586d76

Browse files
committed
Reimplement LegacyChatGPTService with ChatGPTService
1 parent a883763 commit 7586d76

2 files changed

Lines changed: 74 additions & 514 deletions

File tree

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,59 @@ import Foundation
66
import IdentifiedCollections
77
import Preferences
88

9+
public enum ChatGPTServiceError: Error, LocalizedError {
10+
case chatModelNotAvailable
11+
case embeddingModelNotAvailable
12+
case endpointIncorrect
13+
case responseInvalid
14+
case otherError(String)
15+
16+
public var errorDescription: String? {
17+
switch self {
18+
case .chatModelNotAvailable:
19+
return "Chat model is not available, please add a model in the settings."
20+
case .embeddingModelNotAvailable:
21+
return "Embedding model is not available, please add a model in the settings."
22+
case .endpointIncorrect:
23+
return "ChatGPT endpoint is incorrect"
24+
case .responseInvalid:
25+
return "Response is invalid"
26+
case let .otherError(content):
27+
return content
28+
}
29+
}
30+
}
31+
32+
public struct ChatGPTError: Error, Codable, LocalizedError {
33+
public var error: ErrorContent
34+
public init(error: ErrorContent) {
35+
self.error = error
36+
}
37+
38+
public struct ErrorContent: Codable {
39+
public var message: String
40+
public var type: String?
41+
public var param: String?
42+
public var code: String?
43+
44+
public init(
45+
message: String,
46+
type: String? = nil,
47+
param: String? = nil,
48+
code: String? = nil
49+
) {
50+
self.message = message
51+
self.type = type
52+
self.param = param
53+
self.code = code
54+
}
55+
}
56+
57+
public var errorDescription: String? {
58+
error.message
59+
}
60+
}
61+
962
public enum ChatGPTResponse: Equatable {
1063
case status(String)
1164
case partialText(String)
@@ -520,5 +573,11 @@ extension ChatGPTService {
520573

521574
return requestBody
522575
}
576+
577+
578+
func maxTokenForReply(maxToken: Int, remainingTokens: Int?) -> Int? {
579+
guard let remainingTokens else { return nil }
580+
return min(maxToken / 2, remainingTokens)
581+
}
523582
}
524583

0 commit comments

Comments
 (0)