pythongoogle-cloud-platformgoogle-cloud-vertex-aidialogflow-cxrag

How to have a programmatical conversation with an agent created by Agent Builder


I created an agent with No Code tools offered by the Agent Builder GUI: https://vertexaiconversation.cloud.google.com/ I created a playbook and added a few Data store Tools for the agent to use for RAG. I'd like to call this agent programmatically to integrate it into mobile apps or web pages. There's a lot of code related to the classic Dialogflow agents, the Agent Builder is quite new and uses the Gemini 1.0 Pro under the hood.

I've seen this code https://stackoverflow.com/a/78229704/292502 however the question was about DialogFlow ES while the Agent Builder agent is rather a DialogFLow CX agent under the hood (and is listed in the Dialogflow CX dashboard). The Python package is promising, but I haven't found how can I have a conversation with the agent Playbook after I get hold of one.

Or maybe I'm just looking at the wrong place. I was also browsing https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/dialogflow-cx but webhooks, intents and fulfillments are for the "classic" agents. I tried to go over https://github.com/googleapis/google-cloud-python/blob/main/packages/google-cloud-dialogflow-cx/samples/generated_samples/ but haven't find the one which would help me yet.


Solution

  • Looks like the "traditional" Dialogflow CX API calls can invoke an Agent Builder agent:

    import uuid
    from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
    from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
    from google.cloud.dialogflowcx_v3beta1.types import session
    
    PROJECT_ID = "your-project-name"
    LOCATION_ID = "project-region"  # example: us-central1
    AGENT_ID = "a uuid of the agent"
    AGENT = f"projects/{PROJECT_ID}/locations/{LOCATION_ID}/agents/{AGENT_ID}"
    LANGUAGE_CODE = "en-us"
    
    SESSION_ID = uuid.uuid4()
    session_path = f"{AGENT}/sessions/{SESSION_ID}"
    print(f"Session path: {session_path}\n")
    client_options = None
    agent_components = AgentsClient.parse_agent_path(AGENT)
    location_id = agent_components["location"]
    if location_id != "global":
        api_endpoint = f"{LOCATION_ID}-dialogflow.googleapis.com:443"
        print(f"API Endpoint: {api_endpoint}\n")
        client_options = {"api_endpoint": api_endpoint}
    
    session_client = SessionsClient(client_options=client_options)
    
    text = "Your test prompt"
    text_input = session.TextInput(text=text)
    query_input = session.QueryInput(text=text_input, language_code=LANGUAGE_CODE)
    request = session.DetectIntentRequest(
        session=session_path, query_input=query_input
    )
    response = session_client.detect_intent(request=request)
    
    print("=" * 20)
    print(f"Query text: {response.query_result.text}")
    response_messages = [
        " ".join(msg.text.text) for msg in response.query_result.response_messages
    ]
    print(f"Response text: {' '.join(response_messages)}\n")