เรียก QnA Maker ด้วย Postman

สร้าง QnA Maker ตามนี้ Exercise – Create a bot – Learn | Microsoft Docs

เสร็จแล้วก็ Publish จะได้ตัวอย่างการเรียกด้วย Postman และ Curl

POST /knowledgebases/65c21545-xxxx-4cc9-982f-35649a25398a/generateAnswer
Host: https://2021-11-qna.azurewebsites.net/qnamaker
Authorization: EndpointKey 6a5fc60b-xxxx-4121-a921-159eac6eec90
Content-Type: application/json
{"question":"<Your question>"}
curl -X POST https://2021-11-qna.azurewebsites.net/qnamaker/knowledgebases/65c21545-xxxx-4cc9-982f-35649a25398a/generateAnswer -H "Authorization: EndpointKey 6a5fc60b-xxxx-4121-a921-159eac6eec90" -H "Content-type: application/json" -d "{'question':'<Your question>'}"

ลองเรียกด้วย Postman

Response แต่ละแบบ

{
    "error": {
        "code": "Unauthorized",
        "message": "Authorization Failed"
    }
}
{
    "answers": [
        {
            "questions": [],
            "answer": "No good match found in KB.",
            "score": 0.0,
            "id": -1,
            "isDocumentText": false,
            "metadata": []
        }
    ],
    "activeLearningEnabled": false
}
{
    "answers": [
        {
            "questions": [
                "Hello",
                "Hi"
            ],
            "answer": "Hi man!",
            "score": 100.0,
            "id": 9,
            "source": "Editorial",
            "isDocumentText": false,
            "metadata": [],
            "context": {
                "isContextOnly": false,
                "prompts": []
            }
        }
    ],
    "activeLearningEnabled": false
}

Link

Azure AI

Microsoft Certified

#CertifiedExam
1Azure AI FundamentalsExam AI-900
2Azure AI Engineer AssociateExam AI-102
3Azure Data Scientist AssociateExam DP-100

Azure AI Fundamentals , Learning paths

1.Get started with artificial intelligence

2.Explore visual tools for machine learning

3.Explore computer vision

4.Explore natural language processing

5.Explore conversational AI

6.Explore decision support

7.Explore knowledge mining

Choose a bot-building tool

Prepare for AI engineering

Azure Services

Azure Machine Learning studio

Anomaly DetectorDocumentation

QnA MakerDocumentationเรียก QnA Maker ด้วย Postman

Pricing – Face APIDoc

Computer Vision Docs

Custom VisionCustom VisionDoc

Azure Form Recognizer Doc

Language

Cognitive Speech ServicesDoc

Translator documentation

LUISDoc

Azure Bot Service

Bot FrameworkDocumentationbotframework-sdk

Microsoft

Seeing AI App from Microsoft

WebApp ที่ Authorization ด้วย Bearer Token

1.สร้าง WebApp

สร้างโปรเจ็กส์แบบ ASP.NET Web Application ชื่อ WebApp1

เลือก template แบบ Web API

2.แก้ไข routeTemplate

เดิม routeTemplate จะมีค่าเป็น "api/{controller}/{id}" ถ้าต้องการปรับ routeTemplate สามารถปรับได้ที่ไฟล์ App_start/WebApiConfig.cs

เช่นถ้าต้องการให้มี 2 GET Method ใน controller เดียวกัน ให้แยกด้วย action

routeTemplate: "{controller}/{action}/{id}",

และแก้ไขที่ controller

// GET mycontroller/getName
[HttpGet]
[ActionName("getName")]
public string getName([FromBody]myReq1 req) 
{}

// GET mycontroller/getId
[HttpGet]
[ActionName("getId")]
public string getId([FromBody]myReq2 req)
{}

3.รับ Authorization ด้วย Bearer Token

รับ Authorization ด้วย Bearer Token แบบ short string of hexadecimal characters

ตัวอย่างโค๊ดจาก Action Request Token Verification C# Sample

// POST api/values
public void Post([FromBody]string value)
{
    try
    {
        HttpRequestMessage request = Request;

        // Validate that we have a bearer token.
        if (request.Headers.Authorization == null ||
            !string.Equals(request.Headers.Authorization.Scheme, "bearer", StringComparison.OrdinalIgnoreCase) ||
            string.IsNullOrEmpty(request.Headers.Authorization.Parameter))
        {
            log.Error("missing bearer token");
            return;
        }

        // Get the token from the Authorization header 
        string bearerToken = request.Headers.Authorization.Parameter;
        log.Info(string.Format("bearerToken [{0}]", bearerToken));
    }
    catch (Exception ex)
    {
        log.Error(ex.Message);
        log.Error(ex.ToString());
    }
}

ตัวอย่งการเรียก request ด้วย Postman

Posted in C#

การ split ไฟล์ PDF ด้วย iTextSharp

ติดตั้ง

PM> Install-Package iTextSharp -Version 5.5.13.2

หน้าแรกของ PDF คือหน้าที่ 1

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            try
            {
                log.Info("*********************************");
                log.Info("************* BEGIN *************");
                log.Info(AppDomain.CurrentDomain.FriendlyName);

                string sourcePDFpath = "iptest.pdf";
                string outputPDFpath = "optest.pdf";
                int startpage = 1;
                int endpage = 1;
                ExtractPages(sourcePDFpath, outputPDFpath, startpage, endpage);

                log.Info("************** END **************");
                log.Info("");
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.ToString());
            }
        } // end main

        private static void ExtractPages(string sourcePDFpath, string outputPDFpath, int startpage, int endpage)
        {
            PdfReader reader = null;
            Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage = null;

            reader = new PdfReader(sourcePDFpath);
            sourceDocument = new Document(reader.GetPageSizeWithRotation(startpage));
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPDFpath, System.IO.FileMode.Create));

            sourceDocument.Open();

            for (int i = startpage; i <= endpage; i++)
            {
                importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                pdfCopyProvider.AddPage(importedPage);
            }
            sourceDocument.Close();
            reader.Close();
        }
    } // end class
}