Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions documentation-samples/batchtesting/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker;
using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker;
using Microsoft.Azure.CognitiveServices.Knowledge.QnAMaker.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -39,6 +39,7 @@ static void Main(string[] args)
var watch = new Stopwatch();
watch.Start();
var maxLines = inputQueryData.Count;

foreach (var queryData in inputQueryData)
{
try
Expand All @@ -47,36 +48,43 @@ static void Main(string[] args)
var (queryDto, kbId, expectedAnswerId) = GetQueryDTO(queryData);
var response = runtimeClient.Runtime.GenerateAnswer(kbId, queryDto);

var resultLine = new List<string>();
resultLine.Add(lineNumber.ToString());
resultLine.Add(kbId);
resultLine.Add(queryDto.Question);
var topLineNumber = 0;
foreach (var ans in response.Answers)
{
topLineNumber++;

// Add the first answer and its score
var firstResult = response.Answers.FirstOrDefault();
var answer = firstResult?.Answer?.Replace("\n", "\\n");
// Column for line number. Format is {QuestionNumber}.{TopAnswerNumber}
var colLineNumber = $"{lineNumber.ToString()}.{topLineNumber.ToString()}";

resultLine.Add(answer);
resultLine.Add(firstResult?.Score?.ToString());
var resultLine = new List<string>();
resultLine.Add(colLineNumber);
resultLine.Add(kbId);
resultLine.Add(queryDto.Question);

// Add Metadata
var metaDataList = firstResult?.Metadata?.Select(x => $"{x.Name}:{x.Value}")?.ToList();
resultLine.Add(metaDataList == null ? string.Empty : string.Join("|", metaDataList));
var answer = ans?.Answer?.Replace("\n", "\\n");

// Add the QnaId
var firstQnaId = firstResult?.Id?.ToString();
resultLine.Add(firstQnaId);
resultLine.Add(answer);
resultLine.Add(ans?.Score?.ToString());

// Add expected answer and label
if (!string.IsNullOrWhiteSpace(expectedAnswerId))
{
resultLine.Add(expectedAnswerId);
resultLine.Add(firstQnaId == expectedAnswerId ? "Correct" : "Incorrect");
}
// Add Metadata
var metaDataList = ans?.Metadata?.Select(x => $"{x.Name}:{x.Value}")?.ToList();
resultLine.Add(metaDataList == null ? string.Empty : string.Join("|", metaDataList));

var result = string.Join('\t', resultLine);
File.AppendAllText(outputFile, $"{result}{Environment.NewLine}");
PrintProgress(watch, lineNumber, maxLines);
// Add the QnaId
var firstQnaId = ans?.Id?.ToString();
resultLine.Add(firstQnaId);

// Add expected answer and label
if (!string.IsNullOrWhiteSpace(expectedAnswerId))
{
resultLine.Add(expectedAnswerId);
resultLine.Add(firstQnaId == expectedAnswerId ? "Correct" : "Incorrect");
}

var result = string.Join('\t', resultLine);
File.AppendAllText(outputFile, $"{result}{Environment.NewLine}");
PrintProgress(watch, lineNumber, maxLines, colLineNumber);
}
}
catch (Exception ex)
{
Expand All @@ -86,12 +94,12 @@ static void Main(string[] args)

}

private static void PrintProgress(Stopwatch watch, int lineNumber, int maxLines)
private static void PrintProgress(Stopwatch watch, int lineNumber, int maxLines, string colLineNumber = "")
{
var qps = (double)lineNumber / watch.ElapsedMilliseconds * 1000.0;
var remaining = maxLines - lineNumber;
var etasecs = (long)Math.Ceiling(remaining * qps);
Console.WriteLine($"Done : {lineNumber}/{maxLines}. {remaining} remaining. ETA: {etasecs} seconds.");
Console.WriteLine($"Done : {colLineNumber}/{maxLines}. {remaining} remaining. ETA: {etasecs} seconds.");
}

private static (QueryDTO, string, string) GetQueryDTO(List<string> queryData)
Expand Down