支持讯飞和阿里
authorliangzhenying <867174124@qq.com>
Tue, 13 Aug 2024 11:03:06 +0000 (19:03 +0800)
committerliangzhenying <867174124@qq.com>
Tue, 13 Aug 2024 11:03:06 +0000 (19:03 +0800)
MixSdk/Example/Example/ChatViewController.swift
MixSdk/Example/Example/Config.swift
MixSdk/Example/Example/ViewController.swift
MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MixSdk/MixSdk/MixSdk/MSChat.swift
MixSdk/MixSdk/MixSdk/MixSdk.swift

index c3909af..9d1f30d 100644 (file)
@@ -21,7 +21,7 @@ class ChatViewController: UIViewController{
         setupViews()
         setupChatCallBack()
         //设置默认平台
-        self.title = "chat-豆包"
+        self.title = MSPlatform.doubao.title
         MixSdk.shared.setPlatform(.doubao)
     }
     
@@ -68,7 +68,6 @@ class ChatViewController: UIViewController{
         
         chat.callBack = {[weak self] model in
             guard let self = self else {return}
-            print("\(model.type) : \(model.content ?? "")")
 
             if model.type == "start" {
                 textView.text += Date().format(.full) + "[receive] "
@@ -87,31 +86,34 @@ class ChatViewController: UIViewController{
     
     @objc func changePlatform(){
         let vc = UIAlertController(title: nil, message: "请选择", preferredStyle: .actionSheet)
-        let action0 = UIAlertAction(title: "豆包", style: .default) { action in
-            self.title = "chat-豆包"
+        vc.addAction( UIAlertAction(title: MSPlatform.doubao.title, style: .default) { action in
+            self.title = MSPlatform.doubao.title
             MixSdk.shared.setPlatform(.doubao)
-        }
-        vc.addAction(action0)
-        let action1 = UIAlertAction(title: "文心一言", style: .default) { action in
+        })
+        vc.addAction(UIAlertAction(title:MSPlatform.yiyan.title, style: .default) { action in
             MixSdk.shared.setPlatform(.yiyan)
-            self.title = "chat-文心一言"
-        }
-        vc.addAction(action1)
-        let action2 = UIAlertAction(title: "KiMi", style: .default) { action in
+            self.title = MSPlatform.yiyan.title
+        })
+        vc.addAction(UIAlertAction(title: MSPlatform.kimi.title, style: .default) { action in
             MixSdk.shared.setPlatform(.kimi)
-            self.title = "chat-kimi"
-        }
-        vc.addAction(action2)
-        let action3 = UIAlertAction(title: "ChatGPT", style: .default) { action in
+            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 = "chatGPT"
-        }
-        vc.addAction(action3)
+            self.title = MSPlatform.chatGPT.title
+        })
         
-        let action4 = UIAlertAction(title: "取消", style: .cancel) { action in
-        }
-        vc.addAction(action4)
+        vc.addAction(UIAlertAction(title: "取消", style: .cancel) { action in
+        })
         
         self.present(vc, animated: true)
     }
index 5c3f201..234c8f9 100644 (file)
@@ -11,3 +11,9 @@ import Foundation
 
 let gptKey = "sk-proj-04ogvrZQqyaiv1OWw8OZT3BlbkFJG9Gyr8oFvRdmr3oXQNbX"
 let gptModel = "gpt-4-turbo"
+
+
+let deviceId = "281106AFB0F7"
+let productId = "pafOCKLiUbeVHbdZlddH"
+let productKey = "HHosiYUNdG42t1cTgKOzIUPOvAVMI2QT"
+
index e9eab22..4d38e71 100644 (file)
@@ -67,15 +67,13 @@ extension ViewController:UITableViewDelegate,UITableViewDataSource {
 extension ViewController{
     @objc func registerAction() {
         view.makeToastActivity(.center)
-        let deviceId = "281106AFB0F7"
-        let productId = "pafOCKLiUbeVHbdZlddH"
-        let productKey = "HHosiYUNdG42t1cTgKOzIUPOvAVMI2QT"
         MixSdk.shared.auth.register(productId: productId, productKey: productKey, deviceId: deviceId){[weak self] password,error in
             self?.view.hideToastActivity()
             if let password = password{
                 self?.view.makeToast("register success!")
                 print(password)
                 //deviceId和password APP要确保这个一一对应。
+                UserDefaults.standard.setValue(password, forKey: deviceId)
             }
             else{
                 print(error!)
@@ -84,11 +82,15 @@ extension ViewController{
         }
     }
     @objc func loginAction() {
+        if let password = UserDefaults.standard.string(forKey: deviceId){
+            startLogin(password: password)
+        }
+        else{
+            self.view.makeToast("请先注册")
+        }
+    }
+    func startLogin(password:String){
         view.makeToastActivity(.center)
-        let deviceId = "281106AFB0F7"
-        let productKey = "HHosiYUNdG42t1cTgKOzIUPOvAVMI2QT"
-        let password = "aWGKrBWaijsKD5eEsVVU5rjWPI1sVy"//注册获得的密码
-        
         MixSdk.shared.auth.login(productKey:productKey,deviceId: deviceId, password: password){[weak self]  token,error in
             self?.view.hideToastActivity()
             if let err = error{
index 5145d36..5a9a36d 100644 (file)
Binary files a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index a6047f1..60f64a2 100644 (file)
@@ -124,6 +124,7 @@ extension MSChat:WebSocketDelegate{
 //        Logger.log("MSChat receive",data.joined(""))
         
         if let model = MSChatModel.decode(data){
+            Logger.log("\(model.type) : \(model.content ?? "")")
             lastChatModel = model
             callBack?(model)
         }
index 0ceb63f..c234438 100644 (file)
@@ -88,8 +88,10 @@ class MSConfig{
 
 /// Ai平台类型
 public enum MSPlatform {
-    /// 阿里云
-//    case aliyun
+    /// 通义千问
+    case aliyun
+    /// 讯飞星火
+    case xunfei
     /// 文心一言
     case yiyan
     /// 豆包
@@ -101,8 +103,10 @@ public enum MSPlatform {
     
     internal var value:String{
         switch self {
-//        case .aliyun:
-//            return "aliyun"
+        case .aliyun:
+            return "aliyun"
+        case .xunfei:
+            return "xunfei"
         case .yiyan:
             return "baidu"
         case .doubao:
@@ -114,6 +118,23 @@ public enum MSPlatform {
 
         }
     }
+    
+    public var title:String{
+        switch self {
+        case .aliyun:
+            return "通义千问"
+        case .xunfei:
+            return "讯飞星火"
+        case .yiyan:
+            return "文心一言"
+        case .doubao:
+            return "豆包"
+        case .kimi:
+            return "kimi"
+        case .chatGPT:
+            return "ChatGPT"
+        }
+    }
 }