Milvus
Question Answering Using Milvus and Hugging Face
In this notebook we go over how to search for the best answer to questions using Milvus as the Vector Database and Hugging Face as the embedding system.
Packages
We first begin with importing the required packages. In this example, the only non-builtin packages are datasets, transformers, and pymilvus. Transformers and datasets are the Hugging Face packages to create the pipeline and pymilvus is the client for Milvus. If not present on your system, these packages can be installed using pip install transformers datasets pymilvus.
/Users/filiphaltmayer/miniconda3/envs/openai/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm 2023-02-10 15:59:27.832257: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Parameters
Here we can find the main parameters that need to be modified for running with your own accounts. Beside each is a description of what it is.
Milvus
This segment deals with Milvus and setting up the database for this use case. Within Milvus we need to setup a collection and index the collection.
Insert Data
Once we have the collection setup we need to start inserting our data. This is done in three steps: tokenizing the original question, embedding the tokenized question, and inserting the embedding, original question, and answer.
Found cached dataset squad (/Users/filiphaltmayer/.cache/huggingface/datasets/squad/plain_text/1.0.0/d6ec3ceb99ca480ce37cdd35555d6cb2511d223b9150cce08a837ef62ffea453) 100%|██████████| 99/99 [00:00<00:00, 1655.16ex/s]
100%|██████████| 1/1 [00:01<00:00, 1.28s/ba]
Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertModel: ['cls.seq_relationship.bias', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.decoder.weight', 'cls.seq_relationship.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.bias'] - This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). 100%|██████████| 2/2 [04:21<00:00, 130.94s/ba]
Parameter 'function'=<function insert_function at 0x7fb6b19fa280> of the transform datasets.arrow_dataset.Dataset._map_single couldn't be hashed properly, a random hash was used instead. Make sure your transforms and parameters are serializable with pickle or dill for the dataset fingerprinting and caching to work. If you reuse this transform, the caching mechanism will consider it to be different from the previous calls and recompute everything. This warning is only showed once. Subsequent hashing failures won't be showed. 100%|██████████| 2/2 [00:00<00:00, 8.11ba/s]
Asks questions
Once all the data is inserted and indexed within Milvus, we can ask questions and see what the closest answers are.
100%|██████████| 1/1 [00:00<00:00, 30.11ba/s] 100%|██████████| 1/1 [00:03<00:00, 3.89s/ba]
100%|██████████| 2/2 [00:00<00:00, 49.78ba/s]
Question:
When was chemistry invented?
Answer, Distance, Original Question
('the sixteenth through the eighteenth centuries', tensor(6.7038), 'When did modern chemistry come into existence?')
('1911', tensor(19.4356), 'In what year was phenobarbital discovered?')
("the Architects' Registration Council of the United Kingdom", tensor(20.2873), 'What organization was the Royal Institute instrumental in establishing?')
('surprise attack to regain part of the Sinai territory Israel had captured 6 years earlier', tensor(20.4047), 'What was the October War?')
('Internet service providers', tensor(20.4992), 'Who did early court cases focus on?')
('19th', tensor(21.2683), 'In what century was Eisenhower born?')
('meaning "all port" due to the shape of its coast.', tensor(21.4290), 'Why did the Greeks name Palermo Panormos?')
('the Korean border', tensor(21.8102), 'Where did the Chinese military deploy troops in preparation for the arrival of US troops?')
('the printing press', tensor(22.5804), 'What spread the use of texts by Galen within universities?')
('talking monetary policy with the market', tensor(23.7503), 'What does "open mouth operations" mean?')
Question:
When was Eisenhower born?
Answer, Distance, Original Question
('19th', tensor(8.2841), 'In what century was Eisenhower born?')
('the sixteenth through the eighteenth centuries', tensor(20.3792), 'When did modern chemistry come into existence?')
('1790', tensor(21.3025), 'In what year was the Brown Fellowship Society created?')
('1911', tensor(22.0691), 'In what year was phenobarbital discovered?')
('surprise attack to regain part of the Sinai territory Israel had captured 6 years earlier', tensor(22.7447), 'What was the October War?')
('1997', tensor(25.5775), 'What year was the second generation of digimon released?')
('John Williams', tensor(27.5747), 'What composer has Steven Spielberg been associated with since 1974?')
("the Architects' Registration Council of the United Kingdom", tensor(28.0410), 'What organization was the Royal Institute instrumental in establishing?')
('talking monetary policy with the market', tensor(28.2592), 'What does "open mouth operations" mean?')
('the Korean border', tensor(28.8382), 'Where did the Chinese military deploy troops in preparation for the arrival of US troops?')