From: liangzhenying <867174124@qq.com> Date: Sat, 6 Jul 2024 06:59:34 +0000 (+0800) Subject: 新增chatGpt接口支持 X-Git-Tag: deepseek~23 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=e75110082b30b86cb3364e3e5a33f2a30f443521;p=mirrors%2FMixSdk-iOS.git 新增chatGpt接口支持 --- diff --git a/MixSdk/Example/Example.xcodeproj/project.pbxproj b/MixSdk/Example/Example.xcodeproj/project.pbxproj index 15029be..358e7e9 100644 --- a/MixSdk/Example/Example.xcodeproj/project.pbxproj +++ b/MixSdk/Example/Example.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 18C42C8A2C24344D000D8860 /* Sbc2PcmViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18C42C892C24344D000D8860 /* Sbc2PcmViewController.swift */; }; 18DB20482C213D730070D571 /* AiMouseAudioRecord.m in Sources */ = {isa = PBXBuildFile; fileRef = 18DB20462C213D730070D571 /* AiMouseAudioRecord.m */; }; 18DB20492C213D730070D571 /* AiMouseRecordManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18DB20472C213D730070D571 /* AiMouseRecordManager.m */; }; + 18FBC2BB2C38E0FC002240AF /* ChatGptViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FBC2BA2C38E0FC002240AF /* ChatGptViewController.swift */; }; F59940004E1C16053FBD42F5 /* Pods_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B555BC40ACD8F31DBCDD1A2 /* Pods_Example.framework */; }; /* End PBXBuildFile section */ @@ -78,6 +79,7 @@ 18DB20452C213D730070D571 /* AiMouseAudioRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AiMouseAudioRecord.h; sourceTree = ""; }; 18DB20462C213D730070D571 /* AiMouseAudioRecord.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AiMouseAudioRecord.m; sourceTree = ""; }; 18DB20472C213D730070D571 /* AiMouseRecordManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AiMouseRecordManager.m; sourceTree = ""; }; + 18FBC2BA2C38E0FC002240AF /* ChatGptViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatGptViewController.swift; sourceTree = ""; }; 4B555BC40ACD8F31DBCDD1A2 /* Pods_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 759E170040ACED4895C8A49D /* Pods-Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.release.xcconfig"; path = "Target Support Files/Pods-Example/Pods-Example.release.xcconfig"; sourceTree = ""; }; C85BC820E1AA39AC970C9E79 /* Pods-Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.debug.xcconfig"; path = "Target Support Files/Pods-Example/Pods-Example.debug.xcconfig"; sourceTree = ""; }; @@ -144,6 +146,7 @@ 18B74A232C201CE900FABBA9 /* SttViewController.swift */, 18C42B402C230A15000D8860 /* TtsFileViewController.swift */, 18B74A252C201EBD00FABBA9 /* ChatViewController.swift */, + 18FBC2BA2C38E0FC002240AF /* ChatGptViewController.swift */, 18B74A272C201F2500FABBA9 /* TranslateViewController.swift */, 18B74A292C201FF200FABBA9 /* AigcViewController.swift */, 18C42C892C24344D000D8860 /* Sbc2PcmViewController.swift */, @@ -335,6 +338,7 @@ 18B74A212C201CD600FABBA9 /* TtsViewController.swift in Sources */, 18DB20482C213D730070D571 /* AiMouseAudioRecord.m in Sources */, 18204BD82C193F9A00F94E69 /* ViewController.swift in Sources */, + 18FBC2BB2C38E0FC002240AF /* ChatGptViewController.swift in Sources */, 18B74A242C201CE900FABBA9 /* SttViewController.swift in Sources */, 18204BD42C193F9A00F94E69 /* AppDelegate.swift in Sources */, 18B74A262C201EBD00FABBA9 /* ChatViewController.swift in Sources */, diff --git a/MixSdk/Example/Example/ChatGptViewController.swift b/MixSdk/Example/Example/ChatGptViewController.swift new file mode 100644 index 0000000..dc98093 --- /dev/null +++ b/MixSdk/Example/Example/ChatGptViewController.swift @@ -0,0 +1,69 @@ +// +// ChatGptViewController.swift +// Example +// +// Created by incar on 2024/7/6. +// + +import UIKit +import MixSdk +import SnapKit +import Toast_Swift +class ChatGptViewController: UIViewController{ + + let textView = UITextView() + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view. + + setupViews() + //设置平台 + MixSdk.shared.setPlatform(.chatGPT, key: "sk-proj-fdifVB2nPBIVcQeegE0AT3BlbkFJsgOXO5mnrmzox8pnSxMV") + } + + func setupViews(){ + self.title = "ChatGpt" + self.view.backgroundColor = .white + + let toolBar = SendToolBar() + toolBar.textField.text = "我马上大四毕业了,请列出2024年最火的10个行业,并且为我规划未来十年的工作安排" + toolBar.delegate = self + view.addSubview(toolBar) + toolBar.snp.makeConstraints { make in + make.width.centerX.equalToSuperview() + make.height.equalTo(60) + make.bottom.equalToSuperview().inset(20) + } + + textView.isEditable = false + view.addSubview(textView) + textView.snp.makeConstraints { make in + make.top.equalToSuperview().inset(80) + make.left.right.equalToSuperview() + make.bottom.equalTo(toolBar.snp.top) + } + } +} +extension ChatGptViewController:SendToolBarDelegate{ + func sendToolBar(_ toolBar: SendToolBar, _ text: String) { + textView.text += "[send] " + text + "\n" + + let range = NSMakeRange(textView.text.count - 1, 1) + textView.scrollRangeToVisible(range) + let content = text + view.makeToastActivity(.center) + MixSdk.shared.chatGpt.send(content: content) {[weak self] response, error in + guard let self = self else {return} + self.view.hideToastActivity() + if let model = response,let choices = model.choices.first { + textView.text += "[receive] " + choices.message.content + "\n" + let range = NSMakeRange(textView.text.count - 1, 1) + textView.scrollRangeToVisible(range) + } + else if let err = error{ + self.view.makeToast(err.message) + } + } + } +} diff --git a/MixSdk/Example/Example/ChatViewController.swift b/MixSdk/Example/Example/ChatViewController.swift index b17642e..17bb182 100644 --- a/MixSdk/Example/Example/ChatViewController.swift +++ b/MixSdk/Example/Example/ChatViewController.swift @@ -19,11 +19,15 @@ class ChatViewController: UIViewController{ setupViews() setupChatCallBack() + //设置默认平台 + self.title = "chat-阿里云" + MixSdk.shared.setPlatform(.aliyun) } func setupViews(){ self.title = "大模型对话" self.view.backgroundColor = .white + self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "切换平台", style: .done, target: self, action: #selector(changePlatform)) let toolBar = SendToolBar() toolBar.textField.text = "今天天气怎么样" @@ -63,22 +67,71 @@ class ChatViewController: UIViewController{ self.textView.scrollRangeToVisible(range) } } + + + @objc func changePlatform(){ + let vc = UIAlertController(title: nil, message: "请选择", preferredStyle: .actionSheet) + let action0 = UIAlertAction(title: "阿里云", style: .default) { action in + self.title = "chat-阿里云" + MixSdk.shared.setPlatform(.aliyun) + } + vc.addAction(action0) + let action1 = UIAlertAction(title: "火山", style: .default) { action in + MixSdk.shared.setPlatform(.huoshan) + self.title = "chat-火山" + } + vc.addAction(action1) + let action2 = UIAlertAction(title: "ChatGPT", style: .default) { action in + MixSdk.shared.setPlatform(.chatGPT, key: "sk-proj-fdifVB2nPBIVcQeegE0AT3BlbkFJsgOXO5mnrmzox8pnSxMV") + self.title = "chatGPT" + } + vc.addAction(action2) + + let action3 = UIAlertAction(title: "取消", style: .cancel) { action in + + } + vc.addAction(action3) + + self.present(vc, animated: true) + } } extension ChatViewController:SendToolBarDelegate{ func sendToolBar(_ toolBar: SendToolBar, _ text: String) { - if MixSdk.shared.chat.connectState == .connected { - MixSdk.shared.chat.send(content: text) + if(MixSdk.shared.aiPlatform == .chatGPT){ textView.text += "[send] " + text + "\n" let range = NSMakeRange(textView.text.count - 1, 1) textView.scrollRangeToVisible(range) + let content = text + view.makeToastActivity(.center) + MixSdk.shared.chatGpt.send(content: content) {[weak self] response, error in + guard let self = self else {return} + self.view.hideToastActivity() + if let model = response,let choices = model.choices.first { + textView.text += "[receive] " + choices.message.content + "\n" + let range = NSMakeRange(textView.text.count - 1, 1) + textView.scrollRangeToVisible(range) + } + else if let err = error{ + self.view.makeToast(err.message) + } + } } else{ - //已断开连接 - textView.text += "已断开连接。。。" - - let range = NSMakeRange(textView.text.count - 1, 1) - textView.scrollRangeToVisible(range) + if MixSdk.shared.chat.connectState == .connected { + MixSdk.shared.chat.send(content: text) + textView.text += "[send] " + text + "\n" + + let range = NSMakeRange(textView.text.count - 1, 1) + textView.scrollRangeToVisible(range) + } + else{ + //已断开连接 + textView.text += "已断开连接。。。" + + let range = NSMakeRange(textView.text.count - 1, 1) + textView.scrollRangeToVisible(range) + } } } } diff --git a/MixSdk/Example/Example/SttViewController.swift b/MixSdk/Example/Example/SttViewController.swift index acb1132..c7d9058 100644 --- a/MixSdk/Example/Example/SttViewController.swift +++ b/MixSdk/Example/Example/SttViewController.swift @@ -86,8 +86,8 @@ extension SttViewController{ } else{ //5 - MixSdk.shared.stt.stopTranscription() recordMamager.stop() + MixSdk.shared.stt.stopTranscription() } } //3 diff --git a/MixSdk/Example/Example/ViewController.swift b/MixSdk/Example/Example/ViewController.swift index caa401c..f0dd220 100644 --- a/MixSdk/Example/Example/ViewController.swift +++ b/MixSdk/Example/Example/ViewController.swift @@ -18,6 +18,7 @@ class ViewController: UIViewController{ ("翻译","translateAction"), ("aigc","aigcAction"), ("大模型对话","chatAction"), + ("ChatGPT","chatGptAction"), ("长文本合成语音","longTtsAction"), ("文本合成语音","ttsAction"), @@ -25,6 +26,7 @@ class ViewController: UIViewController{ ("语音文件转文本","fileSttAction"), ("语音数据流转文本","sttAction"), ("sbc转pcm","sbc2PcmAction"), + ] override func viewDidLoad() { @@ -84,7 +86,7 @@ extension ViewController{ let deviceId = "C0310000A57B" let productKey = "ccvUoJOLbXn" let secretKey = "nNRjkRpKTxvprGaW" - let deviceSecret = "UGCJcceIrrETjKCBsE2SijSxFu4cPfmL" + let deviceSecret = "twLGAEo3RqlfCY3QLOjppdXkMb5I4dGC" MixSdk.shared.auth.login(productKey: productKey, secretKey: secretKey, deviceId: deviceId, deviceSecrect: deviceSecret){[weak self] isSuccess,error in self?.view.hideToastActivity() if let err = error{ @@ -164,6 +166,9 @@ extension ViewController{ let vc = Sbc2PcmViewController() self.navigationController?.pushViewController(vc, animated: true) } - + @objc func chatGptAction() { + let vc = ChatGptViewController() + self.navigationController?.pushViewController(vc, animated: true) + } } diff --git a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 3c94f15..b8e4363 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.xcodeproj/project.pbxproj b/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj index 6fb6836..c1c527d 100644 --- a/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj +++ b/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj @@ -175,6 +175,8 @@ 18C42C782C24241A000D8860 /* MixSdk-Bridging-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 18C42C762C24241A000D8860 /* MixSdk-Bridging-Header.h */; }; 18C42C7B2C24270D000D8860 /* MSAudioConvert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18C42C7A2C24270D000D8860 /* MSAudioConvert.swift */; }; 18C42C7D2C242A40000D8860 /* liblzma.5.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 18C42C7C2C242A40000D8860 /* liblzma.5.tbd */; }; + 18FBC2B72C38DA05002240AF /* MSChatGpt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FBC2B62C38DA05002240AF /* MSChatGpt.swift */; }; + 18FBC2B92C38DA1D002240AF /* MSChatGptModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18FBC2B82C38DA1D002240AF /* MSChatGptModel.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -347,6 +349,8 @@ 18C42C762C24241A000D8860 /* MixSdk-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MixSdk-Bridging-Header.h"; sourceTree = ""; }; 18C42C7A2C24270D000D8860 /* MSAudioConvert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSAudioConvert.swift; sourceTree = ""; }; 18C42C7C2C242A40000D8860 /* liblzma.5.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = liblzma.5.tbd; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/liblzma.5.tbd; sourceTree = DEVELOPER_DIR; }; + 18FBC2B62C38DA05002240AF /* MSChatGpt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSChatGpt.swift; sourceTree = ""; }; + 18FBC2B82C38DA1D002240AF /* MSChatGptModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSChatGptModel.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -435,6 +439,8 @@ 18C42B3C2C22F2C4000D8860 /* MSTtsModel.swift */, 18B74A1D2C1FDDB400FABBA9 /* MSChat.swift */, 18C42B3E2C22F2E1000D8860 /* MSChatModel.swift */, + 18FBC2B62C38DA05002240AF /* MSChatGpt.swift */, + 18FBC2B82C38DA1D002240AF /* MSChatGptModel.swift */, 181188442C1AC35F00C23FAB /* MSError.swift */, ); path = MixSdk; @@ -891,6 +897,7 @@ 18204BFD2C19488900F94E69 /* Operation.swift in Sources */, 1811883C2C1A8B5200C23FAB /* JSON.swift in Sources */, 181188402C1A8B5200C23FAB /* LRC.swift in Sources */, + 18FBC2B92C38DA1D002240AF /* MSChatGptModel.swift in Sources */, 18204BFC2C19488900F94E69 /* HTTPSecurity.swift in Sources */, 18204BFB2C19488900F94E69 /* Upload.swift in Sources */, 18204BFA2C19488900F94E69 /* Request.swift in Sources */, @@ -909,6 +916,7 @@ 18C42B392C22F1FA000D8860 /* MSSttModel.swift in Sources */, 18C42C6F2C2422C2000D8860 /* AiAudioDecoder.m in Sources */, 18B74A1E2C1FDDB500FABBA9 /* MSChat.swift in Sources */, + 18FBC2B72C38DA05002240AF /* MSChatGpt.swift in Sources */, 184FFC542C21B091006F664D /* MSAigcModel.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1044,6 +1052,7 @@ 18204BC32C193E3D00F94E69 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; @@ -1085,6 +1094,7 @@ 18204BC42C193E3D00F94E69 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; diff --git a/MixSdk/MixSdk/MixSdk/MSChat.swift b/MixSdk/MixSdk/MixSdk/MSChat.swift index f03d02e..d39b7af 100644 --- a/MixSdk/MixSdk/MixSdk/MSChat.swift +++ b/MixSdk/MixSdk/MixSdk/MSChat.swift @@ -7,8 +7,6 @@ import Foundation - - /// 对话回调 public typealias MSChatCallBack = (_ model: MSChatModel) -> Void @@ -64,6 +62,7 @@ public class MSChat { return } let params = [ + "platform":MixSdk.shared.aiPlatform.value, "type":"chat", "content":content, ] diff --git a/MixSdk/MixSdk/MixSdk/MSChatGpt.swift b/MixSdk/MixSdk/MixSdk/MSChatGpt.swift new file mode 100644 index 0000000..1505cf6 --- /dev/null +++ b/MixSdk/MixSdk/MixSdk/MSChatGpt.swift @@ -0,0 +1,80 @@ +// +// MSChatGpt.swift +// MixSdk +// +// Created by incar on 2024/7/6. +// + +import Foundation + + +/// ChatGpt +public typealias MSChatGptCallBack = (_ response: MSChatGptModel?,_ error:MSChatGptError?) -> Void + + +/// 鉴权管理类 +public class MSChatGpt { + + func getUrl()->String{ + return "https://api.openai.com/v1/chat/completions" + } + + /// 发送对话内容 + /// - Parameters: + /// - content: 内容 + /// - completeHandler: 结果会调皮 + public func send(content:String,completeHandler: @escaping MSChatGptCallBack){ + guard let key = MixSdk.shared.aiKey else { + Logger.log("请设置key") + completeHandler(nil,MSChatGptError()) + return + } + let params:[String : Any] = [ + "model": "gpt-3.5-turbo", + "messages": [ + [ + "role": "user", + "content": content + ] + ] + ] +// let params:[String : Any] = [ +// "model": "gpt-3.5-turbo", +// "messages": [ +// [ +// "role": "system", +// "content": "你是一个很聪明的职业规划师" +// ], +// [ +// "role": "user", +// "content": "我马上大四毕业了,请列出2024年最火的10个行业,并且为我规划未来十年的工作安排" +// ] +// ] +// ] + let url = getUrl() + let head:[String:String] = ["Authorization":"Bearer \(key)"] + Logger.log(params) + Logger.log(head) + + HTTP.POST(url,parameters: params,headers: head,requestSerializer: JSONParameterSerializer()){ response in + DispatchQueue.main.sync { + let (dict,error) = MSHttpManager.receiveData(response) + if let error = error { + completeHandler(nil,MSChatGptError()) + return + } + guard let dict = dict else {return completeHandler(nil,MSChatGptError())} + + if let id = dict["id"] as? String,let model = JSON.decode(MSChatGptModel.self, from: response.data){ + completeHandler(model,nil) + } + else if let error = JSON.decode(MSChatGptError.self, from: response.data){ + completeHandler(nil,error) + } + else{ + completeHandler(nil,MSChatGptError()) + } + } + } + } +} diff --git a/MixSdk/MixSdk/MixSdk/MSChatGptModel.swift b/MixSdk/MixSdk/MixSdk/MSChatGptModel.swift new file mode 100644 index 0000000..e3ef7d1 --- /dev/null +++ b/MixSdk/MixSdk/MixSdk/MSChatGptModel.swift @@ -0,0 +1,57 @@ +// +// MSChatGptModel.swift +// MixSdk +// +// Created by incar on 2024/7/6. +// + +import Foundation + +public class MSChatGptError:Codable{ + /// + public var message:String = "response error" + /// +// public var type:String = "" + /// + public var code:String = "invalid_request" +} + + +public class MSChatGptModel:Codable{ + /// id + public var id:String = "" + /// + public var object:String = "" + /// 创建的时间的时间戳 + public var created:Int = 0 + /// 使用的模型 + public var model:String = "" + /// 消息体 + public var choices:[MSChatGptChoices] = [] + /// token使用情况 + public var usage:MSChatGptUsage = MSChatGptUsage() +} + +public class MSChatGptChoices:Codable{ + /// 第几调回复 + public var index:Int = 0 + /// 回复的消息体 + public var message:MSChatGptMessage = MSChatGptMessage() + /// 结束原因 + public var finish_reason:String = "" +} + +/// 回复的消息体 +public class MSChatGptMessage:Codable{ + /// 角色 + public var role:String = "" + /// 内容 + public var content:String = "" +} + +/// token使用情况 +public class MSChatGptUsage:Codable{ + public var prompt_tokens:Int = 0 + public var completion_tokens:Int = 0 + public var total_tokens:Int = 0 +} diff --git a/MixSdk/MixSdk/MixSdk/MSStt.swift b/MixSdk/MixSdk/MixSdk/MSStt.swift index 885841e..752897f 100644 --- a/MixSdk/MixSdk/MixSdk/MSStt.swift +++ b/MixSdk/MixSdk/MixSdk/MSStt.swift @@ -175,6 +175,7 @@ public extension MSStt{ Logger.log("还未鉴权") return } + print("send \(pcmData)") socket.send(data: pcmData) } @@ -208,10 +209,13 @@ extension MSStt:WebSocketDelegate{ } func webSocketMessageText(_ text: String) { Logger.log("MSStt receive",text) - guard let data = text.data,let model = JSON.decode(MSSttEvent.self, from: data) else{ - return + if let data = text.data,let model = JSON.decode(MSSttEvent.self, from: data) { + eventCallBack?(model) + } + else if let data = text.data,let model = JSON.decode(MSSttEventHead.self, from: data) { + eventCallBack?(model.header) +// openWebSocket() } - eventCallBack?(model) } func webSocketMessageData(_ data: Data){ diff --git a/MixSdk/MixSdk/MixSdk/MSSttModel.swift b/MixSdk/MixSdk/MixSdk/MSSttModel.swift index f832a72..3e787ab 100644 --- a/MixSdk/MixSdk/MixSdk/MSSttModel.swift +++ b/MixSdk/MixSdk/MixSdk/MSSttModel.swift @@ -6,6 +6,11 @@ // import Foundation +/// 语音转文字事件 +class MSSttEventHead:Codable{ + var header:MSSttEvent = MSSttEvent() +} + /// 语音识别结果模型 public class MSSttEvent:Codable{ /// 事件 diff --git a/MixSdk/MixSdk/MixSdk/MixSdk.swift b/MixSdk/MixSdk/MixSdk/MixSdk.swift index 22c9122..8c316f8 100644 --- a/MixSdk/MixSdk/MixSdk/MixSdk.swift +++ b/MixSdk/MixSdk/MixSdk/MixSdk.swift @@ -7,11 +7,51 @@ import Foundation +// https://apifox.com/apidoc/shared-054b7e2c-cf4b-4e2b-875c-79fda14ddbbf?pwd=xRzzNg7l +// 访问密码: xRzzNg7l //let wsBaseURL = "ws://121.40.67.151:9099/" -let httpBaseURL = "http://121.40.67.151:9099/" +//let httpBaseURL = "http://121.40.67.151:9099/" let wsHeartBeatTime = 2.0 let wsBaseURL = "ws://118.145.158.207:80/" -//let httpBaseURL = "http://118.145.158.207:80/" +let httpBaseURL = "http://118.145.158.207:80/" + + +/// Ai平台类型 +public enum MSPlatform { + /// 阿里云 + case aliyun + /// 火山 + case huoshan +// /// 文心一言 +// case yiyan +// /// 豆包 +// case doubao +// /// Kimi +// case kimi + /// chatGPT + case chatGPT + + internal var value:String{ + switch self { + case .aliyun: + return "aliyun" + case .huoshan: + return "huoshan" +// case .yiyan: +// return "yiyan" +// case .doubao: +// return "doubao" +// case .kimi: +// return "kimi" + case .chatGPT: + return "chatGPT" + + } + } +} + + + /// SDK 管理类 public class MixSdk:NSObject { @@ -19,10 +59,6 @@ public class MixSdk:NSObject { private override init() { super.init() - -// https://apifox.com/apidoc/shared-054b7e2c-cf4b-4e2b-875c-79fda14ddbbf?pwd=xRzzNg7l -// 访问密码: xRzzNg7l - AiAudioDecoder.sharedInstance().startSBCDecodec() } @@ -35,7 +71,7 @@ public class MixSdk:NSObject { public var logs: [String] { Logger.logs } /// SDK 版本号 - public let version: String = "1.0.4" + public let version: String = "1.0.5" /// 默认为 false public var isDebug: Bool = false { didSet { Logger.isDebug = isDebug } @@ -60,15 +96,25 @@ public class MixSdk:NSObject { /// 语音转文字 public let stt = MSStt() + /// chatGpt + public let chatGpt = MSChatGpt() + - /// 设置aiKey + /// 设置平台 /// - Parameters: - /// - chanel: 渠道,例如"CHAT GPT" + /// - platform: 使用的平台 /// - key: key值 - public func setAiKey(chanel:String,key:String){ - Logger.log(chanel,key) + public func setPlatform(_ platform:MSPlatform,key:String? = nil){ + Logger.log("\nplatform = \(platform) \n key= \(key ?? "")") + aiPlatform = platform + aiKey = key } + /// ai平台 + public var aiPlatform:MSPlatform = .aliyun + /// aikey + public var aiKey:String? = "" + // /// 鉴权 // public lazy var auth:MSAuth = { // return MSAuth() diff --git "a/document/MixSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/document/MixSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" index d7452d2..381e3df 100644 --- "a/document/MixSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" +++ "b/document/MixSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" @@ -2,10 +2,10 @@ ## 介绍 -- 当前版本:v1.0.4 -- 更新内容20240627: - 1. 新增设置AIKey接口。 - 1. 大会话模型、语音转文字、文字转语音增加连接状态回调 +- 当前版本:v1.0.5 +- 更新内容20240706: + 1. 新增chatGpt支持。 + 1. 新增对大话模型平台切换。 ## 环境要求 @@ -20,14 +20,7 @@ ## 使用流程 -### 添加权限 -```swift -info.plist中添加麦克风权限: - -Privacy - Microphone Usage Description - -``` ### SDK初始化 @@ -119,6 +112,21 @@ public class MSAigc { ## 大模型对话 + + +```swift +/// SDK 管理类 +public class MixSdk : NSObject { + /// 对话模型平台切换 + /// - Parameters: + /// - platform: 使用的平台 + /// - key: key值 + public func setPlatform(_ platform: MixSdk.MSPlatform, key: String? = nil) +} +``` + + + ```swift /// 大模型对话管理类 public class MSChat { @@ -135,6 +143,21 @@ public class MSChat { /// - Parameter content: 内容 public func send(content: String) } + +``` + +## ChatGpt + +```swift +/// 鉴权管理类 +public class MSChatGpt { + + /// 发送对话内容 + /// - Parameters: + /// - content: 内容 + /// - completeHandler: 结果会调皮 + public func send(content: String, completeHandler: @escaping MixSdk.MSChatGptCallBack) +} ``` @@ -292,11 +315,20 @@ public class MixSdk : NSObject { /// 语音转文字 public var stt: MixSdk.MSStt - /// 设置aiKey + /// chatGpt + public let chatGpt: MixSdk.MSChatGpt + + /// 设置平台 /// - Parameters: - /// - chanel: 渠道,例如"CHAT GPT" + /// - platform: 使用的平台 /// - key: key值 - public func setAiKey(chanel: String, key: String) + public func setPlatform(_ platform: MixSdk.MSPlatform, key: String? = nil) + + /// ai平台 + public var aiPlatform: MixSdk.MSPlatform + + /// aikey + public var aiKey: String? } ``` @@ -386,6 +418,53 @@ public class MSChatModel : Codable { } ``` +## MSChatGptModel + +```swift +/// 回复的消息体 +public class MSChatGptMessage : Codable { + + /// 角色 + public var role: String + + /// 内容 + public var content: String +} + +public class MSChatGptModel : Codable { + + /// id + public var id: String + + /// + public var object: String + + /// 创建的时间的时间戳 + public var created: Int + + /// 使用的模型 + public var model: String + + /// 消息体 + public var choices: [MixSdk.MSChatGptChoices] + + /// token使用情况 + public var usage: MixSdk.MSChatGptUsage +} + +/// token使用情况 +public class MSChatGptUsage : Codable { + + public var prompt_tokens: Int + + public var completion_tokens: Int + + public var total_tokens: Int +} +``` + + + ### MSSttEvent ```swift