gpt4 book ai didi

android - Dialogflow API v2 集成问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:08 24 4
gpt4 key购买 nike

我在 android 上使用 Dialogflow v1 但我想使用 V2 因为它提供了更多功能并且因为 V1 将是 deprecated 2019 年 10 月 23 日。但是我无法将 API V2 集成到我的代码中。这是 V1 代码:

private void initChatbot() {
final AIConfiguration config = new AIConfiguration("Here put Client access token",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
aiDataService = new AIDataService(this, config);
customAIServiceContext = AIServiceContextBuilder.buildFromSessionId(uuid);// helps to create new session whenever app restarts
aiRequest = new AIRequest();
}

这是V2代码:

private void initV2Chatbot() {
try {
InputStream stream = getResources().openRawResource(R.raw.test_agent_credentials);
GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
String projectId = ((ServiceAccountCredentials)credentials).getProjectId();

SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
sessionsClient = SessionsClient.create(sessionsSettings);
session = SessionName.of(projectId, uuid);
} catch (Exception e) {
e.printStackTrace();
}
}

当我为 V2 编写此代码时出现此错误:

error: cannot find symbol variable test_agent_credentials

我实际上不知道什么是 test_agent_credentials 以及为什么要使用它。谁能告诉我应该将 console.cloud.google Dialogflow İntegration Key Id 放在哪里?

这是 Gradle :

apply plugin: 'com.android.application'

android {
buildToolsVersion "28.0.3"
compileSdkVersion 28
defaultConfig {
applicationId "com.tyagiabhinav.dialogflowchat"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

repositories {
mavenCentral()
}


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// Java V2
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/INDEX.LIST'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.apis:google-api-services-oauth2:v1-rev145-1.25.0'


// Dialogflow SDK dependencies
implementation 'ai.api:sdk:2.0.7@aar'
implementation 'ai.api:libai:1.6.12'

// Java V2
implementation 'com.google.cloud:google-cloud-dialogflow:0.67.0-alpha'
// for Remote Procedure Call to avoid "No functional channel service provider found" error while creating SessionsClient
implementation 'io.grpc:grpc-okhttp:1.15.1'
}

最佳答案

R.raw.test_agent_credentials 指的是假定位于原始资源文件夹中的 test_agent_credentials.json 文件。此 JSON 文件包含您在 created the Dialogflow Integration service account 时下载的完整凭据文件。 .

忽略有关使用 key 的部分。它使用 gloud 命令生成要在 auth header 中使用的 key 。您正在使用 GoogleCredentials.fromStream() 调用执行此操作。

重要的部分是在您保存 JSON key 文件并将其作为原始资源提供给 Android 之前的第 10 步中。参见 this information关于 Android 资源,特别是关于原始资源的部分。原始资源应放在名为“raw”的子文件夹中的“resource”文件夹下。您保存在那里的文件的名称应该与 R.raw 之后的资源名称部分匹配,不带“json”扩展名。

关于android - Dialogflow API v2 集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53970393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com