Gabriel
Gabriel

Reputation: 969

Training Dialogflow Agent using Python SDK

i using dialogflow v2 with python sdk.

All works fine, except when i add a new intent with its training phrases. The bot doesnt recognize the phrases until i enter through the web console and save the intent, when the training start, after that, the bot works well.

Saving the intent from web

I tried to train the intent using the python sdk:

agent_client = dialogflow.AgentsClient(credentials=self.credentials)
response = agent_client.train_agent('projects/' + self.project_id)

The response is 200, but the agent wasnt trained.

Thanks to any clue how to make this work.

Upvotes: 0

Views: 341

Answers (2)

Ivan Aguas
Ivan Aguas

Reputation: 57

This works for me. Currently, I'm using FastApi. The secret is to add the function done(). That would return true or false if the operation finished successfully.

@router.post("/chatbot/train/{project_id}", response_model=IGetResponseBase)
async def train_agent(
    project_id: str, 
) -> Any:

# Create a client
agent_client = AgentsAsyncClient()
parent = agent_client.common_project_path(project_id)

# Initialize request argument(s)
request = TrainAgentRequest(
    parent=parent,
)

# Make the request
operation = await agent_client.train_agent(request=request)

print("Waiting for operation to complete...")


response = await operation.done()

# Handle the response
print('operation', response)

Upvotes: 0

v2a
v2a

Reputation: 11

May be, it helps:

def train_agent(project_id):
    from google.cloud import dialogflow

    agents_client = dialogflow.AgentsClient()
    parent = dialogflow.AgentsClient.common_project_path(project_id)
    response = agents_client.train_agent(
        request={"parent": parent}
    )

    print(response.done())

P.S.: GOOGLE_APPLICATION_CREDENTIALS in my .env

Upvotes: 1

Related Questions