From: liangzhenying <867174124@qq.com> Date: Thu, 25 Jul 2024 01:32:28 +0000 (+0800) Subject: 切换模型优化 X-Git-Tag: deepseek~14 X-Git-Url: http://git.snicker.com.cn/sitemap.xml?a=commitdiff_plain;h=1864a8626ff8e91778e6b9675375116ddbf07bee;p=mirrors%2FMixSdk-iOS.git 切换模型优化 --- diff --git a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 4f588bc..486b258 100644 Binary files a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MixSdk/MixSdk/MixSdk/MSAigc.swift b/MixSdk/MixSdk/MixSdk/MSAigc.swift index 8ef0a9f..18a4640 100644 --- a/MixSdk/MixSdk/MixSdk/MSAigc.swift +++ b/MixSdk/MixSdk/MixSdk/MSAigc.swift @@ -18,9 +18,6 @@ public typealias MSAigcQueryCallBack = (_ imageUrls: [String]?,_ error:MSError?) /// aigcç±» public class MSAigc{ - func getUrl(_ api:String)->String{ - return MSConfig.httpBaseURL + api + MSConfig.headQuery() - } /// 获取算法类型 /// - Parameters: @@ -31,11 +28,11 @@ public class MSAigc{ completeHandler(nil,.unauthorised) return } - let url = getUrl("aigc/get-style") + let url = MSConfig.httpUrl(api: "aigc/get-style") let params = [ "type":type, ] - Logger.log(params) + Logger.log(url,params) HTTP.POST(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { @@ -68,7 +65,7 @@ public class MSAigc{ completeHandler(nil,.unauthorised) return } - let url = getUrl("aigc/get-result") + let url = MSConfig.httpUrl(api: "aigc/get-result") let params:[String:Any] = [ "task_id":task.task_id, "bit_depth":task.bit_depth, @@ -77,7 +74,7 @@ public class MSAigc{ "height":task.height, "width":task.width, ] - Logger.log(params) + Logger.log(url,params) HTTP.POST(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { @@ -115,8 +112,8 @@ public class MSAigc{ completeHandler(nil,MSError.invalidRequest) return } - let url = getUrl("aigc/create-image") - Logger.log(params) + let url = MSConfig.httpUrl(api: "aigc/create-image") + Logger.log(url,params) HTTP.POST(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { diff --git a/MixSdk/MixSdk/MixSdk/MSAuth.swift b/MixSdk/MixSdk/MixSdk/MSAuth.swift index 894fccb..a6f9362 100644 --- a/MixSdk/MixSdk/MixSdk/MSAuth.swift +++ b/MixSdk/MixSdk/MixSdk/MSAuth.swift @@ -111,6 +111,8 @@ public class MSAuth { if let result = dict["success"] as? Bool,result == true,let dataDict = dict["data"] as? [String:Any]{ self.authorized = true self.deviceToken = dataDict["token"] as? String ?? "" + + MixSdk.shared.setupWebsoket() completeHandler(self.deviceToken,nil) } else{ @@ -121,10 +123,3 @@ public class MSAuth { } } -extension MSAuth{ - func addHead(_ params:inout [String:Any]) -> [String:Any] { - params["ut"] = "" - params["is_auth"] = "1" - return params - } -} diff --git a/MixSdk/MixSdk/MixSdk/MSChat.swift b/MixSdk/MixSdk/MixSdk/MSChat.swift index e236f7b..0faa380 100644 --- a/MixSdk/MixSdk/MixSdk/MSChat.swift +++ b/MixSdk/MixSdk/MixSdk/MSChat.swift @@ -12,6 +12,7 @@ public typealias MSChatCallBack = (_ model: MSChatModel) -> Void /// 大模型对话类 public class MSChat { + var timer:Timer? var socket:WebSocket! /// 对话回调 @@ -30,18 +31,20 @@ public class MSChat { } } - init(){ + func setupWebSocket(){ openWebSocket() setupHeartBeat() } func openWebSocket(){ - let url = MSConfig.wsBaseURL + "chat/ws/watches" + MSConfig.headQuery() + let url = MSConfig.webSocketUrl(api: "chat/ws/watches",addPlatform: true) + Logger.log(url) socket = WebSocket(url) socket.delegate = self } func setupHeartBeat(){ - let timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in + timer?.invalidate() + timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in guard let self = self else {return} if socket.readyState == .open{ self.socket.ping() @@ -50,7 +53,7 @@ public class MSChat { self.openWebSocket() } } - RunLoop.current.add(timer, forMode: .common) + RunLoop.current.add(timer!, forMode: .common) } /// 发送内容 @@ -61,7 +64,7 @@ public class MSChat { return } let params:[String:Any] = [ - "platform":MixSdk.shared.aiPlatform.value, +// "platform":MixSdk.shared.aiPlatform.value, "type":"chat", "content":content, ] @@ -75,12 +78,12 @@ public class MSChat { extension MSChat:WebSocketDelegate{ func webSocketOpen() { - // Logger.log("MSChat webSocketOpen") + Logger.log("MSChat webSocketOpen") connectStateCallBack?(.connected) } func webSocketClose(_ code: Int, reason: String, wasClean: Bool) { -// Logger.log("MSChat webSocketClose",code,reason,wasClean) + Logger.log("MSChat webSocketClose",code,reason,wasClean) connectStateCallBack?(.disConnected) } diff --git a/MixSdk/MixSdk/MixSdk/MSChatGpt.swift b/MixSdk/MixSdk/MixSdk/MSChatGpt.swift index 10244a0..885e070 100644 --- a/MixSdk/MixSdk/MixSdk/MSChatGpt.swift +++ b/MixSdk/MixSdk/MixSdk/MSChatGpt.swift @@ -41,7 +41,7 @@ public class MSChatGpt { completeHandler(nil,MSChatGptError()) return } - let fileUrl = URL(fileURLWithPath: filePath) +// let fileUrl = URL(fileURLWithPath: filePath) // let params:[String : Any] = [ //// "model": model, // "model":"whisper-1", diff --git a/MixSdk/MixSdk/MixSdk/MSStt.swift b/MixSdk/MixSdk/MixSdk/MSStt.swift index fb0f0be..c85ddb8 100644 --- a/MixSdk/MixSdk/MixSdk/MSStt.swift +++ b/MixSdk/MixSdk/MixSdk/MSStt.swift @@ -26,6 +26,7 @@ public typealias MSConnectStateCallBack = (_ state:MSConnectState) -> Void /// 语音转文字类 public class MSStt { + var timer:Timer? var socket:WebSocket! var taskId:String? @@ -45,17 +46,20 @@ public class MSStt { } } - init(){ + func setupWebSocket(){ openWebSocket() setupHeartBeat() } func openWebSocket(){ - socket = WebSocket(MSConfig.wsBaseURL + "asr/ws" + MSConfig.headQuery()) + let url = MSConfig.webSocketUrl(api: "asr/ws",addPlatform: false) +// Logger.log(url) + socket = WebSocket(url) socket.delegate = self } func setupHeartBeat(){ - let timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in + timer?.invalidate() + timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in guard let self = self else {return} if socket.readyState == .open{ self.socket.ping() @@ -64,7 +68,7 @@ public class MSStt { self.openWebSocket() } } - RunLoop.current.add(timer, forMode: .common) + RunLoop.current.add(timer!, forMode: .common) } /// 一句话识别 @@ -84,7 +88,7 @@ public class MSStt { return } - var url = MSConfig.httpBaseURL + "asr/sentence-recognize?" + MSConfig.headQuery() + var url = MSConfig.httpUrl(api: "asr/sentence-recognize?") if let value = format{ url += "format=\(value)&" } @@ -94,7 +98,7 @@ public class MSStt { if let value = sample_rate{ url += "sample_rate=\(value)&" } -// Logger.log(url) + Logger.log(url) var request = URLRequest(url: URL(string:url)!) request.httpMethod = "POST" @@ -190,12 +194,12 @@ public extension MSStt{ extension MSStt:WebSocketDelegate{ func webSocketOpen() { - Logger.log("MSStt webSocketOpen") +// Logger.log("MSStt webSocketOpen") connectStateCallBack?(.connected) } func webSocketClose(_ code: Int, reason: String, wasClean: Bool) { - Logger.log("MSStt webSocketClose",code,reason,wasClean) +// Logger.log("MSStt webSocketClose",code,reason,wasClean) connectStateCallBack?(.disConnected) } diff --git a/MixSdk/MixSdk/MixSdk/MSTranslate.swift b/MixSdk/MixSdk/MixSdk/MSTranslate.swift index 62b1bf6..429e594 100644 --- a/MixSdk/MixSdk/MixSdk/MSTranslate.swift +++ b/MixSdk/MixSdk/MixSdk/MSTranslate.swift @@ -15,30 +15,23 @@ public typealias MSLanuageCallBack = (_ list: [MSTranslateLanguage]?,_ error:MSE /// 翻译类 public class MSTranslate { - private func getUrl(_ api:String)->String{ - return MSConfig.httpBaseURL + api + MSConfig.headQuery() - } -} - -public extension MSTranslate{ - /// 通用翻译 /// - Parameters: /// - lang: 语言 /// - text: 需要翻译的文字 /// - completeHandler: 结果回调 - func translate(lang:MSTranslateLanguage,text:String,completeHandler: @escaping MSTranslateCallBack){ + public func translate(lang:MSTranslateLanguage,text:String,completeHandler: @escaping MSTranslateCallBack){ if !MixSdk.shared.auth.authorized { completeHandler(nil,.unauthorised) return } - let url = getUrl("translate/general") + let url = MSConfig.httpUrl(api: "translate/general") let params = [ "sl":"auto", "tl":lang.code, "text":text ] - Logger.log(params) + Logger.log(url,params) HTTP.POST(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { @@ -61,12 +54,13 @@ public extension MSTranslate{ /// 获取支持的语言 /// - Parameter completeHandler: 结果回调 - func getSupportLanguages(completeHandler: @escaping MSLanuageCallBack){ + public func getSupportLanguages(completeHandler: @escaping MSLanuageCallBack){ if !MixSdk.shared.auth.authorized { completeHandler(nil,.unauthorised) return } - let url = getUrl("translate/languages") + let url = MSConfig.httpUrl(api: "translate/languages") + Logger.log(url) HTTP.POST(url,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { diff --git a/MixSdk/MixSdk/MixSdk/MSTts.swift b/MixSdk/MixSdk/MixSdk/MSTts.swift index 2132b33..19269c2 100644 --- a/MixSdk/MixSdk/MixSdk/MSTts.swift +++ b/MixSdk/MixSdk/MixSdk/MSTts.swift @@ -14,6 +14,7 @@ public typealias MSTtsVoiceCallBack = (_ data:Data) -> Void /// 文字转语音类 public class MSTts { + var timer:Timer? var socket:WebSocket! var taskId:String? @@ -35,17 +36,20 @@ public class MSTts { } } - init(){ + func setupWebSocket(){ openWebSocket() setupHeartBeat() } func openWebSocket(){ - socket = WebSocket(MSConfig.wsBaseURL + "tts/stream" + MSConfig.headQuery()) + let url = MSConfig.webSocketUrl(api: "tts/stream",addPlatform: false) +// Logger.log(url) + socket = WebSocket(url) socket.delegate = self } func setupHeartBeat(){ - let timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in + timer?.invalidate() + timer = Timer.scheduledTimer(withTimeInterval: MSConfig.wsHeartBeatTime, repeats: true) { [weak self] _ in guard let self = self else {return} if socket.readyState == .open{ self.socket.ping() @@ -54,7 +58,7 @@ public class MSTts { self.openWebSocket() } } - RunLoop.current.add(timer, forMode: .common) + RunLoop.current.add(timer!, forMode: .common) } } @@ -158,7 +162,7 @@ public extension MSTts{ extension MSTts:WebSocketDelegate{ func webSocketOpen() { - // Logger.log("MSTts webSocketOpen") +// Logger.log("MSTts webSocketOpen") connectStateCallBack?(.connected) } diff --git a/MixSdk/MixSdk/MixSdk/MixSdk.swift b/MixSdk/MixSdk/MixSdk/MixSdk.swift index 5a5b17c..9e27665 100644 --- a/MixSdk/MixSdk/MixSdk/MixSdk.swift +++ b/MixSdk/MixSdk/MixSdk/MixSdk.swift @@ -27,10 +27,24 @@ class MSConfig{ static let httpBaseURL = "https://amp.raisound.com/" static let wsHeartBeatTime = 2.0 - static func headQuery()->String{ +// static func add(api:String)->String{ // return "?platform=aliyun&is_auth=false" -// return "?platform=aliyun&is_auth=1&tu=\(MixSdk.shared.auth.deviceToken)" - return "" +// return api + "?platform=\(MixSdk.shared.aiPlatform.value)&is_auth=1&tu=\(MixSdk.shared.auth.deviceToken)" +// return "" +// } + + static func httpUrl(api:String)->String{ +// return httpBaseURL + api + "?platform=\(MixSdk.shared.aiPlatform.value)&is_auth=1&dt=\(MixSdk.shared.auth.deviceToken)" + return httpBaseURL + api + "" + } + static func webSocketUrl(api:String,addPlatform:Bool)->String{ + if addPlatform{ + return wsBaseURL + api + "?platform=\(MixSdk.shared.aiPlatform.value)" + } + else{ + return wsBaseURL + api + } +// return wsBaseURL + api + "?platform=\(MixSdk.shared.aiPlatform.value)&is_auth=1&dt=\(MixSdk.shared.auth.deviceToken)" } } @@ -38,10 +52,8 @@ class MSConfig{ /// Ai平台类型 public enum MSPlatform { -// /// 阿里云 + /// 阿里云 // case aliyun -// /// 火山 -// case huoshan /// 文心一言 case yiyan /// 豆包 @@ -55,14 +67,12 @@ public enum MSPlatform { switch self { // case .aliyun: // return "aliyun" -// case .huoshan: -// return "huoshan" case .yiyan: - return "yiyan" + return "baidu" case .doubao: return "huoshan" case .kimi: - return "kimi" + return "moonshot" case .chatGPT: return "chatGPT" @@ -133,6 +143,10 @@ public class MixSdk:NSObject { Logger.log("\nplatform = \(platform) \n key= \(key ?? "")") aiPlatform = platform aiKey = key + + if platform != .chatGPT{ + setupWebsoket() + } } /// ai平台 @@ -168,4 +182,9 @@ public class MixSdk:NSObject { // return MSStt() // }() + func setupWebsoket(){ + MixSdk.shared.stt.setupWebSocket() + MixSdk.shared.tts.setupWebSocket() + MixSdk.shared.chat.setupWebSocket() + } }