0

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.

3
  • How did you determine that the response was 200? Commented Mar 14, 2019 at 15:19
  • In the respones object. Commented Mar 15, 2019 at 20:02
  • I'm asking because the SDKs methods are asynchronous, train_agent returns a future which does not initially contain any information about the result of the training operation. Commented Mar 15, 2019 at 20:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.