• Unity 通过jar包形式接入讯飞星火SDK


    最近工作上遇到了要接入gpt相关内容的需求,简单实现了一个安卓端接入讯飞星火的UnitySDK。
    或者也可以接入WebSocket接口的。本文只讲安卓实现
    我使用的Unity版本为2021.3.27f1c2
    Android版本为4.2.2

    1.下载SDK

    登陆讯飞开放平台下载如图所示SDK
    在这里插入图片描述

    2.新建安卓工程

    新建安卓工程,在工程下创建libs文件夹
    在这里插入图片描述
    下载的SDK解压后的aar文件至安卓工程的libs文件夹下
    打开工程的build.gradle
    复制以下代码
    在这里插入图片描述
    上面红框部分是引用aar,下面是打jar包的逻辑

    3.根据原来的demo工程修改代码

    package com.rayneo.sparklib;
    import android.app.Activity;
    import com.iflytek.sparkchain.core.LLM;
    import com.iflytek.sparkchain.core.LLMCallbacks;
    import com.iflytek.sparkchain.core.LLMConfig;
    import com.iflytek.sparkchain.core.SparkChain;
    import com.iflytek.sparkchain.core.SparkChainConfig;
    
    public class SparkTool {
    
        private static final String TAG = "SparkLLM";
        private Activity _unityActivity;
    
        private String domain ="general";
        private String url = "wss://spark-api.xf-yun.com/v1.1/chat";
    
        public int initSDK(String appid,String apiKey,String apiSecret,int logLevel,String domain,String url) {
            // 初始化SDK,Appid等信息在清单中配置
            SparkChainConfig sparkChainConfig = SparkChainConfig.builder();
            sparkChainConfig.appID(appid)
                    .apiKey(apiKey)
                    .apiSecret(apiSecret)//应用申请的appid三元组
                    .logLevel(logLevel);
    
            int ret = SparkChain.getInst().init(getActivity(),sparkChainConfig);
    
            this.domain = domain;
            this.url = url;
    
            return ret;
        }
    
        public void unInitSDK() {
            SparkChain.getInst().unInit();
        }
    
        Activity getActivity(){
            if(null == _unityActivity) {
                try {
                    Class classtype = Class.forName("com.unity3d.player.UnityPlayer");
                    Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
                    _unityActivity = activity;
                } catch (ClassNotFoundException e) {
    
                } catch (IllegalAccessException e) {
    
                } catch (NoSuchFieldException e) {
    
                }
            }
            return _unityActivity;
        }
    
        public int startChat(ISparkLLMProxy unityProxy,String msg) {
    
            LLMConfig llmConfig = LLMConfig.builder();
            llmConfig.domain(domain)
                    .url(url);
            LLM llm = new LLM(llmConfig);
            LLMCallbacks llmCallbacks = new SparkLLMCallbacks(unityProxy);
            llm.registerLLMCallbacks(llmCallbacks);
            String myContext = "myContext";
    
            int ret = llm.arun(msg,myContext);
    
            return ret;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    这几个接口分别是初始化,释放,获取Unity的Activity,开始gpt。

    package com.rayneo.sparklib;
    
    public interface ISparkLLMProxy {
    
        void onLLMResult(int status, String Content);
    
        void onLLMEvent(int eventID, String eventMsg);
    
        void onLLMError(int errorCode, String var2);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.rayneo.sparklib;
    
    import android.util.Log;
    
    import com.iflytek.sparkchain.core.LLMCallbacks;
    import com.iflytek.sparkchain.core.LLMError;
    import com.iflytek.sparkchain.core.LLMEvent;
    import com.iflytek.sparkchain.core.LLMResult;
    
    public class SparkLLMCallbacks implements LLMCallbacks {
    
        private static final String TAG = "SparkLLMCallbacks";
        private ISparkLLMProxy unityProxy;
    
        public SparkLLMCallbacks(ISparkLLMProxy unityProxy) {
            this.unityProxy = unityProxy;
        }
    
        @Override
        public void onLLMResult(LLMResult llmResult, Object usrContext) {
            Log.d(TAG, "onLLMResult\n");
            String content = llmResult.getContent();
            Log.e(TAG, "onLLMResult:" + content);
            int status = llmResult.getStatus();
            if (unityProxy != null) {
                unityProxy.onLLMResult(status, content);
            }
        }
    
        @Override
        public void onLLMEvent(LLMEvent event, Object usrContext) {
            Log.d(TAG, "onLLMEvent\n");
            Log.w(TAG, "onLLMEvent:" + " " + event.getEventID() + " " + event.getEventMsg());
            if (unityProxy != null) {
                unityProxy.onLLMEvent(event.getEventID(), event.getEventMsg());
            }
        }
    
        @Override
        public void onLLMError(LLMError error, Object usrContext) {
            Log.d(TAG, "onLLMError\n");
            Log.e(TAG, "errCode:" + error.getErrCode() + "errDesc:" + error.getErrMsg());
            if (unityProxy != null) {
                unityProxy.onLLMError(error.getErrCode(), error.getErrMsg());
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    实现回调接口,让Unity可以注册
    在这里插入图片描述
    点击此处导出jar包
    在这里插入图片描述

    4.导入Unity工程

    拷贝jar包和aar包至Unity工程此目录下
    在这里插入图片描述

    Androidmanifest增加
    在这里插入图片描述
    权限

    using System;
    using UnityEngine;
    using UnityEngine.UI;
    //using FfalconXR;
    
    public class IFlyLLMHandler : MonoBehaviour
    {
        public InputField input;
    
        public Button button;
    
        public Text text;
    
        private AndroidJavaObject sparkToolInstance;
        private void Start()
        {
            Loom.Initialize();
            sparkToolInstance = new AndroidJavaObject("com.rayneo.sparklib.SparkTool");
    
            InitializeSDK();
    
            button.onClick.AddListener(() =>
            {
                text.text = "";
                StartChat(input.text);
            });
        }
        public void InitializeSDK()
        {
            if (sparkToolInstance != null)
            {
                //输入开放平台的apikey等数据,appid,apiKey,apiSecret,logLevel,domain,url
                int ret = sparkToolInstance.Call("initSDK", "9e803172", "e4045501df3916cad0c4137d43db8b3b", "ZWFiZGYwMjllNTkyYTFmNjE1YTNiMWRk", 0, "general", "");
                Debug.Log("initializeSDK error code is" + ret);
            }
            else
            {
                Debug.LogError("SparkTool instance is null. Make sure the Android plugin is properly imported.");
            }
        }
        public void StartChat(string msg)
        {
            if (sparkToolInstance != null)
            {
                int ret = sparkToolInstance.Call("startChat", new SparkLLMProxy(onLLMResult, onLLMEvent, onLLMError), msg);
                Debug.Log("startChat error code is" + ret);
            }
            else
            {
                Debug.LogError("SparkTool instance is null. Make sure the Android plugin is properly imported.");
            }
        }
    
        private void onLLMError(int errorCode, string error)
        {
            Debug.Log("onLLMError errorCode " + errorCode + " error message " + error);
        }
    
        private void onLLMEvent(int eventID, string eventMsg)
        {
            Debug.Log("onLLMError eventID " + eventID + " eventMsg " + eventMsg);
        }
    
        private void onLLMResult(int status, string content)
        {
            Loom.QueueOnMainThread((p) =>
            {
                text.text += content;
            }, null);
    
            Debug.Log("onLLMResult status " + status + " content " + content);
        }
    
        public void UninitializeSDK()
        {
            if (sparkToolInstance != null)
            {
                sparkToolInstance.Call("unInitSDK");
            }
            else
            {
                Debug.LogError("SparkTool instance is null. Make sure the Android plugin is properly imported.");
            }
        }
    }
    public class SparkLLMProxy : AndroidJavaProxy
    {
    
        private Action onLLMResultCallback;
        private Action onLLMEventCallback;
        private Action onLLMErrorCallback;
    
        public SparkLLMProxy(Action onLLMResult, Action onLLMEvent, Action onLLMError) : base("com.rayneo.sparklib.ISparkLLMProxy")
        {
            onLLMResultCallback = onLLMResult;
            onLLMEventCallback = onLLMEvent;
            onLLMErrorCallback = onLLMError;
        }
    
        public void onLLMResult(int status, string content)
        {
            if (onLLMResultCallback != null)
            {
                onLLMResultCallback(status, content);
            }
        }
    
        public void onLLMEvent(int eventID, string eventMsg)
        {
            if (onLLMEventCallback != null)
            {
                onLLMEventCallback(eventID, eventMsg);
            }
        }
    
        public void onLLMError(int errorCode, string error)
        {
            if (onLLMErrorCallback != null)
            {
                onLLMErrorCallback(errorCode, error);
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    接入安卓接口
    打包运行至手机上
    在这里插入图片描述
    左边输入栏输入文本,点击按钮发送。收到返回即代表成功。

    工程仓库地址为:https://github.com/oneSitDown/spark-unity

  • 相关阅读:
    SnapHelper翻页效果两行代码带你实现不一样的RecyclerView
    C++求解一元一次方程——LeetCode 640
    DNS服务器的访问日志
    win11修改网络算法为BBR2_提升网络环境质量
    【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第一篇 嵌入式Linux入门篇-第二十八章 借助U盘或TF卡拷贝程序到开发板上
    Django学习笔记:第二章django的安装和创建应用
    pytorch如何将bin格式模型导出pt格式模型?
    vue3知识点:provide 与 inject
    长安链EVM合约地址与数据库存储账户的关系及计算
    关于token续签
  • 原文地址:https://blog.csdn.net/weixin_39637610/article/details/133989653