in src/AIHub/Controllers/DocumentComparisonController.cs [43:99]
public async Task<IActionResult> DocumentComparison(string[] document_urls, string[] tab_names, string prompt)
{
//1. Get Documents
model.PdfUrl1 = document_urls[0] + sasUri.Query;
model.PdfUrl2 = document_urls[1] + sasUri.Query;
ViewBag.PdfUrl1 = document_urls[0] + sasUri.Query;
ViewBag.PdfUrl2 = document_urls[1] + sasUri.Query;
model.tabName1= tab_names[0];
model.tabName2= tab_names[1];
string[] output_result = new string[2];
//2. Call Form Recognizer
for (int i = 0; i < document_urls.Length; i++)
{
var client = new DocumentAnalysisClient(new Uri(FormRecogEndpoint), new AzureKeyCredential(FormRecogSubscriptionKey));
var operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", new Uri(document_urls[i] + sasUri.Query));
output_result[i] = operation.Value.Content;
}
try
{
Uri aoaiEndpointUri = new(AOAIendpoint);
AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey)
? new(aoaiEndpointUri, new DefaultAzureCredential())
: new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey));
ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName);
var messages = new ChatMessage[]
{
new SystemChatMessage($@"You are specialized in analyze different versions of the same PDF document. The first Document OCR result is: <<<{output_result[0]}>>> and the second Document OCR result is: <<<{output_result[1]}>>>"),
new UserChatMessage($@"User question: {prompt}"),
};
ChatCompletionOptions chatCompletionOptions = new()
{
MaxTokens = 1000,
Temperature = 0.7f,
FrequencyPenalty = 0,
PresencePenalty = 0,
TopP = 0.95f
};
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions);
model.Message = completion.Content[0].Text;
ViewBag.Message = completion.Content[0].Text;
}
catch (RequestFailedException)
{
throw;
}
return Ok(model);
}