Error trying to restore a Dialogflow CX agent with a data store in the python library

140 Views Asked by At

I'm trying to restore the contents of a Dialogflow CX agent into a new different agent. Basically trying to clone an agent. The original agent has a data store associated with it in order to make LLM responses. The agent was originally exported in the JSON Package format, not raw bytes.

The idea is being able to do it from the python Dialogflow CX library in order to automate the process. But the same error happens when I try to do it from the web UI.

The error in the web UI is:

Cannot restore data store references into an agent that is not associated with a Vertex AI Search and Conversation app. Code: FAILED_PRECONDITION

In the python library, using this function (https://cloud.google.com/python/docs/reference/dialogflow-cx/latest/google.cloud.dialogflowcx_v3.services.agents.AgentsClient#google_cloud_dialogflowcx_v3_services_agents_AgentsClient_restore_agent) throws this pretty similar error:

google.api_core.exceptions.FailedPrecondition: 400 com.google.apps.framework.request.FailedPreconditionException: Cannot restore data store references into an agent that is not associated with a Vertex AI Search and Conversation app. Code: FAILED_PRECONDITION 9: com.google.apps.framework.request.FailedPreconditionException: Cannot restore data store references into an agent that is not associated with a Vertex AI Search and Conversation app. Code: FAILED_PRECONDITION

I get the fact that I need to associate a “Vertex AI Search and Conversation app” to this new agent, and add the same data store to the agent as well. The new agent is in the same account and same Dialogflow CX project than the original one, so the data store shouldn't be a problem.

But I don't know how to do that programmatically. Wasn't able to find a function in the python library to associate to the app: https://cloud.google.com/python/docs/reference/dialogflow-cx/latest

Also tried to look at the Vertex AI SDK for python: https://cloud.google.com/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk But don't know how to do it there either.

Can anyone help me please? Thanks in advance.

1

There are 1 best solutions below

1
Hunter On

It looks like nothing can be done via the Python Client for now, but check out this link. The REST API is available for use.

A quick sketch of something that should get close to you need within Python:

import json
from os.path import join
import subprocess

import requests

def create_chat_engine(
    display_name: str,
    project: str,
    datastore_name: str,
    location: str = "global",
    business_display_name: str = "{company}",
) -> None:
    """
    Create a chat engine that utilizes a data store.

    Parameters
    ----------
    display_name : str
        The display name of the engine and agent.
    project : str
        The project ID of your agent.
    datastore_name : str
        The name of the data store to use. This can either be the full path to the datastore,
        or just the datastore ID.
    business_display_name : str
        Providing your company name helps the model provide higher-quality responses
    """
    access_token = subprocess.run(
        ["gcloud", "auth", "print-access-token"],
        capture_output=True,
        text=True,
        check=True
    ).stdout.strip()
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json; charset=UTF-8',
        'X-Goog-User-Project': f'{project}'
    }

    data = {
        "displayName": display_name,
        "dataStoreIds": [datastore_name],
        "solutionType": "SOLUTION_TYPE_CHAT",
        "chatEngineConfig": {
            "agentCreationConfig":{
                "business": business_display_name,
                "timeZone": "America/Los_Angeles",
                "defaultLanguageCode": "en",
                "location": location
                }
            }
        }

    parent = f"projects/{project}/locations/{location}/collections/default_collection"
    if datastore_name.startswith(join(parent, "datastores")):
        datastore_name = datastore_name.split('/')[-1]
    endpoint_url = f'https://discoveryengine.googleapis.com/v1/{parent}/engines?engineId={datastore_name}'
    response = requests.post(f'{endpoint_url}', data=json.dumps(data), headers=headers, timeout=90)
    json_response = json.loads(response.text)
    if response.status_code != 200:
        raise ValueError(json_response)