@@ -10,12 +10,18 @@ public protocol ChatGPTServiceType {
1010}
1111
1212public enum ChatGPTServiceError : Error , LocalizedError {
13+ case chatModelNotAvailable
14+ case embeddingModelNotAvailable
1315 case endpointIncorrect
1416 case responseInvalid
1517 case otherError( String )
1618
1719 public var errorDescription : String ? {
1820 switch self {
21+ case . chatModelNotAvailable:
22+ return " Chat model is not available, please add a model in the settings. "
23+ case . embeddingModelNotAvailable:
24+ return " Embedding model is not available, please add a model in the settings. "
1925 case . endpointIncorrect:
2026 return " ChatGPT endpoint is incorrect "
2127 case . responseInvalid:
@@ -180,8 +186,12 @@ extension ChatGPTService {
180186
181187 /// Send the memory as prompt to ChatGPT, with stream enabled.
182188 func sendMemory( ) async throws -> AsyncThrowingStream < StreamContent , Error > {
183- guard let url = URL ( string: configuration. endpoint)
184- else { throw ChatGPTServiceError . endpointIncorrect }
189+ guard let model = configuration. model else {
190+ throw ChatGPTServiceError . chatModelNotAvailable
191+ }
192+ guard let url = URL ( string: configuration. endpoint) else {
193+ throw ChatGPTServiceError . endpointIncorrect
194+ }
185195
186196 await memory. refresh ( )
187197
@@ -197,8 +207,6 @@ extension ChatGPTService {
197207 }
198208 let remainingTokens = await memory. remainingTokens
199209
200- let model = configuration. model
201-
202210 let requestBody = CompletionRequestBody (
203211 model: model. info. modelName,
204212 messages: messages,
@@ -287,8 +295,12 @@ extension ChatGPTService {
287295
288296 /// Send the memory as prompt to ChatGPT, with stream disabled.
289297 func sendMemoryAndWait( ) async throws -> ChatMessage ? {
290- guard let url = URL ( string: configuration. endpoint)
291- else { throw ChatGPTServiceError . endpointIncorrect }
298+ guard let model = configuration. model else {
299+ throw ChatGPTServiceError . chatModelNotAvailable
300+ }
301+ guard let url = URL ( string: configuration. endpoint) else {
302+ throw ChatGPTServiceError . endpointIncorrect
303+ }
292304
293305 await memory. refresh ( )
294306
@@ -304,8 +316,6 @@ extension ChatGPTService {
304316 }
305317 let remainingTokens = await memory. remainingTokens
306318
307- let model = configuration. model
308-
309319 let requestBody = CompletionRequestBody (
310320 model: model. info. modelName,
311321 messages: messages,
@@ -357,7 +367,7 @@ extension ChatGPTService {
357367 /// When a function call is detected, but arguments are not yet ready, we can call this
358368 /// to insert a message placeholder in memory.
359369 func prepareFunctionCall( _ call: ChatMessage . FunctionCall , messageId: String ) async {
360- guard var function = functionProvider. function ( named: call. name) else { return }
370+ guard let function = functionProvider. function ( named: call. name) else { return }
361371 let responseMessage = ChatMessage (
362372 id: messageId,
363373 role: . function,
@@ -380,7 +390,7 @@ extension ChatGPTService {
380390 ) async -> String {
381391 let messageId = messageId ?? uuidGenerator ( )
382392
383- guard var function = functionProvider. function ( named: call. name) else {
393+ guard let function = functionProvider. function ( named: call. name) else {
384394 return await fallbackFunctionCall ( call, messageId: messageId)
385395 }
386396
0 commit comments