File: sample_update_knowledge_sources.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (93 lines) | stat: -rw-r--r-- 3,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Sample - Update project knowledge sources and add QnAs & synonyms.

Demonstrates:
  * Creating a project
  * Adding a URL knowledge source
  * Adding QnA pairs
  * Adding synonyms

Environment variables required:
  * AZURE_QUESTIONANSWERING_ENDPOINT
  * AZURE_QUESTIONANSWERING_KEY

Run with: python sample_update_knowledge_sources.py
"""

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.authoring import QuestionAnsweringAuthoringClient, models as _models


def sample_update_knowledge_sources():
    # [START update_knowledge_sources]
    endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
    key = os.environ["AZURE_QUESTIONANSWERING_KEY"]

    client = QuestionAnsweringAuthoringClient(endpoint, AzureKeyCredential(key))
    with client:
        project_name = "MicrosoftFAQProject"
        client.create_project(
            project_name=project_name,
            options={
                "description": "Test project for some Microsoft QnAs",
                "language": "en",
                "multilingualResource": True,
                "settings": {"defaultAnswer": "no answer"},
            },
        )

        sources_poller = client.begin_update_sources(
            project_name=project_name,
            sources=[
                _models.UpdateSourceRecord(
                    op="add",
                    value=_models.UpdateQnaSourceRecord(
                        display_name="MicrosoftFAQ",
                        source="https://www.microsoft.com/en-in/software-download/faq",
                        source_uri="https://www.microsoft.com/en-in/software-download/faq",
                        source_kind="url",
                        content_structure_kind="unstructured",
                        refresh=False,
                    ),
                )
            ],
        )
        sources_poller.result()
        print("Knowledge source added (MicrosoftFAQ)")

        qna_poller = client.begin_update_qnas(
            project_name=project_name,
            qnas=[
                _models.UpdateQnaRecord(
                    op="add",
                    value=_models.QnaRecord(
                        id=1,
                        questions=["What is the easiest way to use azure services in my .NET project?"],
                        answer="Using Microsoft's Azure SDKs",
                        source="manual",
                    ),
                )
            ],
        )
        qna_poller.result()
        print("QnA added (1 record)")

        client.update_synonyms(
            project_name=project_name,
            synonyms=_models.SynonymAssets(
                value=[
                    _models.WordAlterations(alterations=["qnamaker", "qna maker"]),
                    _models.WordAlterations(alterations=["qna", "question and answer"]),
                ]
            ),
        )
        synonyms = client.list_synonyms(project_name=project_name)
        for item in synonyms:  # ItemPaged
            print("Synonyms group:")
            for alt in item["alterations"]:
                print(f"  {alt}")
    # [END update_knowledge_sources]


if __name__ == "__main__":
    sample_update_knowledge_sources()