logo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions

76 lines
1.9 KiB

2 years ago
# Text Embedding with dpr
2 years ago
2 years ago
*author: Kyle He*
## Desription
This operator uses Dense Passage Retrieval (DPR) to convert long text to embeddings.
Dense Passage Retrieval (DPR) is a set of tools and models for state-of-the-art open-domain Q&A research.
It was introduced in Dense Passage Retrieval for Open-Domain Question Answering by Vladimir Karpukhin,
Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, Wen-tau Yih[1].
**DPR** models were proposed in "Dense Passage Retrieval for Open-Domain Question Answering"[2].
2 years ago
In this work, we show that retrieval can be practically implemented using dense representations alone,
where embeddings are learned from a small number of questions and passages by a simple dual-encoder framework[2].
### References
2 years ago
[1].https://huggingface.co/docs/transformers/model_doc/dpr
2 years ago
[2].https://arxiv.org/abs/2004.04906
## Code Example
Use the pretrained model "facebook/dpr-ctx_encoder-single-nq-base"
to generate a text embedding for the sentence "Hello, world.".
*Write the pipeline*:
2 years ago
```python
from towhee import dc
2 years ago
dc.stream(["Hello, world."]) \
.text_embedding.dpr(model_name="facebook/dpr-ctx_encoder-single-nq-base") \
.to_list()
2 years ago
```
## Factory Constructor
2 years ago
Create the operator via the following factory method
2 years ago
***text_embedding.dpr(model_name="")***
2 years ago
**Parameters:**
***model_name***: *str*
2 years ago
​ The model name in string.
The default value is "facebook/dpr-ctx_encoder-single-nq-base".
You can get the list of supported model names by calling `get_model_list` from [dpr.py](https://towhee.io/text-embedding/dpr/src/branch/main/dpr.py).
2 years ago
## Interface
2 years ago
The operator takes a text in string as input.
It loads tokenizer and pre-trained model using model name.
and then return text embedding in ndarray.
2 years ago
**Parameters:**
2 years ago
***text***: *str*
2 years ago
​ The text in string.
2 years ago
**Returns**:
2 years ago
*numpy.ndarray*
2 years ago
​ The text embedding extracted by model.