Faiss-Server uses gRPC to implement vector similarity search based on open source Faiss.
Supported formats of vector files
- .fvecs
- .svm (Libsvm file)
Example of a Libsvm file:
21187279 1:0.2663046344870018 2:0.36652042181588923 3:1.0686633708024278 4:0.48038935355720136 5:0.25852895390543884 6:0.3548201486996048 7:0.5256999599627583 8:0.6950687558969181 9:0.678305894615746 10:0.22776047265501018 21264640 1:0.3939700179792862 2:0.2754414668803289 3:0.9439534329739017 4:0.40100161008614665 5:0.4995636031244379 6:0.013824724791385053 7:0.5587276328436032 8:0.5785080421720411 9:0.2784678162956546 10:0.5387681136371216
The content in the file must be in the format of
tagid 1:xx 2:xx 3:xx
.tagid
can be any character string. In most cases, tagid is an item ID. The digit on the left of each colon (:) is a vector dimension number. In most cases, the digit starts from1
.xx
indicates the value of a dimension. - .index (index file generated by Faiss)
If you directly load a Libsvm file that contains a large amount of data, it requires a long period of time to build an index. You can first generate an index file based on the Libsvm file in offline mode. Then, use Faiss-Server to load the index file. This way, you can build an index in a short period of time.
Load a local vector file
Sample file: article_word2vec.svm
docker run -it -p 9000:9000 -v /home/bruceding.jing:/index datascience-registry.cn-beijing.cr.aliyuncs.com/tools/faiss-server:1.0.0 --index_source=/index/article_word2vec.svm --index_params=Flat
The output shows that the gRPC server is started and listens on port 9000.
Parameter | Description |
---|---|
-p | The -p parameter of Docker is used for port mapping.
In this example, port 9000 is used. It is the default listening port of Faiss-Server. |
/home/bruceding.jing | The path to which the Docker image address is mapped. Configure this parameter based on your business requirements. |
datascience-registry.cn-beijing.cr.aliyuncs.com/tools/faiss-server:1.0.0 | The Docker image address provided by Faiss-Server. |
--index_source | The path of the vector file. |
--index_params | The mode in which a Faiss index is built. In this example, the index is built in Flat mode.
Faiss-Server uses the index_factory function of Faiss to build indexes. For more information
about the |
Load a vector file in OSS
docker run -it -p 9000:9000 datascience-registry.cn-beijing.cr.aliyuncs.com/tools/faiss-server:1.0.0 --index_params=IVF256,Flat --search_params=nprobe=128 --accessid=xxx --accesskey=xxx --endpoint=oss-cn-beijing.aliyuncs.com --bucket_name=faiss-server --object_name=demo_data/article_word2vec.svm
Parameter | Description |
---|---|
--index_params | IVF256,Flat indicates that a clustered index is built, and the indexed data is divided into 256
pieces.
|
--search_params | In this example, Faiss-Server searches for the first 128 clusters whose centroids are closest to the vector. For more information about this parameter, see Index IO, cloning and hyper parameter tuning. |
--bucket_name | The name of the OSS bucket. |
--object_name | The path of the vector file in OSS. |
--endpoint | The endpoint of OSS. In this example, a public endpoint is specified. If a VPC is used, you must specify the internal endpoint of OSS to minimize traffic fees and accelerate the data read speed. |
docker run datascience-registry.cn-beijing.cr.aliyuncs.com/tools/faiss-server:1.0.0 --help
Load vector files in OSS based on the version file
Faiss-Server is an online service. It may require a long period of time to directly load a vector file to Faiss-Server. To save time, you can generate an index file based on the vector file and load the index file to Faiss-Server.
In some scenarios, vector files need to be loaded on a regular basis.
A vector file may have multiple versions, and its latest version is automatically recorded in a version file. Faiss-Server checks the content of the version file in an asynchronous manner. If the version file is updated, Faiss-Server automatically loads the latest index and idxmap files.
- 1. Use a vector recall algorithm to generate a vector file for an item and store the file in an OSS bucket.
- 2 and 3. Faiss-Server reads the vector file from OSS and builds an index.
The version file contains the locations of the index and idxmap files.
- The index file is the generated Faiss index file.
- The idxmap file is a mapping table that maps tag IDs (tagid) to specific vector locations in an index.
- 4. Faiss-Server is always in the started state and listens to the version file.
The following procedure describes how to build an index file in offline mode:
Test Faiss-Server
After you start Faiss-Server, use one of the following tools to test the vector similarity search feature:
faiss-client-go.tar.gz and faiss-client-java.tar.gz
./faiss-client-go --host "11.158.**.**:9000" --vector "1:0.8254435895816826 2:0.3546776922906237 3:0.14982954432756015 4:0.4796270382425792 5:0.5478646494350313 6:0.3771617042371068 7:0.5107318036421319 8:0.7005006312566686 9:0.7030542773195687 10:0.692132791047145" --k 20
Parameter | Description |
---|---|
--host | Specifies the IP address and port number of Faiss-Server. |
--vector | Specifies the requested vector data that is in the same format as the Libsvm file. The dimensions must also be the same as the dimensions in the Libsvm file. In this example, 10-dimensional data is used. |
--k | Specifies the number of the search results to be returned.
The default value is 10. In this example, this parameter is set to 20. |
tagid list
: the tag IDs in the Libsvm file. In most cases, tag IDs are item IDs.