GithubHelp home page GithubHelp logo

vector_server's Introduction

通用向量搜索服务

通用向量搜索服务 VectorServer

基于faiss搭建的通用向量搜索服务,服务加载向量持久化文件, 同时可指定加载数据文件; 通过faiss索引到内存,再通过flask提供API通用接口。

  • API接口提供:
    • 按向量搜索向量;
    • 按索引号搜索向量;
    • 计算两个向量的余弦距离;
  • 如果指定了数据文件,接口同时还会返回向量索引号对应的数据内容;
  • 支持GPU

向量搜索服务应用广泛,可以应用在词库搜索,文档搜索,智能机器人等各种场景。

文件说明

文件名 文件说明
VecSearch_faiss_server.py 服务主程序
test.py 测试程序
/images 截图目录

使用说明

向量搜索服务启动时需要两个参数:

  1. 向量文件。格式为.npy文件,即把向量直接用np.save(filename,v),保存的文件;
  2. 数据文件。数据文件为可选,如果没有指定数据文件,则接口只会返回索引号; 当指定了数据文件后,接口会同时使用txt字段返回对应的文本数据; 数据文件格式为纯文本格式,内容按行分隔,与向量文件的顺序对应, 例如对应的句子,词语,或者其它的业务信息。

服务程序

服务端主程序使用说明

usage: VecSearch_faiss_server.py [-h] [-npy NPY] [-datfile DATFILE]
                                 [-port PORT] [-metric {L2,INNER_PRODUCT}]
                                 [-debug DEBUG] [--gpu GPU]

faiss向量搜索服务 V-1.0.1

optional arguments:
  -h, --help            show this help message and exit
  -npy NPY              向量数据文件名,默认vector.npy
  -datfile DATFILE      文本数据文件名
  -port PORT            监听端口,默认7800
  -metric {L2,INNER_PRODUCT}
                        计算方法:L2=欧式距离;INNER_PRODUCT=向量内积(默认)
  -debug DEBUG          是否调试模式,默认=1
  --gpu GPU             使用GPU,-1=不使用(默认),0=使用第1个,>0=使用全部

注明 当使用内积的方式时,服务会自动对向量进行归一化处理, 这样输出的结果可以认为就是余弦相似度;

测试程序

usage: test.py [-h] [-test_total TEST_TOTAL] [-test_dim TEST_DIM]
               [-test_times TEST_TIMES] [-test_topn TEST_TOPN]

faiss向量搜索服务端测试 V-1.0.2

optional arguments:
  -h, --help            show this help message and exit
  -test_total TEST_TOTAL
                        测试数据条数,默认10万
  -test_dim TEST_DIM    测试数据维度,默认768
  -test_times TEST_TIMES
                        测试次数,默认1000
  -test_topn TEST_TOPN  测试返回条数,默认5

运行测试程序后,会自动生成向量文件,保存为test_dat.npy, 默认参数下 10万条768维度的向量文件大小约为585MB

python test.py

使用案例: 腾讯词向量搜索服务

源码地址: https://github.com/xmxoxo/Tencent_ChineseEmbedding_Process/

vector_server's People

Contributors

xmxoxo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vector_server's Issues

请问VectorSearch_faiss_server中add方法里的q是什么,代码显示报错

 def add (self, vector):
        if not vector.dtype == 'float32':
            vector = vector.astype('float32')

        if self.normalize:
            faiss.normalize_L2(vector)

        if self.xb is None:
            prepos = 0
            # vector = vector[np.newaxis, :]   
            self.xb = vector.copy()
        else:
            prepos = self.xb.shape[0]
            self.xb = np.vstack((self.xb,q))   # 这里的q是什么???
        
        return (prepos, self.xb.shape[0]-1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.