Tích hợp phần trả lời câu hỏi BERT

API Thư viện tác vụ BertQuestionAnswerer tải mô hình Bert và trả lời các câu hỏi dựa trên nội dung của một đoạn văn nhất định. Để biết thêm thông tin, hãy xem tài liệu về mô hình Câu hỏi-Trả lời tại đây .

Các tính năng chính của API BertQuestionAnswerer

  • Lấy hai đầu vào văn bản làm câu hỏi và ngữ cảnh rồi đưa ra danh sách các câu trả lời có thể có.

  • Thực hiện mã thông báo Từ ngữ hoặc Câu văn ngoài biểu đồ trên văn bản đầu vào.

Các mô hình BertQuestionAnswerer được hỗ trợ

Các mô hình sau tương thích với API BertNLClassifier .

Chạy suy luận trong Java

Bước 1: Nhập phần phụ thuộc Gradle và các cài đặt khác

Sao chép tệp mô hình .tflite vào thư mục nội dung của mô-đun Android nơi mô hình sẽ được chạy. Chỉ định rằng tệp sẽ không được nén và thêm thư viện TensorFlow Lite vào tệp build.gradle của mô-đun:

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 (NNAPI is included)
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

Bước 2: Chạy suy luận bằng 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);

Xem mã nguồn để biết thêm chi tiết.

Chạy suy luận trong Swift

Bước 1: Nhập CocoaPods

Thêm nhóm TensorFlowLiteTaskText trong Podfile

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

Bước 2: Chạy suy luận bằng API

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

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

Xem mã nguồn để biết thêm chi tiết.

Chạy suy luận trong 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);

Xem mã nguồn để biết thêm chi tiết.

Chạy suy luận trong Python

Bước 1: Cài đặt gói pip

pip install tflite-support

Bước 2: Sử dụng mô hình

# 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)

Xem mã nguồn để biết thêm tùy chọn cấu hình BertQuestionAnswerer .

Kết quả ví dụ

Dưới đây là một ví dụ về kết quả trả lời của mô hình ALBERT .

Bối cảnh: "Rừng nhiệt đới Amazon, hay nói cách khác, Rừng Amazon, còn được gọi bằng tiếng Anh là Amazonia, là một rừng mưa nhiệt đới lá rộng ẩm ướt trong quần xã Amazon bao phủ hầu hết lưu vực sông Amazon ở Nam Mỹ. Lưu vực này có diện tích 7.000.000 km2 (2.700.000 dặm vuông). ), trong đó 5.500.000 km2 (2.100.000 dặm vuông) được bao phủ bởi rừng nhiệt đới. Khu vực này bao gồm lãnh thổ thuộc chín quốc gia."

Câu hỏi: Rừng nhiệt đới Amazon ở đâu?

Câu trả lời:

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

Hãy dùng thử công cụ demo CLI đơn giản cho BertQuestionAnswerer với mô hình và dữ liệu thử nghiệm của riêng bạn.

Yêu cầu tương thích mô hình

API BertQuestionAnswerer yêu cầu mô hình TFLite với Siêu dữ liệu mô hình TFLite bắt buộc.

Siêu dữ liệu phải đáp ứng các yêu cầu sau:

  • input_process_units cho Trình mã hóa từ/câu

  • 3 tensor đầu vào có tên "ids", "mask" và "segment_ids" cho đầu ra của mã thông báo

  • 2 tensor đầu ra có tên "end_logits" và "start_logits" để biểu thị vị trí tương đối của câu trả lời trong ngữ cảnh