def create_intent(project_id, display_name, training_phrases_parts, message_texts): """Create an intent of the given intent type.""" intents_client = dialogflow_v2.IntentsClient() parent = intents_client.project_agent_path(project_id) training_phrases = [] for training_phrases_part in training_phrases_parts: part = dialogflow_v2.types.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. training_phrase = dialogflow_v2.types.Intent.TrainingPhrase( parts=[part]) training_phrases.append(training_phrase) text = dialogflow_v2.types.Intent.Message.Text(text=message_texts) message = dialogflow_v2.types.Intent.Message(text=text) intent = dialogflow_v2.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) try: response = intents_client.create_intent(parent, intent) print('Intent created: {}'.format(response)) except Exception as e: print "google.api_core.exceptions.FailedPrecondition: " + str(e)
async def construct( # type: ignore self, _global_config: GlobalConfig, time_zone: str, project: str = "nlutestframework", agent: str = "NLUTestFramework" ) -> None: """ Args: _global_config: Global configuration for the whole test framework. time_zone: The time zone, e.g. Europe/Berlin. See https://www.iana.org/time-zones for the list of possible values. project: The name of the Dialogflow project. Defaults to "nlutestframework". agent: The name of the Dialogflow agent. Defaults to "NLUTestFramework". """ self.__time_zone = time_zone self.__project = project self.__agent = agent # Create the various clients to interact with the Dialogflow API clients_config: Dict[str, Any] = {} self.__agents_client = dialogflow_v2.AgentsClient(**clients_config) self.__intents_client = dialogflow_v2.IntentsClient(**clients_config) self.__sessions_client = dialogflow_v2.SessionsClient(**clients_config) await self.__removeIntents()
def test_batch_delete_intents(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_batch_delete_intents', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2.IntentsClient() # Setup Request parent = client.project_agent_path('[PROJECT]') intents = [] response = client.batch_delete_intents(parent, intents) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchDeleteIntentsRequest( parent=parent, intents=intents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_intents(self): # Setup Expected Response next_page_token = '' intents_element = {} intents = [intents_element] expected_response = { 'next_page_token': next_page_token, 'intents': intents } expected_response = intent_pb2.ListIntentsResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2.IntentsClient() # Setup Request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_intents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.intents[0] == resources[0] assert len(channel.requests) == 1 expected_request = intent_pb2.ListIntentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def __init__(self, namespace, project): self.namespace = namespace self.project = project self.agents_client = dialogflow.AgentsClient() self.intents_client = dialogflow.IntentsClient() self.agent = self.load_agent()
def create_intent(project_id, display_name, language_code, training_phrases_parts, messages_texts): credentials = load_credentials_from_config() intents_client = dialogflow.IntentsClient(credentials=credentials) parent = intents_client.project_agent_path(project_id) 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) if isinstance(messages_texts, (str, bytes, bytearray)): messages_texts = [messages_texts] text = dialogflow.types.Intent.Message.Text(text=messages_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent, language_code=language_code) return response
def list_intents(project_id): import dialogflow_v2 as dialogflow intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) intents = intents_client.list_intents(parent) for intent in intents: print('=' * 20) print('Intent name: {}'.format(intent.name)) print('Intent display_name: {}'.format(intent.display_name)) print('Action: {}\n'.format(intent.action)) print('Root followup intent: {}'.format( intent.root_followup_intent_name)) print('Parent followup intent: {}\n'.format( intent.parent_followup_intent_name)) print('Input contexts:') for input_context_name in intent.input_context_names: print('\tName: {}'.format(input_context_name)) print('Output contexts:') for output_context in intent.output_contexts: print('\tName: {}'.format(output_context.name))
def update_intent(self, intent_id, training_phrases_parts, keep_phrases=True): print 'Updating intents...' client = dialogflow.IntentsClient() intent_name = client.intent_path(self.project_id, intent_id) intent_view = None if keep_phrases: intent = client.get_intent(intent_name, intent_view=dialogflow.enums.IntentView.INTENT_VIEW_FULL) else: intent = client.get_intent(intent_name) training_phrases = [] for phrases in training_phrases_parts: parts = [] for training_phrases_part in phrases: part = None if 'entity_type' in training_phrases_part: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part['text'], entity_type=training_phrases_part['entity_type'], alias=training_phrases_part['alias']) else: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part['text']) parts.append(part) training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=parts) training_phrases.append(training_phrase) intent.training_phrases.extend(training_phrases) response = client.update_intent(intent, language_code='en', update_mask=dialogflow.types.FieldMask(paths=['training_phrases'])) print 'Intent {} updated'.format(intent_name)
def test_batch_delete_intents(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_batch_delete_intents', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2.IntentsClient(channel=channel) # Setup Request parent = client.agent_path('[PROJECT]', '[AGENT]') intents = [] response = client.batch_delete_intents(parent, intents) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchDeleteIntentsRequest( parent=parent, intents=intents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_intent(project_id, display_name, training_phrases_parts, message_texts): intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) 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) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent, language_code=LANGUAGE_CODE) print('Intent created: {}'.format(response)) client = dialogflow.AgentsClient() parent = client.project_path(project_id) response = client.train_agent(parent)
def create_intents(): """Создаем интенты с вопросами и ответами из файла questions.json""" with open('questions.json', 'r') as qf: questions = json.load(qf) client = dialogflow_v2.IntentsClient() parent = client.project_agent_path(dialog_project_id) for title, question in questions.items(): intent = { "display_name": title, "messages": [{ "text": { "text": [question['answer']] } }], "training_phrases": [{ "parts": [{ "text": q_text }] } for q_text in question.get('questions')] } try: client.create_intent(parent, intent) except Exception as e: if 'already exists' in str(e): print('Интент уже существует') else: raise e
def create_intent(project_id): intent_client = dialogflow.IntentsClient() parent = intent_client.project_agent_path(project_id) display_name = "create_intent_test" #intent = {} #json format training_phrases_parts = ['example', 'test'] training_phrases = [] for training_phrases_part in training_phrases_parts: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part]) training_phrases.append(training_phrase) # text response message_texts = ['ok', 'thanks'] text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intent_client.create_intent(parent, intent)
def create_intent(project_id, display_name, training_phrases_parts, message_texts): """ Creates an intent with provided name and training phrases (and response messages) """ intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) training_phrases = [] for training_phrases_part in training_phrases_parts: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part]) training_phrases.append(training_phrase) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent( display_name=display_name, training_phrases=training_phrases, messages=[message]) # for p in training_phrases: # print( ) response = intents_client.create_intent(parent, intent) print('Intent created: {}'.format(response))
def delete_intent(project_id, intent_id): """Delete intent with the given intent type and intent value.""" intents_client = dialogflow.IntentsClient() intent_path = intents_client.intent_path(project_id, intent_id) intents_client.delete_intent(intent_path)
def list_intents(self): """ List all intents from chatbot """ client = dialogflow.IntentsClient(credentials=self.credentials) parent = client.project_agent_path(self.project_id) return list(client.list_intents(parent))
def make_intent(project_id): #send formating intent to dialogflow client = dialogflow.IntentsClient() parent = client.project_agent_path(project_id) intents = creating_intent(url) for intent in intents: response = client.create_intent(parent, intent)
def create_intent(PROJECT_ID, display_name, training_phrases_parts, message_texts): """Create an intent of the given intent type.""" intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(PROJECT_ID) training_phrases = [] for training_phrases_part in training_phrases_parts: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part]) training_phrases.append(training_phrase) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent( display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent) print('Intent created: {}'.format(response))
def list_intents(project_id): intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) # print (parent) intents = intents_client.list_intents(parent) intentIds = [] for intent in intents: intentIds.append(format(intent.name)) # for intent in intents: # print('=' * 20) # print('Intent name: {}'.format(intent.name)) # print('Intent display_name: {}'.format(intent.display_name)) # print('Action: {}\n'.format(intent.action)) # print('Root followup intent: {}'.format( # intent.root_followup_intent_name)) # print('Parent followup intent: {}\n'.format( # intent.parent_followup_intent_name)) # # print('Input contexts:') # for input_context_name in intent.input_context_names: # print('\tName: {}'.format(input_context_name)) # # print('Output contexts:') # for output_context in intent.output_contexts: # print('\tName: {}'.format(output_context.name)) return intentIds
def deleteQA(update, context): text = update.message.text if text.lower() == 'yes': badAnswerSelected = context.user_data['answerSelected'] intentName = badAnswerSelected['intentName'] client = dialogflow.IntentsClient() parent = client.project_agent_path(PROJECT_ID) for element in client.list_intents(parent): if element.display_name == intentName: client.delete_intent(element.name) update.message.reply_text('Question Answer Pair deleted') break with open(ratingLog, "r") as ratingFile: ratingData = json.load(ratingFile) ratingFile.close() if intentName in ratingData: ratingData.pop(intentName, None) with open(ratingLog, "w") as ratingFile: json.dump(ratingData, ratingFile) ratingFile.close() update.message.reply_text('Question Answer Pair deleted') else: update.message.reply_text('Question Answer Pair not deleted') return ConversationHandler.END
def test_batch_update_intents(self): # Setup Expected Response expected_response = {} expected_response = intent_pb2.BatchUpdateIntentsResponse( **expected_response) operation = operations_pb2.Operation( name='operations/test_batch_update_intents', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2.IntentsClient(channel=channel) # Setup Request parent = client.agent_path('[PROJECT]', '[AGENT]') language_code = 'languageCode-412800396' response = client.batch_update_intents(parent, language_code) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchUpdateIntentsRequest( parent=parent, language_code=language_code) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_intent(project_id, display_name, training_phrases_parts, message_texts): intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) 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) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent) logging.debug(f'Intent "{response.display_name}" created.')
def main(): load_dotenv() with open("questions.json", "r", encoding="UTF-8") as file: questions = json.load(file) for topic, questions in questions.items(): training_phrases = [{"text": question} for question in questions['questions']] project_id = os.getenv('PROJECT_ID') client = dialogflow_v2.IntentsClient() parent = client.project_agent_path(project_id) intent = { "display_name": topic, "messages": [{ "text": {"text": [questions['answer']]} }], "training_phrases": [ { "parts": training_phrases }, ] } response = client.create_intent(parent, intent) return response
def test_list_intents(self): # Setup Expected Response next_page_token = '' intents_element = {} intents = [intents_element] expected_response = { 'next_page_token': next_page_token, 'intents': intents } expected_response = intent_pb2.ListIntentsResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_intents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.intents[0] == resources[0] assert len(channel.requests) == 1 expected_request = intent_pb2.ListIntentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_intents(self): # Setup Expected Response expected_response = {} expected_response = intent_pb2.BatchUpdateIntentsResponse( **expected_response) operation = operations_pb2.Operation( name="operations/test_batch_update_intents", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2.IntentsClient() # Setup Request parent = client.project_agent_path("[PROJECT]") language_code = "languageCode-412800396" response = client.batch_update_intents(parent, language_code) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchUpdateIntentsRequest( parent=parent, language_code=language_code) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_intent(self, display_name, training_phrases_parts, message_texts): """Create an intent of the given intent type.""" intents_client = dialogflow.IntentsClient(credentials=self.credentials) parent = intents_client.project_agent_path(self.project_id) training_phrases = [] parts = [] training_phrases = [] for phrases in training_phrases_parts: parts = [] for training_phrases_part in phrases: part = None if 'entity_type' in training_phrases_part: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part['text'], entity_type=training_phrases_part['entity_type'], alias=training_phrases_part['alias']) else: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part['text']) parts.append(part) training_phrase = dialogflow.types.Intent.TrainingPhrase( parts=parts) training_phrases.append(training_phrase) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent(display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent)
def get_intent(self, intent_id): """ Get intent by id """ client = dialogflow.IntentsClient(credentials=self.credentials) intent_path = client.intent_path(self.project_id, intent_id) return client.get_intent( intent_path, intent_view=dialogflow.enums.IntentView.INTENT_VIEW_FULL)
def __init__(self, project_id, session_id): self.project_id = project_id self.session_id = session_id self.intents_client = dialogflow.IntentsClient() self.contexts_client = dialogflow.ContextsClient() self.parent = self.intents_client.project_agent_path(project_id) self.session_path = self.contexts_client.session_path( project_id, session_id)
def get_intent_info(project_id, session_id): intent_client = dialogflow.IntentsClient() intent_id = str("7fb45f6d-da59-423a-b179-b020490574a7") name = intent_client.intent_path(project_id, intent_id) #print(name) response = intent_client.get_intent(name, intent_view="INTENT_VIEW_FULL") #print(response) update_intent(project_id, response)
def delete_intent(intent_id): """Delete intent with the given intent type and intent value.""" try: intents_client = dialogflow.IntentsClient() # intent_path = intents_client.intent_path(PROJECT_ID, intent_id) intents_client.delete_intent(intent_id) except Exception as e: print("Error 3:", e)
def list_intents(project_id): intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) intents = intents_client.list_intents(parent) response = intents_client.batch_delete_intents(parent, intents)