@@ -56,77 +56,32 @@ public struct ChatGPTError: Error, Codable, LocalizedError {
5656
5757public actor ChatGPTService : ChatGPTServiceType {
5858 public var systemPrompt : String
59-
60- public var defaultTemperature : Double {
61- min ( max ( 0 , UserDefaults . shared. value ( for: \. chatGPTTemperature) ) , 2 )
62- }
63-
64- var temperature : Double ?
65-
66- public var model : String {
67- let value = UserDefaults . shared. value ( for: \. chatGPTModel)
68- if value. isEmpty { return " gpt-3.5-turbo " }
69- return value
70- }
71-
72- var designatedProvider : ChatFeatureProvider ?
73-
74- public var endpoint : String {
75- switch designatedProvider ?? UserDefaults . shared. value ( for: \. chatFeatureProvider) {
76- case . openAI:
77- let baseURL = UserDefaults . shared. value ( for: \. openAIBaseURL)
78- if baseURL. isEmpty { return " https://api.openai.com/v1/chat/completions " }
79- return " \( baseURL) /v1/chat/completions "
80- case . azureOpenAI:
81- let baseURL = UserDefaults . shared. value ( for: \. azureOpenAIBaseURL)
82- let deployment = UserDefaults . shared. value ( for: \. azureChatGPTDeployment)
83- let version = " 2023-05-15 "
84- if baseURL. isEmpty { return " " }
85- return " \( baseURL) /openai/deployments/ \( deployment) /chat/completions?api-version= \( version) "
86- }
87- }
88-
89- public var apiKey : String {
90- switch designatedProvider ?? UserDefaults . shared. value ( for: \. chatFeatureProvider) {
91- case . openAI:
92- return UserDefaults . shared. value ( for: \. openAIAPIKey)
93- case . azureOpenAI:
94- return UserDefaults . shared. value ( for: \. azureOpenAIAPIKey)
95- }
96- }
97-
98- public var maxToken : Int {
99- UserDefaults . shared. value ( for: \. chatGPTMaxToken)
100- }
101-
10259 public var history : [ ChatMessage ] = [ ] {
10360 didSet { objectWillChange. send ( ) }
10461 }
10562
106- var stop : [ String ]
63+ public var configuration : ChatGPTConfiguration
64+
10765 var uuidGenerator : ( ) -> String = { UUID ( ) . uuidString }
10866 var cancelTask : Cancellable ?
10967 var buildCompletionStreamAPI : CompletionStreamAPIBuilder = OpenAICompletionStreamAPI . init
11068 var buildCompletionAPI : CompletionAPIBuilder = OpenAICompletionAPI . init
11169
11270 public init (
11371 systemPrompt: String = " " ,
114- temperature: Double ? = nil ,
115- stop: [ String ] = [ ] ,
116- designatedProvider: ChatFeatureProvider ? = nil
72+ configuration: ChatGPTConfiguration = UserPreferenceChatGPTConfiguration ( )
11773 ) {
11874 self . systemPrompt = systemPrompt
119- self . temperature = temperature
120- self . stop = stop
121- self . designatedProvider = designatedProvider
75+ self . configuration = configuration
12276 }
12377
12478 public func send(
12579 content: String ,
12680 summary: String ? = nil
12781 ) async throws -> AsyncThrowingStream < String , Error > {
128- guard let url = URL ( string: endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
129-
82+ guard let url = URL ( string: configuration. endpoint)
83+ else { throw ChatGPTServiceError . endpointIncorrect }
84+
13085 if !content. isEmpty || summary != nil {
13186 let newMessage = ChatMessage (
13287 id: uuidGenerator ( ) ,
@@ -140,17 +95,20 @@ public actor ChatGPTService: ChatGPTServiceType {
14095 let ( messages, remainingTokens) = combineHistoryWithSystemPrompt ( )
14196
14297 let requestBody = CompletionRequestBody (
143- model: model,
98+ model: configuration . model,
14499 messages: messages,
145- temperature: temperature ?? defaultTemperature ,
100+ temperature: configuration . temperature,
146101 stream: true ,
147- stop: stop. isEmpty ? nil : stop,
148- max_tokens: maxTokenForReply ( model: model, remainingTokens: remainingTokens)
102+ stop: configuration. stop. isEmpty ? nil : configuration. stop,
103+ max_tokens: maxTokenForReply (
104+ model: configuration. model,
105+ remainingTokens: remainingTokens
106+ )
149107 )
150108
151109 let api = buildCompletionStreamAPI (
152- apiKey,
153- designatedProvider ?? UserDefaults . shared . value ( for : \ . chatFeatureProvider ) ,
110+ configuration . apiKey,
111+ configuration . featureProvider ,
154112 url,
155113 requestBody
156114 )
@@ -181,7 +139,7 @@ public actor ChatGPTService: ChatGPTServiceType {
181139 if let content = delta. content {
182140 continuation. yield ( content)
183141 }
184-
142+
185143 try await Task . sleep ( nanoseconds: 3_500_000 )
186144 }
187145
@@ -205,8 +163,8 @@ public actor ChatGPTService: ChatGPTServiceType {
205163 content: String ,
206164 summary: String ? = nil
207165 ) async throws -> String ? {
208- guard let url = URL ( string: endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
209-
166+ guard let url = URL ( string: configuration . endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
167+
210168 if !content. isEmpty || summary != nil {
211169 let newMessage = ChatMessage (
212170 id: uuidGenerator ( ) ,
@@ -220,17 +178,17 @@ public actor ChatGPTService: ChatGPTServiceType {
220178 let ( messages, remainingTokens) = combineHistoryWithSystemPrompt ( )
221179
222180 let requestBody = CompletionRequestBody (
223- model: model,
181+ model: configuration . model,
224182 messages: messages,
225- temperature: temperature ?? defaultTemperature ,
183+ temperature: configuration . temperature,
226184 stream: true ,
227- stop: stop. isEmpty ? nil : stop,
228- max_tokens: maxTokenForReply ( model: model, remainingTokens: remainingTokens)
185+ stop: configuration . stop. isEmpty ? nil : configuration . stop,
186+ max_tokens: maxTokenForReply ( model: configuration . model, remainingTokens: remainingTokens)
229187 )
230188
231189 let api = buildCompletionAPI (
232- apiKey,
233- designatedProvider ?? UserDefaults . shared . value ( for : \ . chatFeatureProvider ) ,
190+ configuration . apiKey,
191+ configuration . featureProvider ,
234192 url,
235193 requestBody
236194 )
0 commit comments