一、功能预览
| 在线教育 | 智能客服 | 情感陪伴 |
| | | |
三、开通服务
使用 AIConversationKit 前,请先参考 开通服务,领取体验版或开通付费版。
四、环境要求
·系统版本:Android 5.0 (SDK API level 21)及以上
·开发环境:Android Studio 3.5+(Gradle 3.5.4+)
·设备要求:Android 5.0+ 的真机设备
六、步骤 1:下载 AIConversationKit 组件
从 GitHub 克隆/下载代码,将Android/aiconversationkit目录复制到你的工程中(与 app 目录同级)。
七、步骤 2:工程配置
1. 在工程根目录的settings.gradle(或 settings.gradle.kts)文件中添加以下代码,它的作用是将aiconversationkit组件导入到你当前的项目中。
settings.gradlesettings.gradle.ktsinclude ':aiconversationkit'include (":aiconversationkit")
2. 在 app 目录的build.gradle(或 build.gradle.kts)文件中添加以下代码,它的作用是声明当前app对新加入的aiconversationkit组件的依赖。
build.gradlebuild.gradle.ktsapi project(':aiconversationkit')api(project(":aiconversationkit"))
3. 在 proguard-rules.pro 文件中添加以下代码,因为我们在 SDK 内部使用了Java 的反射特性,需要将 SDK 的部分类加入不混淆名单。
-keep class com.tencent.** { *; }
4. 为了防止编译时,AndroidManifest合并过程中产生属性冲突,你需要在 app/src/main/AndroidManifest.xml 文件的 <application> 节点中,添加 tools:replace="android:allowBackup" 和 android:allowBackup="false",用以覆盖组件内的设置。
// app/src/main/AndroidManifest.xml <application android:name=".DemoApplication" android:allowBackup="false" android:icon="@drawable/app_ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:theme="@style/AppTheme" tools:replace="android:allowBackup">
5. 配置 Kotlin 支持(可选)
·如果你的项目已是 Kotlin 工程:可跳过此步。
·如果你的项目尚未配置 Kotlin:请添加对应版本的 Kotlin Gradle 插件:
注意:若需添加 Kotlin Gradle 插件,$kotlin_version 请填入具体版本号,并确保其与你项目的 Android Gradle 插件版本兼容(如 $kotlin_version 设置为 1.9.0 ,对应的 Android Gradle 插件版本是 8.6.0)。可以参见 Kotlin-Gradle 插件兼容性。
// project root build.gradlebuildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }}
八、完成登录
在你的项目中添加登录代码,这是使用 AIConversationKit 各项功能的关键前提:
KotlinJavaval userId = "denny" // 请替换为你的 UserIDval sdkAppId = 1400000001 // 请替换为开通服务控制台的 SDKAppIDval sdkSecretKey = "xxxx" // 请替换为开通服务控制台的 SDKSecretKeyval userSig = GenerateTestUserSig.genTestUserSig(sdkAppId, userId, sdkSecretKey)TUILogin.login(this, sdkAppId, userId, userSig, object : TUICallback() { override fun onSuccess() {} override fun onError(errorCode: Int, errorMessage: String) {}})String userId = "denny"; // 请替换为你的 UserIDint sdkAppId = 1400000001; // 请替换为开通服务控制台的 SDKAppIDString sdkSecretKey = "xxxx"; // 请替换为开通服务控制台的 SDKSecretKeyString userSig = GenerateTestUserSig.genTestUserSig(sdkAppId, userId, sdkSecretKey);TUILogin.login(this, sdkAppId, userId, userSig, new TUICallback() { @Override public void onSuccess() {} @Override public void onError(int errorCode, String errorMessage) {}});
登录接口参数说明
| 参数 | 类型 | 说明 |
| SDKAppID | Int | 从 TRTC 控制台 > 应用管理 获取。 |
| UserID | String | 当前用户的用户ID,仅包含英文字母、数字、连字符和下划线。 |
| userSig | String | 用于腾讯云鉴权的票据。请注意:开发环境:你可以使用本地 GenerateTestUserSig.genTestSig 函数生成 UserSig 或者通过 UserSig 辅助工具 生成临时的 UserSig。生产环境:为了防止密钥泄露,请务必采用服务端生成 UserSig 的方式。详细信息请参见 服务端生成 UserSig。更多信息请参见 如何计算及使用 UserSig。 |
九、集成 AI 对话界面
只需要将 AIConversationView 添加到你的布局文件中即可。
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.trtc.uikit.aiconversationkit.AIConversationView android:id="@+id/app_ai_conversation_view" android:layout_width="match_parent" android:layout_height="match_parent"/></FrameLayout>
十、开始你的第一次 AI 对话
登录成功后,使用以下代码发起 AI 对话:
KotlinJavaval config = AIConversationConfig()val sdkAppId = 1400000001 // 1、替换 sdkAppIdval sdkSecretKey = "xxx" // 2、替换 sdkSecretKeyval aiRobotId = "robot_" + TUILogin.getUserId()val aiRobotSig = GenerateTestUserSig.genTestUserSig(sdkAppId, aiRobotId, sdkSecretKey)config.agentConfig.aiRobotId = aiRobotIdconfig.agentConfig.aiRobotSig = aiRobotSigconfig.secretId = "xxx" // 3、替换 secretIdconfig.secretKey = "xxx" // 4、替换 secretKey// 5、替换 llmConfigconfig.llmConfig = "{\\"LLMType\\":\\"openai\\",\\"Model\\":\\"hunyuan-turbo-latest\\",\\"SystemPrompt\\":\\"你是一个私人助手\\",\\"APIUrl\\":\\"https:xxx\\",\\"APIKey\\":\\"xxx\\",\\"History\\":5,\\"Streaming\\":true}"// 6、替换 ttsConfigconfig.ttsConfig = "{\\"TTSType\\":\\"tencent\\",\\"AppId\\":\\"xxx\\",\\"SecretId\\":\\"xxx\\",\\"SecretKey\\":\\"xxx\\",\\"VoiceType\\":\\"502001\\",\\"Speed\\":1.25,\\"Volume\\":5,\\"PrimaryLanguage\\":1,\\"FastVoiceType\\":\\"\\"}"val aiConversationView = findViewById<AIConversationView>(R.id.app_ai_conversation_view)aiConversationView.startAIConversation(config, null)AIConversationConfig config = new AIConversationConfig();int sdkAppId = 1400000001; // 1、替换 sdkAppIdString sdkSecretKey = "xxx"; // 2、替换 sdkSecretKeyString aiRobotId = "robot_" + TUILogin.getUserId();String aiRobotSig = GenerateTestUserSig.genTestUserSig(sdkAppId, aiRobotId, sdkSecretKey);config.getAgentConfig().setAiRobotId(aiRobotId);config.getAgentConfig().setAiRobotSig(aiRobotSig);config.setSecretId("xxx"); // 3、替换 secretIdconfig.setSecretKey("xxx"); // 4、替换 secretKey// 5、替换 llmConfigconfig.setLlmConfig("{\\"LLMType\\":\\"openai\\",\\"Model\\":\\"hunyuan-turbo-latest\\",\\"SystemPrompt\\":\\"你是一个私人助手\\",\\"APIUrl\\":\\"https:xxx\\",\\"APIKey\\":\\"xxx\\",\\"History\\":5,\\"Streaming\\":true}");// 6、替换 ttsConfigconfig.setTtsConfig("{\\"TTSType\\":\\"tencent\\",\\"AppId\\":\\"xxx\\",\\"SecretId\\":\\"xxx\\",\\"SecretKey\\":\\"xxx\\",\\"VoiceType\\":\\"502001\\",\\"Speed\\":1.25,\\"Volume\\":5,\\"PrimaryLanguage\\":1,\\"FastVoiceType\\":\\"\\"}");AIConversationView aiConversationView = findViewById(R.id.app_ai_conversation_view);aiConversationView.startAIConversation(config, null);
1. SDKAppId 和 SDKSecretKey 使用 完成登录 获取的数据即可。
2. 请到 准备工作 配置 AI 对话后台参数,包括基础配置、STT、LLM、TTS;然后点击右下角的快速跑通,切换到 Android,获取 SecretId、SecretKey 和 Config 参数。
3. 将云 API 的 SecretId 和 SecretKey 复制到 config.secretId 和 config.secretKey 中,SecretId 和 SecretKey 请妥善保存,避免泄露。
4. 将 Config 信息复制到 JSON 解析工具中,例如 JSONUtil,将 LLMConfig 对应的字符串值复制到 config.llmConfig,将 TTSConfig 对应的字符串值复制到 config.ttsConfig 中。
注意:说明:
十一、调节对话效果
为了在 AI 对话中获得最佳体验,清晰的声音采集至关重要。我们提供多种参数设置,帮助你在不同环境下都能获得清晰的语音交互效果。
十二、vadLevel 远场人声抑制
在多人交谈的环境中,其他人声可能会干扰 AI 对你声音的识别。通过调整人声抑制级别,我们可以有效过滤环境中的其他人声,让 AI 更准确地专注于你的语音;你可以通过设置 config 中的 sttConfig.vadLevel 参数来达到期望的效果。
| 级别 | 效果描述 | 使用场景 |
| 0 | 关闭远场人声抑制功能 | 在嘈杂环境下 AI 容易被噪声打断,可能导致无法正常对话。 |
| 1 | 开启轻度抑制模式 | 适合相对安静的环境使用,例如家庭、图书馆。 |
| 2 | 推荐默认设置 | 适用于大多数日常场景(默认值)。 |
| 3 | 开启强力抑制模式 | 适合嘈杂环境使用,例如菜市场、车站。 |
代码配置示例
KotlinJavaval config = AIConversationConfig()config.sttConfig.vadLevel = 3aiConversationView.startAIConversation(config, null)AIConversationConfig config = new AIConversationConfig();config.getSttConfig().setVadLevel(2);aiConversationView.startAIConversation(config, null);
十三、interruptMode 打断模式
根据你的使用习惯,可以设置不同的打断方式,在 AI 说话时进行打断,使对话交互更加自然流畅;你可以通过设置 config 中的 agentConfig.interruptMode 参数来达到期望的效果。
| 模式 | 功能描述 | 适用场景 |
| 0 | 智能模式 | AI 正在说话时,可通过点击屏幕或说话打断。(默认值) |
| 1 | 手动打断 | AI 正在说话时,只能通过点击屏幕打断。 |
代码配置示例
KotlinJavaval config = AIConversationConfig()config.agentConfig.interruptMode = 1aiConversationView.startAIConversation(config, null)AIConversationConfig config = new AIConversationConfig();config.getAgentConfig().setInterruptMode(1);aiConversationView.startAIConversation(config, null);
十四、language 语言识别
根据说话者使用的语言进行设置,AI 才能准确地将语音转换成对应的文字内容;你可以通过设置 config 中的 sttConfig.language 参数来达到期望的效果。
| 语言代码 | 语言名称 | 说明 |
| "zh" | 中文 | 支持中文识别 (默认值) |
| "en" | 英语 | 支持英语识别 |
更多支持的语言,请参考 STTConfig 产品文档。
代码配置示例
KotlinJavaval config = AIConversationConfig()config.sttConfig.language = "en"aiConversationView.startAIConversation(config, null)AIConversationConfig config = new AIConversationConfig();config.getSttConfig().setLanguage("en");aiConversationView.startAIConversation(config, null);
十六、更新背景图
将新的背景图保存到 drawable-xxxhdpi 文件夹中,通过 setBackgroundImage 接口设置你自定义的背景图。
KotlinJavaaiConversationView.setBackgroundResource(R.drawable.app_bg_ai_conversation_full_screen)aiConversationView.setBackgroundImage(R.drawable.app_bg_ai_conversation_full_screen);
最终效果:
十七、图标定制
AIConversationKit 用到的所有图标都存放于 res/drawable-xxhdpi 目录下, 部分示例如下,你可以根据你的诉求来替换目录下的图标。
| 图标路径 | 图标样式 | 详细描述 |
| conversation_ic_ai_state_interrupting | | 打断 AI 说话的按钮。 |
| conversation_ic_mic_state_on | | 本地 Mic 打开时的图标。 |
| conversation_ic_mic_state_off | | 本地 Mic 被静音时的图标。 |
| conversation_ic_hang_up | | 结束 AI 对话的图标。 |
例如,你需要替换 Mic 的图标,将新的图标直接覆盖原来的图标即可。
最终效果:
十八、隐藏 UI
如果你不需要 麦克风、字幕、AI 交互 按钮,可以通过 disableFeatures 将它移除。例如你需要移除麦克风和 AI 交互,可以通过以下代码实现。
KotlinJavaval features = listOf(Feature.MIC, Feature.AI_INTERACTION)aiConversationView.disableFeatures(features)List<Feature> features = List.of(Feature.MIC, Feature.AI_INTERACTION);aiConversationView.disableFeatures(features);
最终效果:
20、每次进房都需要调用登录吗?
不需要。通常你只需要完成一次 TUILogin.login 调用即可,建议将 TUILogin.login 和 TUILogin.logout 与自己的登录业务关联。
21、集成代码后产生如下所示编译报错 allowBackup 异常,如何处理?
·问题原因:多个模块的 AndroidManifest.xml 中都配置了 allowBackup 属性 ,造成冲突。
·解决方法:
1. 在工程的 AndroidManifest.xml 文件中删除 allowBackup 属性或将该属性改为 false,表示关闭备份和恢复功能;
2. 在 AndroidManifest.xml文件的 application 节点中添加 tools:replace="android:allowBackup",表示覆盖其他模块的设置,使用你自己的设置;
22、集成代码后还需要添加麦克风等权限的声明吗?
不需要,AIConversationKit 中已经内置了麦克风等权限的声明,在接入过程中你无需再关心这些权限的声明。