示例#1
0
def test(host, port, dim):
    print("host: %s:%d" % (host, port))

    channel = grpc.insecure_channel("%s:%d" % (host, port))
    stub = pb2_grpc.ServerStub(channel)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    id = 1
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    id = 1
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    id = 2
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    id = 3
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    id = 2
    response = stub.Search(pb2.SearchRequest(id=id, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    response = stub.Remove(pb2.IdRequest(id=2))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    id = 2
    response = stub.Search(pb2.SearchRequest(id=id, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    # response = stub.Remove(pb2.IdRequest(id=1))
    # response = stub.Remove(pb2.IdRequest(id=3))

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)
    def search(self, query: VectorLike, k: int) -> None:
        vec = faiss_pb2.Vector(val=query)
        req = faiss_pb2.SearchRequest(query=vec, k=k)
        res = self.stub.Search(req)

        for i, n in enumerate(res.neighbors):
            print(f'#{i}, id: {n.id}, score: {n.score}')
示例#3
0
def test_key(host, port, dim):
    print("host: %s:%d" % (host, port))

    channel = grpc.insecure_channel("%s:%d" % (host, port))
    stub = pb2_grpc.ServerStub(channel)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    key = "k1"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k1"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k2"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k3"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    key = "k1"
    response = stub.Search(pb2.SearchRequest(key=key, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    key = "k2"
    response = stub.Search(pb2.SearchRequest(key=key, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)
示例#4
0
def main(args):
    # create channel and stub
    address = '{}:{}'.format(args.host, args.port)
    channel = grpc.insecure_channel(address)
    stub = faiss_pb2_grpc.FaissServiceStub(channel)

    request = faiss_pb2.SearchRequest()
    request.top_k = args.top_k
    request.vector.float_val.extend(np.random.rand(1, 100)[0])
    response = stub.Search.future(request, args.timeout)
    print(response.result())
示例#5
0
def _search_by_key(host, key, count, timeout, channel):
    stub = pb2_grpc.ServerStub(channel)
    return stub.Search(pb2.SearchRequest(key=key, count=count))
示例#6
0
def search(host, id, count, timeout):
    with grpc.insecure_channel(host) as channel:
        stub = pb2_grpc.ServerStub(channel)
        response = stub.Search(pb2.SearchRequest(id=id, count=count),
                               timeout=timeout)
        print("response: %s, %s" % (response.ids, response.scores))