|
@ -16,37 +16,21 @@ Search embedding in [Faiss](https://github.com/facebookresearch/faiss), **please |
|
|
|
|
|
|
|
|
## Code Example |
|
|
## Code Example |
|
|
|
|
|
|
|
|
### Insert data into Faiss first |
|
|
|
|
|
|
|
|
|
|
|
```python |
|
|
|
|
|
import numpy as np |
|
|
|
|
|
import towhee |
|
|
|
|
|
|
|
|
|
|
|
vec = np.random.random((10, 100)).astype('float32') |
|
|
|
|
|
ids = list(i for i in range(10)) |
|
|
|
|
|
|
|
|
|
|
|
x = towhee.dc['id'](ids) \ |
|
|
|
|
|
.runas_op['id', 'vec'](func=lambda x: vec[x]) \ |
|
|
|
|
|
.to_faiss['id', 'vec'](findex='index.bin') |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
> please insert data into faiss first. |
|
|
|
|
|
|
|
|
### Example |
|
|
### Example |
|
|
|
|
|
|
|
|
*Write the pipeline in simplified style:* |
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:* |
|
|
|
|
|
|
|
|
```python |
|
|
```python |
|
|
query = vec[0:2] |
|
|
|
|
|
towhee.dc(query) \ |
|
|
|
|
|
.ann_search.faiss(findex='index.bin') |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
from towhee.dc2 import pipe, ops |
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:* |
|
|
|
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
```python |
|
|
|
|
|
query = vec[0:2] |
|
|
|
|
|
towhee.dc['vec'](query) \ |
|
|
|
|
|
.ann_search.faiss['vec', 'results'](findex='index.bin') \ |
|
|
|
|
|
.show() |
|
|
|
|
|
|
|
|
p('cat') |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
<img src="./result.png" height="100px"/> |
|
|
<img src="./result.png" height="100px"/> |
|
|