|
|
|
# ANN Search Operator: Faiss
|
|
|
|
|
|
|
|
*author: shiyu*
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Desription
|
|
|
|
|
|
|
|
Search embedding in [Faiss](https://github.com/facebookresearch/faiss), **please make sure you have inserted data to Faiss before search**.
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Code Example
|
|
|
|
|
|
|
|
> please insert data into faiss first.
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:*
|
|
|
|
|
|
|
|
```python
|
|
|
|
from towhee.dc2 import pipe, ops
|
|
|
|
|
|
|
|
p = pipe.input('vec') \
|
|
|
|
.flat_map('vec', 'rows', ops.ann_search.faiss(findex='index.bin')) \
|
|
|
|
.map('rows', ('id', 'score'), lambda x: (x[0], x[1], x[2])) \
|
|
|
|
.output('id', 'score')
|
|
|
|
|
|
|
|
p('cat')
|
|
|
|
```
|
|
|
|
|
|
|
|
<img src="./result.png" height="100px"/>
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Factory Constructor
|
|
|
|
|
|
|
|
Create the operator via the following factory method:
|
|
|
|
|
|
|
|
***ann_search.faiss(findex)***
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***findex:*** *str* or *faiss.INDEX*
|
|
|
|
|
|
|
|
The path to faiss index file or faiss index.
|
|
|
|
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***query:*** *list*
|
|
|
|
|
|
|
|
Query embeddings in Faiss
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Returns:** *Entity*
|
|
|
|
|
|
|
|
Return the results in Faiss with `key` and `score`.
|