11import AsyncAlgorithms
22import Foundation
3+ import Preferences
34
4- public protocol ChatGPTServiceType {
5+ public protocol ChatGPTServiceType : ObservableObject {
6+ var isReceivingMessage : Bool { get async }
7+ var history : [ ChatMessage ] { get async }
58 func send( content: String , summary: String ? ) async throws -> AsyncThrowingStream < String , Error >
69 func stopReceivingMessage( ) async
710 func clearHistory( ) async
@@ -49,13 +52,31 @@ public struct ChatGPTError: Error, Codable, LocalizedError {
4952 }
5053}
5154
52- public actor ChatGPTService : ChatGPTServiceType , ObservableObject {
53- public var temperature : Double
54- public var model : String
55- public var endpoint : String
56- public var apiKey : String
55+ public actor ChatGPTService : ChatGPTServiceType {
5756 public var systemPrompt : String
58- public var maxToken : Int
57+ public var temperature : Double
58+
59+ public var model : String {
60+ let value = UserDefaults . shared. value ( for: \. chatGPTModel)
61+ if value. isEmpty { return " gpt-3.5-turbo " }
62+ return value
63+ }
64+
65+ public var endpoint : String {
66+ let value = UserDefaults . shared. value ( for: \. chatGPTEndpoint)
67+ if value. isEmpty { return " https://api.openai.com/v1/chat/completions " }
68+
69+ return value
70+ }
71+
72+ public var apiKey : String {
73+ UserDefaults . shared. value ( for: \. openAIAPIKey)
74+ }
75+
76+ public var maxToken : Int {
77+ UserDefaults . shared. value ( for: \. chatGPTMaxToken)
78+ }
79+
5980 public var history : [ ChatMessage ] = [ ] {
6081 didSet { objectWillChange. send ( ) }
6182 }
@@ -69,19 +90,11 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
6990 var buildCompletionStreamAPI : CompletionStreamAPIBuilder = OpenAICompletionStreamAPI . init
7091
7192 public init (
72- systemPrompt: String ,
73- apiKey: String ,
74- endpoint: String ? = nil ,
75- model: String ? = nil ,
76- temperature: Double = 1 ,
77- maxToken: Int = 2048
93+ systemPrompt: String = " " ,
94+ temperature: Double = 1
7895 ) {
7996 self . systemPrompt = systemPrompt
80- self . apiKey = apiKey
81- self . model = model ?? " gpt-3.5-turbo "
8297 self . temperature = temperature
83- self . maxToken = maxToken
84- self . endpoint = endpoint ?? " https://api.openai.com/v1/chat/completions "
8598 }
8699
87100 public func send(
0 commit comments