新增支持chat gpt
authorliangzhenying <867174124@qq.com>
Fri, 14 Feb 2025 06:43:52 +0000 (14:43 +0800)
committerliangzhenying <867174124@qq.com>
Fri, 14 Feb 2025 06:43:52 +0000 (14:43 +0800)
Doc/最新/.~25.1.2(1).docx [deleted file]
IOTBluetooth/IOTBluetooth/Example/ChatViewController.swift
IOTBluetooth/IOTBluetooth/Example/IBSpeaker.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.pbxproj
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAiManager.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift

diff --git a/Doc/最新/.~25.1.2(1).docx b/Doc/最新/.~25.1.2(1).docx
deleted file mode 100644 (file)
index ab7a7ee..0000000
Binary files "a/Doc/\346\234\200\346\226\260/.~25.1.2(1).docx" and /dev/null differ
index 40ccefe..814880f 100644 (file)
@@ -54,6 +54,9 @@ class ChatViewController: UIViewController{
             guard let self = self else {return}
             self.title = mode.title
         }
+        IBAiManager.shared.cancelThink = {[weak self]  in
+            IBSpeaker.shared.stop()
+        }
         IBAiManager.shared.aiResponse = {[weak self] response in
             print(response.description)
             guard let self = self else {return}
index a34681b..312a4ac 100644 (file)
@@ -14,6 +14,7 @@ class IBSpeaker: NSObject, AVSpeechSynthesizerDelegate {
     private var textBuffer: String = "" // 用于存储待播放的文字
     private var isPlaying: Bool = false // 是否正在播放
     
+    
     // MARK: - Initializer
     override init() {
         super.init()
index 40db681..6bd92fb 100644 (file)
@@ -72,7 +72,7 @@
                3F2AF82F2D57125D00635545 /* ChatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F2AF7F92D57118D00635545 /* ChatViewController.swift */; };
                3F2AF8352D573B6200635545 /* ChatCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F2AF8342D573B5D00635545 /* ChatCell.swift */; };
                3F6A07CC2D2D2C3F00F4122A /* IBPassword.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6A07CB2D2D2C3F00F4122A /* IBPassword.swift */; };
-               3F8167202D5DC65B002936D5 /* IBSpeaker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F81671F2D5DC64B002936D5 /* IBSpeaker.swift */; };
+               3F8167242D5DDE1F002936D5 /* IBSpeaker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8167232D5DDE1F002936D5 /* IBSpeaker.swift */; };
                3F9355B72D38FF2100C54DAB /* IBPush.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F9355B62D38FF1900C54DAB /* IBPush.swift */; };
                3FCFBFDE2D548D70003FC090 /* pumpkin.lzma in Resources */ = {isa = PBXBuildFile; fileRef = 3FCFBFDD2D548D70003FC090 /* pumpkin.lzma */; };
                3FFAE0552D3A042900325873 /* IBContact.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FFAE0542D3A03EF00325873 /* IBContact.swift */; };
                3F2AF7F92D57118D00635545 /* ChatViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatViewController.swift; sourceTree = "<group>"; };
                3F2AF8342D573B5D00635545 /* ChatCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatCell.swift; sourceTree = "<group>"; };
                3F6A07CB2D2D2C3F00F4122A /* IBPassword.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBPassword.swift; sourceTree = "<group>"; };
-               3F81671F2D5DC64B002936D5 /* IBSpeaker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBSpeaker.swift; sourceTree = "<group>"; };
+               3F8167232D5DDE1F002936D5 /* IBSpeaker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBSpeaker.swift; sourceTree = "<group>"; };
                3F9355B62D38FF1900C54DAB /* IBPush.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBPush.swift; sourceTree = "<group>"; };
                3FCFBFDD2D548D70003FC090 /* pumpkin.lzma */ = {isa = PBXFileReference; lastKnownFileType = file; path = pumpkin.lzma; sourceTree = "<group>"; };
                3FFAE0542D3A03EF00325873 /* IBContact.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBContact.swift; sourceTree = "<group>"; };
                                8A1DFF512B9EB4F800B735FF /* AppDelegate.swift */,
                                8A1DFF552B9EB4F800B735FF /* ViewController.swift */,
                                8A34B9262BA2AB66000F66BD /* ConnectViewController.swift */,
-                               3F81671F2D5DC64B002936D5 /* IBSpeaker.swift */,
                                8A76B6082BA84C2B0016F2AB /* AbilityViewController.swift */,
                                3F2AF7F92D57118D00635545 /* ChatViewController.swift */,
+                               3F8167232D5DDE1F002936D5 /* IBSpeaker.swift */,
                                3F2AF8342D573B5D00635545 /* ChatCell.swift */,
                                8A878FDF2BA93CF1008C7F26 /* Datas.swift */,
                                8A34B92A2BA2D446000F66BD /* BaseViewController.swift */,
                                8A76B6092BA84C2B0016F2AB /* AbilityViewController.swift in Sources */,
                                3F2AF8352D573B6200635545 /* ChatCell.swift in Sources */,
                                3F2AF82F2D57125D00635545 /* ChatViewController.swift in Sources */,
-                               3F8167202D5DC65B002936D5 /* IBSpeaker.swift in Sources */,
+                               3F8167242D5DDE1F002936D5 /* IBSpeaker.swift in Sources */,
                                8A34B9272BA2AB66000F66BD /* ConnectViewController.swift in Sources */,
                                18EF697A2C47A86D00AA0A36 /* IBTools.swift in Sources */,
                                8A1DFF562B9EB4F800B735FF /* ViewController.swift in Sources */,
index a6580cf..b85db71 100644 (file)
Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate differ
index 3670467..5008c74 100644 (file)
@@ -16,8 +16,8 @@
             endingColumnNumber = "9223372036854775807"
             startingLineNumber = "45"
             endingLineNumber = "45"
-            landmarkName = "playNextChunk()"
-            landmarkType = "7">
+            landmarkName = "IBSpeaker"
+            landmarkType = "3">
          </BreakpointContent>
       </BreakpointProxy>
    </Breakpoints>
index 2c3417a..bf7fbd5 100644 (file)
@@ -17,11 +17,16 @@ public class IBAiManager{
     /// 产品key
     public var productKey:String = ""
     /// chatGptKey
-    public var chatGptKey:String = ""
+    public var chatGptKey:String?
+    /// chatGpt model  gpt3.5-turbo、gpt-4-turbo、gpt-4o  默认是 gpt-4-turbo
+    public var gptModel:String = "gpt-4-turbo"
+    
     /// 使用大模型
     public var useMode:((_ result:MSPlatform) -> Void)?
     /// 使用大模型
     public var aiResponse:((_ result:IBAiResponse) -> Void)?
+    /// 取消当前思考
+    public var cancelThink:(() -> Void)?
     
     var chatModeMap:[Int:MSPlatform] = [0:.yiyan,
                                         1:.chatGPT,
@@ -45,20 +50,46 @@ public class IBAiManager{
     }
     
     func aiValid(_ data:Data){
+        let result = IBGeneralParser.dataPacket(data)
+        let platform = chatModeMap[result.int] ?? .doubao
+        let key = platform == .chatGPT ? chatGptKey : nil
+        useMode?(platform)
+        
         if !isLogin{
             Logger.log("ai未登录!!!")
+            sendAiState(isValid: false, gptKey:  key)
             login()
         }
-        
-        let result = IBGeneralParser.dataPacket(data)
-        let code = result.int
-        let platform = chatModeMap[code] ?? .doubao
-        useMode?(platform)
-        let model = IBAiValid()
-        model.isValid = isLogin
-        if code == 1 {
-            model.key = chatGptKey
+        else{
+            if platform == .chatGPT {
+                checkChatptValid()
+            }
+            else{
+                sendAiState(isValid: true, gptKey: nil)
+            }
+        }
+    }
+    
+    func checkChatptValid(){
+        guard let key = chatGptKey else{
+            Logger.log("请先设置chatGPT key")
+            sendAiState(isValid: false, gptKey:  "")
+            return
         }
+        MixSdk.shared.chatGpt.isValidKey(key) {[weak self] isValid, error in
+            guard let self  = self else {return}
+            if let valid = isValid{
+                sendAiState(isValid: valid, gptKey:  key)
+            }
+            else {
+                sendAiState(isValid: false, gptKey:  key)
+            }
+        }
+    }
+    func sendAiState(isValid:Bool,gptKey:String?){
+        let model = IBAiValid()
+        model.isValid = isValid
+        model.key = gptKey
         IBSender()
             .ability(IBAbility.aiValid)
             .parameters([.aiValid(model)])
@@ -66,6 +97,12 @@ public class IBAiManager{
                 
             }
     }
+    
+    //取消当前思考
+    func cancelThinkAction(){
+        MixSdk.shared.chat.reset()
+        cancelThink?()
+    }
     func aiRequest(_ data:Data){
         let result = IBGeneralParser.dataPacket(data)
         let code = result.int
@@ -78,18 +115,16 @@ public class IBAiManager{
         
 
         let platform = chatModeMap[code] ?? .doubao
-        var key = ""
-        if code == 1 {
-            key = chatGptKey
-            if chatGptKey.isEmpty {
-                Logger.log("请先设置chatGpt Key!")
-            }
-//            let gptModel = "gpt-4-turbo"
-//            MixSdk.shared.chatGpt.changeModel(model:gptModel )
-        }
         Logger.log("使用模型:",platform.title)
-        MixSdk.shared.setPlatform(platform,key: key)
         useMode?(platform)
+        
+        if platform == .chatGPT {
+            MixSdk.shared.chatGpt.changeModel(model:gptModel)
+            MixSdk.shared.setPlatform(platform,key: nil)
+        }
+        else{
+            MixSdk.shared.setPlatform(platform,key: nil)
+        }
     }
     func fectchAiAudio(){
         IBSender()
@@ -136,14 +171,51 @@ extension IBAiManager{
                 model.totalContent = text
                 sendRelpy(model: model)
                 responseId = "\(Date().timeIntervalSince1970)"
-                MixSdk.shared.chat.send(content: text)
+                
+                if MixSdk.shared.aiPlatform == .chatGPT {
+                    sendToChatGpt(content: text)
+                }
+                else{
+                    MixSdk.shared.chat.send(content: text)
+                }
             }
             else{
                 Logger.log(error ?? "")
                 let model = IBAiResponse(responseId)
                 model.type = 3
                 model.state = "end"
-//                model.value = "语音识别错误"
+                sendRelpy(model: model)
+            }
+        }
+    }
+    
+    func sendToChatGpt(content text:String){
+        reply = ""
+        MixSdk.shared.chatGpt.send(content: text) {[weak self] response, error in
+            guard let self  = self else {return}
+            if let model = response,let choices = model.choices.first {
+//                choices.message.content
+                reply = choices.message.content
+                let data1 = IBAiResponse(responseId)
+                data1.state = "middle"
+                data1.type = 2
+                data1.content = choices.message.content
+                data1.totalContent = reply
+                sendRelpy(model: data1)
+                
+                let data2 = IBAiResponse(responseId)
+                data2.state = "end"
+                data2.type = 4
+                data2.content = choices.message.content
+                data2.totalContent = reply
+                sendRelpy(model: data2)
+            }
+            else if let err = error{
+                //错误
+                Logger.log(error ?? "")
+                let model = IBAiResponse(responseId)
+                model.type = 3
+                model.state = "end"
                 sendRelpy(model: model)
             }
         }
@@ -176,6 +248,8 @@ extension IBAiManager{
                 sendRelpy(model: data)
             }
         }
+        
+        
     }
     
     func sendRelpy(model:IBAiResponse){
index 3f111bf..9f54dcd 100644 (file)
@@ -210,6 +210,11 @@ extension IBNotify {
             IBAiManager.shared.aiRequest(data)
             return true
         }
+        //AI取消当前思考
+        if(data.bytes[1] == 0x05 && data.bytes[2] == 0x07){
+            IBAiManager.shared.cancelThinkAction()
+            return true
+        }
         //文件已接收完成
         if data.bytes[1] == 0x06 && data.bytes[2] == 0x03 {
             let result = IBGeneralParser.dataPacket(data)