|
|
|
# ANN Search Operator: Faiss
|
|
|
|
|
|
|
|
*author: shiyu*
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Desription
|
|
|
|
|
|
|
|
|
|
|
|
Only for local test. If you want to use a vector database in a production environment, you can use Milvus(https://github.com/milvus-io/milvus).
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Code Example
|
|
|
|
|
|
|
|
> please insert data into faiss first.
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:*
|
|
|
|
|
|
|
|
```python
|
|
|
|
from towhee import pipe, ops
|
|
|
|
|
|
|
|
p = pipe.input('vec') \
|
|
|
|
.flat_map('vec', 'rows', ops.ann_search.faiss('./data_dir', 5)) \
|
|
|
|
.map('rows', ('id', 'score'), lambda x: (x[0], x[1])) \
|
|
|
|
.output('id', 'score')
|
|
|
|
|
|
|
|
p(<your-vector>)
|
|
|
|
```
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Factory Constructor
|
|
|
|
|
|
|
|
Create the operator via the following factory method:
|
|
|
|
|
|
|
|
***ops.ann_search.faiss_index('./data_dir', 5)***
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***data_dir:*** *str*
|
|
|
|
|
|
|
|
The path to faiss index and meta data.
|
|
|
|
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***query:*** *ndarray*
|
|
|
|
|
|
|
|
Query embedding in Faiss
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Returns:** *Entity*
|
|
|
|
|
|
|
|
Return the results in Faiss with `key` and `score`.
|