com.google.apps.framework.request.NotFoundException: 找不到项目的 DesignTimeAgent

com.google.apps.framework.request.NotFoundException: No DesignTimeAgent found for project

我一直在努力找出为什么会抛出上述错误。我已经阅读并尝试了 google 上的所有解决方案,但问题仍然存在。此代码片段在行中引发错误:

$response = $sessionsClient->detectIntent($session, $queryInput);

感谢您的关注。

<?php
//namespace Google\Cloud\Samples\Auth;
namespace Google\Cloud\Dialogflow;
require 'vendor/autoload.php';

use Google\Cloud\Dialogflow\V2\EntityTypesClient;

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;

$keyfile = "/var/www/html/newestproject-329521-ffa8cd057f77.json";
$projectId = 'newestproject-329521';

detect_intent_texts($projectId, "hi", "12345", $keyfile);

function detect_intent_texts($projectId, $text, $sessionId, $keyfile, $languageCode = 'en-US')
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $keyfile); //your path to file of cred
    // new session
    $test = array('credentials' => $keyfile, 'api_endpoint' => 'europe-west1-dialogflow.googleapis.com:443');
    $sessionsClient = new SessionsClient($test);

    $session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());

    printf('Session path: %s' . PHP_EOL, $session);
echo "h3";

    // create text input
    $textInput = new TextInput();
    $textInput->setText($text);
    $textInput->setLanguageCode($languageCode);
echo "h4";

    // create query input
    $queryInput = new QueryInput();
    $queryInput->setText($textInput);
echo "h5"; 
    // get response and relevant info
    $response = $sessionsClient->detectIntent($session, $queryInput);

    $sessionsClient->close();
}

错误消息可能有多种原因。他们如下:

  • 您的代理可能未链接到您正在访问的项目 ID。请参阅 GitHub and .
  • 中的类似问题
  • 如果您尝试更改代理的位置,我建议您先导出代理,然后通过创建新代理重新开始,这次请确保您选择了正确的区域和项目 ID。因为,除非重新创建代理,否则现有代理的位置不会改变。引自此 doc,

“Once an agent is created, its location cannot change. In order to change an agent's location, you must export and restore to a new agent with a different location.”

  • 您可能没有在 API 请求中指定区域。对于 REST 调用,您必须执行以下两项操作:

    • 为位置 URL 路径参数提供区域 ID。
    • 使用 [region-id]-dialogflow.googleapis.com 作为主机名。

    例如:

  us-central1-dialogflow.googleapis.com/projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID}