private async Task <(IEnumerable <AnalyzeSentimentResult> sentimentResults, IEnumerable <RecognizePiiEntitiesResult> piiResults, IEnumerable <string> errors)> GetOperationResults(int index, string operationId)
        {
            var errors           = new List <string>();
            var sentimentResults = new List <AnalyzeSentimentResult>();
            var piiResults       = new List <RecognizePiiEntitiesResult>();

            try
            {
                Log.LogInformation($"Sending text analytics request for document chunk with id {index}.");
                using var cts = new CancellationTokenSource();
                cts.CancelAfter(RequestTimeout);

                var textAnalyticsOperation = new AnalyzeActionsOperation(operationId, TextAnalyticsClient);

                await textAnalyticsOperation.UpdateStatusAsync().ConfigureAwait(false);

                if (!textAnalyticsOperation.HasCompleted)
                {
                    throw new InvalidOperationException("Tried to get result of not completed text analytics request.");
                }

                await foreach (var documentsInPage in textAnalyticsOperation.GetValuesAsync())
                {
                    foreach (var piiResult in documentsInPage.RecognizePiiEntitiesResults)
                    {
                        if (piiResult.HasError)
                        {
                            errors.Add($"PII recognition failed with error: {piiResult.Error.Message}");
                        }

                        piiResults.AddRange(piiResult.DocumentsResults);
                    }

                    foreach (var sentimentResult in documentsInPage.AnalyzeSentimentResults)
                    {
                        if (sentimentResult.HasError)
                        {
                            errors.Add($"Sentiment analysis failed with error: {sentimentResult.Error.Message}");
                        }

                        sentimentResults.AddRange(sentimentResult.DocumentsResults);
                    }
                }
            }
            catch (OperationCanceledException operationCanceledException)
            {
                throw new TransientFailureException($"Operation was canceled after {RequestTimeout.TotalSeconds} seconds.", operationCanceledException);
            }

            // do not catch throttling errors, rather throw and retry
            catch (RequestFailedException e) when(e.Status != 429)
            {
                errors.Add($"Text analytics request failed with error: {e.Message}");
            }

            return(sentimentResults, piiResults, errors);
        }
Esempio n. 2
0
        public async Task AnalyzeOperationWithPagination()
        {
            TextAnalyticsClient client = GetClient();

            List <string> documents = new();

            for (int i = 0; i < 23; i++)
            {
                documents.Add("Elon Musk is the CEO of SpaceX and Tesla.");
            }

            TextAnalyticsActions batchActions = new TextAnalyticsActions()
            {
                ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>()
                {
                    new ExtractKeyPhrasesAction()
                },
                DisplayName = "AnalyzeOperationWithPagination",
            };

            AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(documents, batchActions);

            Assert.IsFalse(operation.HasCompleted);
            Assert.IsFalse(operation.HasValue);

            Assert.ThrowsAsync <InvalidOperationException>(async() => await Task.Run(() => operation.Value));
            Assert.ThrowsAsync <InvalidOperationException>(async() => await Task.Run(() => operation.GetValuesAsync()));

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasCompleted);
            Assert.IsTrue(operation.HasValue);

            // try async
            //There most be 2 pages as service limit is 20 documents per page
            List <AnalyzeActionsResult> asyncPages = operation.Value.ToEnumerableAsync().Result;

            Assert.AreEqual(2, asyncPages.Count);

            // First page should have 20 results
            Assert.AreEqual(20, asyncPages[0].ExtractKeyPhrasesResults.FirstOrDefault().DocumentsResults.Count);

            // Second page should have remaining 3 results
            Assert.AreEqual(3, asyncPages[1].ExtractKeyPhrasesResults.FirstOrDefault().DocumentsResults.Count);

            // try sync
            //There most be 2 pages as service limit is 20 documents per page
            List <AnalyzeActionsResult> pages = operation.GetValues().AsEnumerable().ToList();

            Assert.AreEqual(2, pages.Count);

            // First page should have 20 results
            Assert.AreEqual(20, pages[0].ExtractKeyPhrasesResults.FirstOrDefault().DocumentsResults.Count);

            // Second page should have remaining 3 results
            Assert.AreEqual(3, pages[1].ExtractKeyPhrasesResults.FirstOrDefault().DocumentsResults.Count);
        }