ผสานรวมเครื่องมือตอบคำถาม BERT

Task Library BertQuestionAnswerer API จะโหลดโมเดล Bert และตอบคำถามโดยอิงตามเนื้อหาของข้อความที่ระบุ ดูข้อมูลเพิ่มเติมได้ที่ตัวอย่างสำหรับโมเดลคำถาม-คำตอบ

ฟีเจอร์หลักของ BertQuestionAnswerer API

  • รับอินพุตข้อความ 2 รายการเป็นคำถามและบริบท แล้วแสดงรายการคำตอบที่เป็นไปได้

  • ดำเนินการสร้างโทเค็น Wordpiece หรือ Sentencepiece นอกกราฟในข้อความ อินพุต

โมเดล BertQuestionAnswerer ที่รองรับ

รุ่นต่อไปนี้ใช้ได้กับ BertNLClassifier API

เรียกใช้การอนุมานใน Java

ขั้นตอนที่ 1: นำเข้าการอ้างอิง Gradle และการตั้งค่าอื่นๆ

คัดลอก.tfliteไฟล์โมเดลไปยังไดเรกทอรีชิ้นงานของโมดูล Android ที่จะเรียกใช้โมเดล ระบุว่าไม่ควรบีบอัดไฟล์ และ เพิ่มไลบรารี TensorFlow Lite ลงในไฟล์ build.gradle ของโมดูล

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Text Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

ขั้นตอนที่ 2: เรียกใช้การอนุมานโดยใช้ API

// Initialization
BertQuestionAnswererOptions options =
    BertQuestionAnswererOptions.builder()
        .setBaseOptions(BaseOptions.builder().setNumThreads(4).build())
        .build();
BertQuestionAnswerer answerer =
    BertQuestionAnswerer.createFromFileAndOptions(
        androidContext, modelFile, options);

// Run inference
List<QaAnswer> answers = answerer.answer(contextOfTheQuestion, questionToAsk);

ดูรายละเอียดเพิ่มเติมได้ในซอร์สโค้ด

เรียกใช้การอนุมานใน Swift

ขั้นตอนที่ 1: นำเข้า CocoaPods

เพิ่มพ็อด TensorFlowLiteTaskText ใน Podfile

target 'MySwiftAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end

ขั้นตอนที่ 2: เรียกใช้การอนุมานโดยใช้ API

// Initialization
let mobileBertAnswerer = TFLBertQuestionAnswerer.questionAnswerer(
      modelPath: mobileBertModelPath)

// Run inference
let answers = mobileBertAnswerer.answer(
      context: context, question: question)

ดูรายละเอียดเพิ่มเติมได้ในซอร์สโค้ด

เรียกใช้การอนุมานใน C++

// Initialization
BertQuestionAnswererOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<BertQuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();

// Run inference with your inputs, `context_of_question` and `question_to_ask`.
std::vector<QaAnswer> positive_results = answerer->Answer(context_of_question, question_to_ask);

ดูรายละเอียดเพิ่มเติมได้ในซอร์สโค้ด

เรียกใช้การอนุมานใน Python

ขั้นตอนที่ 1: ติดตั้งแพ็กเกจ pip

pip install tflite-support

ขั้นตอนที่ 2: การใช้โมเดล

# Imports
from tflite_support.task import text

# Initialization
answerer = text.BertQuestionAnswerer.create_from_file(model_path)

# Run inference
bert_qa_result = answerer.answer(context, question)

ดูตัวเลือกเพิ่มเติมในการกำหนดค่า BertQuestionAnswerer ได้ในซอร์สโค้ด

ตัวอย่างผลการแข่ง

นี่คือตัวอย่างผลลัพธ์คำตอบของโมเดล ALBERT

บริบท: "ป่าดิบชื้นแอมะซอน หรือที่เรียกอีกอย่างว่าป่าแอมะซอน หรือที่รู้จักกันในภาษาอังกฤษว่าอเมซอนเนีย เป็นป่าดิบชื้นเขตร้อนที่มีใบกว้างชื้นในชีวนิเวศแอมะซอนซึ่งครอบคลุมพื้นที่ส่วนใหญ่ของลุ่มน้ำแอมะซอนในอเมริกาใต้ ลุ่มน้ำนี้ ครอบคลุมพื้นที่ 7,000,000 ตร.กม. (2,700,000 ตร.ไมล์) ซึ่งมี ป่าฝนปกคลุม 5,500,000 ตร.กม. (2,100,000 ตร.ไมล์) ภูมิภาคนี้ รวมถึงดินแดนที่เป็นของ 9 ประเทศ"

คำถาม: "ป่าฝนอเมซอนอยู่ที่ไหน"

คำตอบ

answer[0]:  'South America.'
logit: 1.84847, start_index: 39, end_index: 40
answer[1]:  'most of the Amazon basin of South America.'
logit: 1.2921, start_index: 34, end_index: 40
answer[2]:  'the Amazon basin of South America.'
logit: -0.0959535, start_index: 36, end_index: 40
answer[3]:  'the Amazon biome that covers most of the Amazon basin of South America.'
logit: -0.498558, start_index: 28, end_index: 40
answer[4]:  'Amazon basin of South America.'
logit: -0.774266, start_index: 37, end_index: 40

ลองใช้เครื่องมือสาธิต CLI แบบง่ายสำหรับ BertQuestionAnswerer กับโมเดลและข้อมูลทดสอบของคุณเอง

ข้อกำหนดความเข้ากันได้ของโมเดล

BertQuestionAnswerer API คาดหวังโมเดล TFLite ที่มีข้อมูลเมตาของโมเดล TFLite ที่จำเป็น

ข้อมูลเมตาควรเป็นไปตามข้อกำหนดต่อไปนี้

  • input_process_units สำหรับ Wordpiece/Sentencepiece Tokenizer

  • Tensor อินพุต 3 รายการที่มีชื่อ "ids", "mask" และ "segment_ids" สำหรับเอาต์พุตของ ตัวแยกคำ

  • เทนเซอร์เอาต์พุต 2 รายการที่มีชื่อ "end_logits" และ "start_logits" เพื่อระบุ ตำแหน่งสัมพัทธ์ของคำตอบในบริบท