-////
-//// VoiceChatViewController.swift
-//// Example
-////
-//// Created by incar on 2024/7/29.
-////
//
-//import UIKit
-//import MixSdk
-//import SnapKit
-//import Toast_Swift
-//class VoiceChatViewController: UIViewController{
-// var voiceData:NSMutableData = NSMutableData()
-// let textView = UITextView()
-//
-// override func viewDidLoad() {
-// super.viewDidLoad()
-// // Do any additional setup after loading the view.
-//
-// setupViews()
-// setupCallBack()
-// }
-//
-// func setupViews(){
-// self.title = "VoiceChat"
-// self.view.backgroundColor = .white
-//
-// let toolBar = SendRecordToolBar()
-// toolBar.textField.text = "深圳今天天气怎么样"
-// toolBar.delegate = self
-// view.addSubview(toolBar)
-// toolBar.snp.makeConstraints { make in
-// make.width.centerX.equalToSuperview()
-// make.height.equalTo(150)
-// make.bottom.equalToSuperview().inset(0)
-// }
-//
-// 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)
-// }
-// }
-// func setupCallBack(){
-// MixSdk.shared.voiceChat.eventCallBack = { [weak self] model in
-// guard let self = self else {
-// return
-// }
-// if model.header.name == "RespondingEnded"{
-// self.saveToFile()
-// self.voiceData = NSMutableData()
-// }
-// }
-// MixSdk.shared.voiceChat.replyCallBack = {[weak self] data in
-//// print(data);
-// self?.voiceData.append(data)
-// }
-// }
-//
-// func saveToFile(){
-// let pcmPath = NSHomeDirectory() + "/Documents/1.pcm"
-// let mp3Path = NSHomeDirectory() + "/Documents/1.mp3"
-//
-// let url = URL(fileURLWithPath: pcmPath)
-// do {
-// try self.voiceData.write(to: url, options: .atomic)
-// print(pcmPath)
-// let result = MSAudioConvert.pcmToMp3(pcmPath: pcmPath, pcmSampleRate: 24000, mp3Path: mp3Path)
-// if result {
-// PlayerManager.shared.play(filePath: mp3Path)
-// }
-// }
-// catch{
-// print(error)
-// }
-// }
-//}
-//extension VoiceChatViewController:SendRecordToolBarDelegate{
+// ChatViewController.swift
+// Example
//
-// func sendRecordToolBarStartRecord(_ toolBar: SendRecordToolBar) {
-// view.makeToastActivity(.center)
-// //1、开始
-// MixSdk.shared.voiceChat.willSpeedch()
-// }
-//
-// func sendRecordToolBar(_ toolBar: SendRecordToolBar, record data: Data) {
-// //2、发送的语音流
-// //注意demo中的手机录音的语音编码是pcm,如果蓝牙设备传过来的语音数据是sbc,则使用下面的方法
-// MixSdk.shared.voiceChat.sendVoice(pcmData: data)
-//// MixSdk.shared.chatGptTrans.sendVoice(sbcData: data)
-// }
-//
-// func sendRecordToolBarStopRecord(_ toolBar: SendRecordToolBar) {
-// self.view.hideToastActivity()
-// //3、停止,
-//// MixSdk.shared.chatGptTrans.stop {[weak self] text, error in
-//// guard let self = self else {return}
-//// self.view.hideToastActivity()
-//// toolBar.textField.text = text ?? ""
-//// }
-// }
+// Created by incar on 2024/6/17.
//
-// func sendRecordToolBar(_ toolBar: SendRecordToolBar, _ text: String) {
-//// textView.text += "[send] " + text + "\n"
-//// let range = NSMakeRange(textView.text.count - 1, 1)
-//// textView.scrollRangeToVisible(range)
-////
-//// view.makeToastActivity(.center)
-//// MixSdk.shared.chatGpt.send(content: text) {[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)
-//// }
-//// }
-// }
-//}
+
+import UIKit
+import MixSdk
+import SnapKit
+import Toast_Swift
+class VoiceChatViewController: UIViewController{
+
+
+ let textView = UITextView()
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+ // Do any additional setup after loading the view.
+
+ setupViews()
+ setupChatCallBack()
+ //设置默认平台
+ self.title = MSPlatform.deepseek.title
+ MixSdk.shared.setPlatform(.deepseek)
+ }
+
+
+ func setupViews(){
+ self.title = "大模型对话"
+ self.view.backgroundColor = .white
+ self.navigationItem.rightBarButtonItems = [UIBarButtonItem(title: "切换平台", style: .done, target: self, action: #selector(changePlatform)),UIBarButtonItem(title: "clean", style: .done, target: self, action: #selector(cleanAction))]
+
+ let toolBar = SendToolBar()
+ toolBar.textField.text = "写一个800字的作文"
+ toolBar.textField.text = "你是谁"
+
+ 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)
+ }
+ }
+
+ func setupChatCallBack(){
+ let chat = MixSdk.shared.voiceChat
+ chat.connectStateCallBack = {[weak self] state in
+ guard let self = self else {return}
+ if state == .disConnected{
+ textView.text += Date().format(.full) + " 断开连接。。。" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ else{
+ textView.text += Date().format(.full) + " 已连接。。。" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ }
+
+ chat.callBack = {[weak self] model in
+ guard let self = self else {return}
+
+ if model.type == "start" {
+ textView.text += Date().format(.full) + "[receive] "
+ }
+ else if model.type == "end" {
+ textView.text += "\n"
+ }
+ else if model.type == "middle"{
+ textView.text += model.content ?? ""
+ }
+ let range = NSMakeRange(self.textView.text.count - 1, 1)
+ self.textView.scrollRangeToVisible(range)
+ }
+ }
+
+
+ @objc func changePlatform(){
+ let vc = UIAlertController(title: nil, message: "请选择", preferredStyle: .actionSheet)
+ vc.addAction( UIAlertAction(title: MSPlatform.deepseek.title, style: .default) { action in
+ self.title = MSPlatform.deepseek.title
+ MixSdk.shared.setPlatform(.deepseek)
+ })
+ vc.addAction( UIAlertAction(title: MSPlatform.doubao.title, style: .default) { action in
+ self.title = MSPlatform.doubao.title
+ MixSdk.shared.setPlatform(.doubao)
+ })
+ vc.addAction(UIAlertAction(title:MSPlatform.yiyan.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.yiyan)
+ self.title = MSPlatform.yiyan.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.kimi.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.kimi)
+ self.title = MSPlatform.kimi.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.xunfei.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.xunfei)
+ self.title = MSPlatform.xunfei.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.aliyun.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.aliyun)
+ self.title = MSPlatform.aliyun.title
+ })
+ vc.addAction(UIAlertAction(title: MSPlatform.chatGPT.title, style: .default) { action in
+ MixSdk.shared.setPlatform(.chatGPT, key: gptKey)
+ MixSdk.shared.chatGpt.changeModel(model: gptModel)
+ self.title = MSPlatform.chatGPT.title
+ })
+
+ vc.addAction(UIAlertAction(title: "取消", style: .cancel) { action in
+ })
+
+ self.present(vc, animated: true)
+ }
+
+ @objc func cleanAction(){
+ MixSdk.shared.voiceChat.reset()
+ }
+}
+extension VoiceChatViewController:SendToolBarDelegate{
+ func sendToolBar(_ toolBar: SendToolBar, _ text: String) {
+ if(MixSdk.shared.aiPlatform == .chatGPT){
+ textView.text += Date().format(.full) + "[send] " + text + "\n"
+
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ let content = text
+ view.makeToastActivity(.center)
+ // 发送chatgpt
+ 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 += Date().format(.full) + "[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{
+ // 发送其他平台
+ if MixSdk.shared.voiceChat.connectState == .connected {
+ textView.text += Date().format(.full) + "[send] " + text + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+
+ MixSdk.shared.voiceChat.requestToSpeak()
+ MixSdk.shared.voiceChat.send(content: text)
+
+ }
+ else{
+ //已断开连接
+ textView.text += Date().format(.full) + "断开连接。。。" + "\n"
+ let range = NSMakeRange(textView.text.count - 1, 1)
+ textView.scrollRangeToVisible(range)
+ }
+ }
+ }
+}