Multilingual Universal Sentence Encoder Q&A Retrieval

View on TensorFlow.org Run in Google Colab View on GitHub Download notebook See TF Hub models

This is a demo for using Universal Encoder Multilingual Q&A model for question-answer retrieval of text, illustrating the use of question_encoder and response_encoder of the model. We use sentences from SQuAD paragraphs as the demo dataset, each sentence and its context (the text surrounding the sentence) is encoded into high dimension embeddings with the response_encoder. These embeddings are stored in an index built using the simpleneighbors library for question-answer retrieval.

On retrieval a random question is selected from the SQuAD dataset and encoded into high dimension embedding with the question_encoder and query the simpleneighbors index returning a list of approximate nearest neighbors in semantic space.

More models

You can find all currently hosted text embedding models here and all models that have been trained on SQuAD as well here.

Setup

Setup Environment

Setup common imports and functions

/tmpfs/src/tf_docs_env/lib/python3.11/site-packages/tensorflow_hub/__init__.py:61: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  from pkg_resources import parse_version
[nltk_data] Downloading package punkt to /home/kbuilder/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.
[nltk_data] Downloading package punkt_tab to
[nltk_data]     /home/kbuilder/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt_tab.zip.
[nltk_data] Downloading package punkt_tab to
[nltk_data]     /home/kbuilder/nltk_data...
[nltk_data]   Package punkt_tab is already up-to-date!

Run the following code block to download and extract the SQuAD dataset into:

  • sentences is a list of (text, context) tuples - each paragraph from the SQuAD dataset are split into sentences using nltk library and the sentence and paragraph text forms the (text, context) tuple.
  • questions is a list of (question, answer) tuples.

Download and extract SQuAD data

10457 sentences, 10552 questions extracted from SQuAD https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json

Example sentence and context:

sentence:

'The city council organize concerts and other events.'

context:

('Several commemorative events take place every year. Gatherings of thousands '
 'of people on the banks of the Vistula on Midsummer’s Night for a festival '
 'called Wianki (Polish for Wreaths) have become a tradition and a yearly '
 'event in the programme of cultural events in Warsaw. The festival traces its '
 'roots to a peaceful pagan ritual where maidens would float their wreaths of '
 'herbs on the water to predict when they would be married, and to whom. By '
 'the 19th century this tradition had become a festive event, and it continues '
 'today. The city council organize concerts and other events. Each Midsummer’s '
 'Eve, apart from the official floating of wreaths, jumping over fires, '
 "looking for the fern flower, there are musical performances, dignitaries' "
 'speeches, fairs and fireworks by the river bank.')

The following code block setup the tensorflow graph g and session with the Universal Encoder Multilingual Q&A model's question_encoder and response_encoder signatures.

Load model from tensorflow hub

WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1793881093.507527   62647 cuda_platform.cc:52] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected

The following code block compute the embeddings for all the text, context tuples and store them in a simpleneighbors index using the response_encoder.

Compute embeddings and build simpleneighbors index

Computing embeddings for 10457 sentences
0%|          | 0/104 [00:00<?, ?it/s]
simpleneighbors index for 10457 sentences built.

On retrieval, the question is encoded using the question_encoder and the question embedding is used to query the simpleneighbors index.

Retrieve nearest neighbors for a random question from SQuAD