Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Update an Intent #68

Closed
Akashutreja opened this issue Jun 23, 2018 · 9 comments
Closed

Update an Intent #68

Akashutreja opened this issue Jun 23, 2018 · 9 comments
Assignees
Labels
api: dialogflow Issues related to the googleapis/python-dialogflow API. type: question Request for information or clarification. Not an issue.

Comments

@Akashutreja
Copy link

Akashutreja commented Jun 23, 2018

Hi,
can anyone please tell how to update an intent using update_intent? for example, if I want to add some more training phrases after intent has been created or to add message text after intent creation
Thanks

@AlexSissoko
Copy link

AlexSissoko commented Jun 25, 2018

If you look at the type of an intent's training phrases attribute, you'll notice that it's a <google.protobuf.pyext._message.RepeatedCompositeCo>.
As far as I can tell, its list of operations can be found on https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/containers.py.

I'm not sure if this is exactly how you are supposed to do it, but I would first get the intent from the agent, then modify it, and then use it as an argument for update_intent.
The general process is:

client = dialogflow.IntentsClient()
intent = get_intent(project_id), intent_name)
new_training_phrases = [list of new training phrases]
intent.training_phrases.extend(training_phrases)
response = client.update_intent(intent, language_code='en'

get_intent should be a function that I believe you can find in one of the samples
You don't need to input any of the other arguments for update_intent for this to work.
Also, I haven't spent enough time experimenting to see whether or not you can update the intent without copying it over entirely, but it should be relatively simple if you understand the function of the update_mask. Adding message text should be relatively similar

Trying to figure these things out can be pretty infuriating right now since DialogFlow is pretty knew, but the tools are all there so as long as you're patient, you'll figure it out.

@AlexSissoko
Copy link

AlexSissoko commented Jun 27, 2018

Sorry if what I said earlier was a bit murky.
This piece of code should do the trick:

def update_intent(project_id, intent_name, training_phrases):
    client = dialogflow.IntentsClient()
    parent = client.project_agent_path(project_id)
    intents = client.list_intents(parent)
    intent_path = [
        intent.name for intent in intents
        if intent.display_name == intent_name]
    intent = client.get_intent(intent_path[0])
    intent.training_phrases.extend(training_phrases)
    response  = client.update_intent(intent, language_code='en')

@mauropico-dev
Copy link

Hello everyone!
Thanks for that last method. I'm having some issues trying to make it work. This line intent.training_phrases.extend(training_phrases) throws "TypeError: Not a cmessage". Any idea?

I've tried to call the method like this:
update_intent(PROJECT_ID,"1","Test")
update_intent(PROJECT_ID,"1",["Test"])

A print just before that line shows that the intent is correct.
print(intent)
display_name: "1"
priority: 500000
messages {
text {
}
}

Any ideas?
Thanks!

@tigersoldier
Copy link

You need to pass intent_view=dialogflow_v2.enums.IntentView.INTENT_VIEW_FULL to get_intent. Otherwise not everything of the intent is returned.

Training phrases should be defined as follows: https://github.com/dialogflow/dialogflow-python-client-v2/blob/cdb21e624bc0e69518296fa6ef1b5f2746cdcadc/samples/intent_management.py#L70-L76

Here is an updated version of update_intent (not tested, may have bugs):

def update_intent(project_id, intent_id, training_phrases_parts):
    client = dialogflow.IntentsClient()
    intent_name = client. intent_path(project_id, intent_id)
    intent = client.get_intent(name, intent_view=dialogflow_v2.enums.IntentView.INTENT_VIEW_FULL)
    training_phrases = []
    for training_phrases_part in training_phrases_parts:
        part = dialogflow.types.Intent.TrainingPhrase.Part(
            text=training_phrases_part)
        training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part])
        training_phrases.append(training_phrase)
    intent.training_phrases.extend(training_phrases)
    response  = client.update_intent(intent, language_code='en')

@JustinBeckwith JustinBeckwith added 🚨 This issue needs some love. triage me I really want to be triaged. labels Sep 20, 2018
@progresivoJS
Copy link

progresivoJS commented Oct 8, 2018

@tigersoldier
Hi, you update_intent code work well, but there is a problem where entities in training phrase are not recognized. It's recognized just text.
So, when detecting text query, It detect intent(because we add training phrase), but cannot detect entities which the text includes.

In shorts, How to specify the entities in training phrase?

@AlexSissoko
Copy link

@progresivoJS
Each Part in the training phrase is not just text but also the labels that go with it (i.e. the Entity and Parameter that it corresponds to). You have to specify that when you make the training phrase part. So, dialogflow.Types.Intent.TrainingPhrase.Part(text=text, entity_type=entity_type, alias=alias) where text is the string that will be part of the training phrase, entity_type is a string prefixed with @ that will correspond to the entity, and alias is a string that corresponds to the parameter name.
Also, since this is not specifically about updating an intent and more about creating training phrases, you really should have made a new issue so that others can find your question. The current issue should have been closed with @tigersoldier's answer.

@JustinBeckwith JustinBeckwith added type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. 🚨 This issue needs some love. labels Oct 22, 2018
@nnegrey
Copy link
Contributor

nnegrey commented Oct 23, 2018

Hi, this appears to have been answered, but if anyone needs further help here is the docs on BatchUpdateIntentsRequest

@nnegrey nnegrey closed this as completed Oct 23, 2018
@ramachandraiah-putta
Copy link

@progresivoJS
Each Part in the training phrase is not just text but also the labels that go with it (i.e. the Entity and Parameter that it corresponds to). You have to specify that when you make the training phrase part. So, dialogflow.Types.Intent.TrainingPhrase.Part(text=text, entity_type=entity_type, alias=alias) where text is the string that will be part of the training phrase, entity_type is a string prefixed with @ that will correspond to the entity, and alias is a string that corresponds to the parameter name.
Also, since this is not specifically about updating an intent and more about creating training phrases, you really should have made a new issue so that others can find your question. The current issue should have been closed with @tigersoldier's answer.

here it is treating entire text as entity, how can i specify specific word in the text as entity.

@yejindaye
Copy link

how can i delete the current text response and change it to something else??
what should i use instead of .extend?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: dialogflow Issues related to the googleapis/python-dialogflow API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

9 participants