コード例 #1
0
def test_create_document(capsys):
    document_management.create_document(
        PROJECT_ID,
        pytest.KNOWLEDGE_BASE_ID,
        DOCUMENT_DISPLAY_NAME,
        "text/html",
        "FAQ",
        "https://cloud.google.com/storage/docs/faq",
    )
    out, _ = capsys.readouterr()
    assert DOCUMENT_DISPLAY_NAME in out
コード例 #2
0
def test_create_knowledge_base(capsys):
    # Check the knowledge base does not yet exists
    knowledge_base_management.list_knowledge_bases(PROJECT_ID)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) not in out

    # Create a knowledge base
    knowledge_base_management.create_knowledge_base(PROJECT_ID,
                                                    KNOWLEDGE_BASE_NAME)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out

    # List the knowledge base
    knowledge_base_management.list_knowledge_bases(PROJECT_ID)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out

    knowledge_base_id = out.split('knowledgeBases/')[1].rstrip()

    # Get the knowledge base
    knowledge_base_management.get_knowledge_base(PROJECT_ID, knowledge_base_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out

    # Create a Document
    document_management.create_document(
        PROJECT_ID, knowledge_base_id, DOCUMENT_BASE_NAME, 'text/html', 'FAQ',
        'https://cloud.google.com/storage/docs/faq')

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out

    # List the Document
    document_management.list_documents(PROJECT_ID, knowledge_base_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out

    document_id = out.split('documents/')[1].split(' - MIME Type:')[0].rstrip()

    # Get the Document
    document_management.get_document(PROJECT_ID, knowledge_base_id,
                                     document_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out

    # Detect intent with Knowledge Base
    detect_intent_knowledge.detect_intent_knowledge(
        PROJECT_ID, SESSION_ID, 'en-us', knowledge_base_id, TEXTS)

    out, _ = capsys.readouterr()
    assert 'Knowledge results' in out

    # Delete the Document
    document_management.delete_document(PROJECT_ID, knowledge_base_id,
                                        document_id)

    # List the Document
    document_management.list_documents(PROJECT_ID, knowledge_base_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) not in out

    # Delete the Knowledge Base
    knowledge_base_management.delete_knowledge_base(PROJECT_ID,
                                                    knowledge_base_id)

    # List the knowledge base
    knowledge_base_management.list_knowledge_bases(PROJECT_ID)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) not in out
コード例 #3
0
def test_analyze_content_text(capsys):
    """Test analyze content api with text only messages.
    """
    # Create knowledge base.
    knowledge_base_management.create_knowledge_base(
        PROJECT_ID, KNOWLEDGE_BASE_DISPLAY_NAME)
    out, _ = capsys.readouterr()
    knowledge_base_id = out.split('knowledgeBases/')[1].rstrip()
    # Get the knowledge base
    knowledge_base_management.get_knowledge_base(PROJECT_ID, knowledge_base_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(KNOWLEDGE_BASE_DISPLAY_NAME) in out

    # Create documents. Note that you should get read permission of bucket gs://ruogu/parsed_5_24/7157212.html
    # via Pantheon for service account (google application credential account) from here:
    # https://pantheon.corp.google.com/storage/browser/ruogu/parsed_5_24/?project=agent-assistant-demo
    document_management.create_document(PROJECT_ID, knowledge_base_id,
                                        DOCUMENT_DISPLAY_NAME, 'text/html',
                                        'ARTICLE_SUGGESTION',
                                        'gs://ruogu/parsed_5_24/7157212.html')
    out, _ = capsys.readouterr()
    document_id = out.split('documents/')[1].split(' - MIME Type:')[0].rstrip()

    # Get the Document
    document_management.get_document(PROJECT_ID, knowledge_base_id,
                                     document_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(DOCUMENT_DISPLAY_NAME) in out

    # Create conversation profile.
    conversation_profile_management.create_conversation_profile_article_faq(
        project_id=PROJECT_ID,
        display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
        article_suggestion_knowledge_base_id=knowledge_base_id)

    out, _ = capsys.readouterr()
    assert 'Display Name: {}'.format(CONVERSATION_PROFILE_DISPLAY_NAME) in out
    conversation_profile_id = out.split('conversationProfiles/')[1].rstrip()

    # Create conversation.
    conversation_management.create_conversation(
        project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

    out, _ = capsys.readouterr()
    conversation_id = out.split('conversations/')[1].rstrip()

    # Create end user participant.
    participant_management.create_participant(project_id=PROJECT_ID,
                                              conversation_id=conversation_id,
                                              role='END_USER')
    out, _ = capsys.readouterr()
    end_user_id = out.split('participants/')[1].rstrip()

    # Create human agent participant.
    participant_management.create_participant(project_id=PROJECT_ID,
                                              conversation_id=conversation_id,
                                              role='HUMAN_AGENT')
    out, _ = capsys.readouterr()
    human_agent_id = out.split('participants/')[1].rstrip()

    # AnalyzeContent
    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=human_agent_id,
        text='Hi, how are you?')
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text='Hi, I am doing well, how about you?')
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=human_agent_id,
        text='Great. How can I help you?')
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text='So I ordered something, but I do not like it.')
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text='Thinking if I can cancel that order')
    suggestion_out, _ = capsys.readouterr()
    # Currently suggestion_out won't contain the suggestion we want since it
    # takes time for document to be ready to serve.
    # assert 'Cancel an order' in suggestion_out

    # Complete conversation.
    conversation_management.complete_conversation(
        project_id=PROJECT_ID, conversation_id=conversation_id)

    # Delete conversation profile.
    conversation_profile_management.delete_conversation_profile(
        project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

    # Delete document.
    document_management.delete_document(PROJECT_ID, knowledge_base_id,
                                        document_id)

    # Delete the Knowledge Base.
    knowledge_base_management.delete_knowledge_base(PROJECT_ID,
                                                    knowledge_base_id)
コード例 #4
0
def test_analyze_content_text(capsys):
    """Test analyze content api with text only messages.
    """
    # Create knowledge base.
    knowledge_base_management.create_knowledge_base(
        PROJECT_ID, KNOWLEDGE_BASE_DISPLAY_NAME)
    out, _ = capsys.readouterr()
    knowledge_base_id = out.split("knowledgeBases/")[1].rstrip()
    # Get the knowledge base
    knowledge_base_management.get_knowledge_base(PROJECT_ID, knowledge_base_id)

    out, _ = capsys.readouterr()
    assert "Display Name: {}".format(KNOWLEDGE_BASE_DISPLAY_NAME) in out

    # Create documents. Note that you should get read permission of bucket gs://cloud-samples-data/dialogflow/participant_test.html
    # via Pantheon for service account (google application credential account) from here:
    # https://support.google.com/googleshopping/answer/9116497
    document_management.create_document(
        PROJECT_ID,
        knowledge_base_id,
        DOCUMENT_DISPLAY_NAME,
        "text/html",
        "ARTICLE_SUGGESTION",
        "gs://cloud-samples-data/dialogflow/participant_test.html",
    )
    out, _ = capsys.readouterr()
    document_id = out.split("documents/")[1].split(" - MIME Type:")[0].rstrip()

    # Get the Document
    document_management.get_document(PROJECT_ID, knowledge_base_id,
                                     document_id)

    out, _ = capsys.readouterr()
    assert "Display Name: {}".format(DOCUMENT_DISPLAY_NAME) in out

    # Create conversation profile.
    conversation_profile_management.create_conversation_profile_article_faq(
        project_id=PROJECT_ID,
        display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
        article_suggestion_knowledge_base_id=knowledge_base_id,
    )

    out, _ = capsys.readouterr()
    assert "Display Name: {}".format(CONVERSATION_PROFILE_DISPLAY_NAME) in out
    conversation_profile_id = out.split("conversationProfiles/")[1].rstrip()

    # Create conversation.
    conversation_management.create_conversation(
        project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

    out, _ = capsys.readouterr()
    conversation_id = out.split("conversations/")[1].rstrip()

    # Create end user participant.
    participant_management.create_participant(project_id=PROJECT_ID,
                                              conversation_id=conversation_id,
                                              role="END_USER")
    out, _ = capsys.readouterr()
    end_user_id = out.split("participants/")[1].rstrip()

    # Create human agent participant.
    participant_management.create_participant(project_id=PROJECT_ID,
                                              conversation_id=conversation_id,
                                              role="HUMAN_AGENT")
    out, _ = capsys.readouterr()
    human_agent_id = out.split("participants/")[1].rstrip()

    # AnalyzeContent
    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=human_agent_id,
        text="Hi, how are you?",
    )
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text="Hi, I am doing well, how about you?",
    )
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=human_agent_id,
        text="Great. How can I help you?",
    )
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text="So I ordered something, but I do not like it.",
    )
    out, _ = capsys.readouterr()

    participant_management.analyze_content_text(
        project_id=PROJECT_ID,
        conversation_id=conversation_id,
        participant_id=end_user_id,
        text="Thinking if I can cancel that order",
    )
    suggestion_out, _ = capsys.readouterr()
    # Currently suggestion_out won't contain the suggestion we want since it
    # takes time for document to be ready to serve.
    # assert 'Cancel an order' in suggestion_out

    # Complete conversation.
    conversation_management.complete_conversation(
        project_id=PROJECT_ID, conversation_id=conversation_id)

    # Delete conversation profile.
    conversation_profile_management.delete_conversation_profile(
        project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

    # Delete document.
    document_management.delete_document(PROJECT_ID, knowledge_base_id,
                                        document_id)

    # Delete the Knowledge Base.
    knowledge_base_management.delete_knowledge_base(PROJECT_ID,
                                                    knowledge_base_id)