public async Task AnalyzeForm()

in src/AIHub/Controllers/FormAnalyzerController.cs [40:83]


    public async Task<IActionResult> AnalyzeForm(string image_url, string prompt)
    {
        // 1. Get Image
        string image = image_url + sasUri.Query;
        Console.WriteLine(image);
        ViewBag.PdfUrl = image;
        model.PdfUrl = image;

        var client = new DocumentAnalysisClient(new Uri(FormRecogEndpoint), new AzureKeyCredential(FormRecogSubscriptionKey));
        var operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", new Uri(image));
        var result = operation.Value.Content;

        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 understanding PDFs and answering questions about it. Document OCR result is: {result}"),
            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;
        
        model.Image = image;

        return Ok(model);
    }