question

SAI-5851 avatar image
0 Votes"
SAI-5851 asked SAI-5851 commented

How to upload a file to QnA Maker knowledge base using API ?

Hi, I am using python for the QnA maker services API. My aim is to create knowledge base by uploading a file (.pdf, .docx, etc.). I am referring to following code : create-new-knowledge-base.py . But there is no specification of how to pass file arguments. I am new to Azure services, please can anyone help me with that. Thanks.


azure-qna-maker
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SAI-5851 Thanks for the question. You can create your KB by using the QnAMaker APIs. Please follow the document for API's.
In particular, you can use the Update Knowledge Base operation, which will allow you to add/delete QnA Pairs and / or URLs to an existing knowledge base.


0 Votes 0 ·

Basically You can use UpdateKbOperationDTO Class listed here: class

but it allows to update KB with files using code instead of the interface.

Below is new code:

 QnAMakerClient qnaC = new QnAMakerClient(new ApiKeyServiceClientCredentials(<subscription-key>)) { Endpoint = "https://<your-custom-domain>.cognitiveservices.azure.com"};
 log.LogInformation("Delete-->Start");
 List<string> toDelete = new List<string>();
 toDelete.Add("<my-file>");
 var updateDelete = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
     {
         // Create JSON of changes ///
         Add = null, 
         Update = null, 
         Delete = new UpdateKbOperationDTODelete(null, toDelete)
     });
 log.LogInformation("Delete-->Done"); 
    
    
 log.LogInformation("Add-->Start");
 List<FileDTO> toAdd = new List<FileDTO>();
 toAdd.Add(new FileDTO("<my-file>", "<url-to-file>"));
 var updateAdd = await qnaC.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
     {
         // Create JSON of changes ///
         Add = new UpdateKbOperationDTOAdd(null, null, toAdd),
         Update = null,
         Delete = null
     });
 log.LogInformation("Add-->Done"); 


0 Votes 0 ·

Hi ramr-msft, thanks for reaching out the question. I have used create for creating a new knowledge base by uploading a file Multiturn-ContosoBenefits.pdf. But it returns me a error response as follows:

  "errorResponse": {
         "error": {
             "code": "BadArgument",
             "message": "Failed to extract QnAs from the source",
             "details": [
                 {
                     "code": "ExtractionFailure",
                     "message": "Unsupported / Invalid url(s). Failed to extract Q&A from the source",
                     "target": "Multiturn-ContosoBenefits.pdf",
                     "innerError": {
                         "code": "CannotParse"
                     }
                 }
             ]
         }
     }

Please help me out with these.


0 Votes 0 ·

1 Answer

ramr-msft avatar image
0 Votes"
ramr-msft answered SAI-5851 commented

@SAI-5851 Here is the quick-start code to create a KB, We are able to create the KB and verify the same in QnA maker.

Python: https://github.com/Azure-Samples/cognitive-services-quickstart-code/tree/master/python/QnAMaker/rest

118605-image.png
118578-image.png



image.png (21.8 KiB)
image.png (69.2 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi ramr-msft, thanks for reaching out, but I have problem uploading file. I have tried the code for the repo. you shared. There is the same problem, same errorResponse:

 "errorResponse": {
         "error": {
             "code": "BadArgument",
             "message": "Failed to extract QnAs from the source",
             "details": [
                 {
                     "code": "ExtractionFailure",
                     "message": "Unsupported / Invalid url(s). Failed to extract Q&A from the source",
                     "target": "structured.docx",
                     "innerError": {
                         "code": "CannotParse"
                     }
                 }
             ]
         }
     }

Also, now can you guide me how to get the q&a created ? I have tried downloading knowledge base but they are returning something else. Please help me with this too.


0 Votes 0 ·

@SAI-5851 Thanks for the details. Can you please share the request body that you are trying. Please follow the document to create multiple turns of a conversation in QnA Maker.

https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/multiturn-conversation#display-order-supported-in-api

For Multi-turn conversations:

119275-screenshot-224.png


0 Votes 0 ·
screenshot-224.png (63.5 KiB)

I am following this example, I have same request header as in the example create


0 Votes 0 ·