Code Monkey home page Code Monkey logo

node-qdrant's Introduction

node-qdrant

Javascript client library for the Qdrant vector search engine (https://qdrant.tech)

Install

npm install qdrant

Then you can use it in your project:

import { Qdrant } from "qdrant"
const qdrant = new Qdrant("http://localhost:6333/");

Quick Start

Here is a basic example that creates a client connection and adds a new collection pretty_colors to Qdrant. It assumes the Qdrant docker is running at localhost:6333. This quick start is also in the examples folder in this repository.

import { Qdrant } from "qdrant"

const qdrant = new Qdrant("http://localhost:6333/");

const name = "pretty_colors";

/// -------------------------------------------------------------------------
/// Create the new collection with the name and schema
const schema = {
    "name":name,
    "vector_size": 3,
    "distance": "Cosine"
};
let create_result = await qdrant.create_collection(name,schema);
if (create_result.err) {
    console.error(`ERROR:  Couldn't create collection "${name}"!`);
    console.error(create_result.err);
} else {
    console.log(`Success! Collection "${name} created!"`);
    console.log(create_result.response);
}

/// -------------------------------------------------------------------------
/// Show the collection info as it exists in the Qdrant engine
let collection_result = await qdrant.get_collection(name);
if (collection_result.err) {
    console.error(`ERROR:  Couldn't access collection "${name}"!`);
    console.error(collection_result.err);
} else {
    console.log(`Collection "${name} found!"`);
    console.log(collection_result.response);
}

/// -------------------------------------------------------------------------
/// Upload some points - just five RGB colors
let points = [
    { "id": 1, "payload": {"color": "red"}, "vector": [0.9, 0.1, 0.1] },
    { "id": 2, "payload": {"color": "green"}, "vector": [0.1, 0.9, 0.1] },
    { "id": 3, "payload": {"color": "blue"}, "vector": [0.1, 0.1, 0.9] },
    { "id": 4, "payload": {"color": "purple"}, "vector": [1.0, 0.1, 0.9] },
    { "id": 5, "payload": {"color": "cyan"}, "vector": [0.1, 0.9, 0.8] }
]
let upload_result = await qdrant.upload_points(name,points);
if (upload_result.err) {
    console.error(`ERROR:  Couldn't upload to "${name}"!`);
    console.error(upload_result.err);
} else {
    console.log(`Uploaded to "${name} successfully!"`);
    console.log(upload_result.response);
}

/// -------------------------------------------------------------------------
/// Search the closest color (k=1)
let purplish = [0.8,0.1,0.7];
let search_result = await qdrant.search_collection(name,purplish,1);
if (search_result.err) {
    console.error(`ERROR: Couldn't search ${purplish}`);
    console.error(search_result.err);
} else {
    console.log(`Search results for ${purplish}`);
    console.log(search_result.response);
}


/// -------------------------------------------------------------------------
/// Filtered search the closest color
let filter = {
    "must": [
        { "key": "color", "match": { "keyword": "cyan" } }
    ]
}
let filtered_result = await qdrant.search_collection(name,purplish,1,128,filter);
if (filtered_result.err) {
    console.error(`ERROR: Couldn't search ${purplish} with ${filter}`);
    console.error(filtered_result.err);
} else {
    console.log(`Search results for filtered ${purplish}`);
    console.log(filtered_result.response);
}

/// -------------------------------------------------------------------------
/// Delete the collection
let delete_result = await qdrant.delete_collection(name);
if (delete_result.err) {
    console.error(`ERROR:  Couldn't delete "${name}"!`);
    console.error(delete_result.err);
} else {
    console.log(`Deleted "${name} successfully!"`);
    console.log(delete_result.response);
}

Conventions

All methods must be awaited, and return a QdrantResponse object - which only has two properties: err and response.

Always check for presence of err. If err is not null, then the response might not be valid.

Methods

With an qdrant object, just await one of the following methods to interact with the engine and its collections:

create_collection(name,body)

Creates a new collection with name and the schema specified in body

get_collection(name)

Gets the collection information for name

delete_collection(name)

Deletes a collection with name

upload_points(name,points)

Uploads vectors and payloads in points to the collection name

search_collection(name,vector,k,ef,filter)

Searches the collection with a vector, to get the top k most similar points (default 5), using HNSW ef (default is 128), and an optional payload filter.

query_collection(name,query)

Searches the collection with a query that must be fully defined by the caller.

retrieve_points(name,query)

Gets all the points by the array of ids provided

node-qdrant's People

Contributors

binarymax avatar

Stargazers

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

Watchers

 avatar

node-qdrant's Issues

Status error when running example

I keep getting the same errors when I run the example and I am not entirely sure why. It appears to be telling me that the operations have both succeeded and failed.

Here is my stdout:

Success! Collection "pretty_colors created!"
{
  status: {
    error: 'Format error in JSON body: missing field `vectors` at line 1 column 60'
  },
  time: 0
}
Collection "pretty_colors found!"
{
  status: { error: "Not found: Collection `pretty_colors` doesn't exist!" },
  time: 0.000015212
}
Uploaded to "pretty_colors successfully!"
{
  status: { error: "Not found: Collection `pretty_colors` doesn't exist!" },
  time: 0.000015592
}
Search results for 0.8,0.1,0.7
{
  status: { error: "Not found: Collection `pretty_colors` doesn't exist!" },
  time: 0.000011459
}
Search results for filtered 0.8,0.1,0.7
{
  status: {
    error: 'Format error in JSON body: data did not match any variant of untagged enum Condition at line 1 column 118'
  },
  time: 0
}
Deleted "pretty_colors successfully!"
{ result: false, status: 'ok', time: 0.000007451 }

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.