def setup_teardown(): # Create a knowledge base to use in document management client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) response = client.create_knowledge_base(project_path, knowledge_base) pytest.KNOWLEDGE_BASE_ID = response.name.split( '/knowledgeBases/')[1].split('\n')[0] # Create a document to delete knowledge_base_path = client.knowledge_base_path( PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) document = dialogflow.types.Document( display_name=DOCUMENT_DISPLAY_NAME, mime_type='text/html', content_uri='https://cloud.google.com/storage/docs/faq') document.knowledge_types.append( dialogflow.types.Document.KnowledgeType.Value('FAQ')) documents_client = dialogflow.DocumentsClient() response = documents_client.create_document(knowledge_base_path, document) document = response.result(timeout=90) pytest.DOCUMENT_ID = document.name.split('/documents/')[1].split('\n')[0] yield # Delete the created knowledge base client.delete_knowledge_base(knowledge_base_path, force=True)
def test_create_knowledge_base(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = knowledge_base_pb2.KnowledgeBase( **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_v2beta1.KnowledgeBasesClient() # Setup Request parent = client.project_path('[PROJECT]') knowledge_base = {} response = client.create_knowledge_base(parent, knowledge_base) assert expected_response == response assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.CreateKnowledgeBaseRequest( parent=parent, knowledge_base=knowledge_base) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_knowledge_base(PROJECT_ID, display_name): """ Creates a Knowledge base. called by POST /knowledge_base format returned: { "knowledge-base-id": "string", "display-name": "string", "document-amount": 0 } :parameter PROJECT_ID: The GCP project linked with the agent. :parameter display_name: The display name of the Knowledge base. :return knowledge_base_dict: the info of new created knowledege base """ client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase(display_name=display_name) response = client.create_knowledge_base(project_path, knowledge_base) #testing stout print('Knowledge Base created:\n') print('Display Name: {}\n'.format(response.display_name)) print('Knowledge ID: {}\n'.format(response.name.split("/")[-1])) print('Detail Location: {}\n'.format(response.name)) #content returned knowledge_base_dict = dict() knowledge_base_dict["knowledge-base-id"] = str( response.name.split("/")[-1]) knowledge_base_dict["display-name"] = str(response.display_name) knowledge_base_dict["document-amount"] = 0 # returned done, directly use as response return knowledge_base_dict
def test_get_knowledge_base(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" language_code = "languageCode-412800396" expected_response = { "name": name_2, "display_name": display_name, "language_code": language_code, } expected_response = knowledge_base_pb2.KnowledgeBase( **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_v2beta1.KnowledgeBasesClient() # Setup Request name = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") response = client.get_knowledge_base(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.GetKnowledgeBaseRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def detect_intent_texts(project_id, session_id, text, language_code, crop): session_client = dialogflow.SessionsClient() session = session_client.session_path(project_id, session_id) if text: text_input = dialogflow.types.TextInput(text=text, language_code=language_code) query_input = dialogflow.types.QueryInput(text=text_input) knowledgebase_client = dialogflow.KnowledgeBasesClient() Parent = knowledgebase_client.project_path(project_id) knowledge_base_path = '' knowledge_bases = [] for element in knowledgebase_client.list_knowledge_bases( parent=Parent): knowledge_bases.append(element) print(knowledge_bases) if crop == 'wheat': knowledge_base_path = knowledge_bases[0].name else: knowledge_base_path = knowledge_bases[2].name query_params = dialogflow.types.QueryParameters( knowledge_base_names=[knowledge_base_path]) response = session_client.detect_intent(session=session, query_input=query_input, query_params=query_params) print(response.query_result.fulfillment_text) return response.query_result.fulfillment_text
def test_update_knowledge_base(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" language_code = "languageCode-412800396" expected_response = { "name": name, "display_name": display_name, "language_code": language_code, } expected_response = knowledge_base_pb2.KnowledgeBase( **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_v2beta1.KnowledgeBasesClient() # Setup Request knowledge_base = {} response = client.update_knowledge_base(knowledge_base) assert expected_response == response assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.UpdateKnowledgeBaseRequest( knowledge_base=knowledge_base) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_knowledge_bases(self): # Setup Expected Response next_page_token = "" knowledge_bases_element = {} knowledge_bases = [knowledge_bases_element] expected_response = { "next_page_token": next_page_token, "knowledge_bases": knowledge_bases, } expected_response = knowledge_base_pb2.ListKnowledgeBasesResponse( **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_v2beta1.KnowledgeBasesClient() # Setup Request parent = client.project_path("[PROJECT]") paged_list_response = client.list_knowledge_bases(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.knowledge_bases[0] == resources[0] assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.ListKnowledgeBasesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_knowledge_bases(self): # Setup Expected Response next_page_token = '' knowledge_bases_element = {} knowledge_bases = [knowledge_bases_element] expected_response = { 'next_page_token': next_page_token, 'knowledge_bases': knowledge_bases } expected_response = knowledge_base_pb2.ListKnowledgeBasesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') paged_list_response = client.list_knowledge_bases(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.knowledge_bases[0] == resources[0] assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.ListKnowledgeBasesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_knowledge_base(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' expected_response = {'name': name_2, 'display_name': display_name} expected_response = knowledge_base_pb2.KnowledgeBase( **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_v2beta1.KnowledgeBasesClient() # Setup Request name = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') response = client.get_knowledge_base(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.GetKnowledgeBaseRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def teardown(): yield # Delete the created knowledge base client = dialogflow.KnowledgeBasesClient() assert pytest.KNOWLEDGE_BASE_ID is not None knowledge_base_path = client.knowledge_base_path(PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) client.delete_knowledge_base(knowledge_base_path)
def setup(): # Create a knowledge base to delete client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) response = client.create_knowledge_base(project_path, knowledge_base) pytest.KNOWLEDGE_BASE_ID = response.name.split( '/knowledgeBases/')[1].split('\n')[0]
def test_list_knowledge_bases_exception(self): channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup request parent = client.project_path('[PROJECT]') paged_list_response = client.list_knowledge_bases(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_update_knowledge_base_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.KnowledgeBasesClient() with pytest.raises(CustomException): client.update_knowledge_base()
def test_delete_knowledge_base_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup request name = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') with pytest.raises(CustomException): client.delete_knowledge_base(name)
def test_create_knowledge_base_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup request parent = client.project_path('[PROJECT]') knowledge_base = {} with pytest.raises(CustomException): client.create_knowledge_base(parent, knowledge_base)
def test_list_knowledge_bases_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.KnowledgeBasesClient() # Setup request parent = client.project_path("[PROJECT]") paged_list_response = client.list_knowledge_bases(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_delete_knowledge_base_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.KnowledgeBasesClient() # Setup request name = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") with pytest.raises(CustomException): client.delete_knowledge_base(name)
def test_create_knowledge_base_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.KnowledgeBasesClient() # Setup request parent = client.project_path('[PROJECT]') knowledge_base = {} with pytest.raises(CustomException): client.create_knowledge_base(parent, knowledge_base)
def test_delete_knowledge_base(self): channel = ChannelStub() client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup Request name = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') client.delete_knowledge_base(name) assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.DeleteKnowledgeBaseRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def delete_knowledge_base(PROJECT_ID, KID): """ Deletes a specific Knowledge base. called by DELETE /knowledge_base/{KID} :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: Id of the Knowledge base. """ client = dialogflow.KnowledgeBasesClient() knowledge_base_path = client.knowledge_base_path(PROJECT_ID, KID) response = client.delete_knowledge_base(knowledge_base_path) print('Knowledge Base deleted.'.format(response))
def setup_teardown(): # Create a knowledge base to list client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) response = client.create_knowledge_base(project_path, knowledge_base) knowledge_base_id = response.name.split('/knowledgeBases/')[1].split( '\n')[0] yield # Delete the created knowledge base knowledge_base_path = client.knowledge_base_path(PROJECT_ID, knowledge_base_id) client.delete_knowledge_base(knowledge_base_path)
def setup_teardown(): # Create a knowledge base to use in document management client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) knowledge_base = dialogflow.types.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) response = client.create_knowledge_base(project_path, knowledge_base) pytest.KNOWLEDGE_BASE_ID = response.name.split( '/knowledgeBases/')[1].split('\n')[0] yield # Delete the created knowledge base knowledge_base_path = client.knowledge_base_path(PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) client.delete_knowledge_base(knowledge_base_path, force=True)
def test_delete_knowledge_base(self): channel = ChannelStub() patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.KnowledgeBasesClient() # Setup Request name = client.knowledge_base_path("[PROJECT]", "[KNOWLEDGE_BASE]") client.delete_knowledge_base(name) assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.DeleteKnowledgeBaseRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_knowledge_base(project_id, display_name): """Creates a Knowledge base. Args: project_id: The GCP project linked with the agent. display_name: The display name of the Knowledge base.""" import dialogflow_v2beta1 as dialogflow client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(project_id) knowledge_base = dialogflow.types.KnowledgeBase( display_name=display_name) response = client.create_knowledge_base(project_path, knowledge_base) print('Knowledge Base created:\n') print('Display Name: {}\n'.format(response.display_name)) print('Knowledge ID: {}\n'.format(response.name))
def get_knowledge_base(PROJECT_ID, KID): """ Gets a specific Knowledge base. called by GET /knowledge_base/{KID} format returned { "knowledge-base-id": "string", "knowledge-base-name": "string", "document-amount": 0, "documents": [ { "document-id": "searched from knowledge base management", "document-name": "stackoverflow", "MIME-type": "text/csv", "Knowledge-type": "FAQ", "content_uri": "gs://test/test01.csv" } ] } :parameter PROJECT_ID: The GCP project linked with the agent. :parameter KID: Id of the Knowledge base. :return knowledge_base_dict: the details of a specific knowledge base """ client = dialogflow.KnowledgeBasesClient() knowledge_base_path = client.knowledge_base_path(PROJECT_ID, KID) response = client.get_knowledge_base(knowledge_base_path) #testing stout print('Got Knowledge Base:') print(' - Display Name: {}'.format(response.display_name)) print(' - Knowledge ID: {}'.format(response.name.split("/")[-1])) print(' - Detail Location: {}'.format(response.name)) #content returned knowledge_base_dict = dict() knowledge_base_dict["knowledge-base-id"] = str( response.name.split("/")[-1]) knowledge_base_dict["display-name"] = str(response.display_name) knowledge_base_dict["document-amount"] = counter(PROJECT_ID, KID) knowledge_base_dict["documents"] = document_implement.list_documents( PROJECT_ID, KID) # returned done, directly use as response return knowledge_base_dict
def list_knowledge_bases(PROJECT_ID): """List all availabe Knowledge bases. called by GET /knowledge_base format returned: [ { "knowledge-base-id": "string", "display-name": "string", "document-amount": 0 } ] :parameter PROJECT_ID: The GCP project linked with the agent. :return knowledge_base_list: the list of kbs """ client = dialogflow.KnowledgeBasesClient() project_path = client.project_path(PROJECT_ID) #testing stout print('Knowledge Bases for: {}'.format(PROJECT_ID)) for knowledge_base in client.list_knowledge_bases(project_path): print(' - Display Name: {}'.format(knowledge_base.display_name)) print(' - Knowledge ID: {}\n'.format( knowledge_base.name.split("/")[-1])) print(' - Detail Location: {}\n'.format(knowledge_base.name)) #content returned knowledge_base_list = [] for knowledge_base in client.list_knowledge_bases(project_path): knowledge_base_dict = dict() knowledge_base_dict["knowledge-base-id"] = str( knowledge_base.name.split("/")[-1]) knowledge_base_dict["display-name"] = str(knowledge_base.display_name) knowledge_base_dict["document-amount"] = counter( PROJECT_ID, knowledge_base.name.split("/")[-1]) knowledge_base_list.append(knowledge_base_dict) # returned done, directly use as response return knowledge_base_list
def test_get_knowledge_base(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' expected_response = {'name': name_2, 'display_name': display_name} expected_response = knowledge_base_pb2.KnowledgeBase( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2beta1.KnowledgeBasesClient(channel=channel) # Setup Request name = client.knowledge_base_path('[PROJECT]', '[KNOWLEDGE_BASE]') response = client.get_knowledge_base(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = knowledge_base_pb2.GetKnowledgeBaseRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request